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