Cutting a hole in a 2d plane

Hi everyone,

I’m trying to figure out a solution for cutting a rectangular hole in a 2d plane.

For example, let’s say there was a plane in the scene and the user clicked on a spot on this plane; the result is a nice rectangular hole where the user clicked.

After reading through the scripting reference for Raycastcasting and Meshs, I believe I have a solution, though since I am new to C# and Unity I am hoping there might be a more efficient way

//1. Raycast from center of the camera
//2. Find the planes transform that the raycast rit, if it hit at all
//3. Find the point in world space where the raycast hit the plane
//4. Get the vertices of the planes mesh
//5. Calculate new vertices based on where the raycast hit and predetermined values for width and height of 'hole'

I can see this working out if the ‘hole’ cuts into one of the sides of the plane in a way that would leave a 8 sided figure, but I don’t believe my solution accounts for a situation where the ‘hole’ doesn’t touch any side of the plane.

If there are other posts about this I apologize. The forum search, and also the scripting reference search, doesn’t work all the time for me.

you could use a transparent shader and modify the materials texture instead. Use the raycast to get the textures uv coords (I used ngui UICamera.lastHit.textureCoord for that, but I guess the same can be achieved with a raycast) and use SetPixels to copy a transparent square or a blob in to the texture.

You’re fairly close to what I’d use.
My setup would be :

1 : Ensure mesh have mesh colliders.
2 : Raycast from camera.
3 : When the raycast hit result returns an object that you can chop up allow chopping (mouse/keyboard chop key enabled).
4 : If chopping ask the raycast result for the trianlge index (RaycastHit.triangleIndex)
5 : Using the known triangle index locate adjacent triangles by checking remaining triangles to see if they share common edges.
6 : Remove hit and surrounding triangles from the mesh triangle list.
7 : Update mesh with new triangle list.

There’s a few ways to skin this cat but this one is fairly fast. The “complicated” part would be finding adjacent triangles such that the found triangles roughly approximate the hole you’re about to chop. I might repeat my adjacent triangle search until found triangles are outside of the hole and then stop. If I want to prevent holes from being cut into sides of objects I’d compare and reject triangles who’s face normals didn’t match that of the center hit triangle.

Some other ways to modify the mesh might be to use a vertex color shader which you set affected triangle vertex color to transparent. This won’t remove the triangle but make it see through without affecting mesh colliders.

Another way would be to use a Decal shader to paint a hole on the object. In this case you’d rely more on RaycastHit.barycentricCoordinate and paste your hole decal onto the mesh.