]> git.tdb.fi Git - libs/gl.git/blob - blender/io_mspgl/export_material.py
Support exporting splat materials from Blender
[libs/gl.git] / blender / io_mspgl / export_material.py
1 import itertools
2
3 class MaterialExporter:
4         def export_technique_resources(self, ctx, material, resources):
5                 from .export_texture import SamplerExporter, TextureExporter
6                 texture_export = TextureExporter()
7                 sampler_export = SamplerExporter()
8
9                 from .material import Material
10                 if type(material)!=Material:
11                         material = Material(material)
12
13                 textured_props = [p for p in itertools.chain(material.properties, *(s.properties for s in material.sub_materials)) if p.texture]
14
15                 ctx.set_slices(len(textured_props)+1)
16                 for p in textured_props:
17                         ctx.next_slice(p.texture.image)
18
19                         tex_name = texture_export.get_texture_name(p.texture, p.tex_channels)
20                         if tex_name not in resources:
21                                 resources[tex_name] = texture_export.export_texture(p.texture, p.tex_channels)
22
23                         samp_name = sampler_export.get_sampler_name(p.texture)
24                         if samp_name not in resources:
25                                 resources[samp_name] = sampler_export.export_sampler(p.texture)
26
27                 ctx.next_slice(material)
28                 mat_name = material.name+".mat"
29                 if mat_name not in resources:
30                         if material.type:
31                                 resources[mat_name] = self.export_material(material, resources)
32                         else:
33                                 resources[mat_name] = None
34
35                 for thsm in (False, True):
36                         occluder_variant, occluder_spec_values = self.get_occluder_shader_variant(material, thsm)
37                         if occluder_spec_values:
38                                 base_variant = "_thsm" if thsm else ""
39                                 shader_name = "occluder{}.shader".format(occluder_variant)
40                                 if shader_name not in resources:
41                                         resources[shader_name] = self.export_shader(shader_name, "occluder{}.glsl".format(base_variant), occluder_spec_values, resources)
42
43         def export_technique(self, material, resources):
44                 from .material import Material
45                 if type(material)!=Material:
46                         material = Material(material)
47
48                 from .datafile import Resource, Statement, Token
49                 tech_res = Resource(material.name+".tech", "technique")
50
51                 mat_res = resources[material.name+".mat"]
52
53                 blend_st = None
54                 if material.blend_type=='ALPHA':
55                         blend_st = Statement("blend", Token("SRC_ALPHA"), Token("ONE_MINUS_SRC_ALPHA"))
56                 elif material.blend_type=='ADDITIVE':
57                         blend_st = Statement("blend", Token("ONE"), Token("ONE"))
58                 elif material.blend_type=='ADDITIVE_ALPHA':
59                         blend_st = Statement("blend", Token("SRC_ALPHA"), Token("ONE"))
60
61                 if material.render_mode=='CUSTOM':
62                         for m in material.render_methods:
63                                 st = Statement("method", m.tag)
64                                 if mat_res and m.use_material:
65                                         st.sub.append(tech_res.create_reference_statement("material", mat_res))
66
67                                 if m.tag=="blended" and blend_st:
68                                         st.sub.append(blend_st)
69
70                                 shader = m.shader
71                                 if shader.endswith(".glsl"):
72                                         shader += ".shader"
73                                 st.sub.append(Statement("shader", shader))
74
75                                 if material.uniforms:
76                                         ss = Statement("uniforms")
77                                         for u in material.uniforms:
78                                                 ss.sub.append(Statement("uniform", u.name, *u.values[:u.size]))
79                                         st.sub.append(ss)
80
81                                 if material.face_cull=='BACK':
82                                         st.sub.append(Statement("face_cull", Token("CULL_BACK")))
83
84                                 tech_res.statements.append(st)
85                 else:
86                         base_method = "blended" if material.blend_type!='NONE' else ""
87                         st = Statement("method", base_method)
88                         if mat_res:
89                                 st.sub.append(tech_res.create_embed_statement("material", mat_res))
90
91                         if blend_st:
92                                 st.sub.append(blend_st)
93                         elif material.alpha_cutoff>0.0:
94                                 ss = Statement("blend")
95                                 ss.sub.append(Statement("alpha_to_coverage", True))
96                                 st.sub.append(ss)
97                         if material.receive_shadows:
98                                 st.sub.append(Statement("receive_shadows", True))
99                         if material.image_based_lighting:
100                                 st.sub.append(Statement("image_based_lighting", True))
101                         if material.instancing:
102                                 st.sub.append(Statement("instancing", True))
103                         if material.face_cull=='BACK':
104                                 st.sub.append(Statement("face_cull", Token("CULL_BACK")))
105
106                         tech_res.statements.append(st)
107
108                         if material.cast_shadows:
109                                 tech_res.statements.append(self.create_shadow_method(tech_res, material, resources, False));
110                                 tech_res.statements.append(self.create_shadow_method(tech_res, material, resources, True));
111
112                 return tech_res
113
114         def create_shadow_method(self, tech_res, material, resources, thsm):
115                 from .datafile import Statement, Token
116
117                 occluder_variant, occluder_spec_values = self.get_occluder_shader_variant(material, thsm)
118
119                 shader_stem = "occluder{}.shader" if occluder_spec_values else "occluder{}.glsl.shader"
120                 shader_name = shader_stem.format(occluder_variant)
121
122                 st = Statement("method", "shadow"+("_thsm" if thsm else ""))
123                 if occluder_spec_values:
124                         st.sub.append(tech_res.create_reference_statement("shader", resources[shader_name]))
125                 else:
126                         st.sub.append(Statement("shader", shader_name))
127
128                 if occluder_spec_values.get("use_alpha_cutoff"):
129                         ss = Statement("texture", "alpha_map")
130
131                         from .export_texture import TextureExporter
132                         from .export_texture import SamplerExporter
133                         texture_export = TextureExporter()
134                         sampler_export = SamplerExporter()
135
136                         color_prop = material.properties[0]
137                         tex_res = resources[texture_export.get_texture_name(color_prop.texture, color_prop.tex_channels)]
138                         ss.sub.append(tech_res.create_reference_statement("texture", tex_res))
139                         ss.sub.append(tech_res.create_reference_statement("sampler", resources[sampler_export.get_sampler_name(color_prop.texture)]))
140                         st.sub.append(ss)
141
142                         ss = Statement("uniforms")
143                         ss.sub.append(Statement("uniform", "alpha_cutoff", material.alpha_cutoff))
144                         st.sub.append(ss)
145
146                 if material.face_cull=='BACK':
147                         st.sub.append(Statement("face_cull", Token("CULL_BACK")))
148
149                 return st;
150
151         def get_occluder_shader_variant(self, material, thsm):
152                 variant = ""
153                 if thsm:
154                         variant += "_thsm"
155
156                 spec_values = {}
157
158                 if material.cast_shadows and material.alpha_cutoff>0.0:
159                         spec_values["use_alpha_cutoff"] = True
160                         variant += "_masked"
161                 if material.instancing:
162                         spec_values["use_instancing"] = True
163                         variant += "_instanced"
164
165                 return (variant, spec_values)
166
167         def export_material(self, material, resources):
168                 from .datafile import Resource, Statement, Token
169                 mat_res = Resource(material.name+".mat", "material")
170
171                 if material.type!="pbr" and material.type!="unlit" and material.type!="splat":
172                         raise Exception("Can't export material {} of unknown type {}".format(material.name, material.type))
173
174                 mat_res.statements.append(Statement("type", Token(material.type)));
175                 for p in material.properties:
176                         st = self.create_property_statement(mat_res, p, resources)
177                         if st:
178                                 mat_res.statements.append(st)
179
180                 if material.sub_materials:
181                         for k, s in material.array_storage.items():
182                                 mat_res.statements.append(Statement(k.replace("_map", "_storage"), Token(s[0]), s[1], s[2]))
183                         for s in material.sub_materials:
184                                 st = Statement("sub")
185                                 for p in s.properties:
186                                         ss = self.create_property_statement(mat_res, p, resources, raw_texture=True)
187                                         if ss:
188                                                 st.sub.append(ss)
189                                 mat_res.statements.append(st)
190
191                 textures = [p.texture for p in itertools.chain(material.properties, *(s.properties for s in material.sub_materials)) if p.texture]
192                 if textures:
193                         from .export_texture import SamplerExporter
194                         sampler_export = SamplerExporter()
195                         mat_res.statements.append(mat_res.create_reference_statement("sampler", resources[sampler_export.get_sampler_name(textures[0])]))
196                 if material.alpha_cutoff>0.0:
197                         mat_res.statements.append(Statement("alpha_cutoff", material.alpha_cutoff))
198
199                 return mat_res
200
201         def create_property_statement(self, mat_res, prop, resources, *, raw_texture=False):
202                 from .datafile import Statement
203                 if prop.texture:
204                         from .export_texture import TextureExporter
205                         texture_export = TextureExporter()
206                         tex_res = resources[texture_export.get_texture_name(prop.texture, prop.tex_channels)]
207                         if raw_texture:
208                                 for s in tex_res.statements:
209                                         if s.keyword.startswith("external_data"):
210                                                 return mat_res.create_reference_statement(prop.tex_keyword, s.args[0])
211                                         elif s.keyword.startswith("external_image"):
212                                                 return Statement(prop.tex_keyword, s.args[0])
213                         return mat_res.create_reference_statement(prop.tex_keyword, tex_res)
214                 elif not prop.keyword:
215                         return
216                 elif type(prop.value)==tuple:
217                         return Statement(prop.keyword, *prop.value)
218                 else:
219                         return Statement(prop.keyword, prop.value)
220
221         def export_shader(self, name, module, spec_values, resources):
222                 from .datafile import Resource, Statement
223                 shader_res = Resource(name, "shader")
224
225                 st = Statement("module", module)
226                 for k, v in spec_values.items():
227                         st.sub.append(Statement("specialize", k, v))
228                 shader_res.statements.append(st)
229
230                 return shader_res