]> git.tdb.fi Git - libs/gl.git/blob - blender/io_mspgl/export_material.py
Cosmetic fixes
[libs/gl.git] / blender / io_mspgl / export_material.py
1 import os
2
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")
7
8         mat_res = resources[material.name+".mat"]
9
10         st = Statement("pass", "")
11         if mat_res:
12                 st.sub.append(tech_res.create_embed_statement("material", mat_res))
13
14         if material.render_mode=='CUSTOM':
15                 shader = material.shader
16                 if shader.endswith(".glsl"):
17                         shader += ".shader"
18                 st.sub.append(Statement("shader", shader))
19
20                 if material.uniforms:
21                         ss = Statement("uniforms")
22                         for u in material.uniforms:
23                                 ss.sub.append(Statement("uniform", u.name, *u.values[:u.size]))
24                         st.sub.append(ss)
25         elif material.receive_shadows:
26                 st.sub.append(Statement("receive_shadows", True))
27
28         tech_res.statements.append(st)
29
30         if material.shadow_method!='NONE':
31                 st = Statement("pass", "shadow")
32                 st.sub.append(Statement("shader", "_occluder.glsl.shader"))
33                 tech_res.statements.append(st)
34
35         return tech_res
36
37 class MaterialExporter:
38         def create_texture_exporter(self):
39                 from .export_texture import TextureExporter
40                 texture_export = TextureExporter()
41                 return texture_export
42
43         def export_technique_resources(self, material, resources):
44                 from .export_texture import SamplerExporter
45                 texture_export = self.create_texture_exporter()
46                 sampler_export = SamplerExporter()
47
48                 from .material import Material
49                 material = Material(material)
50
51                 for p in material.properties:
52                         if p.texture:
53                                 tex_name = p.texture.image.name+".tex2d"
54                                 if tex_name not in resources:
55                                         resources[tex_name] = texture_export.export_texture(p.texture, p.tex_usage, invert_green=p.invert_green)
56
57                                 samp_name = sampler_export.get_sampler_name(p.texture)
58                                 if samp_name not in resources:
59                                         resources[samp_name] = sampler_export.export_sampler(p.texture)
60
61                 mat_name = material.name+".mat"
62                 if mat_name not in resources:
63                         if material.type:
64                                 resources[mat_name] = self.export_material(material, resources)
65                         else:
66                                 resources[mat_name] = None
67
68         def export_technique(self, material, resources):
69                 return create_technique_resource(material, resources)
70
71         def export_material(self, material, resources):
72                 from .datafile import Resource, Statement, Token
73                 mat_res = Resource(material.name+".mat", "material")
74
75                 if material.type!="pbr" and material.type!="unlit":
76                         raise Exception("Can't export material {} of unknown type {}".format(material.name, material.type))
77
78                 mat_res.statements.append(Statement("type", Token(material.type)));
79                 for p in material.properties:
80                         st = self.create_property_statement(mat_res, p, resources)
81                         if st:
82                                 mat_res.statements.append(st)
83                 textures = [p.texture for p in material.properties if p.texture]
84                 if textures and not all(t.default_filter for t in textures):
85                         from .export_texture import SamplerExporter
86                         sampler_tex = next(t for t in textures if not t.default_filter)
87                         sampler_export = SamplerExporter()
88                         mat_res.statements.append(mat_res.create_reference_statement("sampler", resources[sampler_export.get_sampler_name(sampler_tex)]))
89
90                 return mat_res
91
92         def create_property_statement(self, mat_res, prop, resources):
93                 from .datafile import Statement
94                 if prop.texture:
95                         tex_res = resources[prop.texture.image.name+".tex2d"]
96                         from .util import basename
97                         fn = basename(prop.texture.image.filepath)
98                         if prop.texture.default_filter and fn:
99                                 return Statement(prop.tex_keyword, fn)
100                         else:
101                                 return mat_res.create_reference_statement(prop.tex_keyword, tex_res)
102                 elif not prop.keyword:
103                         return
104                 elif type(prop.value)==tuple:
105                         return Statement(prop.keyword, *prop.value)
106                 else:
107                         return Statement(prop.keyword, prop.value)
108
109
110 class MaterialAtlasExporter:
111         def __init__(self):
112                 pass
113
114         def export_technique_resources(self, material_atlas, resources):
115                 from .datafile import Resource, Statement, Token
116                 base_color_name = material_atlas.name+"_base_color.tex2d"
117                 base_color_res = resources.get(base_color_name)
118                 if not base_color_res:
119                         base_color_res = Resource(base_color_name, "texture2d")
120
121                         base_color_res.statements.append(Statement("storage", Token('SRGB_ALPHA'), *material_atlas.size))
122                         base_color_res.statements.append(Statement("raw_data", material_atlas.base_color_data))
123
124                         resources[base_color_name] = base_color_res
125
126                 sampler_name = "nearest.samp"
127                 sampler_res = resources.get(sampler_name)
128                 if not sampler_res:
129                         sampler_res = Resource(sampler_name, "sampler")
130
131                         sampler_res.statements.append(Statement("filter", Token('NEAREST')))
132
133                         resources[sampler_name] = sampler_res
134
135                 mat_name = material_atlas.name+".mat"
136                 if mat_name not in resources:
137                         mat_res = Resource(mat_name, "material")
138                         mat_res.statements.append(Statement("type", Token('pbr')))
139                         mat_res.statements.append(mat_res.create_reference_statement("base_color_map", base_color_res))
140                         mat_res.statements.append(mat_res.create_reference_statement("sampler", sampler_res))
141
142                         resources[mat_name] = mat_res
143
144         def export_technique(self, material_atlas, resources):
145                 return create_technique_resource(material_atlas, resources)