diff --git a/project/code/TMV/Model.cs b/project/code/TMV/Model.cs index 63708cb..bec2094 100644 --- a/project/code/TMV/Model.cs +++ b/project/code/TMV/Model.cs @@ -62,7 +62,7 @@ public partial class Model : Node3D } } - var mesh = new ArrayMesh(); + var surfaceDataMap = new Dictionary(); foreach (var poly in modelFile.Polygons) { var vertices = new List(); @@ -83,17 +83,26 @@ public partial class Model : Node3D } } - var surfaceData = new MeshSurfaceData(); - surfaceData.AddPolygon(vertices, normal, uvs, uvs); - var array = surfaceData.BuildSurfaceArray(); - mesh.AddSurfaceFromArrays(Mesh.PrimitiveType.Triangles, array); + if (!surfaceDataMap.ContainsKey(poly.Data)) + { + surfaceDataMap.Add(poly.Data, new MeshSurfaceData()); + } + surfaceDataMap[poly.Data].AddPolygon(vertices, normal, uvs, uvs); + } + + var mesh = new ArrayMesh(); + foreach (var (materialId, surfaceData) in surfaceDataMap) + { + var array = surfaceData.BuildSurfaceArray(); + var surfaceIdx = mesh.GetSurfaceCount(); + mesh.AddSurfaceFromArrays(Mesh.PrimitiveType.Triangles, array); for (var i = 0; i < materials.Count; i++) { var m = modelFile.Materials[i]; - if (m.Slot == poly.Data) + if (m.Slot == materialId) { - mesh.SurfaceSetMaterial(poly.Index, materials[i]); + mesh.SurfaceSetMaterial(surfaceIdx, materials[i]); break; } }