- Home /
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?
Sounds a bit complex. Have you considered a bit of boolean operator-style tricking? I.e. you assign a value to each pixel of the screen for how much of mesh 1 is visible and how much of mesh 2 is visible (depending on their respective transparency texture), invert that pixel map for mesh 2, multiply those, crop the resulting image to the size and position of mesh 1's supposed transparency texture and use that? I'm not quite sure I'm getting exactly what you're looking for, though.
Answer by MakeCodeNow · May 28, 2014 at 08:26 PM
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.
I tried Poly2tri, but I couldn't find a way to get it to generate the triangle index array I need, so I thought a masking approach would be better. I'm not dealing with partial transparency or anything. I just want to render a white mask to cancel a black mesh in order to make scenery visible/invisible.
Stencil and depth aren't shaders in Unity are they? What are they and how do I go about using them?
I was referring to the depth and stencil buffers, and to depth and stencil writing and testing. You can control depth testing and both stencil writing and testing in the Unity shader files. You can read about these techniques and how to use them if you read up on depth and stencil testing. For your case, I think you should focus on stencil testing, as that's really what you are doing, using one object to stencil out another object.
Your answer
Follow this Question
Related Questions
How to efficiently render a 2D grid? 1 Answer
Alpha vs non-alpha unlit shaders 0 Answers
See-through hole via shaders on a 2D plane 2 Answers
Hiding part of mesh 0 Answers