Delete tris from mesh without consequences?

Hi :slight_smile: My question is simple… How do I delete ANY tri in a mesh without having any effect on any of the other tris or verts.

I’ve been trying to do this for ages, but deleting the tri either causes a vertex to go out of existence, causing corruption in the data, OR causes all normals after the tri to become 50% inverted!

What are you trying to do? If you just delete a triangle from the Mesh.triangles array, you open a triangular hole in the mesh - and you don’t need to delete the vertices, since they will not be used if not referenced by any triangle. On the other hand, if you want to do a triangle count reduction, you must analyse the neighbor triangles and rearrange them to “cover the hole”; in this case, you probably will need to modify their vertices, uv coordinates and normals to reduce the visual impact. This is not an easy task, and you can have a basic idea reading this article.

Just to play a little with the simple triangle removal, you can attach this script to a mesh and watch the effect of deleting all the odd triangles (starting at 0):

function Start(){
    var mesh : Mesh = GetComponent(MeshFilter).mesh;
    tris = mesh.triangles;
    var qTri = tris.length;
    var q: int = (qTri+3)/6;
    var tris2 = new int[q*3];
    var i: int = 0;
    var j: int = 0;
    while (j < qTri){
        tris2[i++] = tris[j++];
        tris2[i++] = tris[j++];
        tris2[i++] = tris[j++];
        j += 3;
    }
    mesh.triangles = tris2;
}

Notice that no vertices/normals/uv coordinates are deleted, only the triangles. Deleting a vertex is way more complicated, because the triangles refer to the vertex indexes: if you delete vertex 42, for instance, triangles that refer to vertex 43 and above must be modified. Another problem: normals and uv coordinates are associated to the vertices, thus deleting one vertex requires deleting its normal and uv to keep the sync. To make things worse, vertices may be shared by different triangles - you must not delete a vertex if it’s being used by other triangles.

here’s a simpler way of doing it, just sets all the corresponding vertices to zero if a certain condition is met, say the vertex color/texture…

so this deletes like 10 000 vertices at a time from the triangle index. it’s not efficient for saving giant meshes as num vertices is same, but it was fun for me for testing:

for (var child : Transform in allChildren) {
			if( child.gameObject.GetComponent(MeshRenderer) && child.gameObject.GetComponent(MeshCollider) ){
					var mesh2 : Mesh = child.gameObject.GetComponent(MeshFilter).mesh;
					var tris = mesh2.triangles;
					var uvv = mesh2.uv;
					for (var i = 0; i < tris.length; i += 3)
					{
					if(uvv[tris*] == p0) //MYCONDITION*
  •   					{*
    

_ tris = 0; tris[i+1]=0; tris[i+2]=0;_
* }*
* }*
* mesh2.triangles = tris;*