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,
25 self.check_invert_channels,
26 self.check_additive_blend,
32 def set_input_from_linked(self, input_sock):
33 from .util import get_linked_node_and_socket
35 from_node, from_sock = get_linked_node_and_socket(self.node_tree, input_sock)
36 self.input = PropertyNode(self.node_tree, from_node, from_sock)
39 def check_group(self):
40 if self.node.type!='GROUP':
43 from .util import get_linked_node_and_socket
45 output = self.node.node_tree.nodes["Group Output"]
46 from_node, from_sock = get_linked_node_and_socket(self.node.node_tree, output.inputs[0])
47 inner = PropertyNode(self.node.node_tree, from_node, from_sock)
49 # TODO This currently only supports a single operation inside the group
50 if inner.input.node.type=='GROUP_INPUT':
51 self.type = inner.type
52 self.data = inner.data
53 return self.set_input_from_linked(self.node.inputs[0])
55 def check_scale(self):
56 if self.node.type!='MATH':
59 if self.node.operation=='MULTIPLY':
61 if not self.node.inputs[i].is_linked:
63 self.data = self.node.inputs[i].default_value
64 return self.set_input_from_linked(self.node.inputs[1-i])
67 if self.node.type=='RGBTOBW':
69 self.set_input_from_linked(self.node.inputs["Color"])
71 def check_extract(self):
72 if self.node.type=='SEPRGB':
74 self.data = self.socket.name[0]
75 return self.set_input_from_linked(self.node.inputs["Image"])
77 def check_normal(self):
78 if self.node.type=='NORMAL_MAP':
80 return self.set_input_from_linked(self.node.inputs["Color"])
82 def check_invert_channels(self):
83 if self.node.type!='COMBRGB':
86 from .util import get_linked_node_and_socket
90 for c in ("R", "G", "B"):
91 from_node, _ = get_linked_node_and_socket(self.node_tree, self.node.inputs[c])
92 if from_node.type=='MATH' and from_node.operation=='SUBTRACT' and not from_node.inputs[0].is_linked and from_node.inputs[0].default_value==1.0:
94 from_node, _ = get_linked_node_and_socket(self.node_tree, from_node.inputs[1])
96 if from_node.type=='SEPRGB' and (separate is None or from_node==separate):
102 self.data = invert_channels
104 return self.set_input_from_linked(separate.inputs["Image"])
106 def check_additive_blend(self):
107 if self.node.type=='ADD_SHADER':
108 from .util import get_linked_node_and_socket
111 shader, _ = get_linked_node_and_socket(self.node_tree, self.node.inputs[i])
112 if shader.type=='BSDF_TRANSPARENT':
113 self.type = 'ADDITIVE'
114 return self.set_input_from_linked(self.node.inputs[1-i])
116 def check_texture(self):
117 if self.node.type=='TEX_IMAGE':
118 self.type = 'TEXTURE'
120 def get_unlit_inputs(node_tree, node, additive):
121 from .util import get_linked_node_and_socket
123 if node.type=='MIX_SHADER' and not additive:
124 shader1, _ = get_linked_node_and_socket(node_tree, node.inputs[1])
125 shader2, _ = get_linked_node_and_socket(node_tree, node.inputs[2])
126 if shader1.type=='BSDF_TRANSPARENT' and shader2.type=='EMISSION':
127 factor_input = node.inputs["Fac"]
128 factor_from, _ = get_linked_node_and_socket(node_tree, factor_input)
129 color_input = shader2.inputs["Color"]
130 color_from, _ = get_linked_node_and_socket(node_tree, color_input)
131 if factor_from==color_from:
132 return (color_input, factor_input)
133 elif node.type=='EMISSION':
134 color_input = node.inputs["Color"]
136 color_from, _ = get_linked_node_and_socket(node_tree, color_input)
137 if color_from.type=='MIX_RGB' and color_from.blend_type=='MIX':
138 mix_factor_input = color_from.inputs["Fac"]
139 mix_factor_from, _ = get_linked_node_and_socket(node_tree, mix_factor_input)
140 mix_color_input = color_from.inputs["Color2"]
141 mix_color_from, _ = get_linked_node_and_socket(node_tree, mix_color_input)
142 if mix_factor_from==mix_color_from:
143 return (mix_color_input, mix_factor_input)
144 return (color_input, None)
147 class MaterialProperty:
148 def __init__(self, keyword, tex_keyword, value):
149 self.keyword = keyword
150 self.tex_keyword = tex_keyword
153 self.tex_channels = None
156 def set_from_input(self, node_tree, input_socket, alpha_socket=None):
158 if type(self.value)==tuple:
160 self.value = input_socket.default_value[:len(self.value)-1]+(alpha_socket.default_value,)
162 self.value = input_socket.default_value[:len(self.value)]
164 self.value = input_socket.default_value
167 from .util import get_linked_node_and_socket
169 from_node, from_sock = get_linked_node_and_socket(node_tree, input_socket)
173 top_node = PropertyNode(node_tree, from_node, from_sock)
177 channels = ['R', 'G', 'B']
180 elif n.type=='EXTRACT':
182 elif n.type=='INVERT':
183 channels = ['~'+c if c in n.data else c for c in channels]
184 elif n.type=='SCALE':
186 elif n.type=='TEXTURE':
187 self.texture = n.node
191 alpha_from, _ = get_linked_node_and_socket(node_tree, alpha_socket)
192 if alpha_from and alpha_from!=self.texture:
193 raise Exception("Separate textures for color and alpha are not supported")
195 if self.scale==0.0 and self.keyword and type(self.value)!=tuple:
197 self.value = self.scale
198 elif self.scale!=1.0:
199 raise Exception("Unsupported material property scale {}".format(self.scale))
202 self.tex_channels = channels
204 self.tex_channels = ['R', 'G', 'B', 'A']
205 elif type(self.value)==tuple:
206 self.tex_channels = ['R', 'G', 'B']
208 self.tex_channels = ['Y']
210 raise Exception("Unsupported property input node type "+from_node.type)
213 def __init__(self, material):
214 self.name = material.name
218 self.render_mode = material.render_mode
219 self.technique = material.technique
220 self.render_methods = material.render_methods[:]
221 self.uniforms = material.uniforms[:]
222 self.face_cull = 'BACK' if material.use_backface_culling else 'NONE'
223 self.receive_shadows = material.receive_shadows
224 self.cast_shadows = (material.shadow_method!='NONE')
225 self.blend_type = 'ALPHA' if material.blend_method=='BLEND' else 'NONE'
226 self.alpha_cutoff = material.alpha_threshold if material.blend_method=='CLIP' else 0.0
227 self.image_based_lighting = material.image_based_lighting
229 if self.render_mode=='EXTERNAL' and not self.technique:
230 raise Exception("Invalid configuration on material {}: No technique for external rendering".format(self.name))
231 elif self.render_mode=='CUSTOM' and not self.render_methods:
232 raise Exception("Invalid configuration on material {}: No render methods for custom rendering".format(self.name))
234 out_node = next((n for n in material.node_tree.nodes if n.type=='OUTPUT_MATERIAL'), None)
236 raise Exception("No output node found on material {}".format(self.name))
238 from .util import get_linked_node_and_socket
240 from_node, from_sock = get_linked_node_and_socket(material.node_tree, out_node.inputs["Surface"])
242 if self.render_mode=='BUILTIN':
243 raise Exception("Invalid configuration on material {}: Empty material with builtin rendering".format(self.name))
246 surface_node = PropertyNode(material.node_tree, from_node, from_sock)
247 if surface_node.type=='ADDITIVE':
248 self.blend_type = 'ADDITIVE'
249 from_node = surface_node.input.node
251 if from_node.type=='BSDF_PRINCIPLED':
254 base_color = self.create_property("base_color", (0.8, 0.8, 0.8, 1.0))
255 metalness = self.create_property("metalness", 0.0)
256 roughness = self.create_property("roughness", 0.5)
257 normal = self.create_property("normal_map")
258 emission = self.create_property("emission", (0.0, 0.0, 0.0))
260 base_color.set_from_input(material.node_tree, from_node.inputs["Base Color"], from_node.inputs["Alpha"])
261 metalness.set_from_input(material.node_tree, from_node.inputs["Metallic"])
262 roughness.set_from_input(material.node_tree, from_node.inputs["Roughness"])
263 normal.set_from_input(material.node_tree, from_node.inputs["Normal"])
264 emission.set_from_input(material.node_tree, from_node.inputs["Emission"])
265 elif from_node.type=='EMISSION' or from_node.type=='MIX_SHADER':
266 color_input, alpha_input = get_unlit_inputs(material.node_tree, from_node, self.blend_type=='ADDITIVE')
268 raise Exception("Unsupported configuration for unlit material {}".format(self.name))
272 color = self.create_property("color", "texture", (1.0, 1.0, 1.0, 1.0))
274 color.set_from_input(material.node_tree, color_input, alpha_input)
275 if self.blend_type=='ADDITIVE' and alpha_input:
276 self.blend_type = 'ADDITIVE_ALPHA'
278 raise Exception("Unsupported surface node type {} on material {}".format(from_node.type, self.name))
280 sampler_settings = None
281 for p in self.properties:
283 settings = (p.texture.interpolation, p.texture.use_mipmap, p.texture.max_anisotropy)
284 if sampler_settings is None:
285 sampler_settings = settings
286 elif settings!=sampler_settings:
287 raise Exception("Material {} has conflicting texture sampler settings".format(self.name))
289 def create_property(self, *args):
292 prop = MaterialProperty(None, args[0], None)
294 prop = MaterialProperty(args[0], args[0]+"_map", args[1])
296 prop = MaterialProperty(*args)
297 self.properties.append(prop)