3 def create_technique_resource(material, resources):
4 # This operates on a Blender material, not a custom object
5 from .datafile import Resource, Statement
6 tech_res = Resource(material.name+".tech", "technique")
8 mat_res = resources[material.name+".mat"]
10 st = Statement("pass", "")
12 st.sub.append(tech_res.create_embed_statement("material", mat_res))
14 if material.render_mode=='CUSTOM':
15 shader = material.shader
16 if shader.endswith(".glsl"):
18 st.sub.append(Statement("shader", shader))
21 ss = Statement("uniforms")
22 for u in material.uniforms:
23 ss.sub.append(Statement("uniform", u.name, *u.values[:u.size]))
25 elif material.receive_shadows:
26 st.sub.append(Statement("receive_shadows", True))
28 tech_res.statements.append(st)
30 if material.shadow_method!='NONE':
31 st = Statement("pass", "shadow")
32 st.sub.append(Statement("shader", "_occluder.glsl.shader"))
33 tech_res.statements.append(st)
37 class MaterialExporter:
38 def create_texture_exporter(self):
39 from .export_texture import TextureExporter
40 texture_export = TextureExporter()
43 def export_technique_resources(self, material, resources):
44 from .export_texture import SamplerExporter
45 texture_export = self.create_texture_exporter()
46 sampler_export = SamplerExporter()
48 from .material import Material
49 material = Material(material)
51 for p in material.properties:
53 tex_name = p.texture.image.name+".tex2d"
54 if tex_name not in resources:
55 resources[tex_name] = texture_export.export_texture(p.texture, p.tex_usage, invert_green=p.invert_green)
57 samp_name = sampler_export.get_sampler_name(p.texture)
58 if samp_name not in resources:
59 resources[samp_name] = sampler_export.export_sampler(p.texture)
61 mat_name = material.name+".mat"
62 if mat_name not in resources:
64 resources[mat_name] = self.export_material(material, resources)
66 resources[mat_name] = None
68 def export_technique(self, material, resources):
69 return create_technique_resource(material, resources)
71 def export_material(self, material, resources):
72 from .datafile import Resource, Statement, Token
73 mat_res = Resource(material.name+".mat", "material")
75 if material.type!="pbr" and material.type!="unlit":
76 raise Exception("Can't export material {} of unknown type {}".format(material.name, material.type))
78 mat_res.statements.append(Statement("type", Token(material.type)));
79 for p in material.properties:
80 st = self.create_property_statement(mat_res, p, resources)
82 mat_res.statements.append(st)
83 textures = [p.texture for p in material.properties if p.texture]
84 if textures and not all(t.default_filter for t in textures):
85 from .export_texture import SamplerExporter
86 sampler_tex = next(t for t in textures if not t.default_filter)
87 sampler_export = SamplerExporter()
88 mat_res.statements.append(mat_res.create_reference_statement("sampler", resources[sampler_export.get_sampler_name(sampler_tex)]))
92 def create_property_statement(self, mat_res, prop, resources):
93 from .datafile import Statement
95 tex_res = resources[prop.texture.image.name+".tex2d"]
96 from .util import basename
97 fn = basename(prop.texture.image.filepath)
98 if prop.texture.default_filter and fn:
99 return Statement(prop.tex_keyword, fn)
101 return mat_res.create_reference_statement(prop.tex_keyword, tex_res)
102 elif not prop.keyword:
104 elif type(prop.value)==tuple:
105 return Statement(prop.keyword, *prop.value)
107 return Statement(prop.keyword, prop.value)
110 class MaterialAtlasExporter:
114 def export_technique_resources(self, material_atlas, resources):
115 from .datafile import Resource, Statement, Token
116 base_color_name = material_atlas.name+"_base_color.tex2d"
117 base_color_res = resources.get(base_color_name)
118 if not base_color_res:
119 base_color_res = Resource(base_color_name, "texture2d")
121 base_color_res.statements.append(Statement("storage", Token('SRGB_ALPHA'), *material_atlas.size))
122 base_color_res.statements.append(Statement("raw_data", material_atlas.base_color_data))
124 resources[base_color_name] = base_color_res
126 sampler_name = "nearest.samp"
127 sampler_res = resources.get(sampler_name)
129 sampler_res = Resource(sampler_name, "sampler")
131 sampler_res.statements.append(Statement("filter", Token('NEAREST')))
133 resources[sampler_name] = sampler_res
135 mat_name = material_atlas.name+".mat"
136 if mat_name not in resources:
137 mat_res = Resource(mat_name, "material")
138 mat_res.statements.append(Statement("type", Token('pbr')))
139 mat_res.statements.append(mat_res.create_reference_statement("base_color_map", base_color_res))
140 mat_res.statements.append(mat_res.create_reference_statement("sampler", sampler_res))
142 resources[mat_name] = mat_res
144 def export_technique(self, material_atlas, resources):
145 return create_technique_resource(material_atlas, resources)