3 def compute_render_method_hash(material):
5 for m in material.render_methods:
8 descr += "{}={}".format(m.tag, m.shader)
12 def __init__(self, node_tree, node, socket):
13 self.node_tree = node_tree
20 checks = [self.check_group,
23 self.check_invert_green,
24 self.check_additive_blend,
30 def set_input_from_linked(self, input_sock):
31 from .util import get_linked_node_and_socket
33 from_node, from_sock = get_linked_node_and_socket(self.node_tree, input_sock)
34 self.input = PropertyNode(self.node_tree, from_node, from_sock)
37 def check_group(self):
38 if self.node.type!='GROUP':
41 from .util import get_linked_node_and_socket
43 output = self.node.node_tree.nodes["Group Output"]
44 from_node, from_sock = get_linked_node_and_socket(self.node.node_tree, output.inputs[0])
45 inner = PropertyNode(self.node.node_tree, from_node, from_sock)
47 # TODO This currently only supports a single operation inside the group
48 if inner.input.node.type=='GROUP_INPUT':
49 self.type = inner.type
50 self.data = inner.data
51 return self.set_input_from_linked(self.node.inputs[0])
54 if self.node.type=='RGBTOBW':
56 self.set_input_from_linked(self.node.inputs["Color"])
58 def check_normal(self):
59 if self.node.type=='NORMAL_MAP':
61 return self.set_input_from_linked(self.node.inputs["Color"])
63 def check_invert_green(self):
64 if self.node.type!='COMBRGB':
67 from .util import get_linked_node_and_socket
69 green, _ = get_linked_node_and_socket(self.node_tree, self.node.inputs["G"])
70 if not green or green.type!='MATH' or green.operation!='SUBTRACT':
72 green, _ = get_linked_node_and_socket(self.node_tree, green.inputs[1])
74 red, _ = get_linked_node_and_socket(self.node_tree, self.node.inputs["R"])
75 blue, _ = get_linked_node_and_socket(self.node_tree, self.node.inputs["B"])
76 if not red or red.type!='SEPRGB' or blue!=red or green!=red:
79 self.type = 'INVERT_GREEN'
80 return self.set_input_from_linked(red.inputs["Image"])
82 def check_additive_blend(self):
83 if self.node.type=='ADD_SHADER':
84 from .util import get_linked_node_and_socket
87 shader, _ = get_linked_node_and_socket(self.node_tree, self.node.inputs[i])
88 if shader.type=='BSDF_TRANSPARENT':
89 self.type = 'ADDITIVE'
90 return self.set_input_from_linked(self.node.inputs[1-i])
92 def check_texture(self):
93 if self.node.type=='TEX_IMAGE':
96 def get_unlit_inputs(node_tree, node, additive):
97 from .util import get_linked_node_and_socket
99 if node.type=='MIX_SHADER' and not additive:
100 shader1, _ = get_linked_node_and_socket(node_tree, node.inputs[1])
101 shader2, _ = get_linked_node_and_socket(node_tree, node.inputs[2])
102 if shader1.type=='BSDF_TRANSPARENT' and shader2.type=='EMISSION':
103 factor_input = node.inputs["Fac"]
104 factor_from, _ = get_linked_node_and_socket(node_tree, factor_input)
105 color_input = shader2.inputs["Color"]
106 color_from, _ = get_linked_node_and_socket(node_tree, color_input)
107 if factor_from==color_from:
108 return (color_input, factor_input)
109 elif node.type=='EMISSION':
110 color_input = node.inputs["Color"]
112 color_from, _ = get_linked_node_and_socket(node_tree, color_input)
113 if color_from.type=='MIX_RGB' and color_from.blend_type=='MIX':
114 mix_factor_input = color_from.inputs["Fac"]
115 mix_factor_from, _ = get_linked_node_and_socket(node_tree, mix_factor_input)
116 mix_color_input = color_from.inputs["Color2"]
117 mix_color_from, _ = get_linked_node_and_socket(node_tree, mix_color_input)
118 if mix_factor_from==mix_color_from:
119 return (mix_color_input, mix_factor_input)
120 return (color_input, None)
123 class MaterialProperty:
124 def __init__(self, keyword, tex_keyword, value):
125 self.keyword = keyword
126 self.tex_keyword = tex_keyword
129 self.tex_channels = None
131 def set_from_input(self, node_tree, input_socket, alpha_socket=None):
133 if type(self.value)==tuple:
135 self.value = input_socket.default_value[:len(self.value)-1]+(alpha_socket.default_value,)
137 self.value = input_socket.default_value[:len(self.value)]
139 self.value = input_socket.default_value
142 from .util import get_linked_node_and_socket
144 from_node, from_sock = get_linked_node_and_socket(node_tree, input_socket)
148 top_node = PropertyNode(node_tree, from_node, from_sock)
152 channels = ['R', 'G', 'B']
155 elif n.type=='INVERT_GREEN':
157 elif n.type=='TEXTURE':
158 self.texture = n.node
162 alpha_from, _ = get_linked_node_and_socket(node_tree, alpha_socket)
163 if alpha_from and alpha_from!=self.texture:
164 raise Exception("Separate textures for color and alpha are not supported")
168 self.tex_channels = channels
170 self.tex_channels = ['R', 'G', 'B', 'A']
171 elif type(self.value)==tuple:
172 self.tex_channels = ['R', 'G', 'B']
174 self.tex_channels = ['Y']
176 raise Exception("Unsupported property input node type "+from_node.type)
179 def __init__(self, material):
180 self.name = material.name
184 self.render_mode = material.render_mode
185 self.technique = material.technique
186 self.render_methods = material.render_methods[:]
187 self.uniforms = material.uniforms[:]
188 self.receive_shadows = material.receive_shadows
189 self.cast_shadows = (material.shadow_method!='NONE')
190 self.blend_type = 'ALPHA' if material.blend_method=='BLEND' else 'NONE'
191 self.image_based_lighting = material.image_based_lighting
193 if self.render_mode=='EXTERNAL' and not self.technique:
194 raise Exception("Invalid configuration on material {}: No technique for external rendering".format(self.name))
195 elif self.render_mode=='CUSTOM' and not self.render_methods:
196 raise Exception("Invalid configuration on material {}: No render methods for custom rendering".format(self.name))
198 out_node = next((n for n in material.node_tree.nodes if n.type=='OUTPUT_MATERIAL'), None)
200 raise Exception("No output node found on material {}".format(self.name))
202 from .util import get_linked_node_and_socket
204 surface_node, _ = get_linked_node_and_socket(material.node_tree, out_node.inputs["Surface"])
206 if self.render_mode=='BUILTIN':
207 raise Exception("Invalid configuration on material {}: Empty material with builtin rendering".format(self.name))
210 additive_node, _ = check_additive_blend(material.node_tree, surface_node)
212 self.blend_type = 'ADDITIVE'
213 surface_node = additive_node
215 if surface_node.type=='BSDF_PRINCIPLED':
218 base_color = self.create_property("base_color", (0.8, 0.8, 0.8, 1.0))
219 metalness = self.create_property("metalness", 0.0)
220 roughness = self.create_property("roughness", 0.5)
221 normal = self.create_property("normal_map")
222 emission = self.create_property("emission", (0.0, 0.0, 0.0))
224 base_color.set_from_input(material.node_tree, surface_node.inputs["Base Color"], surface_node.inputs["Alpha"])
225 metalness.set_from_input(material.node_tree, surface_node.inputs["Metallic"])
226 roughness.set_from_input(material.node_tree, surface_node.inputs["Roughness"])
227 normal.set_from_input(material.node_tree, surface_node.inputs["Normal"])
228 emission.set_from_input(material.node_tree, surface_node.inputs["Emission"])
229 elif surface_node.type=='EMISSION' or surface_node.type=='MIX_SHADER':
230 color_input, alpha_input = get_unlit_inputs(material.node_tree, surface_node, self.blend_type=='ADDITIVE')
232 raise Exception("Unsupported configuration for unlit material {}".format(self.name))
236 color = self.create_property("color", "texture", (1.0, 1.0, 1.0, 1.0))
238 color.set_from_input(material.node_tree, color_input, alpha_input)
239 if self.blend_type=='ADDITIVE' and alpha_input:
240 self.blend_type = 'ADDITIVE_ALPHA'
242 raise Exception("Unsupported surface node type {} on material {}".format(surface_node.type, self.name))
244 sampler_settings = None
245 for p in self.properties:
247 settings = (p.texture.interpolation, p.texture.use_mipmap, p.texture.max_anisotropy)
248 if sampler_settings is None:
249 sampler_settings = settings
250 elif settings!=sampler_settings:
251 raise Exception("Material {} has conflicting texture sampler settings".format(self.name))
253 def create_property(self, *args):
256 prop = MaterialProperty(None, args[0], None)
258 prop = MaterialProperty(args[0], args[0]+"_map", args[1])
260 prop = MaterialProperty(*args)
261 self.properties.append(prop)
266 def __init__(self, materials):
267 self.render_mode = materials[0].render_mode
268 if self.render_mode=='EXTERNAL':
269 raise Exception("Material atlas with external render mode does not make sense")
271 if self.render_mode=='CUSTOM':
272 self.render_methods = materials[0].render_methods
274 self.render_methods = None
275 if self.render_methods:
276 self.name = "material_atlas_"+os.path.splitext(self.render_methods[0].shader)[0]
278 self.name = "material_atlas"
279 self.receive_shadows = materials[0].receive_shadows
280 self.cast_shadows = (materials[0].shadow_method!='NONE')
281 self.materials = materials
282 self.material_names = [m.name for m in self.materials]
284 method_hash = compute_render_method_hash(self)
285 for m in self.materials:
286 if m.render_mode!=self.render_mode:
287 raise Exception("Conflicting render modes in MaterialAtlas constructor")
288 if self.render_mode=='CUSTOM' and compute_render_method_hash(m)!=method_hash:
289 raise Exception("Conflicting shaders in MaterialAtlas constructor")
290 if m.receive_shadows!=self.receive_shadows or m.shadow_method!=materials[0].shadow_method:
291 raise Exception("Conflicting shadow settings in MaterialAtlas constructor")
293 count = len(self.materials)
295 while size*size*2<count:
298 self.size = (size, size)
300 self.size = (size*2, size)
302 from .util import get_colormap
304 cm = get_colormap(True)
305 self.base_color_data = ""
306 for m in map(Material, self.materials):
307 if any(p.texture for p in m.properties):
308 raise Exception("Texturing is incompatible with material atlas")
309 base_color = [int(cm(c)*255) for c in m.base_color.value]
310 self.base_color_data += "\\x{:02X}\\x{:02X}\\x{:02X}\\xFF".format(*base_color)
311 self.base_color_data += "\\x00\\x00\\x00\\x00"*(self.size[0]*self.size[1]-count)
313 def get_material_uv(self, material):
314 index = self.material_names.index(material.name)
315 x = index%self.size[0]
316 y = index//self.size[0]
317 return ((x+0.5)/self.size[0], (y+0.5)/self.size[1])
319 def create_material_atlas(context, material):
320 if not material.material_atlas:
321 raise Exception("Material is not part of a material atlas")
323 method_hash = compute_render_method_hash(material)
325 for m in context.blend_data.materials:
326 if m.material_atlas and compute_render_method_hash(m)==method_hash:
329 return MaterialAtlas(materials)