checks = [self.check_group,
self.check_scale,
+ self.check_tint,
self.check_gray,
self.check_extract,
self.check_normal,
self.data = self.node.inputs[i].default_value
return self.set_input_from_linked(self.node.inputs[1-i])
+ def check_tint(self):
+ if self.node.type!='MIX_RGB':
+ return
+
+ if self.node.blend_type=='MULTIPLY':
+ for i in range(2):
+ if not self.node.inputs[1+i].is_linked:
+ self.type = 'TINT'
+ self.data = self.node.inputs[1+i].default_value[:]
+ return self.set_input_from_linked(self.node.inputs[2-i])
+
def check_gray(self):
if self.node.type=='RGBTOBW':
self.type = 'GRAY'
return (color_input, None)
return (None, None)
+def get_splat_layers(node_tree, node):
+ from .util import get_linked_node_and_socket
+
+ if node.type!='MIX_SHADER':
+ return
+
+ layers = []
+ while True:
+ factor_from, factor_sock = get_linked_node_and_socket(node_tree, node.inputs["Fac"])
+ if factor_from.type!='SEPRGB':
+ return
+
+ factor_from, _ = get_linked_node_and_socket(node_tree, factor_from.inputs["Image"])
+ if factor_from.type!='VERTEX_COLOR':
+ return
+
+ shader1, _ = get_linked_node_and_socket(node_tree, node.inputs[1])
+ shader2, _ = get_linked_node_and_socket(node_tree, node.inputs[2])
+ layers.append((shader2, factor_from.layer_name, factor_sock.name[0]))
+ if shader1.type=='MIX_SHADER':
+ node = shader1
+ else:
+ layers.append((shader1, None, None))
+ break
+
+ return layers
+
class MaterialProperty:
def __init__(self, keyword, tex_keyword, value):
self.keyword = keyword
self.texture = None
self.tex_channels = None
self.scale = 1.0
+ self.tint = None
def set_from_input(self, node_tree, input_socket, alpha_socket=None):
if self.keyword:
channels = ['~'+c if c in n.data else c for c in channels]
elif n.type=='SCALE':
self.scale = n.data
+ elif n.type=='TINT':
+ self.tint = n.data
elif n.type=='TEXTURE':
self.texture = n.node
n = n.input
else:
raise Exception("Unsupported property input node type "+from_node.type)
+class SubMaterial:
+ def __init__(self):
+ self.properties = []
+ self.weight_source = (None, None)
+
class Material:
def __init__(self, material):
self.name = material.name
self.type = None
self.properties = []
+ self.sub_materials = []
+ self.array_storage = {}
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
+ self.instancing = material.instancing
if self.render_mode=='EXTERNAL' and not self.technique:
raise Exception("Invalid configuration on material {}: No technique for external rendering".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:
+ 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"
+ self.init_pbr_properties(material.node_tree, from_node)
+ elif from_node.type=='EMISSION' or from_node.type=='MIX_SHADER':
+ splat_layers = get_splat_layers(material.node_tree, from_node)
+ if splat_layers:
+ for s in splat_layers:
+ if s[0].type!='BSDF_PRINCIPLED':
+ raise Exception("Unsupported splat layer type {} on splat material {}".format(s[0].type, self.name))
+
+ from .texture import Texture
+
+ self.type = "splat"
+ self.sub_materials = []
+ for l in splat_layers:
+ self.init_pbr_properties(material.node_tree, l[0])
+ sub = SubMaterial()
+ sub.properties = self.properties
+ sub.weight_source = l[1:]
+ self.sub_materials.append(sub)
+ self.properties = []
+
+ for p in sub.properties:
+ if p.texture:
+ texture = Texture(p.texture, p.tex_channels)
+ storage = (texture.pixelformat, texture.width, texture.height)
+ existing = self.array_storage.setdefault(p.tex_keyword, storage)
+ if storage!=existing:
+ raise Exception("Inconsistent storage for {} on splat material {}".format(p.tex_keyword, self.name))
+ else:
+ 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))
+
+ self.type = "unlit"
- 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'
+ 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:
self.properties.append(prop)
return prop
-
-class MaterialAtlas:
- def __init__(self, materials):
- 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_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.uniforms = None
- method_hash = compute_render_method_hash(self)
- for m in self.materials:
- 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
- while size*size*2<count:
- size *= 2
- if size*size>=count:
- self.size = (size, size)
- else:
- self.size = (size*2, size)
-
- from .util import get_colormap
-
- 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)
- x = index%self.size[0]
- y = index//self.size[0]
- return ((x+0.5)/self.size[0], (y+0.5)/self.size[1])
-
-def create_material_atlas(context, material):
- if not material.material_atlas:
- raise Exception("Material is not part of a material atlas")
-
- method_hash = compute_render_method_hash(material)
- materials = []
- for m in context.blend_data.materials:
- if m.material_atlas and compute_render_method_hash(m)==method_hash:
- materials.append(m)
-
- return MaterialAtlas(materials)
+ def init_pbr_properties(self, node_tree, from_node):
+ base_color = self.create_property("base_color", (0.8, 0.8, 0.8, 1.0))
+ tint = self.create_property("tint", (1.0, 1.0, 1.0, 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(node_tree, from_node.inputs["Base Color"], from_node.inputs["Alpha"])
+ if base_color.tint:
+ tint.value = base_color.tint
+ metalness.set_from_input(node_tree, from_node.inputs["Metallic"])
+ roughness.set_from_input(node_tree, from_node.inputs["Roughness"])
+ normal.set_from_input(node_tree, from_node.inputs["Normal"])
+ emission.set_from_input(node_tree, from_node.inputs["Emission"])