I’m trying to remove some of the vertices from the mesh, but I keep getting this error; “The supplied vertex array has less vertices than are referenced by the triangles array.” I don’t know why it’s not letting me. is this because they’re part of a triangle? if this is the case than how can remove them?
var newmesh : Mesh = other.gameObject.GetComponent(MeshFilter).mesh;
var newvertices : Vector3[] = newmesh.vertices;
for (var i=0; i<newvertices.length; i++)
{
if(helperPlane.GetSide(other.transform.TransformPoint(newvertices[i])))
{
// I DON'T KNOW WHAT TO PUT HERE TO DELETE THE UNNECESSARY VERTICES...
}
}
newmesh.vertices = newvertices;
newmesh.RecalculateBounds();
What you need to put there is code that generates a new array which has those vertices removed but the rest still present (so you write all elements to the neew array that you don’t want to delete)
Thanks for the answer dreamora, it’s partially working right now, but for some reason I’m still getting the same error. I also have another question.
When you delete the vertices, it automatically welds the mesh and close the hollow part. is there any way to remove the vertices, but prevent the welding behavior.
Here is the modified version of the script. I don’t now why I’m getting the same error;
var newmesh : Mesh = other.gameObject.GetComponent(MeshFilter).mesh;
var newvertices : Vector3[] = newmesh.vertices;
var emptyList= new Array();
for (var i=0; i<newvertices.length; i++)
{
if(helperPlane.GetSide(other.transform.TransformPoint(newvertices[i])))
{
emptyList[i]=newvertices[i];
}
}
newmesh.vertices = emptyList;
newmesh.RecalculateBounds();
you must update the triangles array in the mesh too. if you cut vertices there are naturally also triangles that are gone (and 2 more vertices you need to remove ie the rest in the triangle)
All I’m doing is, copying the triangles to an empty array, but for some reason it randomly deletes half of the triangles.
var newmesh : Mesh = other.gameObject.GetComponent(MeshFilter).mesh;
var newvertices : Vector3[] = newmesh.vertices;
var newTriangles : int[] = newmesh.triangles;
var triList = new Array();
for (i=0; i < newvertices.length; i += 3)
{
triList[i]= newTriangles[i];
triList[i+1]=newTriangles[i+1];
triList[i+2]=newTriangles[i+2];
}
newmesh.triangles=triList;
you need to remove those triangles where you cut 1+ vertices from as the triangle no longer exists / is no longer valid and if you leave it in, all triangles at the end of the triangles array will mess up and lead to the error you get above cause the vertex index array is no longer 3 times as long as the triangle count
This is totally separate from the previous script. I’m not removing any of the vertices. I did this just for testing the triangles and make sure they’re copied fine.
lol Listen to Dreamora hes telling you what you need to know. Like he said you cannot remove tris/verts without additionally removing verts/tris it is part of. If a Triangle dissapears, then its verts must too and if one vert out of three that make up any given triangle goes missing the triangle must then be removed.