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.show_progress = True
39 self.use_strips = True
40 self.use_degen_tris = False
41 self.max_strip_len = 1024
42 self.optimize_cache = True
44 self.export_lines = False
45 self.export_uv = "UNIT0"
49 self.material_tex = False
50 self.smoothing = "MSPGL"
51 self.export_groups = False
54 def stripify(self, mesh, progress=None):
63 if self.optimize_cache:
64 cache = VertexCache(self.cache_size)
70 # No current island; find any unused face to start from
81 # Find all faces connected to the first one
86 for n in f.get_neighbors():
91 # Unflag the island for the next phase
95 # Find an unused face with as few unused neighbors as possible, but
96 # at least one. This heuristic gives a preference to faces in corners
97 # or along borders of a non-closed island.
104 score = sum(not n.flag for n in f.get_neighbors())
105 if score>0 and score<best:
110 # Create a strip starting from the face. This will flag the faces.
111 strip = mesh.create_strip(face, self.max_strip_len)
113 island_strips.append(strip)
117 # Couldn't find a candidate face for starting a strip, so we're
118 # done with this island
122 # Find the strip that benefits the most from the current
123 # contents of the vertex cache
125 for i in range(len(island_strips)):
126 hits = cache.test_strip(island_strips[i])
131 strip = island_strips.pop(best)
135 cache.fetch_strip(strip)
137 faces_done += len(island)
139 progress.set_progress(float(faces_done)/len(mesh.faces))
141 # Collect any faces that weren't used in strips
142 loose += [f for f in island if not f.flag]
150 cache = VertexCache(self.cache_size)
153 if self.use_degen_tris and strips:
158 # Generate glue elements, ensuring that the next strip begins at
160 glue = [big_strip[-1], s[0]]
166 total_hits += cache.fetch_strip(glue)
170 total_hits += cache.fetch_strip(s)
173 # Add loose faces to the end. This wastes space, using five
174 # elements per triangle and six elements per quad.
176 order = (-1, -2, 0, 1)
178 order = (0, 1, -1, -2)
179 vertices = [f.vertices[i] for i in order[:len(f.vertices)]]
182 glue = [big_strip[-1], vertices[0]]
185 total_hits += cache.fetch_strip(glue)
187 big_strip += vertices
189 total_hits += cache.fetch_strip(vertices)
196 def export(self, context, out_file, objs=None, progress=None):
198 objs = [(o, mathutils.Matrix()) for i in objs]
202 objs = [(o, mathutils.Matrix()) for o in context.selected_objects]
209 children.append((c, m*c.matrix_local))
213 objs = [(context.active_object, mathutils.Matrix())]
216 raise Exception("Nothing to export")
219 raise Exception("Can only export Mesh data")
221 from .mesh import Mesh
222 from .util import Progress
224 if self.show_progress:
226 progress = Progress(context)
227 progress.set_task("Preparing", 0.0, 0.0)
235 if o.data.winding_test:
237 bmesh = o.to_mesh(context.scene, True, "PREVIEW")
238 bmeshes.append(bmesh)
247 progress.set_task("Smoothing", 0.05, 0.35)
248 if self.smoothing=="NONE":
250 mesh.split_smooth(progress)
252 if self.smoothing!="BLENDER":
253 mesh.compute_normals()
255 if self.export_groups:
256 mesh.sort_vertex_groups(self.max_groups)
258 # Create a mapping from vertex group indices to bone indices
259 first_obj = objs[0][0]
260 group_index_map = dict((i, i) for i in range(len(first_obj.vertex_groups)))
261 if first_obj.parent and first_obj.parent.type=="ARMATURE":
262 armature = first_obj.parent.data
263 bone_indices = dict((armature.bones[i].name, i) for i in range(len(armature.bones)))
264 for g in first_obj.vertex_groups:
265 if g.name in bone_indices:
266 group_index_map[g.index] = bone_indices[g.name]
268 if self.material_tex and mesh.materials:
269 mesh.generate_material_uv()
273 if mesh.uv_layers and (self.export_uv!="NONE" or self.material_tex):
274 # Figure out which UV layers to export
275 if self.export_uv=="ALL":
276 texunits = range(len(mesh.uv_layers))
277 elif self.material_tex:
278 # The material UV layer is always the last one
279 texunits = [len(mesh.uv_layers)-1]
282 for i, u in enumerate(mesh.uv_layers):
286 texunits = [(i, mesh.uv_layers[i]) for i in texunits]
287 texunits = [u for u in texunits if not u[1].hidden]
290 # TBN coordinates must be generated before vertices are split by any other layer
291 uv_names = [u.name for i, u in texunits]
292 if self.tbn_uvtex in uv_names:
293 tbn_index = uv_names.index(self.tbn_uvtex)
294 unit = texunits[tbn_index]
295 del texunits[tbn_index]
296 texunits.insert(0, unit)
298 for i, u in texunits:
300 progress.set_task("Splitting UVs", 0.35+0.3*i/len(texunits), 0.35+0.3*(i+1)/len(texunits))
301 mesh.split_uv(i, progress)
302 if self.tbn_vecs and u.name==self.tbn_uvtex:
312 progress.set_task("Creating strips", 0.65, 0.95)
313 strips, loose = self.stripify(mesh, progress)
316 progress.set_task("Writing file", 0.95, 1.0)
318 from .outfile import open_output
319 out_file = open_output(out_file)
323 for i, u in texunits:
324 size = str(len(mesh.vertices[0].uvs[i]))
325 if u.unit==0 or force_unit0:
326 fmt.append("TEXCOORD"+size)
328 fmt.append("TEXCOORD%s_%d"%(size, u.unit))
330 fmt += ["TANGENT3", "BINORMAL3"]
331 if self.export_groups:
332 fmt.append("ATTRIB%d_5"%(self.max_groups*2))
333 fmt.append("VERTEX3")
334 out_file.begin("vertices", *fmt)
340 for v in mesh.vertices:
342 out_file.write("normal3", *v.normal)
344 for i, u in texunits:
345 if v.uvs[i]!=uvs.get(i):
346 size = str(len(v.uvs[i]))
347 if u.unit==0 or force_unit0:
348 out_file.write("texcoord"+size, *v.uvs[i])
350 out_file.write("multitexcoord"+size, u.unit, *v.uvs[i])
354 out_file.write("tangent3", *v.tan)
357 out_file.write("binormal3", *v.bino)
359 if self.export_groups:
360 group_attr = [(group_index_map[g.group], g.weight*v.group_weight_scale) for g in v.groups[:self.max_groups]]
361 while len(group_attr)<self.max_groups:
362 group_attr.append((0, 0.0))
363 group_attr = list(itertools.chain(*group_attr))
364 if group_attr!=group:
365 out_file.write("attrib%d"%len(group_attr), 5, *group_attr)
367 out_file.write("vertex3", *v.co)
370 out_file.begin("batch", "TRIANGLE_STRIP")
374 indices.append(v.index)
376 out_file.write("indices", *indices)
379 out_file.write("indices", *indices)
383 out_file.begin("batch", "TRIANGLES")
385 for i in range(2, len(f.vertices)):
386 out_file.write("indices", f.vertices[0].index, f.vertices[i-1].index, f.vertices[i].index)
389 if self.export_lines and mesh.lines:
390 out_file.begin("batch", "LINES")
392 out_file.write("indices", l.vertices[0].index, l.vertices[1].index)
396 out_file.write("winding", "COUNTERCLOCKWISE")
399 progress.set_task("Done", 1.0, 1.0)
402 bpy.data.meshes.remove(m)