3 def create_technique_resource(material, resources):
4 from .datafile import Resource, Statement, Token
5 tech_res = Resource(material.name+".tech", "technique")
7 mat_res = resources[material.name+".mat"]
10 if material.blend_type=='ALPHA':
11 blend_st = Statement("blend", Token("SRC_ALPHA"), Token("ONE_MINUS_SRC_ALPHA"))
12 elif material.blend_type=='ADDITIVE':
13 blend_st = Statement("blend", Token("ONE"), Token("ONE"))
14 elif material.blend_type=='ADDITIVE_ALPHA':
15 blend_st = Statement("blend", Token("SRC_ALPHA"), Token("ONE"))
17 if material.render_mode=='CUSTOM':
18 for m in material.render_methods:
19 st = Statement("method", m.tag)
20 if mat_res and m.use_material:
21 st.sub.append(tech_res.create_reference_statement("material", mat_res))
23 if m.tag=="blended" and blend_st:
24 st.sub.append(blend_st)
27 if shader.endswith(".glsl"):
29 st.sub.append(Statement("shader", shader))
32 ss = Statement("uniforms")
33 for u in material.uniforms:
34 ss.sub.append(Statement("uniform", u.name, *u.values[:u.size]))
37 tech_res.statements.append(st)
39 base_method = "blended" if material.blend_type!='NONE' else ""
40 st = Statement("method", base_method)
42 st.sub.append(tech_res.create_embed_statement("material", mat_res))
45 st.sub.append(blend_st)
46 if material.receive_shadows:
47 st.sub.append(Statement("receive_shadows", True))
48 if material.image_based_lighting:
49 st.sub.append(Statement("image_based_lighting", True))
51 tech_res.statements.append(st)
53 if material.cast_shadows:
54 st = Statement("method", "shadow")
55 st.sub.append(Statement("shader", "occluder.glsl.shader"))
56 tech_res.statements.append(st)
58 st = Statement("method", "shadow_thsm")
59 st.sub.append(Statement("shader", "occluder_thsm.glsl.shader"))
60 tech_res.statements.append(st)
64 class MaterialExporter:
65 def export_technique_resources(self, ctx, material, resources):
66 from .export_texture import SamplerExporter, TextureExporter
67 texture_export = TextureExporter()
68 sampler_export = SamplerExporter()
70 from .material import Material
71 if type(material)!=Material:
72 material = Material(material)
74 textured_props = [p for p in material.properties if p.texture]
76 ctx.set_slices(len(textured_props)+1)
77 for p in textured_props:
78 ctx.next_slice(p.texture.image)
80 tex_name = p.texture.image.name+".tex"
81 if tex_name not in resources:
82 resources[tex_name] = texture_export.export_texture(p.texture, p.tex_channels)
84 samp_name = sampler_export.get_sampler_name(p.texture)
85 if samp_name not in resources:
86 resources[samp_name] = sampler_export.export_sampler(p.texture)
88 ctx.next_slice(material)
89 mat_name = material.name+".mat"
90 if mat_name not in resources:
92 resources[mat_name] = self.export_material(material, resources)
94 resources[mat_name] = None
96 def export_technique(self, material, resources):
97 from .material import Material
98 if type(material)!=Material:
99 material = Material(material)
101 return create_technique_resource(material, resources)
103 def export_material(self, material, resources):
104 from .datafile import Resource, Statement, Token
105 mat_res = Resource(material.name+".mat", "material")
107 if material.type!="pbr" and material.type!="unlit":
108 raise Exception("Can't export material {} of unknown type {}".format(material.name, material.type))
110 mat_res.statements.append(Statement("type", Token(material.type)));
111 for p in material.properties:
112 st = self.create_property_statement(mat_res, p, resources)
114 mat_res.statements.append(st)
115 textures = [p.texture for p in material.properties if p.texture]
117 from .export_texture import SamplerExporter
118 sampler_export = SamplerExporter()
119 mat_res.statements.append(mat_res.create_reference_statement("sampler", resources[sampler_export.get_sampler_name(textures[0])]))
123 def create_property_statement(self, mat_res, prop, resources):
124 from .datafile import Statement
126 tex_res = resources[prop.texture.image.name+".tex"]
127 return mat_res.create_reference_statement(prop.tex_keyword, tex_res)
128 elif not prop.keyword:
130 elif type(prop.value)==tuple:
131 return Statement(prop.keyword, *prop.value)
133 return Statement(prop.keyword, prop.value)
136 class MaterialAtlasExporter:
140 def export_technique_resources(self, material_atlas, resources):
141 from .datafile import Resource, Statement, Token
142 base_color_name = material_atlas.name+"_base_color.tex"
143 base_color_res = resources.get(base_color_name)
144 if not base_color_res:
145 base_color_res = Resource(base_color_name, "texture")
147 base_color_res.statements.append(Statement("type", Token("\\2d")))
148 base_color_res.statements.append(Statement("storage", Token('SRGB_ALPHA'), *material_atlas.size))
149 base_color_res.statements.append(Statement("raw_data", material_atlas.base_color_data))
151 resources[base_color_name] = base_color_res
153 sampler_name = "nearest.samp"
154 sampler_res = resources.get(sampler_name)
156 sampler_res = Resource(sampler_name, "sampler")
158 sampler_res.statements.append(Statement("filter", Token('NEAREST')))
160 resources[sampler_name] = sampler_res
162 mat_name = material_atlas.name+".mat"
163 if mat_name not in resources:
164 mat_res = Resource(mat_name, "material")
165 mat_res.statements.append(Statement("type", Token('pbr')))
166 mat_res.statements.append(mat_res.create_reference_statement("base_color_map", base_color_res))
167 mat_res.statements.append(mat_res.create_reference_statement("sampler", sampler_res))
169 resources[mat_name] = mat_res
171 def export_technique(self, material_atlas, resources):
172 return create_technique_resource(material_atlas, resources)