3 from .outfile import OutFile
6 def __init__(self, size):
8 self.slots = [-1]*self.size
11 hit = v.index in self.slots
13 self.slots.remove(v.index)
14 self.slots.append(v.index)
19 def fetch_strip(self, strip):
26 def test_strip(self, strip):
28 for i in range(len(strip)):
31 if strip[i].index in self.slots[i:]:
38 self.use_strips = True
39 self.use_degen_tris = True
40 self.max_strip_len = 1024
41 self.optimize_cache = False
43 self.export_lines = True
44 self.export_uv = "UNIT0"
49 self.material_tex = False
51 self.smoothing = "MSPGL"
52 self.export_groups = False
55 def stripify(self, mesh, progress = None):
64 if self.optimize_cache:
65 cache = VertexCache(self.cache_size)
71 # No current island; find any unused face to start from
82 # Find all faces connected to the first one
87 for n in f.get_neighbors():
92 # Unflag the island for the next phase
96 # Find an unused face with as few unused neighbors as possible, but
97 # at least one. This heuristic gives a preference to faces in corners
98 # or along borders of a non-closed island.
105 score = sum(not n.flag for n in f.get_neighbors())
106 if score>0 and score<best:
111 # Create a strip starting from the face. This will flag the faces.
112 strip = mesh.create_strip(face, self.max_strip_len)
114 island_strips.append(strip)
118 # Couldn't find a candidate face for starting a strip, so we're
119 # done with this island
123 # Find the strip that benefits the most from the current
124 # contents of the vertex cache
126 for i in range(len(island_strips)):
127 hits = cache.test_strip(island_strips[i])
132 strip = island_strips.pop(best)
136 cache.fetch_strip(strip)
138 faces_done += len(island)
140 progress.set_progress(float(faces_done)/len(mesh.faces))
142 # Collect any faces that weren't used in strips
143 loose += [f for f in island if not f.flag]
151 cache = VertexCache(self.cache_size)
154 if self.use_degen_tris and strips:
159 # Generate glue elements, ensuring that the next strip begins at
161 glue = [big_strip[-1], s[0]]
167 total_hits += cache.fetch_strip(glue)
171 total_hits += cache.fetch_strip(s)
174 # Add loose faces to the end. This wastes space, using five
175 # elements per triangle and six elements per quad.
177 order = (-1, -2, 0, 1)
179 order = (0, 1, -1, -2)
180 vertices = [f.vertices[i] for i in order[:len(f.vertices)]]
183 glue = [big_strip[-1], vertices[0]]
186 total_hits += cache.fetch_strip(glue)
188 big_strip += vertices
190 total_hits += cache.fetch_strip(vertices)
197 def export(self, context, fn):
199 objs = context.selected_objects
201 objs = [context.active_object]
204 raise Exception("Nothing to export")
207 raise Exception("Can only export Mesh data")
209 from .mesh import Mesh
210 from .util import Progress
212 progress = Progress()
213 progress.set_task("Preparing", 0.0, 0.0)
218 bmesh = o.to_mesh(context.scene, True, "PREVIEW")
219 bmeshes.append(bmesh)
223 mesh.splice(Mesh(bmesh))
225 progress.set_task("Smoothing", 0.05, 0.35)
226 if self.smoothing=="NONE":
228 mesh.split_smooth(progress)
230 if self.smoothing!="BLENDER":
231 mesh.compute_normals()
233 if self.export_groups:
234 mesh.sort_vertex_groups(self.max_groups)
236 # Create a mapping from vertex group indices to bone indices
237 group_index_map = dict((i, i) for i in range(len(objs[0].vertex_groups)))
238 if objs[0].parent and objs[0].parent.type=="ARMATURE":
239 armature = objs[0].parent.data
240 bone_indices = dict((armature.bones[i].name, i) for i in range(len(armature.bones)))
241 for g in objs[0].vertex_groups:
242 if g.name in bone_indices:
243 group_index_map[g.index] = bone_indices[g.name]
245 if self.material_tex and mesh.materials:
246 mesh.generate_material_uv()
249 if mesh.uv_layers and self.export_uv!="NONE":
250 # Figure out which UV layers to export
251 if self.export_uv=="UNIT0":
252 if mesh.uv_layers[0].unit==0:
255 texunits = range(len(mesh.uv_layers))
256 texunits = [(i, mesh.uv_layers[i]) for i in texunits]
257 texunits = [u for u in texunits if not u[1].hidden]
260 # TBN coordinates must be generated before vertices are split by any other layer
261 uv_names = [u.name for i, u in texunits]
262 if self.tbn_uvtex in uv_names:
263 tbn_index = uv_names.index(self.tbn_uvtex)
264 unit = texunits[tbn_index]
265 del texunits[tbn_index]
266 texunits.insert(0, unit)
268 for i, u in texunits:
269 progress.set_task("Splitting UVs", 0.35+0.3*i/len(texunits), 0.35+0.3*(i+1)/len(texunits))
270 mesh.split_uv(i, progress)
271 if self.tbn_vecs and u.name==self.tbn_uvtex:
280 progress.set_task("Creating strips", 0.65, 0.95)
281 strips, loose = self.stripify(mesh, progress)
283 progress.set_task("Writing file", 0.95, 1.0)
285 out_file = OutFile(fn)
287 out_file.begin("mesh")
291 for i, u in texunits:
295 fmt += "_TEXCOORD2%d"%u.unit
297 fmt += "_ATTRIB33_ATTRIB34"
298 if self.export_groups:
299 fmt += "_ATTRIB%d5"%(self.max_groups*2)
301 out_file.begin("vertices", fmt)
303 uvs = [None]*len(texunits)
307 for v in mesh.vertices:
309 out_file.write("normal3", *v.normal)
311 for i, u in texunits:
314 out_file.write("texcoord2", *v.uvs[i])
316 out_file.write("multitexcoord2", u.unit, *v.uvs[i])
320 out_file.write("attrib3", 3, *v.tan)
323 out_file.write("attrib3", 4, *v.bino)
325 if self.export_groups:
326 group_attr = [(group_index_map[g.group], g.weight*v.group_weight_scale) for g in v.groups[:self.max_groups]]
327 while len(group_attr)<self.max_groups:
328 group_attr.append((0, 0.0))
329 group_attr = list(itertools.chain(*group_attr))
330 if group_attr!=group:
331 out_file.write("attrib%d"%len(group_attr), 5, *group_attr)
333 out_file.write("vertex3", *v.co)
336 out_file.begin("batch", "TRIANGLE_STRIP")
340 indices.append(v.index)
342 out_file.write("indices", *indices)
345 out_file.write("indices", *indices)
349 out_file.begin("batch", "TRIANGLES")
351 for i in range(2, len(f.vertices)):
352 out_file.write("indices", f.vertices[0].index, f.vertices[i-1].index, f.vertices[i].index)
355 if self.export_lines and mesh.lines:
356 out_file.write("batch", "LINES")
358 out_file.write("indices", l.vertices[0].index, l.vertices[1].index)
363 out_file.begin("technique")
364 out_file.begin("pass", '""')
366 if self.material_tex:
367 out_file.begin("material")
368 out_file.write("diffuse", 1.0, 1.0, 1.0, 1.0)
371 for u in mesh.uv_layers:
372 if u.name=="material_tex":
374 out_file.begin("texunit", index)
375 out_file.begin("texture2d")
376 out_file.write("min_filter", "NEAREST")
377 out_file.write("mag_filter", "NEAREST")
378 out_file.write("storage", "RGB", len(mesh.materials), 1)
380 for m in mesh.materials:
381 color = [int(c*255) for c in m.diffuse_color]
382 texdata += "\\x%02X\\x%02X\\x%02X"%tuple(color)
384 out_file.write("raw_data", texdata)
388 mat = mesh.materials[0]
389 out_file.begin("material")
390 diff = mat.diffuse_color
391 out_file.write("diffuse", diff.r, diff.g, diff.b, 1.0)
392 amb = diff*mat.ambient
393 out_file.write("ambient", amb.r, amb.g, amb.b, 1.0)
394 spec = mat.specular_color*mat.specular_intensity
395 out_file.write("specular", spec.r, spec.g, spec.b, 1.0)
396 out_file.write("shininess", mat.specular_hardness);
399 if self.textures!="NONE":
400 for slot in mesh.materials[0].texture_slots:
405 if tex.type!="IMAGE":
409 for u in mesh.uv_layers:
410 if u.name==slot.uv_layer:
413 index = mesh.uv_layers[0].unit
415 out_file.begin("texunit", index)
416 if self.textures=="INLINE":
417 out_file.begin("texture2d")
418 out_file.write("min_filter", "LINEAR")
419 out_file.write("storage", "RGBA", tex.image.size[0], tex.image.size[1])
421 for p in tex.image.pixels:
422 texdata += "\\x%02X"%int(p*255)
424 out_file.write("raw_data", texdata)
427 out_file.write("texture", '"%s"'%tex.image.name)
433 progress.set_task("Done", 1.0, 1.0)
436 bpy.data.meshes.remove(m)