changeset 355:26b29ca448fc

add: correct ordering of materials
author Sam <sam@basx.dev>
date Wed, 20 Sep 2023 23:25:44 +0700
parents b87dd3eaa9bd
children befa060d782d
files src/semicongine/resources/mesh.nim
diffstat 1 files changed, 12 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/src/semicongine/resources/mesh.nim	Wed Sep 20 22:52:46 2023 +0700
+++ b/src/semicongine/resources/mesh.nim	Wed Sep 20 23:25:44 2023 +0700
@@ -227,9 +227,11 @@
     # but or current mesh/rendering implementation is only designed for a single material
     # currently this is usually handled by adding the values as shader globals
     # TODO: this is bad
-    mesh[].materials.add material
+    if not mesh[].materials.contains(material):
+      mesh[].materials.add material
   else:
-    mesh[].materials.add DEFAULT_MATERIAL
+    if not mesh[].materials.contains(DEFAULT_MATERIAL):
+      mesh[].materials.add DEFAULT_MATERIAL
 
   if primitiveNode.hasKey("indices"):
     assert mesh[].indexType != None
@@ -285,6 +287,14 @@
   for primitive in meshNode["primitives"]:
     result.addPrimitive(root, primitive, mainBuffer)
 
+  var maxMaterialIndex = 0
+  for material in result[].materials:
+    maxMaterialIndex = max(int(material.index), maxMaterialIndex)
+  var materials = result[].materials
+  result[].materials = newSeqWith(maxMaterialIndex + 1, DEFAULT_MATERIAL)
+  for material in materials:
+    result[].materials[material.index] = material
+
 proc loadNode(root: JsonNode, node: JsonNode, mainBuffer: var seq[uint8]): MeshTree =
   result = MeshTree()
   # mesh