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