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 if material.render_mode=='CUSTOM':
11 for m in material.render_methods:
12 st = Statement("method", m.tag)
13 if mat_res and m.use_material:
14 st.sub.append(tech_res.create_reference_statement("material", mat_res))
17 if shader.endswith(".glsl"):
19 st.sub.append(Statement("shader", shader))
22 ss = Statement("uniforms")
23 for u in material.uniforms:
24 ss.sub.append(Statement("uniform", u.name, *u.values[:u.size]))
27 tech_res.statements.append(st)
29 st = Statement("method", "")
31 st.sub.append(tech_res.create_embed_statement("material", mat_res))
33 if material.render_mode!='CUSTOM':
34 if material.receive_shadows:
35 st.sub.append(Statement("receive_shadows", True))
36 if material.image_based_lighting:
37 st.sub.append(Statement("image_based_lighting", True))
39 tech_res.statements.append(st)
41 if material.shadow_method!='NONE':
42 st = Statement("method", "shadow")
43 st.sub.append(Statement("shader", "occluder.glsl.shader"))
44 tech_res.statements.append(st)
46 st = Statement("method", "shadow_thsm")
47 st.sub.append(Statement("shader", "occluder_thsm.glsl.shader"))
48 tech_res.statements.append(st)
52 class MaterialExporter:
53 def create_texture_exporter(self):
54 from .export_texture import TextureExporter
55 texture_export = TextureExporter()
58 def export_technique_resources(self, material, resources):
59 from .export_texture import SamplerExporter
60 texture_export = self.create_texture_exporter()
61 sampler_export = SamplerExporter()
63 from .material import Material
64 material = Material(material)
66 for p in material.properties:
68 tex_name = p.texture.image.name+".tex2d"
69 if tex_name not in resources:
70 resources[tex_name] = texture_export.export_texture(p.texture, p.tex_usage, invert_green=p.invert_green)
72 samp_name = sampler_export.get_sampler_name(p.texture)
73 if samp_name not in resources:
74 resources[samp_name] = sampler_export.export_sampler(p.texture)
76 mat_name = material.name+".mat"
77 if mat_name not in resources:
79 resources[mat_name] = self.export_material(material, resources)
81 resources[mat_name] = None
83 def export_technique(self, material, resources):
84 return create_technique_resource(material, resources)
86 def export_material(self, material, resources):
87 from .datafile import Resource, Statement, Token
88 mat_res = Resource(material.name+".mat", "material")
90 if material.type!="pbr" and material.type!="unlit":
91 raise Exception("Can't export material {} of unknown type {}".format(material.name, material.type))
93 mat_res.statements.append(Statement("type", Token(material.type)));
94 for p in material.properties:
95 st = self.create_property_statement(mat_res, p, resources)
97 mat_res.statements.append(st)
98 textures = [p.texture for p in material.properties if p.texture]
100 from .export_texture import SamplerExporter
101 sampler_export = SamplerExporter()
102 mat_res.statements.append(mat_res.create_reference_statement("sampler", resources[sampler_export.get_sampler_name(textures[0])]))
106 def create_property_statement(self, mat_res, prop, resources):
107 from .datafile import Statement
109 tex_res = resources[prop.texture.image.name+".tex2d"]
110 from .util import basename
111 return mat_res.create_reference_statement(prop.tex_keyword, tex_res)
112 elif not prop.keyword:
114 elif type(prop.value)==tuple:
115 return Statement(prop.keyword, *prop.value)
117 return Statement(prop.keyword, prop.value)
120 class MaterialAtlasExporter:
124 def export_technique_resources(self, material_atlas, resources):
125 from .datafile import Resource, Statement, Token
126 base_color_name = material_atlas.name+"_base_color.tex2d"
127 base_color_res = resources.get(base_color_name)
128 if not base_color_res:
129 base_color_res = Resource(base_color_name, "texture2d")
131 base_color_res.statements.append(Statement("storage", Token('SRGB_ALPHA'), *material_atlas.size))
132 base_color_res.statements.append(Statement("raw_data", material_atlas.base_color_data))
134 resources[base_color_name] = base_color_res
136 sampler_name = "nearest.samp"
137 sampler_res = resources.get(sampler_name)
139 sampler_res = Resource(sampler_name, "sampler")
141 sampler_res.statements.append(Statement("filter", Token('NEAREST')))
143 resources[sampler_name] = sampler_res
145 mat_name = material_atlas.name+".mat"
146 if mat_name not in resources:
147 mat_res = Resource(mat_name, "material")
148 mat_res.statements.append(Statement("type", Token('pbr')))
149 mat_res.statements.append(mat_res.create_reference_statement("base_color_map", base_color_res))
150 mat_res.statements.append(mat_res.create_reference_statement("sampler", sampler_res))
152 resources[mat_name] = mat_res
154 def export_technique(self, material_atlas, resources):
155 return create_technique_resource(material_atlas, resources)