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