]> git.tdb.fi Git - libs/gl.git/blob - blender/io_mspgl/material.py
3e2bce001943d4c26090129dcd9650153abae9a1
[libs/gl.git] / blender / io_mspgl / material.py
1 import os
2
3 def compute_render_method_hash(material):
4         descr = ""
5         for m in material.render_methods:
6                 if descr:
7                         descr += ","
8                 descr += "{}={}".format(m.tag, m.shader)
9         return hash(descr)
10
11 class PropertyNode:
12         def __init__(self, node_tree, node, socket):
13                 self.node_tree = node_tree
14                 self.node = node
15                 self.socket = socket
16                 self.type = None
17                 self.data = None
18                 self.input = None
19
20                 checks = [self.check_group,
21                         self.check_scale,
22                         self.check_tint,
23                         self.check_gray,
24                         self.check_extract,
25                         self.check_normal,
26                         self.check_invert_channels,
27                         self.check_additive_blend,
28                         self.check_texture]
29                 for c in checks:
30                         if c():
31                                 break
32
33         def set_input_from_linked(self, input_sock):
34                 from .util import get_linked_node_and_socket
35
36                 from_node, from_sock = get_linked_node_and_socket(self.node_tree, input_sock)
37                 self.input = PropertyNode(self.node_tree, from_node, from_sock)
38                 return self.input
39
40         def check_group(self):
41                 if self.node.type!='GROUP':
42                         return
43
44                 from .util import get_linked_node_and_socket
45
46                 output = self.node.node_tree.nodes["Group Output"]
47                 from_node, from_sock = get_linked_node_and_socket(self.node.node_tree, output.inputs[0])
48                 inner = PropertyNode(self.node.node_tree, from_node, from_sock)
49                 if inner.input:
50                         # TODO This currently only supports a single operation inside the group
51                         if inner.input.node.type=='GROUP_INPUT':
52                                 self.type = inner.type
53                                 self.data = inner.data
54                                 return self.set_input_from_linked(self.node.inputs[0])
55
56         def check_scale(self):
57                 if self.node.type!='MATH':
58                         return
59
60                 if self.node.operation=='MULTIPLY':
61                         for i in range(2):
62                                 if not self.node.inputs[i].is_linked:
63                                         self.type = 'SCALE'
64                                         self.data = self.node.inputs[i].default_value
65                                         return self.set_input_from_linked(self.node.inputs[1-i])
66
67         def check_tint(self):
68                 if self.node.type!='MIX_RGB':
69                         return
70
71                 if self.node.blend_type=='MULTIPLY':
72                         for i in range(2):
73                                 if not self.node.inputs[1+i].is_linked:
74                                         self.type = 'TINT'
75                                         self.data = self.node.inputs[1+i].default_value[:]
76                                         return self.set_input_from_linked(self.node.inputs[2-i])
77
78         def check_gray(self):
79                 if self.node.type=='RGBTOBW':
80                         self.type = 'GRAY'
81                         self.set_input_from_linked(self.node.inputs["Color"])
82
83         def check_extract(self):
84                 if self.node.type=='SEPRGB':
85                         self.type = 'EXTRACT'
86                         self.data = self.socket.name[0]
87                         return self.set_input_from_linked(self.node.inputs["Image"])
88
89         def check_normal(self):
90                 if self.node.type=='NORMAL_MAP':
91                         self.type = 'NORMAL'
92                         return self.set_input_from_linked(self.node.inputs["Color"])
93
94         def check_invert_channels(self):
95                 if self.node.type!='COMBRGB':
96                         return
97
98                 from .util import get_linked_node_and_socket
99
100                 separate = None
101                 invert_channels = ""
102                 for c in ("R", "G", "B"):
103                         from_node, _ = get_linked_node_and_socket(self.node_tree, self.node.inputs[c])
104                         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:
105                                 invert_channels += c
106                                 from_node, _ = get_linked_node_and_socket(self.node_tree, from_node.inputs[1])
107
108                         if from_node.type=='SEPRGB' and (separate is None or from_node==separate):
109                                 separate = from_node
110                         else:
111                                 return
112
113                 self.type = 'INVERT'
114                 self.data = invert_channels
115
116                 return self.set_input_from_linked(separate.inputs["Image"])
117
118         def check_additive_blend(self):
119                 if self.node.type=='ADD_SHADER':
120                         from .util import get_linked_node_and_socket
121
122                         for i in range(2):
123                                 shader, _ = get_linked_node_and_socket(self.node_tree, self.node.inputs[i])
124                                 if shader.type=='BSDF_TRANSPARENT':
125                                         self.type = 'ADDITIVE'
126                                         return self.set_input_from_linked(self.node.inputs[1-i])
127
128         def check_texture(self):
129                 if self.node.type=='TEX_IMAGE':
130                         self.type = 'TEXTURE'
131
132 def get_unlit_inputs(node_tree, node, additive):
133         from .util import get_linked_node_and_socket
134
135         if node.type=='MIX_SHADER' and not additive:
136                 shader1, _ = get_linked_node_and_socket(node_tree, node.inputs[1])
137                 shader2, _ = get_linked_node_and_socket(node_tree, node.inputs[2])
138                 if shader1.type=='BSDF_TRANSPARENT' and shader2.type=='EMISSION':
139                         factor_input = node.inputs["Fac"]
140                         factor_from, _ = get_linked_node_and_socket(node_tree, factor_input)
141                         color_input = shader2.inputs["Color"]
142                         color_from, _ = get_linked_node_and_socket(node_tree, color_input)
143                         if factor_from==color_from:
144                                 return (color_input, factor_input)
145         elif node.type=='EMISSION':
146                 color_input = node.inputs["Color"]
147                 if additive:
148                         color_from, _ = get_linked_node_and_socket(node_tree, color_input)
149                         if color_from.type=='MIX_RGB' and color_from.blend_type=='MIX':
150                                 mix_factor_input = color_from.inputs["Fac"]
151                                 mix_factor_from, _ = get_linked_node_and_socket(node_tree, mix_factor_input)
152                                 mix_color_input = color_from.inputs["Color2"]
153                                 mix_color_from, _ = get_linked_node_and_socket(node_tree, mix_color_input)
154                                 if mix_factor_from==mix_color_from:
155                                         return (mix_color_input, mix_factor_input)
156                 return (color_input, None)
157         return (None, None)
158
159 class MaterialProperty:
160         def __init__(self, keyword, tex_keyword, value):
161                 self.keyword = keyword
162                 self.tex_keyword = tex_keyword
163                 self.value = value
164                 self.texture = None
165                 self.tex_channels = None
166                 self.scale = 1.0
167                 self.tint = None
168
169         def set_from_input(self, node_tree, input_socket, alpha_socket=None):
170                 if self.keyword:
171                         if type(self.value)==tuple:
172                                 if alpha_socket:
173                                         self.value = input_socket.default_value[:len(self.value)-1]+(alpha_socket.default_value,)
174                                 else:
175                                         self.value = input_socket.default_value[:len(self.value)]
176                         else:
177                                 self.value = input_socket.default_value
178
179                 if self.tex_keyword:
180                         from .util import get_linked_node_and_socket
181
182                         from_node, from_sock = get_linked_node_and_socket(node_tree, input_socket)
183                         alpha_from = None
184                         if from_node:
185                                 channels = None
186                                 top_node = PropertyNode(node_tree, from_node, from_sock)
187                                 n = top_node
188                                 while n:
189                                         if n.type=='NORMAL':
190                                                 channels = ['R', 'G', 'B']
191                                         elif n.type=='GRAY':
192                                                 channels = ['Y']
193                                         elif n.type=='EXTRACT':
194                                                 channels = [n.data]
195                                         elif n.type=='INVERT':
196                                                 channels = ['~'+c if c in n.data else c for c in channels]
197                                         elif n.type=='SCALE':
198                                                 self.scale = n.data
199                                         elif n.type=='TINT':
200                                                 self.tint = n.data
201                                         elif n.type=='TEXTURE':
202                                                 self.texture = n.node
203                                         n = n.input
204
205                                 if alpha_socket:
206                                         alpha_from, _ = get_linked_node_and_socket(node_tree, alpha_socket)
207                                         if alpha_from and alpha_from!=self.texture:
208                                                 raise Exception("Separate textures for color and alpha are not supported")
209
210                                 if self.scale==0.0 and self.keyword and type(self.value)!=tuple:
211                                         self.texture = None
212                                         self.value = self.scale
213                                 elif self.scale!=1.0:
214                                         raise Exception("Unsupported material property scale {}".format(self.scale))
215                                 elif self.texture:
216                                         if channels:
217                                                 self.tex_channels = channels
218                                         elif alpha_from:
219                                                 self.tex_channels = ['R', 'G', 'B', 'A']
220                                         elif type(self.value)==tuple:
221                                                 self.tex_channels = ['R', 'G', 'B']
222                                         else:
223                                                 self.tex_channels = ['Y']
224                                 else:
225                                         raise Exception("Unsupported property input node type "+from_node.type)
226
227 class Material:
228         def __init__(self, material):
229                 self.name = material.name
230                 self.type = None
231                 self.properties = []
232
233                 self.render_mode = material.render_mode
234                 self.technique = material.technique
235                 self.render_methods = material.render_methods[:]
236                 self.uniforms = material.uniforms[:]
237                 self.face_cull = 'BACK' if material.use_backface_culling else 'NONE'
238                 self.receive_shadows = material.receive_shadows
239                 self.cast_shadows = (material.shadow_method!='NONE')
240                 self.blend_type = 'ALPHA' if material.blend_method=='BLEND' else 'NONE'
241                 self.alpha_cutoff = material.alpha_threshold if material.blend_method=='CLIP' else 0.0
242                 self.image_based_lighting = material.image_based_lighting
243                 self.instancing = material.instancing
244
245                 if self.render_mode=='EXTERNAL' and not self.technique:
246                         raise Exception("Invalid configuration on material {}: No technique for external rendering".format(self.name))
247                 elif self.render_mode=='CUSTOM' and not self.render_methods:
248                         raise Exception("Invalid configuration on material {}: No render methods for custom rendering".format(self.name))
249
250                 out_node = next((n for n in material.node_tree.nodes if n.type=='OUTPUT_MATERIAL'), None)
251                 if not out_node:
252                         raise Exception("No output node found on material {}".format(self.name))
253
254                 from .util import get_linked_node_and_socket
255
256                 from_node, from_sock = get_linked_node_and_socket(material.node_tree, out_node.inputs["Surface"])
257                 if not from_node:
258                         if self.render_mode=='BUILTIN':
259                                 raise Exception("Invalid configuration on material {}: Empty material with builtin rendering".format(self.name))
260                         return
261
262                 surface_node = PropertyNode(material.node_tree, from_node, from_sock)
263                 if surface_node.type=='ADDITIVE':
264                         self.blend_type = 'ADDITIVE'
265                         from_node = surface_node.input.node
266
267                 if from_node.type=='BSDF_PRINCIPLED':
268                         self.type = "pbr"
269
270                         base_color = self.create_property("base_color", (0.8, 0.8, 0.8, 1.0))
271                         tint = self.create_property("tint", (1.0, 1.0, 1.0, 1.0))
272                         metalness = self.create_property("metalness", 0.0)
273                         roughness = self.create_property("roughness", 0.5)
274                         normal = self.create_property("normal_map")
275                         emission = self.create_property("emission", (0.0, 0.0, 0.0))
276
277                         base_color.set_from_input(material.node_tree, from_node.inputs["Base Color"], from_node.inputs["Alpha"])
278                         if base_color.tint:
279                                 tint.value = base_color.tint
280                         metalness.set_from_input(material.node_tree, from_node.inputs["Metallic"])
281                         roughness.set_from_input(material.node_tree, from_node.inputs["Roughness"])
282                         normal.set_from_input(material.node_tree, from_node.inputs["Normal"])
283                         emission.set_from_input(material.node_tree, from_node.inputs["Emission"])
284                 elif from_node.type=='EMISSION' or from_node.type=='MIX_SHADER':
285                         color_input, alpha_input = get_unlit_inputs(material.node_tree, from_node, self.blend_type=='ADDITIVE')
286                         if not color_input:
287                                 raise Exception("Unsupported configuration for unlit material {}".format(self.name))
288
289                         self.type = "unlit"
290
291                         color = self.create_property("color", "texture", (1.0, 1.0, 1.0, 1.0))
292
293                         color.set_from_input(material.node_tree, color_input, alpha_input)
294                         if self.blend_type=='ADDITIVE' and alpha_input:
295                                 self.blend_type = 'ADDITIVE_ALPHA'
296                 else:
297                         raise Exception("Unsupported surface node type {} on material {}".format(from_node.type, self.name))
298
299                 sampler_settings = None
300                 for p in self.properties:
301                         if p.texture:
302                                 settings = (p.texture.interpolation, p.texture.use_mipmap, p.texture.max_anisotropy)
303                                 if sampler_settings is None:
304                                         sampler_settings = settings
305                                 elif settings!=sampler_settings:
306                                         raise Exception("Material {} has conflicting texture sampler settings".format(self.name))
307
308         def create_property(self, *args):
309                 prop = None
310                 if len(args)==1:
311                         prop = MaterialProperty(None, args[0], None)
312                 elif len(args)==2:
313                         prop = MaterialProperty(args[0], args[0]+"_map", args[1])
314                 else:
315                         prop = MaterialProperty(*args)
316                 self.properties.append(prop)
317                 return prop