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))
19 tech_res.statements.append(st)
23 class MaterialExporter:
25 self.use_textures = True
26 self.inline_texture_data = False
28 def create_texture_exporter(self):
29 from .export_texture import TextureExporter
30 texture_export = TextureExporter()
31 texture_export.inline_data = self.inline_texture_data
34 def export_technique_resources(self, material, resources):
35 from .export_texture import SamplerExporter
36 texture_export = self.create_texture_exporter()
37 sampler_export = SamplerExporter()
39 from .material import Material
40 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)
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=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
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 st = Statement(material.type)
71 for p in material.properties:
72 ss = self.create_property_statement(mat_res, p, resources)
76 first_tex = (p.texture for p in material.properties if p.texture).__next__()
77 if first_tex and not first_tex.default_filter:
78 from .export_texture import SamplerExporter
79 sampler_export = SamplerExporter()
80 st.sub.append(mat_res.create_reference_statement("sampler", resources[sampler_export.get_sampler_name(first_tex)]))
81 mat_res.statements.append(st)
85 def create_property_statement(self, mat_res, prop, resources):
86 from .datafile import Statement
87 if self.use_textures and prop.texture:
88 tex_res = resources[prop.texture.image.name+".tex2d"]
89 from .util import basename
90 fn = basename(prop.texture.image.filepath)
91 if prop.texture.default_filter and fn:
92 return Statement(prop.tex_keyword, fn)
94 return mat_res.create_reference_statement(prop.tex_keyword, tex_res)
95 elif not prop.keyword:
97 elif type(prop.value)==tuple:
98 return Statement(prop.keyword, *prop.value)
100 return Statement(prop.keyword, prop.value)
103 class MaterialMapExporter:
107 def export_technique_resources(self, material_map, resources):
108 from .datafile import Resource, Statement, Token
109 base_color_name = material_map.name+"_base_color.tex2d"
110 base_color_res = resources.get(base_color_name)
111 if not base_color_res:
112 base_color_res = Resource(base_color_name, "texture2d")
114 base_color_res.statements.append(Statement("storage", Token('SRGB_ALPHA'), *material_map.size))
115 base_color_res.statements.append(Statement("raw_data", material_map.base_color_data))
117 resources[base_color_name] = base_color_res
119 sampler_name = "nearest.samp"
120 sampler_res = resources.get(sampler_name)
122 sampler_res = Resource(sampler_name, "sampler")
124 sampler_res.statements.append(Statement("filter", Token('NEAREST')))
126 resources[sampler_name] = sampler_res
128 mat_name = material_map.name+".mat"
129 if mat_name not in resources:
130 mat_res = Resource(mat_name, "material")
131 st = Statement("pbr")
132 st.sub.append(mat_res.create_reference_statement("base_color_map", base_color_res))
133 st.sub.append(mat_res.create_reference_statement("sampler", sampler_res))
134 mat_res.statements.append(st)
136 resources[mat_name] = mat_res
138 def export_technique(self, material_map, *, resources):
139 return create_technique_resource(material_map, resources)