]> 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 842261303ddb01c0ea412fc5e5bde2a6f3b6973f..e92aee2f3e55303c0ddc25461316221e865c7df1 100644 (file)
@@ -18,7 +18,9 @@ class PropertyNode:
                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,
@@ -50,11 +52,28 @@ class PropertyNode:
                                self.data = inner.data
                                return self.set_input_from_linked(self.node.inputs[0])
 
+       def check_scale(self):
+               if self.node.type!='MATH':
+                       return
+
+               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'
@@ -132,6 +151,7 @@ class MaterialProperty:
                self.value = value
                self.texture = None
                self.tex_channels = None
+               self.scale = 1.0
 
        def set_from_input(self, node_tree, input_socket, alpha_socket=None):
                if self.keyword:
@@ -157,8 +177,12 @@ class MaterialProperty:
                                                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
@@ -168,7 +192,12 @@ class MaterialProperty:
                                        if alpha_from and alpha_from!=self.texture:
                                                raise Exception("Separate textures for color and alpha are not supported")
 
-                               if self.texture:
+                               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:
@@ -190,9 +219,11 @@ class Material:
                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:
@@ -206,18 +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
 
-               additive_node, _ = check_additive_blend(material.node_tree, surface_node)
-               if additive_node:
+               surface_node = PropertyNode(material.node_tree, from_node, from_sock)
+               if surface_node.type=='ADDITIVE':
                        self.blend_type = 'ADDITIVE'
-                       surface_node = additive_node
+                       from_node = surface_node.input.node
 
-               if surface_node.type=='BSDF_PRINCIPLED':
+               if from_node.type=='BSDF_PRINCIPLED':
                        self.type = "pbr"
 
                        base_color = self.create_property("base_color", (0.8, 0.8, 0.8, 1.0))
@@ -226,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, self.blend_type=='ADDITIVE')
+                       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))
 
@@ -244,7 +275,7 @@ class Material:
                        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: