Merge model faces that share a material

This commit is contained in:
Jarrod Doyle 2024-08-18 14:01:43 +01:00
parent dbabe1527c
commit eaf20f7f4a
Signed by: Jayrude
GPG Key ID: 38B57B16E7C0ADF7
1 changed files with 16 additions and 7 deletions

View File

@ -62,7 +62,7 @@ public partial class Model : Node3D
} }
} }
var mesh = new ArrayMesh(); var surfaceDataMap = new Dictionary<int, MeshSurfaceData>();
foreach (var poly in modelFile.Polygons) foreach (var poly in modelFile.Polygons)
{ {
var vertices = new List<Vector3>(); var vertices = new List<Vector3>();
@ -83,17 +83,26 @@ public partial class Model : Node3D
} }
} }
var surfaceData = new MeshSurfaceData(); if (!surfaceDataMap.ContainsKey(poly.Data))
surfaceData.AddPolygon(vertices, normal, uvs, uvs); {
var array = surfaceData.BuildSurfaceArray(); surfaceDataMap.Add(poly.Data, new MeshSurfaceData());
mesh.AddSurfaceFromArrays(Mesh.PrimitiveType.Triangles, array); }
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++) for (var i = 0; i < materials.Count; i++)
{ {
var m = modelFile.Materials[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; break;
} }
} }