]> git.tdb.fi Git - libs/gl.git/blob - blender/io_mspgl/material.py
Remove support for material atlases from the Blender exporter
[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_gray,
23                         self.check_extract,
24                         self.check_normal,
25                         self.check_invert_channels,
26                         self.check_additive_blend,
27                         self.check_texture]
28                 for c in checks:
29                         if c():
30                                 break
31
32         def set_input_from_linked(self, input_sock):
33                 from .util import get_linked_node_and_socket
34
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)
37                 return self.input
38
39         def check_group(self):
40                 if self.node.type!='GROUP':
41                         return
42
43                 from .util import get_linked_node_and_socket
44
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)
48                 if inner.input:
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])
54
55         def check_scale(self):
56                 if self.node.type!='MATH':
57                         return
58
59                 if self.node.operation=='MULTIPLY':
60                         for i in range(2):
61                                 if not self.node.inputs[i].is_linked:
62                                         self.type = 'SCALE'
63                                         self.data = self.node.inputs[i].default_value
64                                         return self.set_input_from_linked(self.node.inputs[1-i])
65
66         def check_gray(self):
67                 if self.node.type=='RGBTOBW':
68                         self.type = 'GRAY'
69                         self.set_input_from_linked(self.node.inputs["Color"])
70
71         def check_extract(self):
72                 if self.node.type=='SEPRGB':
73                         self.type = 'EXTRACT'
74                         self.data = self.socket.name[0]
75                         return self.set_input_from_linked(self.node.inputs["Image"])
76
77         def check_normal(self):
78                 if self.node.type=='NORMAL_MAP':
79                         self.type = 'NORMAL'
80                         return self.set_input_from_linked(self.node.inputs["Color"])
81
82         def check_invert_channels(self):
83                 if self.node.type!='COMBRGB':
84                         return
85
86                 from .util import get_linked_node_and_socket
87
88                 separate = None
89                 invert_channels = ""
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:
93                                 invert_channels += c
94                                 from_node, _ = get_linked_node_and_socket(self.node_tree, from_node.inputs[1])
95
96                         if from_node.type=='SEPRGB' and (separate is None or from_node==separate):
97                                 separate = from_node
98                         else:
99                                 return
100
101                 self.type = 'INVERT'
102                 self.data = invert_channels
103
104                 return self.set_input_from_linked(separate.inputs["Image"])
105
106         def check_additive_blend(self):
107                 if self.node.type=='ADD_SHADER':
108                         from .util import get_linked_node_and_socket
109
110                         for i in range(2):
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])
115
116         def check_texture(self):
117                 if self.node.type=='TEX_IMAGE':
118                         self.type = 'TEXTURE'
119
120 def get_unlit_inputs(node_tree, node, additive):
121         from .util import get_linked_node_and_socket
122
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"]
135                 if additive:
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)
145         return (None, None)
146
147 class MaterialProperty:
148         def __init__(self, keyword, tex_keyword, value):
149                 self.keyword = keyword
150                 self.tex_keyword = tex_keyword
151                 self.value = value
152                 self.texture = None
153                 self.tex_channels = None
154                 self.scale = 1.0
155
156         def set_from_input(self, node_tree, input_socket, alpha_socket=None):
157                 if self.keyword:
158                         if type(self.value)==tuple:
159                                 if alpha_socket:
160                                         self.value = input_socket.default_value[:len(self.value)-1]+(alpha_socket.default_value,)
161                                 else:
162                                         self.value = input_socket.default_value[:len(self.value)]
163                         else:
164                                 self.value = input_socket.default_value
165
166                 if self.tex_keyword:
167                         from .util import get_linked_node_and_socket
168
169                         from_node, from_sock = get_linked_node_and_socket(node_tree, input_socket)
170                         alpha_from = None
171                         if from_node:
172                                 channels = None
173                                 top_node = PropertyNode(node_tree, from_node, from_sock)
174                                 n = top_node
175                                 while n:
176                                         if n.type=='NORMAL':
177                                                 channels = ['R', 'G', 'B']
178                                         elif n.type=='GRAY':
179                                                 channels = ['Y']
180                                         elif n.type=='EXTRACT':
181                                                 channels = [n.data]
182                                         elif n.type=='INVERT':
183                                                 channels = ['~'+c if c in n.data else c for c in channels]
184                                         elif n.type=='SCALE':
185                                                 self.scale = n.data
186                                         elif n.type=='TEXTURE':
187                                                 self.texture = n.node
188                                         n = n.input
189
190                                 if alpha_socket:
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")
194
195                                 if self.scale==0.0 and self.keyword and type(self.value)!=tuple:
196                                         self.texture = None
197                                         self.value = self.scale
198                                 elif self.scale!=1.0:
199                                         raise Exception("Unsupported material property scale {}".format(self.scale))
200                                 elif self.texture:
201                                         if channels:
202                                                 self.tex_channels = channels
203                                         elif alpha_from:
204                                                 self.tex_channels = ['R', 'G', 'B', 'A']
205                                         elif type(self.value)==tuple:
206                                                 self.tex_channels = ['R', 'G', 'B']
207                                         else:
208                                                 self.tex_channels = ['Y']
209                                 else:
210                                         raise Exception("Unsupported property input node type "+from_node.type)
211
212 class Material:
213         def __init__(self, material):
214                 self.name = material.name
215                 self.type = None
216                 self.properties = []
217
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
228
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))
233
234                 out_node = next((n for n in material.node_tree.nodes if n.type=='OUTPUT_MATERIAL'), None)
235                 if not out_node:
236                         raise Exception("No output node found on material {}".format(self.name))
237
238                 from .util import get_linked_node_and_socket
239
240                 from_node, from_sock = get_linked_node_and_socket(material.node_tree, out_node.inputs["Surface"])
241                 if not from_node:
242                         if self.render_mode=='BUILTIN':
243                                 raise Exception("Invalid configuration on material {}: Empty material with builtin rendering".format(self.name))
244                         return
245
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
250
251                 if from_node.type=='BSDF_PRINCIPLED':
252                         self.type = "pbr"
253
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))
259
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')
267                         if not color_input:
268                                 raise Exception("Unsupported configuration for unlit material {}".format(self.name))
269
270                         self.type = "unlit"
271
272                         color = self.create_property("color", "texture", (1.0, 1.0, 1.0, 1.0))
273
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'
277                 else:
278                         raise Exception("Unsupported surface node type {} on material {}".format(from_node.type, self.name))
279
280                 sampler_settings = None
281                 for p in self.properties:
282                         if p.texture:
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))
288
289         def create_property(self, *args):
290                 prop = None
291                 if len(args)==1:
292                         prop = MaterialProperty(None, args[0], None)
293                 elif len(args)==2:
294                         prop = MaterialProperty(args[0], args[0]+"_map", args[1])
295                 else:
296                         prop = MaterialProperty(*args)
297                 self.properties.append(prop)
298                 return prop