]> git.tdb.fi Git - libs/gl.git/blob - blender/io_mspgl/material.py
Rename material map to material atlas in the exporter
[libs/gl.git] / blender / io_mspgl / material.py
1 import os
2
3 def get_linked_node_and_socket(node_tree, socket):
4         for l in node_tree.links:
5                 if socket==l.to_socket:
6                         return (l.from_node, l.from_socket)
7                 elif socket==l.from_socket:
8                         return (l.to_node, l.to_socket)
9         return (None, None)
10
11 class MaterialProperty:
12         def __init__(self, keyword, tex_keyword, value):
13                 self.keyword = keyword
14                 self.tex_keyword = tex_keyword
15                 self.value = value
16                 self.texture = None
17                 self.tex_usage = None
18
19         def set_from_input(self, node_tree, input_socket, alpha_socket=None):
20                 if self.keyword:
21                         if type(self.value)==tuple:
22                                 if alpha_socket:
23                                         self.value = input_socket.default_value[:len(self.value)-1]+(alpha_socket.default_value,)
24                                 else:
25                                         self.value = input_socket.default_value[:len(self.value)]
26                         else:
27                                 self.value = input_socket.default_value
28
29                 if self.tex_keyword:
30                         from_node, _ = get_linked_node_and_socket(node_tree, input_socket)
31                         alpha_from = None
32                         if from_node:
33                                 if from_node.type=='NORMAL_MAP':
34                                         from_node, _ = get_linked_node_and_socket(node_tree, from_node.inputs["Color"])
35
36                                 if alpha_socket:
37                                         alpha_from, _ = get_linked_node_and_socket(node_tree, alpha_socket)
38                                         if alpha_from and alpha_from!=from_node:
39                                                 raise Exception("Separate textures for color and alpha are not supported")
40
41                                 if from_node.type=='TEX_IMAGE':
42                                         self.texture = from_node
43                                         if alpha_from:
44                                                 self.tex_usage = 'RGBA'
45                                         elif type(self.value)==tuple:
46                                                 self.tex_usage = 'RGB'
47                                         else:
48                                                 self.tex_usage = 'GRAY'
49                                 else:
50                                         raise Exception("Unsupported property input node type "+from_node.type)
51
52 class Material:
53         def __init__(self, material):
54                 self.name = material.name
55                 self.type = None
56                 self.properties = []
57
58                 self.render_mode = material.render_mode
59                 self.technique = material.technique
60                 self.shader = material.shader
61
62                 if self.render_mode=='EXTERNAL' and not self.technique:
63                         raise Exception("Missing technique with external rendering mode")
64                 elif self.render_mode=='CUSTOM' and not self.shader:
65                         raise Exception("Missing shader with custom rendering mode")
66
67                 out_node = None
68                 for n in material.node_tree.nodes:
69                         if n.type=='OUTPUT_MATERIAL':
70                                 out_node = n
71                                 break
72
73                 if not out_node:
74                         raise Exception("No material output node found")
75
76                 surface_node, _ = get_linked_node_and_socket(material.node_tree, out_node.inputs["Surface"])
77                 if not surface_node:
78                         if self.render_mode=='BUILTIN':
79                                 raise Exception("Empty material can't use builtin rendering mode")
80                         return
81                 elif surface_node.type=='BSDF_PRINCIPLED':
82                         self.type = "pbr"
83
84                         base_color = self.create_property("base_color", (0.8, 0.8, 0.8, 1.0))
85                         metalness = self.create_property("metalness", 0.0)
86                         roughness = self.create_property("roughness", 0.5)
87                         normal = self.create_property("normal")
88                         emission = self.create_property("emission", (0.0, 0.0, 0.0))
89
90                         base_color.set_from_input(material.node_tree, surface_node.inputs["Base Color"], surface_node.inputs["Alpha"])
91                         metalness.set_from_input(material.node_tree, surface_node.inputs["Metallic"])
92                         roughness.set_from_input(material.node_tree, surface_node.inputs["Roughness"])
93                         normal.set_from_input(material.node_tree, surface_node.inputs["Normal"])
94                         emission.set_from_input(material.node_tree, surface_node.inputs["Emission"])
95                 elif surface_node.type=='EMISSION':
96                         self.type = "unlit"
97
98                         color = self.create_property("color", "texture", (1.0, 1.0, 1.0, 1.0))
99
100                         color.set_from_input(material.node_tree, surface_node.inputs["Color"])
101                 else:
102                         raise Exception("Unsupported surface node type "+surface_node.type)
103
104                 sampler_settings = None
105                 for p in self.properties:
106                         if p.texture:
107                                 settings = (p.texture.default_filter, p.texture.interpolation, p.texture.use_mipmap, p.texture.max_anisotropy)
108                                 if sampler_settings is None:
109                                         sampler_settings = settings
110                                 elif settings!=sampler_settings:
111                                         raise Exception("Conflicting sampler settings in material textures")
112
113         def create_property(self, *args):
114                 prop = None
115                 if len(args)==1:
116                         prop = MaterialProperty(None, args[0], None)
117                 elif len(args)==2:
118                         prop = MaterialProperty(args[0], args[0]+"_map", args[1])
119                 else:
120                         prop = MaterialProperty(*args)
121                 self.properties.append(prop)
122                 return prop
123
124
125 class MaterialAtlas:
126         def __init__(self, materials):
127                 self.render_mode = materials[0].render_mode
128                 if self.render_mode=='EXTERNAL':
129                         raise Exception("Material atlas with external render mode does not make sense")
130
131                 self.shader = materials[0].shader
132                 if self.shader:
133                         self.name = "material_atlas_"+os.path.splitext(self.shader)[0]
134                 else:
135                         self.name = "material_atlas"
136                 self.materials = materials
137                 self.material_names = [m.name for m in self.materials]
138                 for m in self.materials:
139                         if m.render_mode!=self.render_mode:
140                                 raise Exception("Conflicting render modes in MaterialAtlas constructor")
141                         if self.render_mode=='CUSTOM' and m.shader!=self.shader:
142                                 raise Exception("Conflicting shaders in MaterialAtlas constructor")
143
144                 count = len(self.materials)
145                 size = 1
146                 while size*size*2<count:
147                         size *= 2
148                 if size*size>=count:
149                         self.size = (size, size)
150                 else:
151                         self.size = (size*2, size)
152
153                 from .util import get_colormap
154
155                 cm = get_colormap(True)
156                 self.base_color_data = ""
157                 for m in map(Material, self.materials):
158                         if any(p.texture for p in m.properties):
159                                 raise Exception("Texturing is incompatible with material atlas")
160                         base_color = [int(cm(c)*255) for c in m.base_color.value]
161                         self.base_color_data += "\\x{:02X}\\x{:02X}\\x{:02X}\\xFF".format(*base_color)
162                 self.base_color_data += "\\x00\\x00\\x00\\x00"*(self.size[0]*self.size[1]-count)
163
164         def get_material_uv(self, material):
165                 index = self.material_names.index(material.name)
166                 x = index%self.size[0]
167                 y = index//self.size[0]
168                 return ((x+0.5)/self.size[0], (y+0.5)/self.size[1])
169
170 def create_material_atlas(context, material):
171         if not material.material_atlas:
172                 raise Exception("Material is not part of a material atlas")
173
174         shader = material.shader
175         materials = []
176         for m in context.blend_data.materials:
177                 if m.material_atlas and m.shader==shader:
178                         materials.append(m)
179
180         mat_map = MaterialAtlas(materials)
181
182         return mat_map