]> git.tdb.fi Git - libs/gl.git/blob - blender/io_mspgl/export_material.py
Move material and texture export to their own classes
[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':
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                 image_texture_slots = [s for s in material.texture_slots if s and s.texture.type=='IMAGE']
48
49                 if material.technique:
50                         if not obj.inherit_tech:
51                                 return []
52
53                         st = Statement("inherit", material.technique)
54                         for slot in image_texture_slots:
55                                 name = image_name(slot.texture.image)
56                                 if slot.use_map_color_diffuse:
57                                         st.sub.append(Statement("texture", "diffuse_map", name))
58                                 elif slot.use_map_normal:
59                                         st.sub.append(Statement("texture", "normal_map", name))
60                         if material.override_material:
61                                 st.sub.append(tech_res.create_reference_statement("material", "surface", mat_res))
62                         tech_res.statements.append(st)
63                 else:
64                         st = Statement("pass", "")
65                         st.sub.append(tech_res.create_embed_statement("material", mat_res))
66
67                         if self.textures!='NONE':
68                                 diffuse_tex = None
69                                 for slot in image_texture_slots:
70                                         if slot.use_map_color_diffuse:
71                                                 diffuse_tex = slot.texture
72                                                 break
73
74                                 if diffuse_tex:
75                                         ss = Statement("texunit", 0)
76                                         if self.textures=='INLINE':
77                                                 tex_res = resources[slot.texture.name+".tex2d"]
78                                                 ss.sub.append(tech_res.create_embed_statement("texture2d", tex_res))
79                                         elif tex.image:
80                                                 ss.sub.append(Statement("texture", image_name(tex.image)))
81                                         st.sub.append(ss)
82
83                         tech_res.statements.append(st)
84
85                 return tech_res
86
87         def export_material(self, material):
88                 from .datafile import Resource, Statement
89                 mat_res = Resource(material.name+".mat")
90                 statements = mat_res.statements
91
92                 cm = get_colormap(material.srgb_colors)
93                 if any(s.use_map_color_diffuse for s in material.texture_slots if s):
94                         statements.append(Statement("diffuse", 1.0, 1.0, 1.0, 1.0))
95                         amb = cm(material.ambient)
96                         statements.append(Statement("ambient", amb, amb, amb, 1.0))
97                 else:
98                         diff = material.diffuse_color*material.diffuse_intensity
99                         statements.append(Statement("diffuse", cm(diff.r), cm(diff.g), cm(diff.b), 1.0))
100                         amb = diff*material.ambient
101                         statements.append(Statement("ambient", cm(amb.r), cm(amb.g), cm(amb.b), 1.0))
102                 spec = material.specular_color*material.specular_intensity
103                 statements.append(Statement("specular", cm(spec.r), cm(spec.g), cm(spec.g), 1.0))
104                 statements.append(Statement("shininess", material.specular_hardness))
105
106                 return mat_res