]> git.tdb.fi Git - libs/gl.git/blob - blender/io_mspgl/export_material.py
Implement image-based lighting in PbrMaterial
[libs/gl.git] / blender / io_mspgl / export_material.py
1 import os
2
3 def create_technique_resource(material, resources):
4         # This operates on a Blender material, not a custom object
5         from .datafile import Resource, Statement
6         tech_res = Resource(material.name+".tech", "technique")
7
8         mat_res = resources[material.name+".mat"]
9
10         st = Statement("pass", "")
11         if mat_res:
12                 st.sub.append(tech_res.create_embed_statement("material", mat_res))
13
14         if material.render_mode=='CUSTOM':
15                 shader = material.shader
16                 if shader.endswith(".glsl"):
17                         shader += ".shader"
18                 st.sub.append(Statement("shader", shader))
19
20                 if material.uniforms:
21                         ss = Statement("uniforms")
22                         for u in material.uniforms:
23                                 ss.sub.append(Statement("uniform", u.name, *u.values[:u.size]))
24                         st.sub.append(ss)
25         else:
26                 if material.receive_shadows:
27                         st.sub.append(Statement("receive_shadows", True))
28                 if material.image_based_lighting:
29                         st.sub.append(Statement("image_based_lighting", True))
30
31         tech_res.statements.append(st)
32
33         if material.shadow_method!='NONE':
34                 st = Statement("pass", "shadow")
35                 st.sub.append(Statement("shader", "_occluder.glsl.shader"))
36                 tech_res.statements.append(st)
37
38         return tech_res
39
40 class MaterialExporter:
41         def create_texture_exporter(self):
42                 from .export_texture import TextureExporter
43                 texture_export = TextureExporter()
44                 return texture_export
45
46         def export_technique_resources(self, material, resources):
47                 from .export_texture import SamplerExporter
48                 texture_export = self.create_texture_exporter()
49                 sampler_export = SamplerExporter()
50
51                 from .material import Material
52                 material = Material(material)
53
54                 for p in material.properties:
55                         if p.texture:
56                                 tex_name = p.texture.image.name+".tex2d"
57                                 if tex_name not in resources:
58                                         resources[tex_name] = texture_export.export_texture(p.texture, p.tex_usage, invert_green=p.invert_green)
59
60                                 samp_name = sampler_export.get_sampler_name(p.texture)
61                                 if samp_name not in resources:
62                                         resources[samp_name] = sampler_export.export_sampler(p.texture)
63
64                 mat_name = material.name+".mat"
65                 if mat_name not in resources:
66                         if material.type:
67                                 resources[mat_name] = self.export_material(material, resources)
68                         else:
69                                 resources[mat_name] = None
70
71         def export_technique(self, material, resources):
72                 return create_technique_resource(material, resources)
73
74         def export_material(self, material, resources):
75                 from .datafile import Resource, Statement, Token
76                 mat_res = Resource(material.name+".mat", "material")
77
78                 if material.type!="pbr" and material.type!="unlit":
79                         raise Exception("Can't export material {} of unknown type {}".format(material.name, material.type))
80
81                 mat_res.statements.append(Statement("type", Token(material.type)));
82                 for p in material.properties:
83                         st = self.create_property_statement(mat_res, p, resources)
84                         if st:
85                                 mat_res.statements.append(st)
86                 textures = [p.texture for p in material.properties if p.texture]
87                 if textures and not all(t.default_filter for t in textures):
88                         from .export_texture import SamplerExporter
89                         sampler_tex = next(t for t in textures if not t.default_filter)
90                         sampler_export = SamplerExporter()
91                         mat_res.statements.append(mat_res.create_reference_statement("sampler", resources[sampler_export.get_sampler_name(sampler_tex)]))
92
93                 return mat_res
94
95         def create_property_statement(self, mat_res, prop, resources):
96                 from .datafile import Statement
97                 if prop.texture:
98                         tex_res = resources[prop.texture.image.name+".tex2d"]
99                         from .util import basename
100                         fn = basename(prop.texture.image.filepath)
101                         if prop.texture.default_filter and fn:
102                                 return Statement(prop.tex_keyword, fn)
103                         else:
104                                 return mat_res.create_reference_statement(prop.tex_keyword, tex_res)
105                 elif not prop.keyword:
106                         return
107                 elif type(prop.value)==tuple:
108                         return Statement(prop.keyword, *prop.value)
109                 else:
110                         return Statement(prop.keyword, prop.value)
111
112
113 class MaterialAtlasExporter:
114         def __init__(self):
115                 pass
116
117         def export_technique_resources(self, material_atlas, resources):
118                 from .datafile import Resource, Statement, Token
119                 base_color_name = material_atlas.name+"_base_color.tex2d"
120                 base_color_res = resources.get(base_color_name)
121                 if not base_color_res:
122                         base_color_res = Resource(base_color_name, "texture2d")
123
124                         base_color_res.statements.append(Statement("storage", Token('SRGB_ALPHA'), *material_atlas.size))
125                         base_color_res.statements.append(Statement("raw_data", material_atlas.base_color_data))
126
127                         resources[base_color_name] = base_color_res
128
129                 sampler_name = "nearest.samp"
130                 sampler_res = resources.get(sampler_name)
131                 if not sampler_res:
132                         sampler_res = Resource(sampler_name, "sampler")
133
134                         sampler_res.statements.append(Statement("filter", Token('NEAREST')))
135
136                         resources[sampler_name] = sampler_res
137
138                 mat_name = material_atlas.name+".mat"
139                 if mat_name not in resources:
140                         mat_res = Resource(mat_name, "material")
141                         mat_res.statements.append(Statement("type", Token('pbr')))
142                         mat_res.statements.append(mat_res.create_reference_statement("base_color_map", base_color_res))
143                         mat_res.statements.append(mat_res.create_reference_statement("sampler", sampler_res))
144
145                         resources[mat_name] = mat_res
146
147         def export_technique(self, material_atlas, resources):
148                 return create_technique_resource(material_atlas, resources)