]> git.tdb.fi Git - libs/gl.git/commitdiff
Recognize transparent materials in Blender and export them accordingly
authorMikko Rasa <tdb@tdb.fi>
Sat, 16 Oct 2021 10:11:49 +0000 (13:11 +0300)
committerMikko Rasa <tdb@tdb.fi>
Sat, 16 Oct 2021 16:03:00 +0000 (19:03 +0300)
Objects with transparency are exported into a separate, Z-sorted
sub-scene.

blender/io_mspgl/export_material.py
blender/io_mspgl/export_scene.py
blender/io_mspgl/material.py
blender/io_mspgl/scene.py

index 7e8f60660b4563eddc2e04f4f93ae2b3ff635e41..62319b8a93efcf878bdd751e52a38894f564e19a 100644 (file)
@@ -1,7 +1,7 @@
 import os
 
 def create_technique_resource(material, resources):
-       from .datafile import Resource, Statement
+       from .datafile import Resource, Statement, Token
        tech_res = Resource(material.name+".tech", "technique")
 
        mat_res = resources[material.name+".mat"]
@@ -25,10 +25,14 @@ def create_technique_resource(material, resources):
 
                        tech_res.statements.append(st)
        else:
-               st = Statement("method", "")
+               base_method = "blended" if material.blended else ""
+               st = Statement("method", base_method)
                if mat_res:
                        st.sub.append(tech_res.create_embed_statement("material", mat_res))
 
+               if material.blended:
+                       ss.sub.append(Statement("blend", Token("SRC_ALPHA"), Token("ONE_MINUS_SRC_ALPHA")))
+
                if material.render_mode!='CUSTOM':
                        if material.receive_shadows:
                                st.sub.append(Statement("receive_shadows", True))
index 1a1bbab062859d794e23361a16cb03d927b40829..162a1882b5e8d135168600da6e0bd2550fd00f21 100644 (file)
@@ -37,19 +37,28 @@ class SceneExporter:
                from .datafile import Resource, Statement, Token
                scene_res = Resource(scene.name+".scene", "scene")
 
-               if scene.background_set:
+               if scene.background_set or (scene.instances and scene.blended_instances):
                        scene_res.statements.append(Statement("type", Token("ordered")))
                        if scene.background_set:
                                scene_res.statements.append(scene_res.create_reference_statement("scene", resources[scene.background_set.name+".scene"]))
 
-                       st = Statement("scene")
-                       st.sub.append(Statement("type", Token("simple")))
-                       self.add_instances(scene_res, st.sub, scene.instances, resources)
-                       scene_res.statements.append(st)
+                       if scene.instances:
+                               st = Statement("scene")
+                               st.sub.append(Statement("type", Token("simple")))
+                               self.add_instances(scene_res, st.sub, scene.instances, resources)
+                               scene_res.statements.append(st)
+
+                       if scene.blended_instances:
+                               st = Statement("scene")
+                               st.sub.append(Statement("type", Token("zsorted")))
+                               self.add_instances(scene_res, st.sub, scene.blended_instances, resources)
+                               scene_res.statements.append(st)
                else:
-                       scene_res.statements.append(Statement("type", Token("simple")))
+                       scene_type = "zsorted" if scene.blended_instances else "simple"
+                       scene_res.statements.append(Statement("type", Token(scene_type)))
 
                        self.add_instances(scene_res, scene_res.statements, scene.instances, resources)
+                       self.add_instances(scene_res, scene_res.statements, scene.blended_instances, resources)
 
                return scene_res
 
@@ -118,11 +127,32 @@ class SceneExporter:
                ss.sub.append(Statement("depth", 1.0))
                seq_res.statements.append(ss)
 
-               ss = Statement("step", "", "content")
-               ss.sub.append(Statement("depth_test", Token("LEQUAL")))
-               ss.sub.append(seq_res.create_reference_statement("lighting", resources[scene.name+".lightn"]))
-               ss.sub.append(seq_res.create_reference_statement("scene", resources[scene.name+".scene"]))
-               seq_res.statements.append(ss)
+               scene_res = resources[scene.name+".scene"]
+               lighting_res = resources[scene.name+".lightn"]
+
+               any_opaque = False
+               any_blended = False
+               s = scene
+               while s:
+                       if s.instances:
+                               any_opaque = True
+                       if s.blended_instances:
+                               any_blended = True
+                       s = s.background_set
+
+               if any_opaque:
+                       ss = Statement("step", "", "content")
+                       ss.sub.append(Statement("depth_test", Token("LEQUAL")))
+                       ss.sub.append(seq_res.create_reference_statement("lighting", lighting_res))
+                       ss.sub.append(seq_res.create_reference_statement("scene", scene_res))
+                       seq_res.statements.append(ss)
+
+               if any_blended:
+                       ss = Statement("step", "blended", "content")
+                       ss.sub.append(Statement("depth_test", Token("LEQUAL")))
+                       ss.sub.append(seq_res.create_reference_statement("lighting", lighting_res))
+                       ss.sub.append(seq_res.create_reference_statement("scene", scene_res))
+                       seq_res.statements.append(ss)
 
                if scene.use_ao:
                        ss = Statement("ambient_occlusion")
index 73f33d5c29a669eccdb235e0823dea95a1086209..88e251b8d7d4f9cd4e569a956a82286a332ddfb8 100644 (file)
@@ -124,6 +124,7 @@ class Material:
                self.uniforms = material.uniforms[:]
                self.receive_shadows = material.receive_shadows
                self.cast_shadows = (material.shadow_method!='NONE')
+               self.blended = (material.blend_method=='BLEND')
                self.image_based_lighting = material.image_based_lighting
 
                if self.render_mode=='EXTERNAL' and not self.technique:
index 28c30ad78b58a452309f9f24552edb6b56647ac2..8f4f285b195d3af543b1a2b64c9ef592a626b6d3 100644 (file)
@@ -23,6 +23,7 @@ class Scene:
                self.camera = scene.camera
                self.prototypes = []
                self.instances = []
+               self.blended_instances = []
                self.lights = []
                self.ambient_light = mathutils.Color((0.0, 0.0, 0.0))
                self.exposure = scene.view_settings.exposure
@@ -55,8 +56,11 @@ class Scene:
                        if o.type=='MESH':
                                clones = [c for c in objects if is_same_object(o, c)]
                                self.prototypes.append(o)
+                               instance_list = self.instances
+                               if o.material_slots and o.material_slots[0].material and o.material_slots[0].material.blend_method=='BLEND':
+                                       instance_list = self.blended_instances
                                for c in clones:
-                                       self.instances.append(Instance(c, o))
+                                       instance_list.append(Instance(c, o))
                                        processed.add(c.name)
                        elif o.type=='LIGHT':
                                self.lights.append(o)