]> git.tdb.fi Git - libs/gl.git/blob - blender/io_mspgl/export_material.py
Wrap material before passing it to create_technique_resource
[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         if material.render_mode=='CUSTOM':
10                 for m in material.render_methods:
11                         st = Statement("method", m.tag)
12                         if mat_res and m.use_material:
13                                 st.sub.append(tech_res.create_reference_statement("material", mat_res))
14
15                         shader = m.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
26                         tech_res.statements.append(st)
27         else:
28                 st = Statement("method", "")
29                 if mat_res:
30                         st.sub.append(tech_res.create_embed_statement("material", mat_res))
31
32                 if material.render_mode!='CUSTOM':
33                         if material.receive_shadows:
34                                 st.sub.append(Statement("receive_shadows", True))
35                         if material.image_based_lighting:
36                                 st.sub.append(Statement("image_based_lighting", True))
37
38                 tech_res.statements.append(st)
39
40                 if material.cast_shadows:
41                         st = Statement("method", "shadow")
42                         st.sub.append(Statement("shader", "occluder.glsl.shader"))
43                         tech_res.statements.append(st)
44
45                         st = Statement("method", "shadow_thsm")
46                         st.sub.append(Statement("shader", "occluder_thsm.glsl.shader"))
47                         tech_res.statements.append(st)
48
49         return tech_res
50
51 class MaterialExporter:
52         def export_technique_resources(self, material, resources):
53                 from .export_texture import SamplerExporter, TextureExporter
54                 texture_export = TextureExporter()
55                 sampler_export = SamplerExporter()
56
57                 from .material import Material
58                 if type(material)!=Material:
59                         material = Material(material)
60
61                 for p in material.properties:
62                         if p.texture:
63                                 tex_name = p.texture.image.name+".tex2d"
64                                 if tex_name not in resources:
65                                         resources[tex_name] = texture_export.export_texture(p.texture, p.tex_usage, invert_green=p.invert_green)
66
67                                 samp_name = sampler_export.get_sampler_name(p.texture)
68                                 if samp_name not in resources:
69                                         resources[samp_name] = sampler_export.export_sampler(p.texture)
70
71                 mat_name = material.name+".mat"
72                 if mat_name not in resources:
73                         if material.type:
74                                 resources[mat_name] = self.export_material(material, resources)
75                         else:
76                                 resources[mat_name] = None
77
78         def export_technique(self, material, resources):
79                 from .material import Material
80                 if type(material)!=Material:
81                         material = Material(material)
82
83                 return create_technique_resource(material, resources)
84
85         def export_material(self, material, resources):
86                 from .datafile import Resource, Statement, Token
87                 mat_res = Resource(material.name+".mat", "material")
88
89                 if material.type!="pbr" and material.type!="unlit":
90                         raise Exception("Can't export material {} of unknown type {}".format(material.name, material.type))
91
92                 mat_res.statements.append(Statement("type", Token(material.type)));
93                 for p in material.properties:
94                         st = self.create_property_statement(mat_res, p, resources)
95                         if st:
96                                 mat_res.statements.append(st)
97                 textures = [p.texture for p in material.properties if p.texture]
98                 if textures:
99                         from .export_texture import SamplerExporter
100                         sampler_export = SamplerExporter()
101                         mat_res.statements.append(mat_res.create_reference_statement("sampler", resources[sampler_export.get_sampler_name(textures[0])]))
102
103                 return mat_res
104
105         def create_property_statement(self, mat_res, prop, resources):
106                 from .datafile import Statement
107                 if prop.texture:
108                         tex_res = resources[prop.texture.image.name+".tex2d"]
109                         from .util import basename
110                         return mat_res.create_reference_statement(prop.tex_keyword, tex_res)
111                 elif not prop.keyword:
112                         return
113                 elif type(prop.value)==tuple:
114                         return Statement(prop.keyword, *prop.value)
115                 else:
116                         return Statement(prop.keyword, prop.value)
117
118
119 class MaterialAtlasExporter:
120         def __init__(self):
121                 pass
122
123         def export_technique_resources(self, material_atlas, resources):
124                 from .datafile import Resource, Statement, Token
125                 base_color_name = material_atlas.name+"_base_color.tex2d"
126                 base_color_res = resources.get(base_color_name)
127                 if not base_color_res:
128                         base_color_res = Resource(base_color_name, "texture2d")
129
130                         base_color_res.statements.append(Statement("storage", Token('SRGB_ALPHA'), *material_atlas.size))
131                         base_color_res.statements.append(Statement("raw_data", material_atlas.base_color_data))
132
133                         resources[base_color_name] = base_color_res
134
135                 sampler_name = "nearest.samp"
136                 sampler_res = resources.get(sampler_name)
137                 if not sampler_res:
138                         sampler_res = Resource(sampler_name, "sampler")
139
140                         sampler_res.statements.append(Statement("filter", Token('NEAREST')))
141
142                         resources[sampler_name] = sampler_res
143
144                 mat_name = material_atlas.name+".mat"
145                 if mat_name not in resources:
146                         mat_res = Resource(mat_name, "material")
147                         mat_res.statements.append(Statement("type", Token('pbr')))
148                         mat_res.statements.append(mat_res.create_reference_statement("base_color_map", base_color_res))
149                         mat_res.statements.append(mat_res.create_reference_statement("sampler", sampler_res))
150
151                         resources[mat_name] = mat_res
152
153         def export_technique(self, material_atlas, resources):
154                 return create_technique_resource(material_atlas, resources)