]> git.tdb.fi Git - libs/gl.git/commitdiff
Recognize a node graph which gives transparency to unlit materials
authorMikko Rasa <tdb@tdb.fi>
Fri, 15 Oct 2021 10:48:19 +0000 (13:48 +0300)
committerMikko Rasa <tdb@tdb.fi>
Sat, 16 Oct 2021 10:41:09 +0000 (13:41 +0300)
blender/io_mspgl/material.py

index eb4c7c2444b18da82b6318585ff098b5f2af9e05..167751c34208e2b185bb0bdf382eaa559ef2c03c 100644 (file)
@@ -39,6 +39,25 @@ def check_invert_green(node_tree, node):
 
        return get_linked_node_and_socket(node_tree, red.inputs["Image"])
 
 
        return get_linked_node_and_socket(node_tree, red.inputs["Image"])
 
+def get_unlit_inputs(node_tree, node):
+       if node.type=='GROUP':
+               return check_group(node_tree, node, get_unlit_inputs)
+       elif node.type=='MIX_SHADER':
+               from .util import get_linked_node_and_socket
+
+               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':
+                       factor_input = node.inputs["Fac"]
+                       factor_from, _ = get_linked_node_and_socket(node_tree, factor_input)
+                       color_input = shader2.inputs["Color"]
+                       color_from, _ = get_linked_node_and_socket(node_tree, color_input)
+                       if factor_from==color_from:
+                               return (color_input, factor_input)
+       elif node.type=='EMISSION':
+               return (node.inputs["Color"], None)
+       return (None, None)
+
 class MaterialProperty:
        def __init__(self, keyword, tex_keyword, value):
                self.keyword = keyword
 class MaterialProperty:
        def __init__(self, keyword, tex_keyword, value):
                self.keyword = keyword
@@ -136,12 +155,16 @@ class Material:
                        roughness.set_from_input(material.node_tree, surface_node.inputs["Roughness"])
                        normal.set_from_input(material.node_tree, surface_node.inputs["Normal"])
                        emission.set_from_input(material.node_tree, surface_node.inputs["Emission"])
                        roughness.set_from_input(material.node_tree, surface_node.inputs["Roughness"])
                        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':
+               elif surface_node.type=='EMISSION' or surface_node.type=='MIX_SHADER':
+                       color_input, alpha_input = get_unlit_inputs(material.node_tree, surface_node)
+                       if not color_input:
+                               raise Exception("Unsupported configuration for unlit material {}".format(self.name))
+
                        self.type = "unlit"
 
                        color = self.create_property("color", "texture", (1.0, 1.0, 1.0, 1.0))
 
                        self.type = "unlit"
 
                        color = self.create_property("color", "texture", (1.0, 1.0, 1.0, 1.0))
 
-                       color.set_from_input(material.node_tree, surface_node.inputs["Color"])
+                       color.set_from_input(material.node_tree, color_input, alpha_input)
                else:
                        raise Exception("Unsupported surface node type {} on material {}".format(surface_node.type, self.name))
 
                else:
                        raise Exception("Unsupported surface node type {} on material {}".format(surface_node.type, self.name))