]> git.tdb.fi Git - libs/gl.git/blob - blender/io_mspgl/export_material.py
Refactor texture handling in the material exporter
[libs/gl.git] / blender / io_mspgl / export_material.py
1 import os
2
3 def linear_to_srgb(l):
4         if l<0.0031308:
5                 return 12.92*l
6         else:
7                 return 1.055*(l**(1/2.4))-0.055
8
9 def get_colormap(srgb):
10         if srgb:
11                 return linear_to_srgb
12         else:
13                 return lambda x: x
14
15 def image_name(i):
16         fp = i.filepath
17         if fp:
18                 return os.path.split(fp)[1]
19         else:
20                 return i.name
21
22
23 class MaterialExporter:
24         def __init__(self):
25                 self.textures = 'REF'
26
27         def export_technique_resources(self, material, resources):
28                 from .export_texture import TextureExporter
29                 texture_export = TextureExporter()
30
31                 mat_name = material.name+".mat"
32                 if mat_name not in resources:
33                         resources[mat_name] = self.export_material(material)
34
35                 if self.textures=='INLINE':
36                         for s in material.texture_slots:
37                                 if s and s.texture.type=='IMAGE' and s.texture.image:
38                                         tex_name = s.texture.name+".tex2d"
39                                         if tex_name not in resources:
40                                                 resources[tex_name] = texture_export.export_texture(s.texture)
41
42         def export_technique(self, material, *, resources):
43                 from .datafile import Resource, Statement
44                 tech_res = Resource(material.name+".tech")
45
46                 mat_res = resources[material.name+".mat"]
47                 textures = {}
48                 if self.textures!='NONE':
49                         image_texture_slots = [s for s in material.texture_slots if s and s.texture.type=='IMAGE' and s.texture.image]
50                         for s in image_texture_slots:
51                                 if s.use_map_color_diffuse:
52                                         textures["diffuse_map"] = s.texture
53                                 elif s.use_map_normal:
54                                         textures["normal_map"] = s.texture
55
56                 if material.technique:
57                         if not obj.inherit_tech:
58                                 return []
59
60                         st = Statement("inherit", material.technique)
61                         for s, t in textures.items():
62                                 st.sub.append(Statement("texture", s, image_name(t.image)))
63                         if material.override_material:
64                                 st.sub.append(tech_res.create_reference_statement("material", "surface", mat_res))
65                         tech_res.statements.append(st)
66                 else:
67                         st = Statement("pass", "")
68                         st.sub.append(tech_res.create_embed_statement("material", mat_res))
69
70                         if "diffuse_map" in textures:
71                                 diffuse_tex = textures["diffuse_map"]
72                                 ss = Statement("texunit", 0)
73                                 if self.textures=='INLINE':
74                                         ss.sub.append(tech_res.create_embed_statement("texture2d", resources[diffuse_tex.name+".tex2d"]))
75                                 else:
76                                         ss.sub.append(Statement("texture", image_name(diffuse_tex.image)))
77                                 st.sub.append(ss)
78
79                         tech_res.statements.append(st)
80
81                 return tech_res
82
83         def export_material(self, material):
84                 from .datafile import Resource, Statement
85                 mat_res = Resource(material.name+".mat")
86                 statements = mat_res.statements
87
88                 cm = get_colormap(material.srgb_colors)
89                 if any(s.use_map_color_diffuse for s in material.texture_slots if s):
90                         statements.append(Statement("diffuse", 1.0, 1.0, 1.0, 1.0))
91                         amb = cm(material.ambient)
92                         statements.append(Statement("ambient", amb, amb, amb, 1.0))
93                 else:
94                         diff = material.diffuse_color*material.diffuse_intensity
95                         statements.append(Statement("diffuse", cm(diff.r), cm(diff.g), cm(diff.b), 1.0))
96                         amb = diff*material.ambient
97                         statements.append(Statement("ambient", cm(amb.r), cm(amb.g), cm(amb.b), 1.0))
98                 spec = material.specular_color*material.specular_intensity
99                 statements.append(Statement("specular", cm(spec.r), cm(spec.g), cm(spec.g), 1.0))
100                 statements.append(Statement("shininess", material.specular_hardness))
101
102                 return mat_res