]> git.tdb.fi Git - libs/gl.git/blobdiff - blender/io_mspgl/export_object.py
Add an option to use shared meshes when exporting
[libs/gl.git] / blender / io_mspgl / export_object.py
index b910add12fad3ed86ef397861de92fc8bb6a50b9..1fccfb79d1cf26116e506d561d25040bd1da36a4 100644 (file)
@@ -1,4 +1,5 @@
 import os
+import mathutils
 
 def linear_to_srgb(l):
        if l<0.0031308:
@@ -27,6 +28,7 @@ class ObjectExporter:
                self.srgb_colors = True
                self.textures = "REF"
                self.separate_mesh = False
+               self.shared_mesh = True
                self.separate_tech = False
                self.external_tech = True
                self.shared_tech = True
@@ -48,6 +50,18 @@ class ObjectExporter:
                from .outfile import open_output
                out_file = open_output(out_file)
 
+               p1 = max(((v.co, v.co.length) for v in obj.data.vertices), key=lambda x:x[1])[0]
+               p2 = max(((v.co, (v.co-p1).length) for v in obj.data.vertices), key=lambda x:x[1])[0]
+               center = (p1+p2)/2
+               radius = (p1-p2).length/2
+               for v in obj.data.vertices:
+                       d = v.co-center
+                       if d.length>radius:
+                               center += d*(1-radius/d.length)/2
+                               radius = (radius+d.length)/2
+
+               out_file.write("bounding_sphere_hint", center[0], center[1], center[2], radius)
+
                prev_mesh = None
                prev_tech = (None, None)
                for i, l in enumerate(lods):
@@ -57,7 +71,7 @@ class ObjectExporter:
 
                        same_mesh = (l.data.name==prev_mesh)
                        if i==0 or not same_mesh:
-                               mesh = self.export_object_mesh(context, out_file, i, objs, progress)
+                               mesh = self.export_object_mesh(context, out_file, l, objs, progress)
                                prev_mesh = l.data.name
 
                        same_tech = True
@@ -75,15 +89,21 @@ class ObjectExporter:
                        if i>0:
                                out_file.end()
 
-       def export_object_mesh(self, context, out_file, lod_index, objs, progress):
+       def export_object_mesh(self, context, out_file, lod, objs, progress):
                from .export_mesh import MeshExporter
                mesh_export = MeshExporter()
                for k, v in self.__dict__.items():
                        setattr(mesh_export, k, v)
 
+               lod_index = 0
+               if lod.lod_for_parent:
+                       lod_index = lod.lod_index
+
                if self.separate_mesh:
                        from .outfile import open_output
                        path, name = external_name(out_file, ".mesh", lod_index)
+                       if self.shared_mesh:
+                               name = lod.data.name+".mesh"
                        mesh_out = open_output(os.path.join(path, name))
                        mesh = mesh_export.export(context, mesh_out, objs, progress)
                        out_file.write("mesh", '"{}"'.format(name))