]> git.tdb.fi Git - libs/gl.git/commitdiff
Refactor creation of batches in the Blender exporter
authorMikko Rasa <tdb@tdb.fi>
Sat, 13 Aug 2022 19:44:58 +0000 (22:44 +0300)
committerMikko Rasa <tdb@tdb.fi>
Sat, 13 Aug 2022 19:44:58 +0000 (22:44 +0300)
blender/io_mspgl/export_mesh.py
blender/io_mspgl/mesh.py

index 012386185faa7755388f3aa108e5d02a093446ec..3f5acf9ed35f5cdd8e9c0a32eee6f0faa0dd406b 100644 (file)
@@ -90,22 +90,10 @@ class MeshExporter:
 
                statements.append(st)
 
-               if mesh.use_strips:
-                       for s in mesh.vertex_sequence:
-                               st = Statement("batch", Token("TRIANGLE_STRIP"))
-                               for i in range(0, len(s), 32):
-                                       st.sub.append(Statement("indices", *(v.index for v in s[i:i+32])))
-                               statements.append(st)
-               else:
-                       st = Statement("batch", Token('TRIANGLES'))
-                       for f in mesh.faces:
-                               st.sub.append(Statement("indices", *(v.index for v in f.vertices)))
-                       statements.append(st)
-
-               if mesh.lines:
-                       st = Statement("batch", Token('LINES'))
-                       for l in mesh.lines:
-                               st.sub.append(Statement("indices", *(v.index for v in l.vertices)))
+               for b in mesh.batches:
+                       st = Statement("batch", Token(b.primitive_type))
+                       for i in range(0, len(b.vertices), 32):
+                               st.sub.append(Statement("indices", *(v.index for v in b.vertices[i:i+32])))
                        statements.append(st)
 
                task.set_progress(1.0)
index bf5d108ba5e27caa68c9767638702aca3b9ea7d6..ba9db8022c7e50652e5ed6258fab1821e1b7f229 100644 (file)
@@ -75,6 +75,12 @@ class VertexGroup:
                        self.weight = 0.0
 
 
+class Batch:
+       def __init__(self, pt):
+               self.primitive_type = pt
+               self.vertices = []
+
+
 class Face:
        def __init__(self, face):
                self.index = face.index
@@ -158,6 +164,7 @@ class Mesh:
                self.use_uv = mesh.use_uv
                self.tangent_uvtex = mesh.tangent_uvtex
                self.use_strips = mesh.use_strips
+               self.use_lines = mesh.use_lines
                self.vertex_groups = mesh.vertex_groups
 
                # Clone basic data
@@ -242,7 +249,7 @@ class Mesh:
                                        if normal_prop and normal_prop.texture:
                                                self.tangent_vecs = True
 
-               self.vertex_sequence = []
+               self.batches = []
 
        def transform(self, matrix):
                for v in self.vertices:
@@ -634,6 +641,17 @@ class Mesh:
                self.reorder_faces(subtask)
 
                subtask = task.task("Building sequence", 1.0)
+               if self.use_strips:
+                       self.build_tristrip_sequence(subtask)
+               else:
+                       self.build_triangle_sequence(subtask)
+
+               if self.use_lines:
+                       self.build_line_sequence()
+
+               self.reorder_vertices()
+
+       def build_tristrip_sequence(self, task):
                sequence = None
                for i, f in enumerate(self.faces):
                        if sequence:
@@ -657,10 +675,23 @@ class Mesh:
                                        sequence += to_add
 
                        if not sequence:
-                               sequence = f.vertices[:]
-                               self.vertex_sequence.append(sequence)
+                               self.batches.append(Batch("TRIANGLE_STRIP"))
+                               sequence = self.batches[-1].vertices
+                               sequence += f.vertices
+
+                       task.set_progress(i/len(self.faces))
+
+       def build_triangle_sequence(self, task):
+               batch = Batch("TRIANGLES")
+               for f in self.faces:
+                       batch.vertices += f.vertices
+               self.batches.append(batch)
 
-                       subtask.set_progress(i/len(self.faces))
+       def build_line_sequence(self):
+               batch = Batch("LINES")
+               for l in self.lines:
+                       batch.vertices += l.vertices
+               self.batches.append(batch)
 
                self.reorder_vertices()
 
@@ -752,8 +783,8 @@ class Mesh:
                        v.index = -1
 
                reordered_vertices = []
-               for s in self.vertex_sequence:
-                       for v in s:
+               for b in self.batches:
+                       for v in b.vertices:
                                if v.index<0:
                                        v.index = len(reordered_vertices)
                                        reordered_vertices.append(v)