How to mask/clipping a sprite?


I need a solution for masking sprites like GUI Clipping mode or this in Unity.
I found this solution but it uses discard; and it masks everything. Also results are not smooth as I want.

Maybe It is possible by using a second texture with alpha for sprite shader? But I have no idea how to implement it. I couldn’t find anything similar on shadertoy(such a terrible site :frowning: ).

Does anyone has a solution for sprite base(not for everything) masking (with shader+stencil perhaps)?



I’m on my phone right now, but perhaps you could use some variation of this?


I’ll publish an example for how you can do this relatively soon, I need to strip it out of an existing project.

What I do is I have a render target where I draw white masks onto a black clear. So on your GOs of whatever you want masked you need to attach a sprite with the mask shape as a white png with transparency. If you wanted some fading in the mask just erase parts of it. A black/white png with no transparency would probably work too.

Then you use the “mask”, with all of the masks drawn onto it, render target as the extra alpha in a shader to mask off areas of whatever you want masked. This requires masked objects to be drawn to their own render predicate too.

The final product is then drawn to a unit quad like other post processing effects are. If you follow the color grading tutorial you can probably learn enough to implement this yourself.


Also interested in this! Would be great to mask a sprite.