]> git.tdb.fi Git - libs/gl.git/blobdiff - blender/io_mspgl/material.py
Refactor face cull settings in Blender
[libs/gl.git] / blender / io_mspgl / material.py
index 167751c34208e2b185bb0bdf382eaa559ef2c03c..e92aee2f3e55303c0ddc25461316221e865c7df1 100644 (file)
@@ -8,43 +8,119 @@ def compute_render_method_hash(material):
                descr += "{}={}".format(m.tag, m.shader)
        return hash(descr)
 
-def check_group(node_tree, group, func):
-       from .util import get_linked_node_and_socket
-
-       output = group.node_tree.nodes["Group Output"]
-       from_node, _ = get_linked_node_and_socket(group.node_tree, output.inputs[0])
-       if from_node:
-               from_node, _ = func(group.node_tree, from_node)
-               if from_node and from_node.type=='GROUP_INPUT':
-                       return get_linked_node_and_socket(node_tree, group.inputs[0])
-       return (None, None)
+class PropertyNode:
+       def __init__(self, node_tree, node, socket):
+               self.node_tree = node_tree
+               self.node = node
+               self.socket = socket
+               self.type = None
+               self.data = None
+               self.input = None
+
+               checks = [self.check_group,
+                       self.check_scale,
+                       self.check_gray,
+                       self.check_extract,
+                       self.check_normal,
+                       self.check_invert_channels,
+                       self.check_additive_blend,
+                       self.check_texture]
+               for c in checks:
+                       if c():
+                               break
+
+       def set_input_from_linked(self, input_sock):
+               from .util import get_linked_node_and_socket
 
-def check_invert_green(node_tree, node):
-       if node.type=='GROUP':
-               return check_group(node_tree, node, check_invert_green)
-       elif node.type!='COMBRGB':
-               return (None, None)
+               from_node, from_sock = get_linked_node_and_socket(self.node_tree, input_sock)
+               self.input = PropertyNode(self.node_tree, from_node, from_sock)
+               return self.input
 
-       from .util import get_linked_node_and_socket
+       def check_group(self):
+               if self.node.type!='GROUP':
+                       return
 
-       green, g_sock = 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])
+               from .util import get_linked_node_and_socket
 
-       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"])
-       if not red or red.type!='SEPRGB' or blue!=red or green!=red:
-               return (None, None)
+               output = self.node.node_tree.nodes["Group Output"]
+               from_node, from_sock = get_linked_node_and_socket(self.node.node_tree, output.inputs[0])
+               inner = PropertyNode(self.node.node_tree, from_node, from_sock)
+               if inner.input:
+                       # TODO This currently only supports a single operation inside the group
+                       if inner.input.node.type=='GROUP_INPUT':
+                               self.type = inner.type
+                               self.data = inner.data
+                               return self.set_input_from_linked(self.node.inputs[0])
+
+       def check_scale(self):
+               if self.node.type!='MATH':
+                       return
 
-       return get_linked_node_and_socket(node_tree, red.inputs["Image"])
+               if self.node.operation=='MULTIPLY':
+                       for i in range(2):
+                               if not self.node.inputs[i].is_linked:
+                                       self.type = 'SCALE'
+                                       self.data = self.node.inputs[i].default_value
+                                       return self.set_input_from_linked(self.node.inputs[1-i])
+
+       def check_gray(self):
+               if self.node.type=='RGBTOBW':
+                       self.type = 'GRAY'
+                       self.set_input_from_linked(self.node.inputs["Color"])
+
+       def check_extract(self):
+               if self.node.type=='SEPRGB':
+                       self.type = 'EXTRACT'
+                       self.data = self.socket.name[0]
+                       return self.set_input_from_linked(self.node.inputs["Image"])
+
+       def check_normal(self):
+               if self.node.type=='NORMAL_MAP':
+                       self.type = 'NORMAL'
+                       return self.set_input_from_linked(self.node.inputs["Color"])
+
+       def check_invert_channels(self):
+               if self.node.type!='COMBRGB':
+                       return
 
-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
 
+               separate = None
+               invert_channels = ""
+               for c in ("R", "G", "B"):
+                       from_node, _ = get_linked_node_and_socket(self.node_tree, self.node.inputs[c])
+                       if from_node.type=='MATH' and from_node.operation=='SUBTRACT' and not from_node.inputs[0].is_linked and from_node.inputs[0].default_value==1.0:
+                               invert_channels += c
+                               from_node, _ = get_linked_node_and_socket(self.node_tree, from_node.inputs[1])
+
+                       if from_node.type=='SEPRGB' and (separate is None or from_node==separate):
+                               separate = from_node
+                       else:
+                               return
+
+               self.type = 'INVERT'
+               self.data = invert_channels
+
+               return self.set_input_from_linked(separate.inputs["Image"])
+
+       def check_additive_blend(self):
+               if self.node.type=='ADD_SHADER':
+                       from .util import get_linked_node_and_socket
+
+                       for i in range(2):
+                               shader, _ = get_linked_node_and_socket(self.node_tree, self.node.inputs[i])
+                               if shader.type=='BSDF_TRANSPARENT':
+                                       self.type = 'ADDITIVE'
+                                       return self.set_input_from_linked(self.node.inputs[1-i])
+
+       def check_texture(self):
+               if self.node.type=='TEX_IMAGE':
+                       self.type = 'TEXTURE'
+
+def get_unlit_inputs(node_tree, node, additive):
+       from .util import get_linked_node_and_socket
+
+       if 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 +131,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 +150,8 @@ 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
+               self.scale = 1.0
 
        def set_from_input(self, node_tree, input_socket, alpha_socket=None):
                if self.keyword:
