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