- Home /
Cropping mesh triangles at a given level
I'm trying to slice a mesh off at "sealevel". I can remove tris that are completely below sealevel, but I'm not sure how to alter tris that have one or two verts that cross the sealevel boundary so that they chop at sealevel, as in the picture below.
Any help on this would be gratefully received :) My maths is just failing me.
// get this object's position
float height = transform.position.y;
// get the mesh parts
Vector3[] verts = mesh.vertices;
List tris = new List(mesh.triangles);
// empty the mesh
mesh.Clear();
// remove tris
int count = tris.Count / 3;
for(int i = count-1; i >= 0; i--)
{
Vector3 v1 = verts[tris[i*3 + 0]];
Vector3 v2 = verts[tris[i*3 + 1]];
Vector3 v3 = verts[tris[i*3 + 2]];
// remove tris that are completely below sealevel
if(v1.y + height <= sealevel && v2.y + height <= sealevel && v3.y + height <= sealevel)
{
tris.RemoveRange(i*3,3);
continue;
}
}
image11.jpg
(71.0 kB)
Comment
Anyone any ideas how to programmatically adjust mesh triangles so they "chop" at a given height or plane?
Unity Wiki $$anonymous$$athf3D class has a LinePlaneIntersection() method that will allow you to calculate the intersection and therefore where to move the vertices associated with each triangle. Note that vertices are in local space.