public static bool ExportMesh(AssetItem item, string exportPath) { var m_Mesh = (Mesh)item.Asset; if (m_Mesh.m_VertexCount <= 0) { return(false); } if (!TryExportFile(exportPath, item, ".obj", out var exportFullPath)) { return(false); } var sb = new StringBuilder(); sb.AppendLine("g " + m_Mesh.m_Name); #region Vertices if (m_Mesh.m_Vertices == null || m_Mesh.m_Vertices.Length == 0) { return(false); } var c = 3; if (m_Mesh.m_Vertices.Length == m_Mesh.m_VertexCount * 4) { c = 4; } for (var v = 0; v < m_Mesh.m_VertexCount; v++) { sb.AppendFormat("v {0} {1} {2}\r\n", -m_Mesh.m_Vertices[v * c], m_Mesh.m_Vertices[v * c + 1], m_Mesh.m_Vertices[v * c + 2]); } #endregion #region UV if (m_Mesh.m_UV0?.Length > 0) { if (m_Mesh.m_UV0.Length == m_Mesh.m_VertexCount * 2) { c = 2; } else if (m_Mesh.m_UV0.Length == m_Mesh.m_VertexCount * 3) { c = 3; } for (var v = 0; v < m_Mesh.m_VertexCount; v++) { sb.AppendFormat("vt {0} {1}\r\n", m_Mesh.m_UV0[v * c], m_Mesh.m_UV0[v * c + 1]); } } #endregion #region Normals if (m_Mesh.m_Normals?.Length > 0) { if (m_Mesh.m_Normals.Length == m_Mesh.m_VertexCount * 3) { c = 3; } else if (m_Mesh.m_Normals.Length == m_Mesh.m_VertexCount * 4) { c = 4; } for (var v = 0; v < m_Mesh.m_VertexCount; v++) { sb.AppendFormat("vn {0} {1} {2}\r\n", -m_Mesh.m_Normals[v * c], m_Mesh.m_Normals[v * c + 1], m_Mesh.m_Normals[v * c + 2]); } } #endregion #region Face var sum = 0; for (var i = 0; i < m_Mesh.m_SubMeshes.Length; i++) { sb.AppendLine($"g {m_Mesh.m_Name}_{i}"); var indexCount = (int)m_Mesh.m_SubMeshes[i].indexCount; var end = sum + indexCount / 3; for (var f = sum; f < end; f++) { sb.AppendFormat("f {0}/{0}/{0} {1}/{1}/{1} {2}/{2}/{2}\r\n", m_Mesh.m_Indices[f * 3 + 2] + 1, m_Mesh.m_Indices[f * 3 + 1] + 1, m_Mesh.m_Indices[f * 3] + 1); } sum = end; } #endregion sb.Replace("NaN", "0"); File.WriteAllText(exportFullPath, sb.ToString()); return(true); }
public static bool ExportTexture2D(AssetItem item, string exportPath) { var m_Texture2D = (Texture2D)item.Asset; if (Properties.Settings.Default.convertTexture) { var bitmap = m_Texture2D.ConvertToBitmap(true); if (bitmap == null) { return(false); } ImageFormat format = null; var ext = Properties.Settings.Default.convertType; var tga = false; switch (ext) { case "BMP": format = ImageFormat.Bmp; break; case "PNG": format = ImageFormat.Png; break; case "JPEG": format = ImageFormat.Jpeg; break; case "TGA": tga = true; break; } if (!TryExportFile(exportPath, item, "." + ext.ToLower(), out var exportFullPath)) { return(false); } if (tga) { var file = new TGA(bitmap); file.Save(exportFullPath); } else { try { bitmap.Save(exportFullPath, format); bitmap.Dispose(); } catch (ExternalException) { return(ExportTexture2D(item, exportPath)); } } return(true); } else { if (!TryExportFile(exportPath, item, ".tex", out var exportFullPath)) { return(false); } File.WriteAllBytes(exportFullPath, m_Texture2D.image_data.GetData()); return(true); } }
private static bool TryExportFile(string dir, AssetItem item, string extension, out string fullPath) { return(TryExportFile(dir, item, extension, out fullPath, true)); }