public override Asset Import(string path) { MSHS mshs = MSHS.Load(path); Model model = new Model(); string name = Path.GetFileNameWithoutExtension(path); int meshnum = 0; foreach (var tdrmesh in mshs.Meshes) { ModelMesh mesh = new ModelMesh(); mesh.Name = name + meshnum++.ToString("0000"); ModelMeshPart meshpart = new ModelMeshPart(); meshpart.PrimitiveType = OpenTK.Graphics.OpenGL.PrimitiveType.Triangles; SceneManager.Current.UpdateProgress(string.Format("Processing {0}", mesh.Name)); for (int i = 0; i < tdrmesh.Faces.Count; i++) { var face = tdrmesh.Faces[i]; var v1 = tdrmesh.Vertexes[face.V1]; var v2 = tdrmesh.Vertexes[face.V2]; var v3 = tdrmesh.Vertexes[face.V3]; meshpart.AddFace( new OpenTK.Vector3[] { new OpenTK.Vector3(v1.Position.X, v1.Position.Y, v1.Position.Z), new OpenTK.Vector3(v2.Position.X, v2.Position.Y, v2.Position.Z), new OpenTK.Vector3(v3.Position.X, v3.Position.Y, v3.Position.Z) }, new OpenTK.Vector3[] { new OpenTK.Vector3(v1.Normal.X, v1.Normal.Y, v1.Normal.Z), new OpenTK.Vector3(v2.Normal.X, v2.Normal.Y, v2.Normal.Z), new OpenTK.Vector3(v3.Normal.X, v3.Normal.Y, v3.Normal.Z) }, new OpenTK.Vector2[] { new OpenTK.Vector2(v1.UV.X, v1.UV.Y), new OpenTK.Vector2(v2.UV.X, v2.UV.Y), new OpenTK.Vector2(v3.UV.X, v3.UV.Y) } ); } mesh.AddModelMeshPart(meshpart); model.SetName(mesh.Name, model.AddMesh(mesh)); } return(model); }
public override void Export(Asset asset, string path) { Model model = asset as Model; MSHS mshs = new MSHS(); foreach (ModelMesh mesh in model.Meshes) { TDRMesh msh = new TDRMesh { Mode = TDRMesh.MSHMode.Tri }; int offset = 0; foreach (ModelMeshPart meshpart in mesh.MeshParts) { offset += msh.Vertices.Count; foreach (Vertex v in meshpart.VertexBuffer.Data) { TDRVertex vert = new TDRVertex { Position = v.Position, UV = new Vector2(v.UV.X, v.UV.Y), Colour = new Vector4(v.Colour.R, v.Colour.G, v.Colour.B, v.Colour.A) }; msh.Vertices.Add(vert); } for (int i = 0; i < meshpart.IndexBuffer.Data.Count; i += 3) { msh.Faces.Add(new TDRFace { V1 = offset + meshpart.IndexBuffer.Data[i + 0], V2 = offset + meshpart.IndexBuffer.Data[i + 1], V3 = offset + meshpart.IndexBuffer.Data[i + 2] }); } Console.WriteLine("Saved mesh"); } mshs.Meshes.Add(msh); } mshs.Save(path); Console.WriteLine("Saved MSHS"); }
public override Asset Import(string path) { MSHS mshs = MSHS.Load(path); Model model = new Model(); string name = Path.GetFileNameWithoutExtension(path); int meshnum = 0; foreach (TDRMesh tdrmesh in mshs.Meshes) { ModelMesh mesh = new ModelMesh { Name = $"{name}{meshnum++:0000}" }; ModelMeshPart meshpart = new ModelMeshPart { PrimitiveType = PrimitiveType.Triangles }; SceneManager.Current.UpdateProgress($"Processing {mesh.Name}"); TDRVertex v1 = null, v2 = null, v3 = null; switch (tdrmesh.Mode) { case TDRMesh.MSHMode.NGon: for (int i = 0; i < tdrmesh.Faces.Count; i++) { TDRFace face = tdrmesh.Faces[i]; for (int j = 2; j < face.VertexCount; j++) { int flip = j % 2; v1 = face.Vertices[j - (2 + flip)]; v2 = face.Vertices[j - 1]; v3 = face.Vertices[j - 0]; meshpart.AddFace( new Vector3[] { v1.Position, v2.Position, v3.Position }, new Vector3[] { v1.Normal, v2.Normal, v3.Normal }, new Vector2[] { v1.UV, v2.UV, v3.UV } ); } } break; case TDRMesh.MSHMode.TriIndexedPosition: for (int i = 0; i < tdrmesh.Faces.Count; i++) { TDRFace face = tdrmesh.Faces[i]; v1 = face.Vertices[0]; v2 = face.Vertices[1]; v3 = face.Vertices[2]; meshpart.AddFace( new Vector3[] { tdrmesh.Positions[v1.PositionIndex], tdrmesh.Positions[v2.PositionIndex], tdrmesh.Positions[v3.PositionIndex] }, new Vector3[] { v1.Normal, v2.Normal, v3.Normal }, new Vector2[] { v1.UV, v2.UV, v3.UV } ); } break; case TDRMesh.MSHMode.Tri: for (int i = 0; i < tdrmesh.Faces.Count; i++) { TDRFace face = tdrmesh.Faces[i]; v1 = tdrmesh.Vertices[face.V1]; v2 = tdrmesh.Vertices[face.V2]; v3 = tdrmesh.Vertices[face.V3]; meshpart.AddFace( new Vector3[] { v1.Position, v2.Position, v3.Position }, new Vector3[] { v1.Normal, v2.Normal, v3.Normal }, new Vector2[] { v1.UV, v2.UV, v3.UV } ); } break; } mesh.AddModelMeshPart(meshpart); model.SetName(mesh.Name, model.AddMesh(mesh)); } return(model); }