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