From 3d6f8d2781beb692e11488c34871ec62174e58df Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Sat, 13 Aug 2022 22:44:58 +0300 Subject: [PATCH] Refactor creation of batches in the Blender exporter --- blender/io_mspgl/export_mesh.py | 20 +++------------ blender/io_mspgl/mesh.py | 43 ++++++++++++++++++++++++++++----- 2 files changed, 41 insertions(+), 22 deletions(-) diff --git a/blender/io_mspgl/export_mesh.py b/blender/io_mspgl/export_mesh.py index 01238618..3f5acf9e 100644 --- a/blender/io_mspgl/export_mesh.py +++ b/blender/io_mspgl/export_mesh.py @@ -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) diff --git a/blender/io_mspgl/mesh.py b/blender/io_mspgl/mesh.py index bf5d108b..ba9db802 100644 --- a/blender/io_mspgl/mesh.py +++ b/blender/io_mspgl/mesh.py @@ -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) -- 2.45.2