]> git.tdb.fi Git - libs/gl.git/blob - blender/io_mspgl/export_material.py
Use bpy.path.basename instead of a custom function
[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, ctx, 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                 textured_props = [p for p in material.properties if p.texture]
75
76                 ctx.set_slices(len(textured_props)+1)
77                 for p in textured_props:
78                         ctx.next_slice(p.texture.image)
79
80                         tex_name = p.texture.image.name+".tex"
81                         if tex_name not in resources:
82                                 resources[tex_name] = texture_export.export_texture(p.texture, p.tex_channels)
83
84                         samp_name = sampler_export.get_sampler_name(p.texture)
85                         if samp_name not in resources:
86                                 resources[samp_name] = sampler_export.export_sampler(p.texture)
87
88                 ctx.next_slice(material)
89                 mat_name = material.name+".mat"
90                 if mat_name not in resources:
91                         if material.type:
92                                 resources[mat_name] = self.export_material(material, resources)
93                         else:
94                                 resources[mat_name] = None
95
96         def export_technique(self, material, resources):
97                 from .material import Material
98                 if type(material)!=Material:
99                         material = Material(material)
100
101                 return create_technique_resource(material, resources)
102
103         def export_material(self, material, resources):
104                 from .datafile import Resource, Statement, Token
105                 mat_res = Resource(material.name+".mat", "material")
106
107                 if material.type!="pbr" and material.type!="unlit":
108                         raise Exception("Can't export material {} of unknown type {}".format(material.name, material.type))
109
110                 mat_res.statements.append(Statement("type", Token(material.type)));
111                 for p in material.properties:
112                         st = self.create_property_statement(mat_res, p, resources)
113                         if st:
114                                 mat_res.statements.append(st)
115                 textures = [p.texture for p in material.properties if p.texture]
116                 if textures:
117                         from .export_texture import SamplerExporter
118                         sampler_export = SamplerExporter()
119                         mat_res.statements.append(mat_res.create_reference_statement("sampler", resources[sampler_export.get_sampler_name(textures[0])]))
120
121                 return mat_res
122
123         def create_property_statement(self, mat_res, prop, resources):
124                 from .datafile import Statement
125                 if prop.texture:
126                         tex_res = resources[prop.texture.image.name+".tex"]
127                         return mat_res.create_reference_statement(prop.tex_keyword, tex_res)
128                 elif not prop.keyword:
129                         return
130                 elif type(prop.value)==tuple:
131                         return Statement(prop.keyword, *prop.value)
132                 else:
133                         return Statement(prop.keyword, prop.value)
134
135
136 class MaterialAtlasExporter:
137         def __init__(self):
138                 pass
139
140         def export_technique_resources(self, material_atlas, resources):
141                 from .datafile import Resource, Statement, Token
142                 base_color_name = material_atlas.name+"_base_color.tex"
143                 base_color_res = resources.get(base_color_name)
144                 if not base_color_res:
145                         base_color_res = Resource(base_color_name, "texture")
146
147                         base_color_res.statements.append(Statement("type", Token("\\2d")))
148                         base_color_res.statements.append(Statement("storage", Token('SRGB_ALPHA'), *material_atlas.size))
149                         base_color_res.statements.append(Statement("raw_data", material_atlas.base_color_data))
150
151                         resources[base_color_name] = base_color_res
152
153                 sampler_name = "nearest.samp"
154                 sampler_res = resources.get(sampler_name)
155                 if not sampler_res:
156                         sampler_res = Resource(sampler_name, "sampler")
157
158                         sampler_res.statements.append(Statement("filter", Token('NEAREST')))
159
160                         resources[sampler_name] = sampler_res
161
162                 mat_name = material_atlas.name+".mat"
163                 if mat_name not in resources:
164                         mat_res = Resource(mat_name, "material")
165                         mat_res.statements.append(Statement("type", Token('pbr')))
166                         mat_res.statements.append(mat_res.create_reference_statement("base_color_map", base_color_res))
167                         mat_res.statements.append(mat_res.create_reference_statement("sampler", sampler_res))
168
169                         resources[mat_name] = mat_res
170
171         def export_technique(self, material_atlas, resources):
172                 return create_technique_resource(material_atlas, resources)