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