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 st.sub.append(Statement("shader", material.shader))
16 tech_res.statements.append(st)
20 class MaterialExporter:
22 self.use_textures = True
23 self.inline_texture_data = False
25 def create_texture_exporter(self):
26 from .export_texture import TextureExporter
27 texture_export = TextureExporter()
28 texture_export.inline_data = self.inline_texture_data
31 def export_technique_resources(self, material, resources):
32 from .export_texture import SamplerExporter
33 texture_export = self.create_texture_exporter()
34 sampler_export = SamplerExporter()
36 from .material import Material
37 material = Material(material)
40 for p in material.properties.values():
42 tex_name = p.texture.image.name+".tex2d"
43 if tex_name not in resources:
44 resources[tex_name] = texture_export.export_texture(p.texture, p.tex_usage)
46 samp_name = sampler_export.get_sampler_name(p.texture)
47 if samp_name not in resources:
48 resources[samp_name] = sampler_export.export_sampler(p.texture)
50 mat_name = material.name+".mat"
51 if mat_name not in resources:
53 resources[mat_name] = self.export_material(material, resources=resources)
55 resources[mat_name] = None
57 def export_technique(self, material, *, resources):
58 return create_technique_resource(material, resources)
60 def export_material(self, material, *, resources):
61 from .datafile import Resource, Statement
62 mat_res = Resource(material.name+".mat", "material")
64 if material.type!="pbr":
65 raise Exception("Can't export unknown material type "+material.type)
67 st = Statement(material.type)
68 for kw, p in material.properties.items():
69 ss = self.create_property_statement(mat_res, p, kw, resources)
73 first_tex = (p.texture for p in material.properties.values() if p.texture).__next__()
74 if first_tex and not first_tex.default_filter:
75 from .export_texture import SamplerExporter
76 sampler_export = SamplerExporter()
77 st.sub.append(mat_res.create_reference_statement("sampler", resources[sampler_export.get_sampler_name(first_tex)]))
78 mat_res.statements.append(st)
82 def create_property_statement(self, mat_res, prop, keyword, resources):
83 from .datafile import Statement
84 if self.use_textures and prop.texture:
85 tex_res = resources[prop.texture.image.name+".tex2d"]
86 fn = os.path.basename(prop.texture.image.filepath)
87 if prop.texture.default_filter and fn:
88 return Statement(keyword+"_map", fn)
90 return mat_res.create_reference_statement(keyword+"_map", tex_res)
91 elif prop.value is None:
93 elif type(prop.value)==tuple:
94 return Statement(keyword, *prop.value)
96 return Statement(keyword, prop.value)
99 class MaterialMapExporter:
103 def export_technique_resources(self, material_map, resources):
104 from .datafile import Resource, Statement, Token
105 base_color_name = material_map.name+"_base_color.tex2d"
106 base_color_res = resources.get(base_color_name)
107 if not base_color_res:
108 base_color_res = Resource(base_color_name, "texture2d")
110 base_color_res.statements.append(Statement("storage", Token('SRGB_ALPHA'), *material_map.size))
111 base_color_res.statements.append(Statement("raw_data", material_map.base_color_data))
113 resources[base_color_name] = base_color_res
115 sampler_name = "nearest.samp"
116 sampler_res = resources.get(sampler_name)
118 sampler_res = Resource(sampler_name, "sampler")
120 sampler_res.statements.append(Statement("filter", Token('NEAREST')))
122 resources[sampler_name] = sampler_res
124 mat_name = material_map.name+".mat"
125 if mat_name not in resources:
126 mat_res = Resource(mat_name, "material")
127 st = Statement("pbr")
128 st.sub.append(mat_res.create_reference_statement("base_color_map", base_color_res))
129 st.sub.append(mat_res.create_reference_statement("sampler", sampler_res))
130 mat_res.statements.append(st)
132 resources[mat_name] = mat_res
134 def export_technique(self, material_map, *, resources):
135 return create_technique_resource(material_map, resources)