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