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", "")
10 st.sub.append(tech_res.create_embed_statement("material", mat_res))
12 if material.render_mode=='CUSTOM':
13 st.sub.append(Statement("shader", material.shader))
15 tech_res.statements.append(st)
19 class MaterialExporter:
21 self.use_textures = True
22 self.inline_texture_data = False
24 def create_texture_exporter(self):
25 from .export_texture import TextureExporter
26 texture_export = TextureExporter()
27 texture_export.inline_data = self.inline_texture_data
30 def export_technique_resources(self, material, resources):
31 from .export_texture import SamplerExporter
32 texture_export = self.create_texture_exporter()
33 sampler_export = SamplerExporter()
35 from .material import Material
36 material = Material(material)
39 for p in material.properties.values():
41 tex_name = p.texture.image.name+".tex2d"
42 if tex_name not in resources:
43 resources[tex_name] = texture_export.export_texture(p.texture, p.tex_usage)
45 samp_name = sampler_export.get_sampler_name(p.texture)
46 if samp_name not in resources:
47 resources[samp_name] = sampler_export.export_sampler(p.texture)
49 mat_name = material.name+".mat"
50 if mat_name not in resources:
51 resources[mat_name] = self.export_material(material, resources=resources)
53 def export_technique(self, material, *, resources):
54 return create_technique_resource(material, resources)
56 def export_material(self, material, *, resources):
57 from .datafile import Resource, Statement
58 mat_res = Resource(material.name+".mat", "material")
61 for kw, p in material.properties.items():
62 ss = self.create_property_statement(mat_res, p, kw, resources)
66 first_tex = (p.texture for p in material.properties.values() if p.texture).__next__()
67 if first_tex and not first_tex.default_filter:
68 from .export_texture import SamplerExporter
69 sampler_export = SamplerExporter()
70 st.sub.append(mat_res.create_reference_statement("sampler", resources[sampler_export.get_sampler_name(first_tex)]))
71 mat_res.statements.append(st)
75 def create_property_statement(self, mat_res, prop, keyword, resources):
76 from .datafile import Statement
77 if self.use_textures and prop.texture:
78 tex_res = resources[prop.texture.image.name+".tex2d"]
79 fn = os.path.basename(prop.texture.image.filepath)
80 if prop.texture.default_filter and fn:
81 return Statement(keyword+"_map", fn)
83 return mat_res.create_reference_statement(keyword+"_map", tex_res)
84 elif prop.value is None:
86 elif type(prop.value)==tuple:
87 return Statement(keyword, *prop.value)
89 return Statement(keyword, prop.value)
92 class MaterialMapExporter:
96 def export_technique_resources(self, material_map, resources):
97 from .datafile import Resource, Statement, Token
98 base_color_name = material_map.name+"_base_color.tex2d"
99 base_color_res = resources.get(base_color_name)
100 if not base_color_res:
101 base_color_res = Resource(base_color_name, "texture2d")
103 base_color_res.statements.append(Statement("storage", Token('SRGB_ALPHA'), *material_map.size))
104 base_color_res.statements.append(Statement("raw_data", material_map.base_color_data))
106 resources[base_color_name] = base_color_res
108 sampler_name = "nearest.samp"
109 sampler_res = resources.get(sampler_name)
111 sampler_res = Resource(sampler_name, "sampler")
113 sampler_res.statements.append(Statement("filter", Token('NEAREST')))
115 resources[sampler_name] = sampler_res
117 mat_name = material_map.name+".mat"
118 if mat_name not in resources:
119 mat_res = Resource(mat_name, "material")
120 st = Statement("pbr")
121 st.sub.append(mat_res.create_reference_statement("base_color_map", base_color_res))
122 st.sub.append(mat_res.create_reference_statement("sampler", sampler_res))
123 mat_res.statements.append(st)
125 resources[mat_name] = mat_res
127 def export_technique(self, material_map, *, resources):
128 return create_technique_resource(material_map, resources)