1 def create_technique_resource(material, resources):
2 from .datafile import Resource, Statement, Token
3 tech_res = Resource(material.name+".tech", "technique")
5 mat_res = resources[material.name+".mat"]
8 if material.blend_type=='ALPHA':
9 blend_st = Statement("blend", Token("SRC_ALPHA"), Token("ONE_MINUS_SRC_ALPHA"))
10 elif material.blend_type=='ADDITIVE':
11 blend_st = Statement("blend", Token("ONE"), Token("ONE"))
12 elif material.blend_type=='ADDITIVE_ALPHA':
13 blend_st = Statement("blend", Token("SRC_ALPHA"), Token("ONE"))
15 if material.render_mode=='CUSTOM':
16 for m in material.render_methods:
17 st = Statement("method", m.tag)
18 if mat_res and m.use_material:
19 st.sub.append(tech_res.create_reference_statement("material", mat_res))
21 if m.tag=="blended" and blend_st:
22 st.sub.append(blend_st)
25 if shader.endswith(".glsl"):
27 st.sub.append(Statement("shader", shader))
30 ss = Statement("uniforms")
31 for u in material.uniforms:
32 ss.sub.append(Statement("uniform", u.name, *u.values[:u.size]))
35 tech_res.statements.append(st)
37 base_method = "blended" if material.blend_type!='NONE' else ""
38 st = Statement("method", base_method)
40 st.sub.append(tech_res.create_embed_statement("material", mat_res))
43 st.sub.append(blend_st)
44 if material.receive_shadows:
45 st.sub.append(Statement("receive_shadows", True))
46 if material.image_based_lighting:
47 st.sub.append(Statement("image_based_lighting", True))
49 tech_res.statements.append(st)
51 if material.cast_shadows:
52 st = Statement("method", "shadow")
53 st.sub.append(Statement("shader", "occluder.glsl.shader"))
54 tech_res.statements.append(st)
56 st = Statement("method", "shadow_thsm")
57 st.sub.append(Statement("shader", "occluder_thsm.glsl.shader"))
58 tech_res.statements.append(st)
62 class MaterialExporter:
63 def export_technique_resources(self, ctx, material, resources):
64 from .export_texture import SamplerExporter, TextureExporter
65 texture_export = TextureExporter()
66 sampler_export = SamplerExporter()
68 from .material import Material
69 if type(material)!=Material:
70 material = Material(material)
72 textured_props = [p for p in material.properties if p.texture]
74 ctx.set_slices(len(textured_props)+1)
75 for p in textured_props:
76 ctx.next_slice(p.texture.image)
78 tex_name = p.texture.image.name+".tex"
79 if tex_name not in resources:
80 resources[tex_name] = texture_export.export_texture(p.texture, p.tex_channels)
82 samp_name = sampler_export.get_sampler_name(p.texture)
83 if samp_name not in resources:
84 resources[samp_name] = sampler_export.export_sampler(p.texture)
86 ctx.next_slice(material)
87 mat_name = material.name+".mat"
88 if mat_name not in resources:
90 resources[mat_name] = self.export_material(material, resources)
92 resources[mat_name] = None
94 def export_technique(self, material, resources):
95 from .material import Material
96 if type(material)!=Material:
97 material = Material(material)
99 return create_technique_resource(material, resources)
101 def export_material(self, material, resources):
102 from .datafile import Resource, Statement, Token
103 mat_res = Resource(material.name+".mat", "material")
105 if material.type!="pbr" and material.type!="unlit":
106 raise Exception("Can't export material {} of unknown type {}".format(material.name, material.type))
108 mat_res.statements.append(Statement("type", Token(material.type)));
109 for p in material.properties:
110 st = self.create_property_statement(mat_res, p, resources)
112 mat_res.statements.append(st)
113 textures = [p.texture for p in material.properties if p.texture]
115 from .export_texture import SamplerExporter
116 sampler_export = SamplerExporter()
117 mat_res.statements.append(mat_res.create_reference_statement("sampler", resources[sampler_export.get_sampler_name(textures[0])]))
121 def create_property_statement(self, mat_res, prop, resources):
122 from .datafile import Statement
124 tex_res = resources[prop.texture.image.name+".tex"]
125 return mat_res.create_reference_statement(prop.tex_keyword, tex_res)
126 elif not prop.keyword:
128 elif type(prop.value)==tuple:
129 return Statement(prop.keyword, *prop.value)
131 return Statement(prop.keyword, prop.value)
134 class MaterialAtlasExporter:
138 def export_technique_resources(self, material_atlas, resources):
139 from .datafile import Resource, Statement, Token
140 base_color_name = material_atlas.name+"_base_color.tex"
141 base_color_res = resources.get(base_color_name)
142 if not base_color_res:
143 base_color_res = Resource(base_color_name, "texture")
145 base_color_res.statements.append(Statement("type", Token("\\2d")))
146 base_color_res.statements.append(Statement("storage", Token('SRGB_ALPHA'), *material_atlas.size))
147 base_color_res.statements.append(Statement("raw_data", material_atlas.base_color_data))
149 resources[base_color_name] = base_color_res
151 sampler_name = "nearest.samp"
152 sampler_res = resources.get(sampler_name)
154 sampler_res = Resource(sampler_name, "sampler")
156 sampler_res.statements.append(Statement("filter", Token('NEAREST')))
158 resources[sampler_name] = sampler_res
160 mat_name = material_atlas.name+".mat"
161 if mat_name not in resources:
162 mat_res = Resource(mat_name, "material")
163 mat_res.statements.append(Statement("type", Token('pbr')))
164 mat_res.statements.append(mat_res.create_reference_statement("base_color_map", base_color_res))
165 mat_res.statements.append(mat_res.create_reference_statement("sampler", sampler_res))
167 resources[mat_name] = mat_res
169 def export_technique(self, material_atlas, resources):
170 return create_technique_resource(material_atlas, resources)