]> git.tdb.fi Git - libs/gl.git/blob - blender/io_mspgl/export_material.py
Some bugfixes to texture exporting
[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
16 class MaterialExporter:
17         def __init__(self):
18                 self.single_file = True
19                 self.use_textures = True
20
21         def create_texture_exporter(self):
22                 from .export_texture import TextureExporter
23                 texture_export = TextureExporter()
24                 texture_export.inline_data = self.single_file
25                 return texture_export
26
27         def export_technique_resources(self, material, resources):
28                 texture_export = self.create_texture_exporter()
29
30                 mat_name = material.name+".mat"
31                 if mat_name not in resources:
32                         resources[mat_name] = self.export_material(material)
33
34                 if self.use_textures:
35                         for s in material.texture_slots:
36                                 if s and s.texture.type=='IMAGE' and s.texture.image:
37                                         tex_name = s.texture.name+".tex2d"
38                                         if tex_name not in resources:
39                                                 resources[tex_name] = texture_export.export_texture(s.texture)
40
41         def export_technique(self, material, *, resources):
42                 from .datafile import Resource, Statement
43                 tech_res = Resource(material.name+".tech")
44
45                 mat_res = resources[material.name+".mat"]
46                 textures = {}
47                 if self.use_textures:
48                         image_texture_slots = [s for s in material.texture_slots if s and s.texture.type=='IMAGE' and s.texture.image]
49                         for s in image_texture_slots:
50                                 if s.use_map_color_diffuse:
51                                         textures["diffuse_map"] = s.texture
52                                 elif s.use_map_normal:
53                                         textures["normal_map"] = s.texture
54
55                 if material.technique:
56                         if not obj.inherit_tech:
57                                 return []
58
59                         if self.single_file:
60                                 raise Exception("Can't export inherited technique to a single file")
61
62                         st = Statement("inherit", material.technique)
63                         for s, t in textures.items():
64                                 if t.default_filter:
65                                         st.sub.append(Statement("texture", s, image_name(t.image)))
66                                 else:
67                                         st.sub.append(tech_res.create_reference_statement("texture", s, resources[t.name+".tex2d"]))
68                         if material.override_material:
69                                 st.sub.append(tech_res.create_reference_statement("material", "surface", mat_res))
70                         tech_res.statements.append(st)
71                 else:
72                         st = Statement("pass", "")
73                         if self.single_file:
74                                 st.sub.append(tech_res.create_embed_statement("material", mat_res))
75                         else:
76                                 st.sub.append(tech_res.create_reference_statement("material", mat_res))
77
78                         if "diffuse_map" in textures:
79                                 diffuse_tex = textures["diffuse_map"]
80                                 tex_res = resources[diffuse_tex.name+".tex2d"]
81                                 ss = Statement("texunit", 0)
82                                 if self.single_file:
83                                         ss.sub.append(tech_res.create_embed_statement("texture2d", tex_res))
84                                 elif not diffuse_tex.default_filter:
85                                         ss.sub.append(tech_res.create_reference_statement("texture", tex_res))
86                                 else:
87                                         ss.sub.append(Statement("texture", image_name(diffuse_tex.image)))
88                                 st.sub.append(ss)
89
90                         tech_res.statements.append(st)
91
92                 return tech_res
93
94         def export_material(self, material):
95                 from .datafile import Resource, Statement
96                 mat_res = Resource(material.name+".mat")
97                 statements = mat_res.statements
98
99                 cm = get_colormap(material.srgb_colors)
100                 if any(s.use_map_color_diffuse for s in material.texture_slots if s):
101                         statements.append(Statement("diffuse", 1.0, 1.0, 1.0, 1.0))
102                         amb = cm(material.ambient)
103                         statements.append(Statement("ambient", amb, amb, amb, 1.0))
104                 else:
105                         diff = material.diffuse_color*material.diffuse_intensity
106                         statements.append(Statement("diffuse", cm(diff.r), cm(diff.g), cm(diff.b), 1.0))
107                         amb = diff*material.ambient
108                         statements.append(Statement("ambient", cm(amb.r), cm(amb.g), cm(amb.b), 1.0))
109                 spec = material.specular_color*material.specular_intensity
110                 statements.append(Statement("specular", cm(spec.r), cm(spec.g), cm(spec.g), 1.0))
111                 statements.append(Statement("shininess", material.specular_hardness))
112
113                 return mat_res