Deleting one triangle out of mesh? Cut hole in mesh in runtime?

I am experimenting with terrain generation. I am using Unity’s built-in plane, and using randomized variables to deform the position of each vertex in the plane.

Sometimes, the mesh will end up folded inside itself, in such a way that there are some triangles whose back side face the player. Since you can’t see these triangles, and you can pass through them, sometimes you wind up getting stuck inside the mesh, unable to get out.

Some solutions I have found would involve turning off backface culling (then the player would at least be able to see these triangles), but this would mean a big performance hit and still wouldn’t solve getting stuck.

One solution I have thought of is to allow the user to break through, by deleting a triangle so that he/she can get out of the cave in which they are stuck.

The question: Is it possible to delete a single triangle from a mesh during runtime, so that there is a hole in the mesh through which the player can escape?

Edit: Strange - last night there was a comment on this, but coming back to reread it, it’s now gone. I wonder why that poster deleted his response?

I think this is the wrong solution, and may cause more problems. It would be better to avoid this folding back thing - you could for instance vary only the vertex Y coordinate, and/or modify the X and Z coordinates at most 50% of the distance between vertices.

Anyway, you can delete triangles from the mesh to make holes: get the triangles from the mesh in an array, delete the ones you want and assign the array back to the mesh triangles - but don’t delete vertices! some of them may be shared by other triangles.

There’s this interesting answer from @Bunny83 that removes all triangles whose normals are below some angle in a mesh collider - maybe it can show you some useful mesh tricks.

Backface culling is no performance hit if most of your faces are frontfacing anyway. Also, you would only give your “terrain” the shader with culling set to off, so for all your other models, backface culling is still in effect.

Yes, in theory you could remove a single triangle from a mesh’s triangle list. However, I don’t think Unity gives you any convenience functions to determine a very specific triangle within a mesh. Physics.Raycast only gives you the object (and hence the whole mesh) that was hit, not a specific triangle. So in the worst case you would have to re-implement raycasting for triangles (check any raytracing tutorial on how to do that) to walk through the triangle list of your mesh, and then remove it from the list.

But maybe the better approach would be to constrain your deformation parameters somehow, so that backfacing triangles don’t occur in the first place?

EDIT: Actually, there is a convenience function to find the corresponding triangle index of a RaycastHit, if the object is a MeshCollider: RaycastHit.triangleIndex. With this, your original approach should be easy to implement. Thanks for the pointer, @Fattie.

This may be a little more complex than you are looking for but instead of working with a mesh right away you could generate a “point cloud”. Each point is at a coordinate like (3,1,5) (3,0,5) etc… To generate interesting shapes and figures look into noise generating algorithms like Perlin Noise. After this use an algorithm like Marching Cubes to connect up all of the vertices. Unity comes with a nice script to generate the noise and the wiki has a marching cube example here Marching Cubes. Those two algorithms should get you on the right track. Hope that helps!

Another possibility is to use a fractal subdivision algorithm, which has the advantage of creating very realistic terrains, with unlimited detail. See http://www.gameprogrammer.com/fractal.html or elsewhere