X-Git-Url: http://git.tdb.fi/?p=libs%2Fgl.git;a=blobdiff_plain;f=blender%2Fio_mspgl%2Fmaterial.py;h=b30a2ef5d90fef6e7ec1aa1337d6fa5002661136;hp=470ffc5784e1d4fd0b28d006dfe720863a172257;hb=1ebae998eb54c3f33900dc76de3b34c2d5252e58;hpb=736a076cf12aca02492eae6c77eff846bde0cdda diff --git a/blender/io_mspgl/material.py b/blender/io_mspgl/material.py index 470ffc57..b30a2ef5 100644 --- a/blender/io_mspgl/material.py +++ b/blender/io_mspgl/material.py @@ -1,20 +1,256 @@ import os -class MaterialMap: +def compute_render_method_hash(material): + descr = "" + for m in material.render_methods: + if descr: + descr += "," + 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) + +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 .util import get_linked_node_and_socket + + 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, _ = get_linked_node_and_socket(node_tree, green.inputs[1]) + + 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 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 + self.tex_keyword = tex_keyword + self.value = value + self.texture = None + self.tex_channels = None + + def set_from_input(self, node_tree, input_socket, alpha_socket=None): + if self.keyword: + if type(self.value)==tuple: + if alpha_socket: + self.value = input_socket.default_value[:len(self.value)-1]+(alpha_socket.default_value,) + else: + self.value = input_socket.default_value[:len(self.value)] + else: + self.value = input_socket.default_value + + if self.tex_keyword: + from .util import get_linked_node_and_socket + + from_node, _ = get_linked_node_and_socket(node_tree, input_socket) + alpha_from = None + if from_node: + 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 + 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) + if alpha_from and alpha_from!=from_node: + raise Exception("Separate textures for color and alpha are not supported") + + if from_node.type=='TEX_IMAGE': + self.texture = from_node + if channels: + self.tex_channels = channels + elif alpha_from: + self.tex_channels = ['R', 'G', 'B', 'A'] + elif type(self.value)==tuple: + self.tex_channels = ['R', 'G', 'B'] + else: + self.tex_channels = ['Y'] + else: + raise Exception("Unsupported property input node type "+from_node.type) + +class Material: + def __init__(self, material): + self.name = material.name + self.type = None + self.properties = [] + + 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: + raise Exception("Invalid configuration on material {}: No technique for external rendering".format(self.name)) + elif self.render_mode=='CUSTOM' and not self.render_methods: + raise Exception("Invalid configuration on material {}: No render methods for custom rendering".format(self.name)) + + out_node = next((n for n in material.node_tree.nodes if n.type=='OUTPUT_MATERIAL'), None) + if not out_node: + raise Exception("No output node found on material {}".format(self.name)) + + 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: + 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: + 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)) + metalness = self.create_property("metalness", 0.0) + roughness = self.create_property("roughness", 0.5) + 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') + 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, 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)) + + sampler_settings = None + for p in self.properties: + if p.texture: + settings = (p.texture.interpolation, p.texture.use_mipmap, p.texture.max_anisotropy) + if sampler_settings is None: + sampler_settings = settings + elif settings!=sampler_settings: + raise Exception("Material {} has conflicting texture sampler settings".format(self.name)) + + def create_property(self, *args): + prop = None + if len(args)==1: + prop = MaterialProperty(None, args[0], None) + elif len(args)==2: + prop = MaterialProperty(args[0], args[0]+"_map", args[1]) + else: + prop = MaterialProperty(*args) + self.properties.append(prop) + return prop + + +class MaterialAtlas: def __init__(self, materials): - self.technique = materials[0].technique - if self.technique: - self.name = "material_map_"+os.path.splitext(self.technique)[0] + self.render_mode = materials[0].render_mode + if self.render_mode=='EXTERNAL': + raise Exception("Material atlas with external render mode does not make sense") + + if self.render_mode=='CUSTOM': + self.render_methods = materials[0].render_methods + else: + self.render_methods = None + if self.render_methods: + self.name = "material_atlas_"+os.path.splitext(self.render_methods[0].shader)[0] else: - self.name = "material_map" + self.name = "material_atlas" + self.receive_shadows = materials[0].receive_shadows + self.cast_shadows = (materials[0].shadow_method!='NONE') self.materials = materials self.material_names = [m.name for m in self.materials] - self.srgb_colors = materials[0].srgb_colors + self.uniforms = None + method_hash = compute_render_method_hash(self) for m in self.materials: - if m.technique!=self.technique: - raise Exception("Conflicting techniques in MaterialMap constructor") - if m.srgb_colors!=self.srgb_colors: - raise Exception("Conflicting colorspace settings in MaterialMap constructor") + if m.render_mode!=self.render_mode: + raise Exception("Conflicting render modes in MaterialAtlas constructor") + if self.render_mode=='CUSTOM' and compute_render_method_hash(m)!=method_hash: + raise Exception("Conflicting shaders in MaterialAtlas constructor") + if m.receive_shadows!=self.receive_shadows or m.shadow_method!=materials[0].shadow_method: + raise Exception("Conflicting shadow settings in MaterialAtlas constructor") count = len(self.materials) size = 1 @@ -27,12 +263,14 @@ class MaterialMap: from .util import get_colormap - cm = get_colormap(self.srgb_colors) - self.diffuse_data = "" - for m in self.materials: - diff = [int(cm(c)*255) for c in m.diffuse_color] - self.diffuse_data += "\\x{:02X}\\x{:02X}\\x{:02X}\\xFF".format(*diff) - self.diffuse_data += "\\x00\\x00\\x00\\x00"*(self.size[0]*self.size[1]-count) + cm = get_colormap(True) + self.base_color_data = "" + for m in map(Material, self.materials): + if any(p.texture for p in m.properties): + raise Exception("Texturing is incompatible with material atlas") + base_color = [int(cm(c)*255) for c in m.base_color.value] + self.base_color_data += "\\x{:02X}\\x{:02X}\\x{:02X}\\xFF".format(*base_color) + self.base_color_data += "\\x00\\x00\\x00\\x00"*(self.size[0]*self.size[1]-count) def get_material_uv(self, material): index = self.material_names.index(material.name) @@ -40,16 +278,14 @@ class MaterialMap: y = index//self.size[0] return ((x+0.5)/self.size[0], (y+0.5)/self.size[1]) -def create_material_map(context, material): - if not material.material_map: - raise Exception("Material is not part of a material map") +def create_material_atlas(context, material): + if not material.material_atlas: + raise Exception("Material is not part of a material atlas") - tech = material.technique + method_hash = compute_render_method_hash(material) materials = [] for m in context.blend_data.materials: - if m.material_map and m.technique==tech: + if m.material_atlas and compute_render_method_hash(m)==method_hash: materials.append(m) - mat_map = MaterialMap(materials) - - return mat_map + return MaterialAtlas(materials)