- Home /
Delete tris from mesh without consequences?
Hi :) 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!
It depends on what kind of triangle mesh you use. If you use triangle-strips, it's not that easy. If you use a "normal" triangle list (without calling Optimise()) it's like @aldonaletto: said. Just remove the 3 indices of the triangle you want to remove.
The behaviour you explain sounds like you removed only one index (or two) which will mess up the indices after the deleted one since every triangle is made up of 3 indices.
I am positive I am deleting all three indexes. However, I have used Optimize() calls in the creation of the mesh, so, that may be my issue! Thank you for the information!
Answer by aldonaletto · Jan 08, 2012 at 04:13 AM
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.
Hi :) I have a procedurally generated mesh, no errors, looks fine. However, I need holes be punched into it. They don't need any particular shape, I just want the tris to "disappear" however, whenever I do this, 50% of the tris' normals get inverted (in an On-Off pattern). This can become exacerbated when all of the tris connected to a vertex get removed. This then causes unity to throw errors about there being more normal data than there are vertexes. I assume removing all the tris that connect to a vertex causes Unity to remove the vertex automatically. I use a slightly different strategy for removing the tris than you use, though they are the same in concept. I've used your way, and with the same results, just a heck of alot more lag.
In this simple script, the normals were not affected by the triangle removal - but notice that I don't do anything else but deleting the triangle! No RecalculateNormals, RecalculateBounds etc. Are you doing something else with the mesh?
@Flynn: ins$$anonymous$$d of saying you do it differently, how about edit your question and add the relevant code you use. We can't tell if there's something wrong with it without seeing it.
@aldonaletto: Your example is a bit confusing... Why do you delete all odd trianges? A simple function that removes a certain triangle would be a bit clearer i guess.
@Bunny83, my intent was to check if several triangles could be removed without any further consequences - and by coincidence I also had an almost ready script to do that...
Doing something else with the mesh yes, quite a bit. I will post the relevant code here... EDIT turns out, I was wrong in thinking it was the hole cutting causing the issues! It was actually the generation. $$anonymous$$esh.optimize was fixing it, however, when I had $$anonymous$$esh.optimize, cutting holes ruined it. So I was mislead into thinking it wasn't mesh.optimize, as the effect happened with and without mesh.optimize. However, I noticed when removing both optimize, and holecutting, that the error was still happening. $$anonymous$$y tris were backwards due to some unknown issue with tri generation! Swapping two of the vertices in the offending tris fixed the problem!
Answer by frogsbo · Jun 07, 2014 at 03:39 PM
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[i]] == p0) //MYCONDITION
{
tris[i] = 0; tris[i+1]=0; tris[i+2]=0;
}
}
mesh2.triangles = tris;
Your answer
![](https://koobas.hobune.stream/wayback/20220613062751im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Why vertex positions appear (0.0, 0.0, 0.0) ? 1 Answer
minimize vertex artifacting for procedural mesh manipulations - vertex collision same object? 0 Answers
Mesh vertex count differs on Mac/Windows 1 Answer
Mesh triangles don't match wireframe view? 0 Answers
Perlin Noise Spherical Terrain 2 Answers