]> git.tdb.fi Git - libs/gl.git/blob - blender/io_mspgl/export_material.py
Handle certain shader graph patterns that occur in imported glTF files
[libs/gl.git] / blender / io_mspgl / export_material.py
1 def create_technique_resource(material, resources):
2         from .datafile import Resource, Statement, Token
3         tech_res = Resource(material.name+".tech", "technique")
4
5         mat_res = resources[material.name+".mat"]
6
7         blend_st = None
8         if material.blend_type=='ALPHA':
9                 blend_st = Statement("blend", Token("SRC_ALPHA"), Token("ONE_MINUS_SRC_ALPHA"))
10         elif material.blend_type=='ADDITIVE':
11                 blend_st = Statement("blend", Token("ONE"), Token("ONE"))
12         elif material.blend_type=='ADDITIVE_ALPHA':
13                 blend_st = Statement("blend", Token("SRC_ALPHA"), Token("ONE"))
14
15         if material.render_mode=='CUSTOM':
16                 for m in material.render_methods:
17                         st = Statement("method", m.tag)
18                         if mat_res and m.use_material:
19                                 st.sub.append(tech_res.create_reference_statement("material", mat_res))
20
21                         if m.tag=="blended" and blend_st:
22                                 st.sub.append(blend_st)
23
24                         shader = m.shader
25                         if shader.endswith(".glsl"):
26                                 shader += ".shader"
27                         st.sub.append(Statement("shader", shader))
28
29                         if material.uniforms:
30                                 ss = Statement("uniforms")
31                                 for u in material.uniforms:
32                                         ss.sub.append(Statement("uniform", u.name, *u.values[:u.size]))
33                                 st.sub.append(ss)
34
35                         tech_res.statements.append(st)
36         else:
37                 base_method = "blended" if material.blend_type!='NONE' else ""
38                 st = Statement("method", base_method)
39                 if mat_res:
40                         st.sub.append(tech_res.create_embed_statement("material", mat_res))
41
42                 if blend_st:
43                         st.sub.append(blend_st)
44                 if material.receive_shadows:
45                         st.sub.append(Statement("receive_shadows", True))
46                 if material.image_based_lighting:
47                         st.sub.append(Statement("image_based_lighting", True))
48
49                 tech_res.statements.append(st)
50
51                 if material.cast_shadows:
52                         st = Statement("method", "shadow")
53                         st.sub.append(Statement("shader", "occluder.glsl.shader"))
54                         tech_res.statements.append(st)
55
56                         st = Statement("method", "shadow_thsm")
57                         st.sub.append(Statement("shader", "occluder_thsm.glsl.shader"))
58                         tech_res.statements.append(st)
59
60         return tech_res
61
62 class MaterialExporter:
63         def export_technique_resources(self, ctx, material, resources):
64                 from .export_texture import SamplerExporter, TextureExporter
65                 texture_export = TextureExporter()
66                 sampler_export = SamplerExporter()
67
68                 from .material import Material
69                 if type(material)!=Material:
70                         material = Material(material)
71
72                 textured_props = [p for p in material.properties if p.texture]
73
74                 ctx.set_slices(len(textured_props)+1)
75                 for p in textured_props:
76                         ctx.next_slice(p.texture.image)
77
78                         tex_name = texture_export.get_texture_name(p.texture, p.tex_channels)
79                         if tex_name not in resources:
80                                 resources[tex_name] = texture_export.export_texture(p.texture, p.tex_channels)
81
82                         samp_name = sampler_export.get_sampler_name(p.texture)
83                         if samp_name not in resources:
84                                 resources[samp_name] = sampler_export.export_sampler(p.texture)
85
86                 ctx.next_slice(material)
87                 mat_name = material.name+".mat"
88                 if mat_name not in resources:
89                         if material.type:
90                                 resources[mat_name] = self.export_material(material, resources)
91                         else:
92                                 resources[mat_name] = None
93
94         def export_technique(self, material, resources):
95                 from .material import Material
96                 if type(material)!=Material:
97                         material = Material(material)
98
99                 return create_technique_resource(material, resources)
100
101         def export_material(self, material, resources):
102                 from .datafile import Resource, Statement, Token
103                 mat_res = Resource(material.name+".mat", "material")
104
105                 if material.type!="pbr" and material.type!="unlit":
106                         raise Exception("Can't export material {} of unknown type {}".format(material.name, material.type))
107
108                 mat_res.statements.append(Statement("type", Token(material.type)));
109                 for p in material.properties:
110                         st = self.create_property_statement(mat_res, p, resources)
111                         if st:
112                                 mat_res.statements.append(st)
113                 textures = [p.texture for p in material.properties if p.texture]
114                 if textures:
115                         from .export_texture import SamplerExporter
116                         sampler_export = SamplerExporter()
117                         mat_res.statements.append(mat_res.create_reference_statement("sampler", resources[sampler_export.get_sampler_name(textures[0])]))
118
119                 return mat_res
120
121         def create_property_statement(self, mat_res, prop, resources):
122                 from .datafile import Statement
123                 if prop.texture:
124                         from .export_texture import TextureExporter
125                         texture_export = TextureExporter()
126                         tex_res = resources[texture_export.get_texture_name(prop.texture, prop.tex_channels)]
127                         return mat_res.create_reference_statement(prop.tex_keyword, tex_res)
128                 elif not prop.keyword:
129                         return
130                 elif type(prop.value)==tuple:
131                         return Statement(prop.keyword, *prop.value)
132                 else:
133                         return Statement(prop.keyword, prop.value)
134
135
136 class MaterialAtlasExporter:
137         def __init__(self):
138                 pass
139
140         def export_technique_resources(self, material_atlas, resources):
141                 from .datafile import Resource, Statement, Token
142                 base_color_name = material_atlas.name+"_base_color.tex"
143                 base_color_res = resources.get(base_color_name)
144                 if not base_color_res:
145                         base_color_res = Resource(base_color_name, "texture")
146
147                         base_color_res.statements.append(Statement("type", Token("\\2d")))
148                         base_color_res.statements.append(Statement("storage", Token('SRGB_ALPHA'), *material_atlas.size))
149                         base_color_res.statements.append(Statement("raw_data", material_atlas.base_color_data))
150
151                         resources[base_color_name] = base_color_res
152
153                 sampler_name = "nearest.samp"
154                 sampler_res = resources.get(sampler_name)
155                 if not sampler_res:
156                         sampler_res = Resource(sampler_name, "sampler")
157
158                         sampler_res.statements.append(Statement("filter", Token('NEAREST')))
159
160                         resources[sampler_name] = sampler_res
161
162                 mat_name = material_atlas.name+".mat"
163                 if mat_name not in resources:
164                         mat_res = Resource(mat_name, "material")
165                         mat_res.statements.append(Statement("type", Token('pbr')))
166                         mat_res.statements.append(mat_res.create_reference_statement("base_color_map", base_color_res))
167                         mat_res.statements.append(mat_res.create_reference_statement("sampler", sampler_res))
168
169                         resources[mat_name] = mat_res
170
171         def export_technique(self, material_atlas, resources):
172                 return create_technique_resource(material_atlas, resources)