1 def create_shadow_method(tech_res, material, resources, detail):
2 from .datafile import Statement, Token
4 color_prop = next((p for p in material.properties if p.keyword and "color" in p.keyword), None)
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")
11 from .export_texture import TextureExporter
12 from .export_texture import SamplerExporter
13 texture_export = TextureExporter()
14 sampler_export = SamplerExporter()
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)]))
21 ss = Statement("uniforms")
22 ss.sub.append(Statement("uniform", "alpha_cutoff", material.alpha_cutoff))
25 st.sub.append(Statement("shader", "occluder{}.glsl.shader".format(detail)))
27 if material.face_cull=='BACK':
28 st.sub.append(Statement("face_cull", Token("CULL_BACK")))
32 def create_technique_resource(material, resources):
33 from .datafile import Resource, Statement, Token
34 tech_res = Resource(material.name+".tech", "technique")
36 mat_res = resources[material.name+".mat"]
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"))
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))
52 if m.tag=="blended" and blend_st:
53 st.sub.append(blend_st)
56 if shader.endswith(".glsl"):
58 st.sub.append(Statement("shader", shader))
61 ss = Statement("uniforms")
62 for u in material.uniforms:
63 ss.sub.append(Statement("uniform", u.name, *u.values[:u.size]))
66 if material.face_cull=='BACK':
67 st.sub.append(Statement("face_cull", Token("CULL_BACK")))
69 tech_res.statements.append(st)
71 base_method = "blended" if material.blend_type!='NONE' else ""
72 st = Statement("method", base_method)
74 st.sub.append(tech_res.create_embed_statement("material", mat_res))
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")))
85 tech_res.statements.append(st)
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"));
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()
99 from .material import Material
100 if type(material)!=Material:
101 material = Material(material)
103 textured_props = [p for p in material.properties if p.texture]
105 ctx.set_slices(len(textured_props)+1)
106 for p in textured_props:
107 ctx.next_slice(p.texture.image)
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)
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)
117 ctx.next_slice(material)
118 mat_name = material.name+".mat"
119 if mat_name not in resources:
121 resources[mat_name] = self.export_material(material, resources)
123 resources[mat_name] = None
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)
131 def export_technique(self, material, resources):
132 from .material import Material
133 if type(material)!=Material:
134 material = Material(material)
136 return create_technique_resource(material, resources)
138 def export_material(self, material, resources):
139 from .datafile import Resource, Statement, Token
140 mat_res = Resource(material.name+".mat", "material")
142 if material.type!="pbr" and material.type!="unlit":
143 raise Exception("Can't export material {} of unknown type {}".format(material.name, material.type))
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)
149 mat_res.statements.append(st)
150 textures = [p.texture for p in material.properties if p.texture]
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))
160 def create_property_statement(self, mat_res, prop, resources):
161 from .datafile import Statement
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:
169 elif type(prop.value)==tuple:
170 return Statement(prop.keyword, *prop.value)
172 return Statement(prop.keyword, prop.value)
174 def export_shader(self, name, module, spec_values, resources):
175 from .datafile import Resource, Statement
176 shader_res = Resource(name, "shader")
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)
186 class MaterialAtlasExporter:
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")
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))
201 resources[base_color_name] = base_color_res
203 sampler_name = "nearest.samp"
204 sampler_res = resources.get(sampler_name)
206 sampler_res = Resource(sampler_name, "sampler")
208 sampler_res.statements.append(Statement("filter", Token('NEAREST')))
210 resources[sampler_name] = sampler_res
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))
219 resources[mat_name] = mat_res
221 def export_technique(self, material_atlas, resources):
222 return create_technique_resource(material_atlas, resources)