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