5 def __init__(self, size):
7 self.slots = [-1]*self.size
10 hit = v.index in self.slots
12 self.slots.remove(v.index)
13 self.slots.append(v.index)
18 def fetch_strip(self, strip):
25 def test_strip(self, strip):
27 for i in range(len(strip)):
30 if strip[i].index in self.slots[i:]:
37 self.use_strips = True
38 self.use_degen_tris = True
39 self.max_strip_len = 1024
40 self.optimize_cache = False
42 self.export_lines = True
43 self.export_uv = "UNIT0"
47 self.material_tex = False
48 self.smoothing = "MSPGL"
49 self.export_groups = False
52 def stripify(self, mesh, progress = None):
61 if self.optimize_cache:
62 cache = VertexCache(self.cache_size)
68 # No current island; find any unused face to start from
79 # Find all faces connected to the first one
84 for n in f.get_neighbors():
89 # Unflag the island for the next phase
93 # Find an unused face with as few unused neighbors as possible, but
94 # at least one. This heuristic gives a preference to faces in corners
95 # or along borders of a non-closed island.
102 score = sum(not n.flag for n in f.get_neighbors())
103 if score>0 and score<best:
108 # Create a strip starting from the face. This will flag the faces.
109 strip = mesh.create_strip(face, self.max_strip_len)
111 island_strips.append(strip)
115 # Couldn't find a candidate face for starting a strip, so we're
116 # done with this island
120 # Find the strip that benefits the most from the current
121 # contents of the vertex cache
123 for i in range(len(island_strips)):
124 hits = cache.test_strip(island_strips[i])
129 strip = island_strips.pop(best)
133 cache.fetch_strip(strip)
135 faces_done += len(island)
137 progress.set_progress(float(faces_done)/len(mesh.faces))
139 # Collect any faces that weren't used in strips
140 loose += [f for f in island if not f.flag]
148 cache = VertexCache(self.cache_size)
151 if self.use_degen_tris and strips:
156 # Generate glue elements, ensuring that the next strip begins at
158 glue = [big_strip[-1], s[0]]
164 total_hits += cache.fetch_strip(glue)
168 total_hits += cache.fetch_strip(s)
171 # Add loose faces to the end. This wastes space, using five
172 # elements per triangle and six elements per quad.
174 order = (-1, -2, 0, 1)
176 order = (0, 1, -1, -2)
177 vertices = [f.vertices[i] for i in order[:len(f.vertices)]]
180 glue = [big_strip[-1], vertices[0]]
183 total_hits += cache.fetch_strip(glue)
185 big_strip += vertices
187 total_hits += cache.fetch_strip(vertices)
194 def export(self, context, out_file):
196 objs = context.selected_objects
207 objs = [context.active_object]
210 raise Exception("Nothing to export")
213 raise Exception("Can only export Mesh data")
215 from .mesh import Mesh
216 from .util import Progress
218 progress = Progress(context)
219 progress.set_task("Preparing", 0.0, 0.0)
224 bmesh = o.to_mesh(context.scene, True, "PREVIEW")
225 bmeshes.append(bmesh)
229 mesh.splice(Mesh(bmesh))
231 progress.set_task("Smoothing", 0.05, 0.35)
232 if self.smoothing=="NONE":
234 mesh.split_smooth(progress)
236 if self.smoothing!="BLENDER":
237 mesh.compute_normals()
239 if self.export_groups:
240 mesh.sort_vertex_groups(self.max_groups)
242 # Create a mapping from vertex group indices to bone indices
243 group_index_map = dict((i, i) for i in range(len(objs[0].vertex_groups)))
244 if objs[0].parent and objs[0].parent.type=="ARMATURE":
245 armature = objs[0].parent.data
246 bone_indices = dict((armature.bones[i].name, i) for i in range(len(armature.bones)))
247 for g in objs[0].vertex_groups:
248 if g.name in bone_indices:
249 group_index_map[g.index] = bone_indices[g.name]
251 if self.material_tex and mesh.materials:
252 mesh.generate_material_uv()
256 if mesh.uv_layers and (self.export_uv!="NONE" or self.material_tex):
257 # Figure out which UV layers to export
258 if self.export_uv=="ALL":
259 texunits = range(len(mesh.uv_layers))
260 elif self.material_tex:
261 # The material UV layer is always the last one
262 texunits = [len(mesh.uv_layers)-1]
265 for i, u in enumerate(mesh.uv_layers):
269 texunits = [(i, mesh.uv_layers[i]) for i in texunits]
270 texunits = [u for u in texunits if not u[1].hidden]
273 # TBN coordinates must be generated before vertices are split by any other layer
274 uv_names = [u.name for i, u in texunits]
275 if self.tbn_uvtex in uv_names:
276 tbn_index = uv_names.index(self.tbn_uvtex)
277 unit = texunits[tbn_index]
278 del texunits[tbn_index]
279 texunits.insert(0, unit)
281 for i, u in texunits:
282 progress.set_task("Splitting UVs", 0.35+0.3*i/len(texunits), 0.35+0.3*(i+1)/len(texunits))
283 mesh.split_uv(i, progress)
284 if self.tbn_vecs and u.name==self.tbn_uvtex:
293 progress.set_task("Creating strips", 0.65, 0.95)
294 strips, loose = self.stripify(mesh, progress)
296 progress.set_task("Writing file", 0.95, 1.0)
298 from .outfile import open_output
299 out_file = open_output(out_file)
303 for i, u in texunits:
304 if u.unit==0 or force_unit0:
305 fmt.append("TEXCOORD2")
307 fmt.append("TEXCOORD2_%d"%u.unit)
309 fmt += ["TANGENT3", "BINORMAL3"]
310 if self.export_groups:
311 fmt.append("ATTRIB%d_5"%(self.max_groups*2))
312 fmt.append("VERTEX3")
313 out_file.begin("vertices", *fmt)
319 for v in mesh.vertices:
321 out_file.write("normal3", *v.normal)
323 for i, u in texunits:
324 if v.uvs[i]!=uvs.get(i):
325 if u.unit==0 or force_unit0:
326 out_file.write("texcoord2", *v.uvs[i])
328 out_file.write("multitexcoord2", u.unit, *v.uvs[i])
332 out_file.write("tangent3", *v.tan)
335 out_file.write("binormal3", *v.bino)
337 if self.export_groups:
338 group_attr = [(group_index_map[g.group], g.weight*v.group_weight_scale) for g in v.groups[:self.max_groups]]
339 while len(group_attr)<self.max_groups:
340 group_attr.append((0, 0.0))
341 group_attr = list(itertools.chain(*group_attr))
342 if group_attr!=group:
343 out_file.write("attrib%d"%len(group_attr), 5, *group_attr)
345 out_file.write("vertex3", *v.co)
348 out_file.begin("batch", "TRIANGLE_STRIP")
352 indices.append(v.index)
354 out_file.write("indices", *indices)
357 out_file.write("indices", *indices)
361 out_file.begin("batch", "TRIANGLES")
363 for i in range(2, len(f.vertices)):
364 out_file.write("indices", f.vertices[0].index, f.vertices[i-1].index, f.vertices[i].index)
367 if self.export_lines and mesh.lines:
368 out_file.write("batch", "LINES")
370 out_file.write("indices", l.vertices[0].index, l.vertices[1].index)
373 progress.set_task("Done", 1.0, 1.0)
376 bpy.data.meshes.remove(m)