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 export_technique_resources(self, material, resources):
54 from .export_texture import SamplerExporter, TextureExporter
55 texture_export = TextureExporter()
56 sampler_export = SamplerExporter()
58 from .material import Material
59 material = Material(material)
61 for p in material.properties:
63 tex_name = p.texture.image.name+".tex2d"
64 if tex_name not in resources:
65 resources[tex_name] = texture_export.export_texture(p.texture, p.tex_usage, invert_green=p.invert_green)
67 samp_name = sampler_export.get_sampler_name(p.texture)
68 if samp_name not in resources:
69 resources[samp_name] = sampler_export.export_sampler(p.texture)
71 mat_name = material.name+".mat"
72 if mat_name not in resources:
74 resources[mat_name] = self.export_material(material, resources)
76 resources[mat_name] = None
78 def export_technique(self, material, resources):
79 return create_technique_resource(material, resources)
81 def export_material(self, material, resources):
82 from .datafile import Resource, Statement, Token
83 mat_res = Resource(material.name+".mat", "material")
85 if material.type!="pbr" and material.type!="unlit":
86 raise Exception("Can't export material {} of unknown type {}".format(material.name, material.type))
88 mat_res.statements.append(Statement("type", Token(material.type)));
89 for p in material.properties:
90 st = self.create_property_statement(mat_res, p, resources)
92 mat_res.statements.append(st)
93 textures = [p.texture for p in material.properties if p.texture]
95 from .export_texture import SamplerExporter
96 sampler_export = SamplerExporter()
97 mat_res.statements.append(mat_res.create_reference_statement("sampler", resources[sampler_export.get_sampler_name(textures[0])]))
101 def create_property_statement(self, mat_res, prop, resources):
102 from .datafile import Statement
104 tex_res = resources[prop.texture.image.name+".tex2d"]
105 from .util import basename
106 return mat_res.create_reference_statement(prop.tex_keyword, tex_res)
107 elif not prop.keyword:
109 elif type(prop.value)==tuple:
110 return Statement(prop.keyword, *prop.value)
112 return Statement(prop.keyword, prop.value)
115 class MaterialAtlasExporter:
119 def export_technique_resources(self, material_atlas, resources):
120 from .datafile import Resource, Statement, Token
121 base_color_name = material_atlas.name+"_base_color.tex2d"
122 base_color_res = resources.get(base_color_name)
123 if not base_color_res:
124 base_color_res = Resource(base_color_name, "texture2d")
126 base_color_res.statements.append(Statement("storage", Token('SRGB_ALPHA'), *material_atlas.size))
127 base_color_res.statements.append(Statement("raw_data", material_atlas.base_color_data))
129 resources[base_color_name] = base_color_res
131 sampler_name = "nearest.samp"
132 sampler_res = resources.get(sampler_name)
134 sampler_res = Resource(sampler_name, "sampler")
136 sampler_res.statements.append(Statement("filter", Token('NEAREST')))
138 resources[sampler_name] = sampler_res
140 mat_name = material_atlas.name+".mat"
141 if mat_name not in resources:
142 mat_res = Resource(mat_name, "material")
143 mat_res.statements.append(Statement("type", Token('pbr')))
144 mat_res.statements.append(mat_res.create_reference_statement("base_color_map", base_color_res))
145 mat_res.statements.append(mat_res.create_reference_statement("sampler", sampler_res))
147 resources[mat_name] = mat_res
149 def export_technique(self, material_atlas, resources):
150 return create_technique_resource(material_atlas, resources)