I want to achieve in Unity a see through effect like the one in these examples:
In my specific scenario there are a couple of requirements:
- Sprites are using alpha blending, and sprites have transparent areas.
- There are 2 kind of elements that occlude the character. One should create the silhouette effect, and the other one should behave like normal.
For occluding elements that create the silhouette I enable ZWrite, and disable it for elements that doesn't.
For the character I tried setting the queue of the shader to transparent+1, and added this pass:
Pass
{
ZTest Greater
Lighting Off
Color [_Color]
}
And the effect works partially:
The silhouette is drawn all over the character, even the parts that are transparent. Transparent parts shouldn't create a silhouette.
The silhouette is created when the character is behind a sprite, even if that part of the sprite is transparent. Being behind a transparent part of the sprite shouldn't create the silhouette.
The character appears infront the rest of the elements, even if it is behind them. I guess this is because setting the queue to Transparent+1. But if I leave it as Transparent, the character is drawn in the correct order, but the silhouette is never seen.
I tried to follow these tips someone gave me, but I'm unable to get it to work:
1) Leave the pass that renders the sprites as is.
2) Add a pass that writes to the z-buffer, but has a shader that uses clip() to discard pixels based on alpha. You can't use the z-buffer to make soft z-tests without using MSAA and alpha-to coverage. The quality of that won't be great, but it's the best you can do. A faster alternative is a pattern or noise dither, or a good old fashioned threshold if your sprites all have fairly sharp edges.
3) Add third pass to occludable objects that draws the occlusion color using the z-test and make sure it's drawn as a final pass.
I'm kind of new to shaders, specially in Unity, and I just can't figure out how to make it work properly.