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:
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 st.sub.append(self.create_property_statement(mat_res, material.base_color, "base_color", resources))
62 st.sub.append(self.create_property_statement(mat_res, material.metalness, "metalness", resources))
63 st.sub.append(self.create_property_statement(mat_res, material.roughness, "roughness", resources))
64 st.sub.append(self.create_property_statement(mat_res, material.normal, "normal", resources, tex_only=True))
65 st.sub.append(self.create_property_statement(mat_res, material.emission, "emission", resources))
67 first_tex = (p.texture for p in material.properties if p.texture).__next__
68 if first_tex and not first_tex.default_filter:
69 from .export_texture import SamplerExporter
70 sampler_export = SamplerExporter()
71 st.sub.append(mat_res.create_reference_statement("sampler", resources[sampler_export.get_sampler_name(first_tex)]))
72 mat_res.statements.append(st)
76 def create_property_statement(self, mat_res, prop, keyword, resources, *, tex_only=False):
77 from .datafile import Statement
78 if self.use_textures and prop.texture:
79 tex_res = resources[prop.texture.image.name+".tex2d"]
80 fn = os.path.basename(prop.texture.image.filepath)
81 if prop.texture.default_filter and fn:
82 return Statement(keyword+"_map", fn)
84 return mat_res.create_reference_statement(keyword+"_map", tex_res)
87 elif type(prop.value)==tuple:
88 return Statement(keyword, *prop.value)
90 return Statement(keyword, prop.value)
93 class MaterialMapExporter:
97 def export_technique_resources(self, material_map, resources):
98 from .datafile import Resource, Statement, Token
99 base_color_name = material_map.name+"_base_color.tex2d"
100 base_color_res = resources.get(base_color_name)
101 if not base_color_res:
102 base_color_res = Resource(base_color_name, "texture2d")
104 base_color_res.statements.append(Statement("storage", Token('SRGB_ALPHA'), *material_map.size))
105 base_color_res.statements.append(Statement("raw_data", material_map.base_color_data))
107 resources[base_color_name] = base_color_res
109 sampler_name = "nearest.samp"
110 sampler_res = resources.get(sampler_name)
112 sampler_res = Resource(sampler_name, "sampler")
114 sampler_res.statements.append(Statement("filter", Token('NEAREST')))
116 resources[sampler_name] = sampler_res
118 mat_name = material_map.name+".mat"
119 if mat_name not in resources:
120 mat_res = Resource(mat_name, "material")
121 st = Statement("pbr")
122 st.sub.append(mat_res.create_reference_statement("base_color_map", base_color_res))
123 st.sub.append(mat_res.create_reference_statement("sampler", sampler_res))
124 mat_res.statements.append(st)
126 resources[mat_name] = mat_res
128 def export_technique(self, material_map, *, resources):
129 return create_technique_resource(material_map, resources)