- Home /

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

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;
```

` `

**Answer** by Wolfram
·
Aug 19, 2010 at 10:07 AM

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.

**Answer** by StephanK
·
Aug 05, 2010 at 09:09 AM

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; ?

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.

### Follow this Question

### Related Questions

How to colorize a face of a Mesh 0 Answers

using grayscale texture as heightmap, vertices and pixel don't match 0 Answers

Optimized vertex manipulation 1 Answer

Weird triangle indexing problem 1 Answer

Checking If A Triangle Is In A Mesh 1 Answer