public static void N3DS_NW4C_GFX_ToOBJ(String BCMDLPath, int ModelIdx, String OBJPath) { CGFX c = new CGFX(File.ReadAllBytes(BCMDLPath)); if (c.Data.Models == null || c.Data.Models.Length <= ModelIdx) { throw new Exception("Model does not exist in the specified CGFX file!"); } CMDL Model = c.Data.Models[ModelIdx]; OBJ o = Model.ToOBJ(); o.MTLPath = Path.GetFileNameWithoutExtension(OBJPath) + ".mtl"; MTL m = Model.ToMTL(Path.GetFileNameWithoutExtension(OBJPath) + "_Tex"); byte[] d = o.Write(); byte[] d2 = m.Write(); File.Create(OBJPath).Close(); File.WriteAllBytes(OBJPath, d); File.Create(Path.ChangeExtension(OBJPath, "mtl")).Close(); File.WriteAllBytes(Path.ChangeExtension(OBJPath, "mtl"), d2); Directory.CreateDirectory(Path.GetDirectoryName(OBJPath) + "\\" + Path.GetFileNameWithoutExtension(OBJPath) + "_Tex"); foreach (var v in c.Data.Textures) { if (!(v is ImageTextureCtr)) { continue; } ((ImageTextureCtr)v).GetBitmap().Save(Path.GetDirectoryName(OBJPath) + "\\" + Path.GetFileNameWithoutExtension(OBJPath) + "_Tex\\" + v.Name + ".png"); } }
public bool Convert(int FilterIndex, String Path) { switch (FilterIndex) { case 0: { DAE o = ToDAE(0); File.Create(Path).Close(); File.WriteAllBytes(Path, o.Write()); Directory.CreateDirectory(System.IO.Path.GetDirectoryName(Path) + "\\Tex"); foreach (var v in Data.Textures) { if (!(v is ImageTextureCtr)) { continue; } ((ImageTextureCtr)v).GetBitmap().Save(System.IO.Path.GetDirectoryName(Path) + "\\Tex\\" + v.Name + ".png"); } return(true); } case 1: { if (Data.Models.Length == 0) { return(false); } OBJ o = ToOBJ(0); o.MTLPath = System.IO.Path.GetFileNameWithoutExtension(Path) + ".mtl"; MTL m = ToMTL(0); byte[] d = o.Write(); byte[] d2 = m.Write(); File.Create(Path).Close(); File.WriteAllBytes(Path, d); File.Create(System.IO.Path.ChangeExtension(Path, "mtl")).Close(); File.WriteAllBytes(System.IO.Path.ChangeExtension(Path, "mtl"), d2); Directory.CreateDirectory(System.IO.Path.GetDirectoryName(Path) + "\\Tex"); foreach (var v in Data.Textures) { //if (v.NrLevels > 2) v.GetBitmap(2).Save(System.IO.Path.GetDirectoryName(Path) + "\\Tex\\" + v.Name + ".png"); //else if (v.NrLevels > 1) v.GetBitmap(1).Save(System.IO.Path.GetDirectoryName(Path) + "\\Tex\\" + v.Name + ".png"); //else v.GetBitmap(0).Save(System.IO.Path.GetDirectoryName(Path) + "\\Tex\\" + v.Name + ".png"); if (!(v is ImageTextureCtr)) { continue; } ((ImageTextureCtr)v).GetBitmap().Save(System.IO.Path.GetDirectoryName(Path) + "\\Tex\\" + v.Name + ".png"); } return(true); } default: return(false); } }
private void toolStripButton1_Click(object sender, EventArgs e) { saveFileDialog1.FileName = Model.Name; if (saveFileDialog1.ShowDialog() == DialogResult.OK && saveFileDialog1.FileName.Length > 0) { switch (saveFileDialog1.FilterIndex - 1) { case 0: { DAE o = Model.ToDAE(Resource); File.Create(saveFileDialog1.FileName).Close(); File.WriteAllBytes(saveFileDialog1.FileName, o.Write()); Directory.CreateDirectory(Path.GetDirectoryName(saveFileDialog1.FileName) + "\\Tex"); foreach (var v in Resource.Data.Textures) { if (!(v is ImageTextureCtr)) { continue; } ((ImageTextureCtr)v).GetBitmap().Save(Path.GetDirectoryName(saveFileDialog1.FileName) + "\\Tex\\" + v.Name + ".png"); } break; } case 1: { OBJ o = Model.ToOBJ(); o.MTLPath = Path.GetFileNameWithoutExtension(saveFileDialog1.FileName) + ".mtl"; MTL m = Model.ToMTL("Tex"); byte[] d = o.Write(); byte[] d2 = m.Write(); File.Create(saveFileDialog1.FileName).Close(); File.WriteAllBytes(saveFileDialog1.FileName, d); File.Create(Path.ChangeExtension(saveFileDialog1.FileName, "mtl")).Close(); File.WriteAllBytes(Path.ChangeExtension(saveFileDialog1.FileName, "mtl"), d2); Directory.CreateDirectory(Path.GetDirectoryName(saveFileDialog1.FileName) + "\\Tex"); foreach (var v in Resource.Data.Textures) { //if (v.NrLevels > 2) v.GetBitmap(2).Save(System.IO.Path.GetDirectoryName(Path) + "\\Tex\\" + v.Name + ".png"); //else if (v.NrLevels > 1) v.GetBitmap(1).Save(System.IO.Path.GetDirectoryName(Path) + "\\Tex\\" + v.Name + ".png"); //else v.GetBitmap(0).Save(System.IO.Path.GetDirectoryName(Path) + "\\Tex\\" + v.Name + ".png"); if (!(v is ImageTextureCtr)) { continue; } ((ImageTextureCtr)v).GetBitmap().Save(Path.GetDirectoryName(saveFileDialog1.FileName) + "\\Tex\\" + v.Name + ".png"); } break; } } } }
public bool Convert(int FilterIndex, string Path) { switch (FilterIndex) { case 0: MTL mtl = null; OBJ o = ToOBJ(out mtl); string mtlPath = Path.Replace(".obj", ".mtl"); o.MTLPath = System.IO.Path.GetFileName(mtlPath); byte[] d = o.Write(); byte[] m = mtl.Write(); File.Create(Path).Close(); File.Create(mtlPath).Close(); File.WriteAllBytes(Path, d); File.WriteAllBytes(mtlPath, m); return(true); default: return(false); } }
void ConvertEFE(byte[] Data, string Name) { _3DS.NintendoWare.GFX.CGFX mod = new _3DS.NintendoWare.GFX.CGFX(Data); CommonFiles.OBJ o = mod.Data.Models[0].ToOBJ(); o.MTLPath = Path.GetFileNameWithoutExtension(Name) + ".mtl"; MTL m = mod.Data.Models[0].ToMTL("Tex"); byte[] d = o.Write(); byte[] d2 = m.Write(); File.Create(Name).Close(); File.WriteAllBytes(Name, d); File.Create(Path.ChangeExtension(Name, "mtl")).Close(); File.WriteAllBytes(Path.ChangeExtension(Name, "mtl"), d2); Directory.CreateDirectory(Path.GetDirectoryName(Name) + "\\Tex"); foreach (var v in mod.Data.Textures) { if (!(v is ImageTextureCtr)) { continue; } ((ImageTextureCtr)v).GetBitmap().Save(Path.GetDirectoryName(Name) + "\\Tex\\" + v.Name + ".png"); } }
protected override void _Write(BinaryWriter writer) { int objNum = 0; int totalPositions = 0; int totalNormals = 0; int totalUVs = 0; MTL mtl = new MTL(Textures); if (String.IsNullOrEmpty(FilePath)) { //TODO: Make this somehow better throw new ArgumentException("Filepath was not given."); } string mtlPath = ""; string dir = Path.GetDirectoryName(FilePath); if (String.IsNullOrEmpty(dir) || dir == "\\" || Path.GetExtension(FilePath) == ".OBJ" || Path.GetExtension(FilePath) == ".obj") { mtlPath = Path.ChangeExtension(FilePath, ".mtl"); } else { mtlPath = String.Format("{0}\\{1}", Path.GetDirectoryName(FilePath), Path.ChangeExtension(FilePath, ".mtl")); } mtl.Write(mtlPath); writer.WriteASCII("# OBJ Generated by ShenmueDKSharp\n"); writer.WriteASCII(String.Format("mtllib {0}\n", Path.GetFileName(mtlPath))); StringBuilder sbVertices = new StringBuilder(); StringBuilder sbNormals = new StringBuilder(); StringBuilder sbUVs = new StringBuilder(); StringBuilder sbMeshes = new StringBuilder(); foreach (ModelNode node in RootNode.GetAllNodes()) { Matrix4 transformMatrix = node.GetTransformMatrix(); if (node.Faces.Count == 0) { continue; } Vector3[] positions = node.VertexPositions.ToArray(); Vector3[] normals = node.VertexNormals.ToArray(); Vector2[] uvs = node.VertexUVs.ToArray(); if (positions.Length == 0) { continue; } sbMeshes.Append(String.Format("o obj_{0}\n", objNum)); for (int i = 0; i < positions.Length; i++) { positions[i] = Vector3.TransformPosition(positions[i], transformMatrix); sbVertices.Append(String.Format(m_cultureInfo, "v {0:F6} {1:F6} {2:F6}\n", positions[i].X, positions[i].Y, positions[i].Z)); } if (uvs.Length > 0) { for (int i = 0; i < uvs.Length; i++) { sbUVs.Append(String.Format(m_cultureInfo, "vt {0:F6} {1:F6}\n", uvs[i].X, uvs[i].Y)); } } if (normals.Length > 0) { for (int i = 0; i < normals.Length; i++) { //normals[i] = Vector3.TransformPosition(normals[i], transformMatrix); sbNormals.Append(String.Format(m_cultureInfo, "vn {0:F6} {1:F6} {2:F6}\n", normals[i].X, normals[i].Y, normals[i].Z)); } } foreach (MeshFace face in node.Faces) { uint textureIndex = face.TextureIndex; Texture texture = face.Material.Texture; if (textureIndex < mtl.MaterialNames.Count) { sbMeshes.Append(String.Format("usemtl {0}\n", mtl.MaterialNames[(int)textureIndex])); } sbMeshes.Append("s 1\n"); bool hasUV = face.UVIndices.Count > 0; bool hasNormal = face.NormalIndices.Count > 0; if (face.Type == MeshFace.PrimitiveType.Triangles) { for (int i = 0; i < face.PositionIndices.Count - 2; i += 3) { int posIndex1 = face.PositionIndices[i] + totalPositions + 1; int posIndex2 = face.PositionIndices[i + 1] + totalPositions + 1; int posIndex3 = face.PositionIndices[i + 2] + totalPositions + 1; if (hasNormal) { int normIndex1 = face.NormalIndices[i] + totalNormals + 1; int normIndex2 = face.NormalIndices[i + 1] + totalNormals + 1; int normIndex3 = face.NormalIndices[i + 2] + totalNormals + 1; if (hasUV) { int uvIndex1 = face.UVIndices[i] + totalUVs + 1; int uvIndex2 = face.UVIndices[i + 1] + totalUVs + 1; int uvIndex3 = face.UVIndices[i + 2] + totalUVs + 1; sbMeshes.Append(String.Format(GetVertexFormatString(hasUV, hasNormal), posIndex1, posIndex2, posIndex3, uvIndex1, uvIndex2, uvIndex3, normIndex1, normIndex2, normIndex3)); } else { sbMeshes.Append(String.Format(GetVertexFormatString(hasUV, hasNormal), posIndex1, posIndex2, posIndex3, normIndex1, normIndex2, normIndex3)); } } else { sbMeshes.Append(String.Format(GetVertexFormatString(hasUV, hasNormal), posIndex1, posIndex2, posIndex3)); } } } else if (face.Type == MeshFace.PrimitiveType.TriangleStrip) { for (int i = 0; i < face.PositionIndices.Count - 2; i++) { int posIndex1 = face.PositionIndices[i] + totalPositions + 1; int posIndex2 = face.PositionIndices[i + 1] + totalPositions + 1; int posIndex3 = face.PositionIndices[i + 2] + totalPositions + 1; if (hasNormal) { int normIndex1 = face.NormalIndices[i] + totalNormals + 1; int normIndex2 = face.NormalIndices[i + 1] + totalNormals + 1; int normIndex3 = face.NormalIndices[i + 2] + totalNormals + 1; if (hasUV) { int uvIndex1 = face.UVIndices[i] + totalUVs + 1; int uvIndex2 = face.UVIndices[i + 1] + totalUVs + 1; int uvIndex3 = face.UVIndices[i + 2] + totalUVs + 1; if ((i & 1) == 1) { sbMeshes.Append(String.Format(GetVertexFormatString(hasUV, hasNormal), posIndex1, posIndex2, posIndex3, uvIndex1, uvIndex2, uvIndex3, normIndex1, normIndex2, normIndex3)); } else { sbMeshes.Append(String.Format(GetVertexFormatString(hasUV, hasNormal), posIndex1, posIndex3, posIndex2, uvIndex1, uvIndex3, uvIndex2, normIndex1, normIndex3, normIndex2)); } } else { if ((i & 1) == 1) { sbMeshes.Append(String.Format(GetVertexFormatString(hasUV, hasNormal), posIndex1, posIndex2, posIndex3, normIndex1, normIndex2, normIndex3)); } else { sbMeshes.Append(String.Format(GetVertexFormatString(hasUV, hasNormal), posIndex1, posIndex3, posIndex2, normIndex1, normIndex3, normIndex2)); } } } else { if ((i & 1) == 1) { sbMeshes.Append(String.Format(GetVertexFormatString(hasUV, hasNormal), posIndex1, posIndex2, posIndex3)); } else { sbMeshes.Append(String.Format(GetVertexFormatString(hasUV, hasNormal), posIndex1, posIndex3, posIndex2)); } } } } } objNum++; totalPositions += positions.Length; totalNormals += normals.Length; totalUVs += uvs.Length; } writer.WriteASCII(sbVertices.ToString()); writer.WriteASCII(sbUVs.ToString()); writer.WriteASCII(sbNormals.ToString()); writer.WriteASCII(sbMeshes.ToString()); writer.WriteASCII(String.Format("# Total Positions: {0}\n", totalPositions)); writer.WriteASCII(String.Format("# Total Normals: {0}\n", totalNormals)); writer.WriteASCII(String.Format("# Total UVs: {0}\n", totalUVs)); }
private void FrmObjImport_Load(object sender, EventArgs e) { OpenFileDialog opn = new OpenFileDialog(); opn.Title = "Open a model file"; opn.Filter = "Supported formats (.bcmdl, .obj)|*.bcmdl; *.obj"; bool ok = true; if (opn.ShowDialog() != DialogResult.OK) { ok = false; } if (Path.GetExtension(opn.FileName).ToLower() == ".obj") { if (MessageBox.Show("The obj will be converted to bcmdl with Every File Explorer's method, this is known to have problems, especially with models made in sketchup.\r\nDo you want to continue ?", "Warning", MessageBoxButtons.YesNo) == DialogResult.Yes) { modelPath = opn.FileName; ObjModelPath = modelPath; IsObj = true; render.addModel(modelPath, "Model", new System.Windows.Media.Media3D.Vector3D(0, 0, 0), new System.Windows.Media.Media3D.Vector3D(1, 1, 1), 0, 0, 0); } else { MessageBox.Show("You can convert the model to bcmdl with the leaked tools"); this.Close(); } } else if (Path.GetExtension(opn.FileName).ToLower() == ".bcmdl") { tmpPath = Path.GetTempPath() + "TmpT4D"; string Name = tmpPath + "\\model.obj"; Directory.CreateDirectory(tmpPath); CGFX mod = null; mod = new _3DS.NintendoWare.GFX.CGFX(File.ReadAllBytes(opn.FileName)); CommonFiles.OBJ o = mod.Data.Models[0].ToOBJ(); o.MTLPath = Path.GetFileNameWithoutExtension(Name) + ".mtl"; MTL m = mod.Data.Models[0].ToMTL("Tex"); byte[] d = o.Write(); byte[] d2 = m.Write(); File.Create(Name).Close(); File.WriteAllBytes(Name, d); File.Create(Path.ChangeExtension(Name, "mtl")).Close(); File.WriteAllBytes(Path.ChangeExtension(Name, "mtl"), d2); Directory.CreateDirectory(tmpPath + "\\Tex"); foreach (var v in mod.Data.Textures) { if (!(v is ImageTextureCtr)) { continue; } ((ImageTextureCtr)v).GetBitmap().Save(tmpPath + "\\Tex\\" + v.Name + ".png"); } modelPath = opn.FileName; ObjModelPath = Name; IsObj = false; render.addModel(Name, "Model", new System.Windows.Media.Media3D.Vector3D(0, 0, 0), new System.Windows.Media.Media3D.Vector3D(1, 1, 1), 0, 0, 0); textBox1.Text = mod.Data.Models[0].Name; textBox1.Enabled = false; render.SetSortFrequency(0); } else { if (ok) { MessageBox.Show("File not supported"); } else { MessageBox.Show("You must select your model file to use this function"); } this.Close(); } }