Cancel out mesh alpha

Hello everyone!

I’m rendering a mesh programatically and I would like to know if there’s a way to make another mesh, that’s in front of my first one, act like an “x-ray screen” of sorts and cancel out the alpha for the underlying mesh.

To clarify the question further: I have mesh 1 and mesh 2. Mesh 1 is a black mesh that hides objects behind it. Mesh 2 is in front of mesh 1 on the player perspective. I want to make it so that where mesh 2 intersects or projects against mesh 1, mesh 1 is fully transparent.

I’m in 2D btw. The mesh is supposed to be the player’s current field of vision.

Can it be done?

You need some way to test this intersection, and there are two per-pixel options, depth and stencil. Either one can work, depending on how your scene is setup, but stencil may be the better approach. You’ll need to render the base object and the mask first and then the rest of the scene. If the base object is partially transparent outside the mask, then that is trickier. One option would be to render the object and the mask to a render target and then use that render target where you currently use the base sprite.

If you want to take a geometric approach, there are some really good C# mesh triangulation libraries that can handle holes. Check out poly2tri and Triangle.NET.