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