]> git.tdb.fi Git - libs/gl.git/blob - blender/io_mspgl/export_object.py
Only use texunit zero in exporter-generated techniques
[libs/gl.git] / blender / io_mspgl / export_object.py
1 import os
2 import mathutils
3
4 def linear_to_srgb(l):
5         if l<0.0031308:
6                 return 12.92*l
7         else:
8                 return 1.055*(l**(1/2.4))-0.055
9
10 def get_colormap(srgb):
11         if srgb:
12                 return linear_to_srgb
13         else:
14                 return lambda x: x
15
16 def image_name(i):
17         fp = i.filepath
18         if fp:
19                 return os.path.split(fp)[1]
20         else:
21                 return i.name
22
23 def external_name(out_file, ext, index):
24         path, base = os.path.split(out_file.filename)
25         base = os.path.splitext(base)[0]
26         if index>0:
27                 base += "_lod{}".format(index)
28         return path, base+ext
29
30
31 class ObjectExporter:
32         def __init__(self):
33                 self.textures = "REF"
34                 self.separate_mesh = False
35                 self.shared_mesh = True
36                 self.separate_tech = False
37                 self.shared_tech = True
38                 self.export_lods = True
39
40         def export(self, context, out_file, obj=None, progress=None):
41                 if obj is None:
42                         obj = context.active_object
43
44                 lods = [obj]
45                 for c in obj.children:
46                         if c.lod_for_parent:
47                                 if c.lod_index>=len(lods):
48                                         lods += [None]*(c.lod_index+1-len(lods))
49                                 lods[c.lod_index] = c
50
51                 from .outfile import open_output
52                 out_file = open_output(out_file)
53
54                 p1 = max(((v.co, v.co.length) for v in obj.data.vertices), key=lambda x:x[1])[0]
55                 p2 = max(((v.co, (v.co-p1).length) for v in obj.data.vertices), key=lambda x:x[1])[0]
56                 center = (p1+p2)/2
57                 radius = (p1-p2).length/2
58                 for v in obj.data.vertices:
59                         d = v.co-center
60                         if d.length>radius:
61                                 center += d*(1-radius/d.length)/2
62                                 radius = (radius+d.length)/2
63
64                 out_file.write("bounding_sphere_hint", center[0], center[1], center[2], radius)
65
66                 prev_mesh = None
67                 prev_tech = (None, None)
68                 for i, l in enumerate(lods):
69                         if i>0:
70                                 out_file.begin("level_of_detail", i)
71
72                         if i==0 or l.data.name!=prev_mesh:
73                                 mesh = self.export_object_mesh(context, out_file, l, progress)
74                                 prev_mesh = l.data.name
75
76                         same_tech = True
77                         mat = None
78                         if l.material_slots and l.material_slots[0].material:
79                                 mat = l.material_slots[0].material.name
80                                 if mat!=prev_tech[1]:
81                                         same_tech = False
82                         if l.technique!=prev_tech[0]:
83                                 same_tech = False
84                         if i==0 or not same_tech:
85                                 self.export_object_technique(l, mesh, out_file, i)
86                                 prev_tech = (l.technique, mat)
87
88                         if i>0:
89                                 out_file.end()
90
91         def export_object_mesh(self, context, out_file, obj, progress):
92                 from .export_mesh import MeshExporter
93                 mesh_export = MeshExporter()
94                 for k, v in self.__dict__.items():
95                         setattr(mesh_export, k, v)
96
97                 lod_index = 0
98                 if obj.lod_for_parent:
99                         lod_index = obj.lod_index
100
101                 if self.separate_mesh:
102                         from .outfile import open_output
103                         path, name = external_name(out_file, ".mesh", lod_index)
104                         if self.shared_mesh:
105                                 name = obj.data.name+".mesh"
106                         mesh_out = open_output(os.path.join(path, name))
107                         mesh = mesh_export.export(context, mesh_out, obj, progress)
108                         out_file.write("mesh", '"{}"'.format(name))
109                 else:
110                         out_file.begin("mesh")
111                         mesh = mesh_export.export(context, out_file, obj, progress)
112                         out_file.end()
113
114                 return mesh
115
116         def export_object_technique(self, obj, mesh, out_file, lod_index):
117                 material = None
118                 if obj.material_slots:
119                         material = obj.material_slots[0].material
120
121                 from .outfile import open_output
122                 path, name = external_name(out_file, ".tech", lod_index)
123
124                 if obj.technique:
125                         if obj.inherit_tech and material and (obj.override_material or material.texture_slots):
126                                 out_file.begin("technique")
127                                 out_file.begin("inherit", '"{}"'.format(obj.technique))
128                                 for slot in material.texture_slots:
129                                         if slot and slot.texture.type=="IMAGE":
130                                                 name = image_name(slot.texture.image)
131                                                 if slot.use_map_color_diffuse:
132                                                         out_file.write("texture", '"diffuse_map"', '"{}"'.format(name))
133                                                 elif slot.use_map_normal:
134                                                         out_file.write("texture", '"normal_map"', '"{}"'.format(name))
135                                 if obj.override_material:
136                                         mat_name = material.name+".mat"
137                                         mat_out = open_output(os.path.join(path, mat_name))
138                                         self.export_material(material, mat_out)
139                                         out_file.write("material", '"surface"', '"{}"'.format(mat_name))
140                                 out_file.end()
141                                 out_file.end()
142                         else:
143                                 out_file.write("technique", '"{}"'.format(obj.technique))
144                 elif self.separate_tech:
145                         if self.shared_tech and material:
146                                 name = material.name+".tech"
147                         tech_out = open_output(os.path.join(path, name))
148                         self.export_technique_definition(material, mesh, tech_out)
149                         out_file.write("technique", '"{}"'.format(name))
150                 else:
151                         out_file.begin("technique")
152                         self.export_technique_definition(material, obj.material_tex, mesh, out_file)
153                         out_file.end()
154
155         def export_technique_definition(self, material, material_tex, mesh, out_file):
156                 out_file.begin("pass", '""')
157                 if material:
158                         if material_tex:
159                                 out_file.begin("material")
160                                 out_file.write("diffuse", 1.0, 1.0, 1.0, 1.0)
161                                 out_file.end()
162                                 out_file.begin("texunit", 0)
163                                 out_file.begin("texture2d")
164                                 out_file.write("min_filter", "NEAREST")
165                                 out_file.write("mag_filter", "NEAREST")
166                                 out_file.write("storage", "RGB", len(mesh.materials), 1)
167                                 texdata = '"'
168                                 for m in mesh.materials:
169                                         cm = get_colormap(m.srgb_colors)
170                                         color = [int(cm(c)*255) for c in m.diffuse_color*m.diffuse_intensity]
171                                         texdata += "\\x%02X\\x%02X\\x%02X"%tuple(color)
172                                 texdata += '"'
173                                 out_file.write("raw_data", texdata)
174                                 out_file.end()
175                                 out_file.end()
176                         else:
177                                 out_file.begin("material")
178                                 self.export_material(material, out_file)
179                                 out_file.end()
180
181                         if self.textures!="NONE":
182                                 diffuse_tex = None
183                                 for slot in material.texture_slots:
184                                         if slot and slot.texture.type=="IMAGE" and slot.use_map_color_diffuse:
185                                                 diffuse_tex = slot.texture
186                                                 break
187
188                                 if diffuse_tex:
189                                         out_file.begin("texunit", 0)
190                                         if self.textures=="INLINE":
191                                                 out_file.begin("texture2d")
192                                                 out_file.write("min_filter", "LINEAR")
193                                                 out_file.write("storage", "RGBA", diffuse_tex.image.size[0], diffuse_tex.image.size[1])
194                                                 texdata = '"'
195                                                 for p in diffuse_tex.image.pixels:
196                                                         texdata += "\\x%02X"%int(p*255)
197                                                 texdata += '"'
198                                                 out_file.write("raw_data", texdata)
199                                                 out_file.end()
200                                         else:
201                                                 out_file.write("texture", '"%s"'%image_name(diffuse_tex.image))
202                                         out_file.end()
203
204                 out_file.end()
205
206         def export_material(self, mat, out_file):
207                 cm = get_colormap(mat.srgb_colors)
208                 if any((s and s.use_map_color_diffuse) for s in mat.texture_slots):
209                         out_file.write("diffuse", 1.0, 1.0, 1.0, 1.0)
210                         amb = cm(mat.ambient)
211                         out_file.write("ambient", amb, amb, amb, 1.0)
212                 else:
213                         diff = mat.diffuse_color*mat.diffuse_intensity
214                         out_file.write("diffuse", cm(diff.r), cm(diff.g), cm(diff.b), 1.0)
215                         amb = diff*mat.ambient
216                         out_file.write("ambient", cm(amb.r), cm(amb.g), cm(amb.b), 1.0)
217                 spec = mat.specular_color*mat.specular_intensity
218                 out_file.write("specular", spec.r, spec.g, spec.b, 1.0)
219                 out_file.write("shininess", mat.specular_hardness)