@@ -80,35 +166,46 @@ class MaterialProperty:
                if self.tex_keyword:
                        from .util import get_linked_node_and_socket
 
-                       from_node, _ = get_linked_node_and_socket(node_tree, input_socket)
+                       from_node, from_sock = get_linked_node_and_socket(node_tree, input_socket)
                        alpha_from = None
                        if from_node:
-                               usage = 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)
-                                       if invert:
-                                               from_node = invert
-                                               self.invert_green = True
-                                       usage = 'RGB'
-                               elif from_node.type=='RGBTOBW':
-                                       from_node, _ = get_linked_node_and_socket(node_tree, from_node.inputs["Color"])
+                               channels = None
+                               top_node = PropertyNode(node_tree, from_node, from_sock)
+                               n = top_node
+                               while n:
+                                       if n.type=='NORMAL':
+                                               channels = ['R', 'G', 'B']
+                                       elif n.type=='GRAY':
+                                               channels = ['Y']
+                                       elif n.type=='EXTRACT':
+                                               channels = [n.data]
+                                       elif n.type=='INVERT':
+                                               channels = ['~'+c if c in n.data else c for c in channels]
+                                       elif n.type=='SCALE':
+                                               self.scale = n.data
+                                       elif n.type=='TEXTURE':
+                                               self.texture = n.node
+                                       n = n.input
 
                                if alpha_socket:
                                        alpha_from, _ = get_linked_node_and_socket(node_tree, alpha_socket)
-                                       if alpha_from and alpha_from!=from_node:
+                                       if alpha_from and alpha_from!=self.texture:
                                                raise Exception("Separate textures for color and alpha are not supported")
 
-                               if from_node.type=='TEX_IMAGE':
-                                       self.texture = from_node
-                                       if usage:
-                                               self.tex_usage = usage
+                               if self.scale==0.0 and self.keyword and type(self.value)!=tuple:
+                                       self.texture = None
+                                       self.value = self.scale
+                               elif self.scale!=1.0:
+                                       raise Exception("Unsupported material property scale {}".format(self.scale))
+                               elif self.texture:
+                                       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)
 
@@ -121,8 +218,12 @@ class Material:
                self.render_mode = material.render_mode
                self.technique = material.technique
                self.render_methods = material.render_methods[:]
+               self.uniforms = material.uniforms[:]
+               self.face_cull = 'BACK' if material.use_backface_culling else 'NONE'
                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.alpha_cutoff = material.alpha_threshold if material.blend_method=='CLIP' else 0.0
                self.image_based_lighting = material.image_based_lighting
 
                if self.render_mode=='EXTERNAL' and not self.technique:
@@ -136,12 +237,18 @@ class Material:
 
                from .util import get_linked_node_and_socket
 
-               surface_node, _ = get_linked_node_and_socket(material.node_tree, out_node.inputs["Surface"])
-               if not surface_node:
+               from_node, from_sock = get_linked_node_and_socket(material.node_tree, out_node.inputs["Surface"])
+               if not from_node:
                        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':
+
+               surface_node = PropertyNode(material.node_tree, from_node, from_sock)
+               if surface_node.type=='ADDITIVE':
+                       self.blend_type = 'ADDITIVE'
+                       from_node = surface_node.input.node
+
+               if from_node.type=='BSDF_PRINCIPLED':
                        self.type = "pbr"
 
                        base_color = self.create_property("base_color", (0.8, 0.8, 0.8, 1.0))
@@ -150,13 +257,13 @@ class Material:
                        normal = self.create_property("normal_map")
                        emission = self.create_property("emission", (0.0, 0.0, 0.0))
 
-                       base_color.set_from_input(material.node_tree, surface_node.inputs["Base Color"], surface_node.inputs["Alpha"])
-                       metalness.set_from_input(material.node_tree, surface_node.inputs["Metallic"])
-                       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' or surface_node.type=='MIX_SHADER':
-                       color_input, alpha_input = get_unlit_inputs(material.node_tree, surface_node)
+                       base_color.set_from_input(material.node_tree, from_node.inputs["Base Color"], from_node.inputs["Alpha"])
+                       metalness.set_from_input(material.node_tree, from_node.inputs["Metallic"])
+                       roughness.set_from_input(material.node_tree, from_node.inputs["Roughness"])
+                       normal.set_from_input(material.node_tree, from_node.inputs["Normal"])
+                       emission.set_from_input(material.node_tree, from_node.inputs["Emission"])
+               elif from_node.type=='EMISSION' or from_node.type=='MIX_SHADER':
+                       color_input, alpha_input = get_unlit_inputs(material.node_tree, from_node, self.blend_type=='ADDITIVE')
                        if not color_input:
                                raise Exception("Unsupported configuration for unlit material {}".format(self.name))
 
@@ -165,8 +272,10 @@ 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))
+                       raise Exception("Unsupported surface node type {} on material {}".format(from_node.type, self.name))
 
                sampler_settings = None
                for p in self.properties: