]> git.tdb.fi Git - libs/gl.git/commitdiff
Recognize additional blend types in the Blender exporter
authorMikko Rasa <tdb@tdb.fi>
Sat, 16 Oct 2021 12:54:11 +0000 (15:54 +0300)
committerMikko Rasa <tdb@tdb.fi>
Sat, 16 Oct 2021 16:03:01 +0000 (19:03 +0300)
blender/io_mspgl/export_material.py
blender/io_mspgl/material.py

index 6a194dfbe59e5352a79a3d2f6344c5970e72fbd8..826ab92688429e69cf5b735c06227674fa8fbd64 100644 (file)
@@ -7,8 +7,12 @@ def create_technique_resource(material, resources):
        mat_res = resources[material.name+".mat"]
 
        blend_st = None
-       if material.blended:
+       if material.blend_type=='ALPHA':
                blend_st = Statement("blend", Token("SRC_ALPHA"), Token("ONE_MINUS_SRC_ALPHA"))
+       elif material.blend_type=='ADDITIVE':
+               blend_st = Statement("blend", Token("ONE"), Token("ONE"))
+       elif material.blend_type=='ADDITIVE_ALPHA':
+               blend_st = Statement("blend", Token("SRC_ALPHA"), Token("ONE"))
 
        if material.render_mode=='CUSTOM':
                for m in material.render_methods:
@@ -32,7 +36,7 @@ def create_technique_resource(material, resources):
 
                        tech_res.statements.append(st)
        else:
-               base_method = "blended" if material.blended else ""
+               base_method = "blended" if material.blend_type!='NONE' else ""
                st = Statement("method", base_method)
                if mat_res:
                        st.sub.append(tech_res.create_embed_statement("material", mat_res))
index 88e251b8d7d4f9cd4e569a956a82286a332ddfb8..bb8bd80bf68a0778ba2f2fe70c4bc1762c1fe451 100644 (file)
@@ -39,12 +39,27 @@ def check_invert_green(node_tree, node):
 
        return get_linked_node_and_socket(node_tree, red.inputs["Image"])
 
-def get_unlit_inputs(node_tree, node):
+def check_additive_blend(node_tree, node):
        if node.type=='GROUP':
-               return check_group(node_tree, node, get_unlit_inputs)
-       elif node.type=='MIX_SHADER':
+               return check_group(node_tree, node, check_additive_blend)
+       elif node.type=='ADD_SHADER':
                from .util import get_linked_node_and_socket
 
+               shader1, _ = get_linked_node_and_socket(node_tree, node.inputs[0])
+               shader2, _ = get_linked_node_and_socket(node_tree, node.inputs[1])
+               if shader1.type=='BSDF_TRANSPARENT':
+                       return get_linked_node_and_socket(node_tree, node.inputs[1])
+               elif shader2.type=='BSDF_TRANSPARENT':
+                       return get_linked_node_and_socket(node_tree, node.inputs[0])
+
+       return (None, None)
+
+def get_unlit_inputs(node_tree, node, additive):
+       from .util import get_linked_node_and_socket
+
+       if node.type=='GROUP':
+               return check_group(node_tree, node, get_unlit_inputs)
+       elif node.type=='MIX_SHADER' and not additive:
                shader1, _ = get_linked_node_and_socket(node_tree, node.inputs[1])
                shader2, _ = get_linked_node_and_socket(node_tree, node.inputs[2])
                if shader1.type=='BSDF_TRANSPARENT' and shader2.type=='EMISSION':
@@ -55,7 +70,17 @@ def get_unlit_inputs(node_tree, node):
                        if factor_from==color_from:
                                return (color_input, factor_input)
        elif node.type=='EMISSION':
-               return (node.inputs["Color"], None)
+               color_input = node.inputs["Color"]
+               if additive:
+                       color_from, _ = get_linked_node_and_socket(node_tree, color_input)
+                       if color_from.type=='MIX_RGB' and color_from.blend_type=='MIX':
+                               mix_factor_input = color_from.inputs["Fac"]
+                               mix_factor_from, _ = get_linked_node_and_socket(node_tree, mix_factor_input)
+                               mix_color_input = color_from.inputs["Color2"]
+                               mix_color_from, _ = get_linked_node_and_socket(node_tree, mix_color_input)
+                               if mix_factor_from==mix_color_from:
+                                       return (mix_color_input, mix_factor_input)
+               return (color_input, None)
        return (None, None)
 
 class MaterialProperty:
@@ -124,7 +149,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.blend_type = 'ALPHA' if material.blend_method=='BLEND' else 'NONE'
                self.image_based_lighting = material.image_based_lighting
 
                if self.render_mode=='EXTERNAL' and not self.technique:
@@ -143,7 +168,13 @@ class Material:
                        if self.render_mode=='BUILTIN':
                                raise Exception("Invalid configuration on material {}: Empty material with builtin rendering".format(self.name))
                        return
-               elif surface_node.type=='BSDF_PRINCIPLED':
+
+               additive_node, _ = check_additive_blend(material.node_tree, surface_node)
+               if additive_node:
+                       self.blend_type = 'ADDITIVE'
+                       surface_node = additive_node
+
+               if surface_node.type=='BSDF_PRINCIPLED':
                        self.type = "pbr"
 
                        base_color = self.create_property("base_color", (0.8, 0.8, 0.8, 1.0))
@@ -158,7 +189,7 @@ class Material:
                        normal.set_from_input(material.node_tree, surface_node.inputs["Normal"])
                        emission.set_from_input(material.node_tree, surface_node.inputs["Emission"])
                elif surface_node.type=='EMISSION' or surface_node.type=='MIX_SHADER':
-                       color_input, alpha_input = get_unlit_inputs(material.node_tree, surface_node)
+                       color_input, alpha_input = get_unlit_inputs(material.node_tree, surface_node, self.blend_type=='ADDITIVE')
                        if not color_input:
                                raise Exception("Unsupported configuration for unlit material {}".format(self.name))
 
@@ -167,6 +198,8 @@ class Material:
                        color = self.create_property("color", "texture", (1.0, 1.0, 1.0, 1.0))
 
                        color.set_from_input(material.node_tree, color_input, alpha_input)
+                       if self.blend_type=='ADDITIVE' and alpha_input:
+                               self.blend_type = 'ADDITIVE_ALPHA'
                else:
                        raise Exception("Unsupported surface node type {} on material {}".format(surface_node.type, self.name))