From 56da2fc295c1ae1f8d99abad35ec5d59013e2abe Mon Sep 17 00:00:00 2001 From: Luca Di Leo Date: Wed, 25 May 2022 16:37:32 +0200 Subject: [PATCH] Unused materials are now discarded --- Obj2Tiles.Library/Geometry/MeshT.cs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/Obj2Tiles.Library/Geometry/MeshT.cs b/Obj2Tiles.Library/Geometry/MeshT.cs index 37c7aae..414c05b 100644 --- a/Obj2Tiles.Library/Geometry/MeshT.cs +++ b/Obj2Tiles.Library/Geometry/MeshT.cs @@ -942,11 +942,14 @@ private void RemoveUnusedVerticesAndUvs() var newVertexes = new Dictionary(_vertices.Count); var newUvs = new Dictionary(_textureVertices.Count); + var newMaterials = new Dictionary(_materials.Count); for (var f = 0; f < _faces.Count; f++) { var face = _faces[f]; + // Vertices + var vA = _vertices[face.IndexA]; var vB = _vertices[face.IndexB]; var vC = _vertices[face.IndexC]; @@ -966,6 +969,8 @@ private void RemoveUnusedVerticesAndUvs() face.IndexC = newVC; + // Texture vertices + var uvA = _textureVertices[face.TextureIndexA]; var uvB = _textureVertices[face.TextureIndexB]; var uvC = _textureVertices[face.TextureIndexC]; @@ -985,10 +990,20 @@ private void RemoveUnusedVerticesAndUvs() face.TextureIndexC = newUvC; + // Materials + + var material = _materials[face.MaterialIndex]; + + if (!newMaterials.TryGetValue(material, out var newMaterial)) + newMaterial = newMaterials.AddIndex(material); + + face.MaterialIndex = newMaterial; + } _vertices = newVertexes.Keys.ToList(); _textureVertices = newUvs.Keys.ToList(); + _materials = newMaterials.Keys.ToList(); }