Light mask overlay without lighting the sky
I'm using an overlay to create a lighting effect in a 2D game. The overlay is a grey-scale mesh and I multiply each pixel of the mesh with the pixel already written to the framebuffer.
This works great. The problem is that the mesh shouldn't draw over the background/sky because lighting shouldn't affect those. I have a few solutions for this but I keep thinking I'm missing something simpler.
Solution 1.
Draw everything except the background with camera 2
Render the overlay and make sure the shader doesn't alter the existing pixel's alpha values (so anywhere the background would show through has an alpha of 0)
Render camera 2 to a render texture
Add the render texture to a screen sized plane that sits in front of the background
Compose the background and the render texture using alpha blending
Solution 2.
Set the shaders for the background and overlay so that they don't write to the z buffer
Render the background
Set the shader for everything else to write a specific z buffer value, say 0.5
Render everything remaining except for the overlay
Set the overlay shader to only write to pixels if it's z value is greater than the zbuffer. Position it somewhere where it's z value will be greater than 0.5
Any other solutions that I'm not thinking of?
Posted Oct 18, 2018, 6:11 AM UTC
Just use stencil buffer. Write some value to it in a background shader, then in your shadow mesh shader make it only render where stencil value doesnt equal the one you wrote in the background shader.
https://docs.unity3d.com/Manual/SL-Stencil.html