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()
7 from .material import Material
8 if type(material)!=Material:
9 material = Material(material)
11 textured_props = [p for p in material.properties if p.texture]
13 ctx.set_slices(len(textured_props)+1)
14 for p in textured_props:
15 ctx.next_slice(p.texture.image)
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)
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)
25 ctx.next_slice(material)
26 mat_name = material.name+".mat"
27 if mat_name not in resources:
29 resources[mat_name] = self.export_material(material, resources)
31 resources[mat_name] = None
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)
41 def export_technique(self, material, resources):
42 from .material import Material
43 if type(material)!=Material:
44 material = Material(material)
46 from .datafile import Resource, Statement, Token
47 tech_res = Resource(material.name+".tech", "technique")
49 mat_res = resources[material.name+".mat"]
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"))
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))
65 if m.tag=="blended" and blend_st:
66 st.sub.append(blend_st)
69 if shader.endswith(".glsl"):
71 st.sub.append(Statement("shader", shader))
74 ss = Statement("uniforms")
75 for u in material.uniforms:
76 ss.sub.append(Statement("uniform", u.name, *u.values[:u.size]))
79 if material.face_cull=='BACK':
80 st.sub.append(Statement("face_cull", Token("CULL_BACK")))
82 tech_res.statements.append(st)
84 base_method = "blended" if material.blend_type!='NONE' else ""
85 st = Statement("method", base_method)
87 st.sub.append(tech_res.create_embed_statement("material", mat_res))
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))
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")))
104 tech_res.statements.append(st)
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));
112 def create_shadow_method(self, tech_res, material, resources, thsm):
113 from .datafile import Statement, Token
115 occluder_variant, occluder_spec_values = self.get_occluder_shader_variant(material, thsm)
117 shader_stem = "occluder{}.shader" if occluder_spec_values else "occluder{}.glsl.shader"
118 shader_name = shader_stem.format(occluder_variant)
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]))
124 st.sub.append(Statement("shader", shader_name))
126 if occluder_spec_values.get("use_alpha_cutoff"):
127 ss = Statement("texture", "alpha_map")
129 from .export_texture import TextureExporter
130 from .export_texture import SamplerExporter
131 texture_export = TextureExporter()
132 sampler_export = SamplerExporter()
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)]))
140 ss = Statement("uniforms")
141 ss.sub.append(Statement("uniform", "alpha_cutoff", material.alpha_cutoff))
144 if material.face_cull=='BACK':
145 st.sub.append(Statement("face_cull", Token("CULL_BACK")))
149 def get_occluder_shader_variant(self, material, thsm):
156 if material.cast_shadows and material.alpha_cutoff>0.0:
157 spec_values["use_alpha_cutoff"] = True
159 if material.instancing:
160 spec_values["use_instancing"] = True
161 variant += "_instanced"
163 return (variant, spec_values)
165 def export_material(self, material, resources):
166 from .datafile import Resource, Statement, Token
167 mat_res = Resource(material.name+".mat", "material")
169 if material.type!="pbr" and material.type!="unlit":
170 raise Exception("Can't export material {} of unknown type {}".format(material.name, material.type))
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)
176 mat_res.statements.append(st)
177 textures = [p.texture for p in material.properties if p.texture]
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))
187 def create_property_statement(self, mat_res, prop, resources):
188 from .datafile import Statement
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:
196 elif type(prop.value)==tuple:
197 return Statement(prop.keyword, *prop.value)
199 return Statement(prop.keyword, prop.value)
201 def export_shader(self, name, module, spec_values, resources):
202 from .datafile import Resource, Statement
203 shader_res = Resource(name, "shader")
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)