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