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()
9 from .material import Material
10 if type(material)!=Material:
11 material = Material(material)
13 textured_props = [p for p in itertools.chain(material.properties, *(s.properties for s in material.sub_materials)) if p.texture]
15 ctx.set_slices(len(textured_props)+1)
16 for p in textured_props:
17 ctx.next_slice(p.texture.image)
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)
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)
27 ctx.next_slice(material)
28 mat_name = material.name+".mat"
29 if mat_name not in resources:
31 resources[mat_name] = self.export_material(material, resources)
33 resources[mat_name] = None
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)
43 def export_technique(self, material, resources):
44 from .material import Material
45 if type(material)!=Material:
46 material = Material(material)
48 from .datafile import Resource, Statement, Token
49 tech_res = Resource(material.name+".tech", "technique")
51 mat_res = resources[material.name+".mat"]
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"))
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))
67 if m.tag=="blended" and blend_st:
68 st.sub.append(blend_st)
71 if shader.endswith(".glsl"):
73 st.sub.append(Statement("shader", shader))
76 ss = Statement("uniforms")
77 for u in material.uniforms:
78 ss.sub.append(Statement("uniform", u.name, *u.values[:u.size]))
81 if material.face_cull=='BACK':
82 st.sub.append(Statement("face_cull", Token("CULL_BACK")))
84 tech_res.statements.append(st)
86 base_method = "blended" if material.blend_type!='NONE' else ""
87 st = Statement("method", base_method)
89 st.sub.append(tech_res.create_embed_statement("material", mat_res))
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))
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")))
106 tech_res.statements.append(st)
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));
114 def create_shadow_method(self, tech_res, material, resources, thsm):
115 from .datafile import Statement, Token
117 occluder_variant, occluder_spec_values = self.get_occluder_shader_variant(material, thsm)
119 shader_stem = "occluder{}.shader" if occluder_spec_values else "occluder{}.glsl.shader"
120 shader_name = shader_stem.format(occluder_variant)
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]))
126 st.sub.append(Statement("shader", shader_name))
128 if occluder_spec_values.get("use_alpha_cutoff"):
129 ss = Statement("texture", "alpha_map")
131 from .export_texture import TextureExporter
132 from .export_texture import SamplerExporter
133 texture_export = TextureExporter()
134 sampler_export = SamplerExporter()
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)]))
142 ss = Statement("uniforms")
143 ss.sub.append(Statement("uniform", "alpha_cutoff", material.alpha_cutoff))
146 if material.face_cull=='BACK':
147 st.sub.append(Statement("face_cull", Token("CULL_BACK")))
151 def get_occluder_shader_variant(self, material, thsm):
158 if material.cast_shadows and material.alpha_cutoff>0.0:
159 spec_values["use_alpha_cutoff"] = True
161 if material.instancing:
162 spec_values["use_instancing"] = True
163 variant += "_instanced"
165 return (variant, spec_values)
167 def export_material(self, material, resources):
168 from .datafile import Resource, Statement, Token
169 mat_res = Resource(material.name+".mat", "material")
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))
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)
178 mat_res.statements.append(st)
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)
189 mat_res.statements.append(st)
191 textures = [p.texture for p in itertools.chain(material.properties, *(s.properties for s in material.sub_materials)) if p.texture]
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))
201 def create_property_statement(self, mat_res, prop, resources, *, raw_texture=False):
202 from .datafile import Statement
204 from .export_texture import TextureExporter
205 texture_export = TextureExporter()
206 tex_res = resources[texture_export.get_texture_name(prop.texture, prop.tex_channels)]
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:
216 elif type(prop.value)==tuple:
217 return Statement(prop.keyword, *prop.value)
219 return Statement(prop.keyword, prop.value)
221 def export_shader(self, name, module, spec_values, resources):
222 from .datafile import Resource, Statement
223 shader_res = Resource(name, "shader")
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)