3 def create_technique_resource(material, resources):
4 from .datafile import Resource, Statement
5 tech_res = Resource(material.name+".tech", "technique")
7 mat_res = resources[material.name+".mat"]
9 st = Statement("pass", "")
11 st.sub.append(tech_res.create_embed_statement("material", mat_res))
13 if material.render_mode=='CUSTOM':
14 shader = material.shader
15 if shader.endswith(".glsl"):
17 st.sub.append(Statement("shader", shader))
20 ss = Statement("uniforms")
21 for u in material.uniforms:
22 ss.sub.append(Statement("uniform", u.name, *u.values[:u.size]))
25 tech_res.statements.append(st)
29 class MaterialExporter:
30 def create_texture_exporter(self):
31 from .export_texture import TextureExporter
32 texture_export = TextureExporter()
35 def export_technique_resources(self, material, resources):
36 from .export_texture import SamplerExporter
37 texture_export = self.create_texture_exporter()
38 sampler_export = SamplerExporter()
40 from .material import Material
41 material = Material(material)
43 for p in material.properties:
45 tex_name = p.texture.image.name+".tex2d"
46 if tex_name not in resources:
47 resources[tex_name] = texture_export.export_texture(p.texture, p.tex_usage, invert_green=p.invert_green)
49 samp_name = sampler_export.get_sampler_name(p.texture)
50 if samp_name not in resources:
51 resources[samp_name] = sampler_export.export_sampler(p.texture)
53 mat_name = material.name+".mat"
54 if mat_name not in resources:
56 resources[mat_name] = self.export_material(material, resources)
58 resources[mat_name] = None
60 def export_technique(self, material, resources):
61 return create_technique_resource(material, resources)
63 def export_material(self, material, resources):
64 from .datafile import Resource, Statement, Token
65 mat_res = Resource(material.name+".mat", "material")
67 if material.type!="pbr" and material.type!="unlit":
68 raise Exception("Can't export unknown material type "+material.type)
70 mat_res.statements.append(Statement("type", Token(material.type)));
71 for p in material.properties:
72 st = self.create_property_statement(mat_res, p, resources)
74 mat_res.statements.append(st)
75 textures = [p.texture for p in material.properties if p.texture]
76 if textures and not all(t.default_filter for t in textures):
77 from .export_texture import SamplerExporter
78 sampler_tex = next(t for t in textures if not t.default_filter)
79 sampler_export = SamplerExporter()
80 mat_res.statements.append(mat_res.create_reference_statement("sampler", resources[sampler_export.get_sampler_name(sampler_tex)]))
84 def create_property_statement(self, mat_res, prop, resources):
85 from .datafile import Statement
87 tex_res = resources[prop.texture.image.name+".tex2d"]
88 from .util import basename
89 fn = basename(prop.texture.image.filepath)
90 if prop.texture.default_filter and fn:
91 return Statement(prop.tex_keyword, fn)
93 return mat_res.create_reference_statement(prop.tex_keyword, tex_res)
94 elif not prop.keyword:
96 elif type(prop.value)==tuple:
97 return Statement(prop.keyword, *prop.value)
99 return Statement(prop.keyword, prop.value)
102 class MaterialAtlasExporter:
106 def export_technique_resources(self, material_atlas, resources):
107 from .datafile import Resource, Statement, Token
108 base_color_name = material_atlas.name+"_base_color.tex2d"
109 base_color_res = resources.get(base_color_name)
110 if not base_color_res:
111 base_color_res = Resource(base_color_name, "texture2d")
113 base_color_res.statements.append(Statement("storage", Token('SRGB_ALPHA'), *material_atlas.size))
114 base_color_res.statements.append(Statement("raw_data", material_atlas.base_color_data))
116 resources[base_color_name] = base_color_res
118 sampler_name = "nearest.samp"
119 sampler_res = resources.get(sampler_name)
121 sampler_res = Resource(sampler_name, "sampler")
123 sampler_res.statements.append(Statement("filter", Token('NEAREST')))
125 resources[sampler_name] = sampler_res
127 mat_name = material_atlas.name+".mat"
128 if mat_name not in resources:
129 mat_res = Resource(mat_name, "material")
130 mat_res.statements.append(Statement("type", Token('pbr')))
131 mat_res.statements.append(mat_res.create_reference_statement("base_color_map", base_color_res))
132 mat_res.statements.append(mat_res.create_reference_statement("sampler", sampler_res))
134 resources[mat_name] = mat_res
136 def export_technique(self, material_atlas, resources):
137 return create_technique_resource(material_atlas, resources)