]> git.tdb.fi Git - libs/gl.git/blobdiff - blender/io_mspgl/material.py
Make texture channel handling in the Blender exporter more flexible
[libs/gl.git] / blender / io_mspgl / material.py
index 73f33d5c29a669eccdb235e0823dea95a1086209..b30a2ef5d90fef6e7ec1aa1337d6fa5002661136 100644 (file)
@@ -27,24 +27,39 @@ def check_invert_green(node_tree, node):
 
        from .util import get_linked_node_and_socket
 
-       green, g_sock = get_linked_node_and_socket(node_tree, node.inputs["G"])
+       green, _ = get_linked_node_and_socket(node_tree, node.inputs["G"])
        if not green or green.type!='MATH' or green.operation!='SUBTRACT':
                return (None, None)
-       green, g_sock = get_linked_node_and_socket(node_tree, green.inputs[1])
+       green, _ = get_linked_node_and_socket(node_tree, green.inputs[1])
 
-       red, r_sock = get_linked_node_and_socket(node_tree, node.inputs["R"])
-       blue, b_sock = get_linked_node_and_socket(node_tree, node.inputs["B"])
+       red, _ = get_linked_node_and_socket(node_tree, node.inputs["R"])
+       blue, _ = get_linked_node_and_socket(node_tree, node.inputs["B"])
        if not red or red.type!='SEPRGB' or blue!=red or green!=red:
                return (None, None)
 
        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:
@@ -64,8 +89,7 @@ class MaterialProperty:
                self.tex_keyword = tex_keyword
                self.value = value
                self.texture = None
-               self.tex_usage = None
-               self.invert_green = False
+               self.tex_channels = None
 
        def set_from_input(self, node_tree, input_socket, alpha_socket=None):
                if self.keyword:
@@ -83,16 +107,17 @@ class MaterialProperty:
                        from_node, _ = get_linked_node_and_socket(node_tree, input_socket)
                        alpha_from = None
                        if from_node:
-                               usage = None
+                               channels = None
                                if from_node.type=='NORMAL_MAP':
                                        from_node, _ = get_linked_node_and_socket(node_tree, from_node.inputs["Color"])
                                        invert, _ = check_invert_green(node_tree, from_node)
+                                       channels = ['R', 'G', 'B']
                                        if invert:
                                                from_node = invert
-                                               self.invert_green = True
-                                       usage = 'RGB'
+                                               channels[1] = '~G';
                                elif from_node.type=='RGBTOBW':
                                        from_node, _ = get_linked_node_and_socket(node_tree, from_node.inputs["Color"])
+                                       channels = ['Y']
 
                                if alpha_socket:
                                        alpha_from, _ = get_linked_node_and_socket(node_tree, alpha_socket)
@@ -101,14 +126,14 @@ class MaterialProperty:
 
                                if from_node.type=='TEX_IMAGE':
                                        self.texture = from_node
-                                       if usage:
-                                               self.tex_usage = usage
+                                       if channels:
+                                               self.tex_channels = channels
                                        elif alpha_from:
-                                               self.tex_usage = 'RGBA'
+                                               self.tex_channels = ['R', 'G', 'B', 'A']
                                        elif type(self.value)==tuple:
-                                               self.tex_usage = 'RGB'
+                                               self.tex_channels = ['R', 'G', 'B']
                                        else:
-                                               self.tex_usage = 'GRAY'
+                                               self.tex_channels = ['Y']
                                else:
                                        raise Exception("Unsupported property input node type "+from_node.type)
 
@@ -124,6 +149,7 @@ class Material:
                self.uniforms = material.uniforms[:]
                self.receive_shadows = material.receive_shadows
                self.cast_shadows = (material.shadow_method!='NONE')
+               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:
@@ -142,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))
@@ -157,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))
 
@@ -166,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))