X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=blender%2Fio_mspgl%2Fmaterial.py;h=bb8bd80bf68a0778ba2f2fe70c4bc1762c1fe451;hb=9656026f49b0063e035825fec2a96e17b6de93af;hp=eb4c7c2444b18da82b6318585ff098b5f2af9e05;hpb=08942dd73918a51baefbe7344b62afc0cad42e55;p=libs%2Fgl.git diff --git a/blender/io_mspgl/material.py b/blender/io_mspgl/material.py index eb4c7c24..bb8bd80b 100644 --- a/blender/io_mspgl/material.py +++ b/blender/io_mspgl/material.py @@ -39,6 +39,50 @@ def check_invert_green(node_tree, node): return get_linked_node_and_socket(node_tree, red.inputs["Image"]) +def check_additive_blend(node_tree, node): + if node.type=='GROUP': + 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': + 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': + 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: def __init__(self, keyword, tex_keyword, value): self.keyword = keyword @@ -102,8 +146,10 @@ class Material: self.render_mode = material.render_mode self.technique = material.technique self.render_methods = material.render_methods[:] + 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: @@ -122,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)) @@ -136,12 +188,18 @@ 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"]) - 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, self.blend_type=='ADDITIVE') + 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)) - color.set_from_input(material.node_tree, surface_node.inputs["Color"]) + 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))