I’m making a 2D game that zombies come out of the ground. The visuals are similar to Plants Vs. Zombies. There are 4 lanes that spawn zombies. I’m having trouble to find a good solution for that. There are 3 sprites that I’m working it: the background, the tomb and the zombie. I could simply get piece of the ground image and put above the zombie, but that would not be a great solution, because I want to change the position of the tombs and change the background image. I would have to slice the background for each situation. The obvious solution was to mask the zombie part that is beneath the ground. I found some mask shader on the Internet, and for all that worked on Unity 5, they masked everything that was above the mask. I could not configure the shader to select the sprites that I wanted to mask or to specify any parameter that limited the layer that the mask would affect. Here are the images of the game to illustrate what I’m saying:
This is two zombies getting out of the tomb. The Tomb on the center of the image is on the Sorting Layer “Tomb1”, the zombie on the Sorting Layer “Zombie1”, the tomb beneath in on the Sorting Layer “Tomb2”, and the zombie “Zombie2”.
When I apply the mask at the Sorting Layer “Zombie1”, it masks everything above, including the “Tomb2” and “Zombie2”. My question is if it is possible to limit the mask to have effect only in the Sorting Layer that the mask is included. The shader that I’m using is this:
I found that there is an Alpha Version of Unity with Mask Component to SpriteRenderer, but I don’t have the Pro License, and this game will be release before december (the month that this feature will be released, according to Unity Road Map). I appreciate for reading this far.
Here is what you can do if you want to try things on your own.
For Masks, create two versions of a shader… one to mark the mask’s presence, other to remove the mask after its done.
Create a shader that increments the stencil buffer whenever the mask is rendered. This shader discards a pixel if the mask pixel has alpha above a pre-selected cutoff (something like 0.2).
Create a shader (or variation of the first) that decrements the stencil buffer whenever the mask is rendered. Also discards pixel if alpha is above the pre-selected cutoff.
For maskables (zombies), modify the Defaults-Shader available in Unity to use the stencil buffer and draw only if the stencil value > 0 (or whatever ref value you start with for the masks)
Encapsulate zombies in the same layer with Masks (start with ON, end with OFF for the layer)
Thank you very much!! It worked!!! The keyword that I was missing was “Stencil”. I didn’t know the existence of this buffer and its use for 2D games. I found this forum thread: http://forum.unity3d.com/threads/is-it-possible-to-clip-a-sprite-with-another-sprite.254144/ and with a little googling and your tutorial I could make it work.
I’ll just check the shaders and share with you all.
I created a copy of this mask shader changing the shader name to “Sprites/Stencil Mask Restore” and Stencil Ref value to 0. Then, I created the materials for the shaders mentioned. Finally, set up the scene as @sandboxed explained. First the mask sprites, then the sprites to be masked and finally the restore mask sprite. I put the last one as a child of the mask sprite, to easily fit one over the other, and set the appropriate material.
Here is the result, thanks to @sandboxed
Cool!!!
With SpriteMasking coming to Unity in 5.4, you will not need to bother about setting up the order of mask and maskables. Also masks will be able to affect multiple layers.
Hi, I used this shader and it is working but it doesn’t have soft edges. Is it possible to use the alpha channel to get soft edged blending rather than the all or nothing threshold?