3 def create_technique_resource(material, resources):
4 from .datafile import Resource, Statement, Token
5 tech_res = Resource(material.name+".tech", "technique")
7 mat_res = resources[material.name+".mat"]
10 if material.blend_type=='ALPHA':
11 blend_st = Statement("blend", Token("SRC_ALPHA"), Token("ONE_MINUS_SRC_ALPHA"))
12 elif material.blend_type=='ADDITIVE':
13 blend_st = Statement("blend", Token("ONE"), Token("ONE"))
14 elif material.blend_type=='ADDITIVE_ALPHA':
15 blend_st = Statement("blend", Token("SRC_ALPHA"), Token("ONE"))
17 if material.render_mode=='CUSTOM':
18 for m in material.render_methods:
19 st = Statement("method", m.tag)
20 if mat_res and m.use_material:
21 st.sub.append(tech_res.create_reference_statement("material", mat_res))
23 if m.tag=="blended" and blend_st:
24 st.sub.append(blend_st)
27 if shader.endswith(".glsl"):
29 st.sub.append(Statement("shader", shader))
32 ss = Statement("uniforms")
33 for u in material.uniforms:
34 ss.sub.append(Statement("uniform", u.name, *u.values[:u.size]))
37 tech_res.statements.append(st)
39 base_method = "blended" if material.blend_type!='NONE' else ""
40 st = Statement("method", base_method)
42 st.sub.append(tech_res.create_embed_statement("material", mat_res))
45 st.sub.append(blend_st)
46 if material.receive_shadows:
47 st.sub.append(Statement("receive_shadows", True))
48 if material.image_based_lighting:
49 st.sub.append(Statement("image_based_lighting", True))
51 tech_res.statements.append(st)
53 if material.cast_shadows:
54 st = Statement("method", "shadow")
55 st.sub.append(Statement("shader", "occluder.glsl.shader"))
56 tech_res.statements.append(st)
58 st = Statement("method", "shadow_thsm")
59 st.sub.append(Statement("shader", "occluder_thsm.glsl.shader"))
60 tech_res.statements.append(st)
64 class MaterialExporter:
65 def export_technique_resources(self, material, resources):
66 from .export_texture import SamplerExporter, TextureExporter
67 texture_export = TextureExporter()
68 sampler_export = SamplerExporter()
70 from .material import Material
71 if type(material)!=Material:
72 material = Material(material)
74 for p in material.properties:
76 tex_name = p.texture.image.name+".tex2d"
77 if tex_name not in resources:
78 resources[tex_name] = texture_export.export_texture(p.texture, p.tex_usage, invert_green=p.invert_green)
80 samp_name = sampler_export.get_sampler_name(p.texture)
81 if samp_name not in resources:
82 resources[samp_name] = sampler_export.export_sampler(p.texture)
84 mat_name = material.name+".mat"
85 if mat_name not in resources:
87 resources[mat_name] = self.export_material(material, resources)
89 resources[mat_name] = None
91 def export_technique(self, material, resources):
92 from .material import Material
93 if type(material)!=Material:
94 material = Material(material)
96 return create_technique_resource(material, resources)
98 def export_material(self, material, resources):
99 from .datafile import Resource, Statement, Token
100 mat_res = Resource(material.name+".mat", "material")
102 if material.type!="pbr" and material.type!="unlit":
103 raise Exception("Can't export material {} of unknown type {}".format(material.name, material.type))
105 mat_res.statements.append(Statement("type", Token(material.type)));
106 for p in material.properties:
107 st = self.create_property_statement(mat_res, p, resources)
109 mat_res.statements.append(st)
110 textures = [p.texture for p in material.properties if p.texture]
112 from .export_texture import SamplerExporter
113 sampler_export = SamplerExporter()
114 mat_res.statements.append(mat_res.create_reference_statement("sampler", resources[sampler_export.get_sampler_name(textures[0])]))
118 def create_property_statement(self, mat_res, prop, resources):
119 from .datafile import Statement
121 tex_res = resources[prop.texture.image.name+".tex2d"]
122 from .util import basename
123 return mat_res.create_reference_statement(prop.tex_keyword, tex_res)
124 elif not prop.keyword:
126 elif type(prop.value)==tuple:
127 return Statement(prop.keyword, *prop.value)
129 return Statement(prop.keyword, prop.value)
132 class MaterialAtlasExporter:
136 def export_technique_resources(self, material_atlas, resources):
137 from .datafile import Resource, Statement, Token
138 base_color_name = material_atlas.name+"_base_color.tex2d"
139 base_color_res = resources.get(base_color_name)
140 if not base_color_res:
141 base_color_res = Resource(base_color_name, "texture2d")
143 base_color_res.statements.append(Statement("storage", Token('SRGB_ALPHA'), *material_atlas.size))
144 base_color_res.statements.append(Statement("raw_data", material_atlas.base_color_data))
146 resources[base_color_name] = base_color_res
148 sampler_name = "nearest.samp"
149 sampler_res = resources.get(sampler_name)
151 sampler_res = Resource(sampler_name, "sampler")
153 sampler_res.statements.append(Statement("filter", Token('NEAREST')))
155 resources[sampler_name] = sampler_res
157 mat_name = material_atlas.name+".mat"
158 if mat_name not in resources:
159 mat_res = Resource(mat_name, "material")
160 mat_res.statements.append(Statement("type", Token('pbr')))
161 mat_res.statements.append(mat_res.create_reference_statement("base_color_map", base_color_res))
162 mat_res.statements.append(mat_res.create_reference_statement("sampler", sampler_res))
164 resources[mat_name] = mat_res
166 def export_technique(self, material_atlas, resources):
167 return create_technique_resource(material_atlas, resources)