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 *= 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 *= tri;*_
_*i++;*_
_*print("Added " + i + " elements to new triangles array");*_
_*}*_
_*}*_
_*// print("Added " + i + "elements to new triangles array");*_
_*mesh.triangles = newTriangles;*_
_*mesh.vertices = newVerts;*_
_*```*_