# Trouble recalculating 3D mesh's triangles after deleting verts

 0 I'm probably doing this really inefficiently, but I'm having trouble recalculating the triangles array of a mesh I'm deleting vertices from. I can delete the vertices without issue (I think), but I'm getting hung up on the triangles array. What I have so far works for deleting vertices, but it overflows the length of the new triangles array I'm initializing. `````` // First pass; null out any verts that are below the ocean in the vertex array vertices = mesh.vertices; var dist : float = 0.0; var numberOfVerticesLeft : int = vertices.length; print ("original vertices array length is " + vertices.length); for (var vertex in vertices) { dist = Vector3.Distance(Vector3.zero, vertex); if (dist < planetRadius) { numberOfVerticesLeft--; vertex = Vector3.zero; } } print("New vertices array will be this long: " + numberOfVerticesLeft); // Second pass; build up the new vertex array only from the good values var newVerts = new Vector3[numberOfVerticesLeft]; i = 0; for (vertex in vertices) { if (vertex != Vector3.zero) { newVerts[i] = vertex; i++; } } print("Added " + i + " elements to new vertex array"); // Third pass; loop through the triangle array to discover // which elements point to zeroed vertices, then set 'em to -1 var triangles = mesh.triangles; var numberOfTrianglesLeft : int = triangles.length; print ("original triangles array length is " + triangles.length); for (var tri in mesh.triangles) { if (vertices[tri] == Vector3.zero) { numberOfTrianglesLeft--; tri = -1; } } print("New triangles array will be this long: " + numberOfTrianglesLeft); // Fourth pass; build up the new triangle array only from the good values var newTriangles = new int[numberOfTrianglesLeft]; i = 0; for (tri in triangles) { if (tri != -1) { newTriangles[i] = tri; i++; print("Added " + i + " elements to new triangles array"); } } // print("Added " + i + "elements to new triangles array"); mesh.triangles = newTriangles; mesh.vertices = newVerts; `````` more ▼ asked Aug 05 '10 at 04:04 AM Sladuuch 63 ● 2 ● 2 ● 4 add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users

 0 Before you start messing with the vertices array of a mesh, you need to issue a mesh.Clear(). At the end of your code, you need to assign the vertices first, and the triangles last. mesh.RecalculateBounds() will be called automatically by reassigning mesh.triangles. more ▼ answered Aug 19 '10 at 10:07 AM Wolfram 9k ● 8 ● 20 ● 52 add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users
 0 I'm not sure why you get the overflow, but even if it wouldn't, your triangles would point to vertex indices that were relevant in the old vertices array and are probably irrelevant in the new one, as you deleted some vertices in between and vertices[6] now could be newVertices[3]. On which line are you getting the overflow? newTriangles[i] = tri; ? more ▼ answered Aug 05 '10 at 09:09 AM StephanK 6k ● 39 ● 53 ● 93 add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users

By Email:

Topics:

x1358
x300
x137
x116
x33

asked: Aug 05 '10 at 04:04 AM

Seen: 1403 times

Last Updated: Aug 05 '10 at 04:04 AM