]> git.tdb.fi Git - libs/gl.git/blob - blender/io_mspgl/export_material.py
Add missing includes
[libs/gl.git] / blender / io_mspgl / export_material.py
1 import os
2
3 def create_technique_resource(material, resources):
4         from .datafile import Resource, Statement, Token
5         tech_res = Resource(material.name+".tech", "technique")
6
7         mat_res = resources[material.name+".mat"]
8
9         blend_st = None
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"))
16
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))
22
23                         if m.tag=="blended" and blend_st:
24                                 st.sub.append(blend_st)
25
26                         shader = m.shader
27                         if shader.endswith(".glsl"):
28                                 shader += ".shader"
29                         st.sub.append(Statement("shader", shader))
30
31                         if material.uniforms:
32                                 ss = Statement("uniforms")
33                                 for u in material.uniforms:
34                                         ss.sub.append(Statement("uniform", u.name, *u.values[:u.size]))
35                                 st.sub.append(ss)
36
37                         tech_res.statements.append(st)
38         else:
39                 base_method = "blended" if material.blend_type!='NONE' else ""
40                 st = Statement("method", base_method)
41                 if mat_res:
42                         st.sub.append(tech_res.create_embed_statement("material", mat_res))
43
44                 if blend_st:
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))
50
51                 tech_res.statements.append(st)
52
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)
57
58                         st = Statement("method", "shadow_thsm")
59                         st.sub.append(Statement("shader", "occluder_thsm.glsl.shader"))
60                         tech_res.statements.append(st)
61
62         return tech_res
63
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()
69
70                 from .material import Material
71                 if type(material)!=Material:
72                         material = Material(material)
73
74                 for p in material.properties:
75                         if p.texture:
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)
79
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)
83
84                 mat_name = material.name+".mat"
85                 if mat_name not in resources:
86                         if material.type:
87                                 resources[mat_name] = self.export_material(material, resources)
88                         else:
89                                 resources[mat_name] = None
90
91         def export_technique(self, material, resources):
92                 from .material import Material
93                 if type(material)!=Material:
94                         material = Material(material)
95
96                 return create_technique_resource(material, resources)
97
98         def export_material(self, material, resources):
99                 from .datafile import Resource, Statement, Token
100                 mat_res = Resource(material.name+".mat", "material")
101
102                 if material.type!="pbr" and material.type!="unlit":
103                         raise Exception("Can't export material {} of unknown type {}".format(material.name, material.type))
104
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)
108                         if st:
109                                 mat_res.statements.append(st)
110                 textures = [p.texture for p in material.properties if p.texture]
111                 if textures:
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])]))
115
116                 return mat_res
117
118         def create_property_statement(self, mat_res, prop, resources):
119                 from .datafile import Statement
120                 if prop.texture:
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:
125                         return
126                 elif type(prop.value)==tuple:
127                         return Statement(prop.keyword, *prop.value)
128                 else:
129                         return Statement(prop.keyword, prop.value)
130
131
132 class MaterialAtlasExporter:
133         def __init__(self):
134                 pass
135
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")
142
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))
145
146                         resources[base_color_name] = base_color_res
147
148                 sampler_name = "nearest.samp"
149                 sampler_res = resources.get(sampler_name)
150                 if not sampler_res:
151                         sampler_res = Resource(sampler_name, "sampler")
152
153                         sampler_res.statements.append(Statement("filter", Token('NEAREST')))
154
155                         resources[sampler_name] = sampler_res
156
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))
163
164                         resources[mat_name] = mat_res
165
166         def export_technique(self, material_atlas, resources):
167                 return create_technique_resource(material_atlas, resources)