I've been trying to crack this all day, spent a lot of time experimenting with the stencil buffer and while I was getting some interesting behavior, I was not getting what I intended.

I would like to be able to hide the sections of the sprites that are behind the other transparent sprites in their sorting order.

enter image description here

I tried a bunch of stuff, and ended up with a similar issue as seen here

And yes, I have seen this and other related posts

The shader basically works...but in reverse, so it happily draws the lower layers on top and hides the upper layers where there is overlap.

enter image description here

Figure this would be a common problem for people using a sprite rig--anyone have any ideas? Ideally would be able to use one material for all the sprites!

  • \$\begingroup\$ Only backwards hiding is possible with the stencil buffer, because sorting layers are drawn with the painter's algorithm: objects at the back get drawn first, before objects in front get to tell the stencil buffer "don't draw anything else here". You're also limited to hard aliased polygon edges that way, because the stencil test is all or nothing and runs before the fragment shader can clip out transparent regions. Have you tried using the technique you've found, together with reversing the order of your sprites so the "front" ones are at the back and get drawn first? \$\endgroup\$
    – DMGregory
    Commented Oct 17, 2023 at 11:30
  • \$\begingroup\$ Thanks for the reply! I did try that, it still doesn't work that well...makes a lot of holes. Also it seems really janky to abandon reason with the sprite sorting order as it is used in other places quite a bit. \$\endgroup\$ Commented Oct 17, 2023 at 19:40
  • \$\begingroup\$ Then unfortunately the only solution available to you is to pre-render this object opaque to an off-screen render texture, then blend that render texture at partial opacity with your scene. No solution using the stencil buffer will do what you want, so you'll have to remove that requirement from your question. \$\endgroup\$
    – DMGregory
    Commented Oct 17, 2023 at 19:43
  • \$\begingroup\$ Thanks, I also tried using the camera sorting layer texture but ran into some issues with it seeming to recursively work on itself since all the objects are in one layer. Not to mention getting the offset and scaling right seems to be a big issue... \$\endgroup\$ Commented Oct 17, 2023 at 21:37


You must log in to answer this question.

Browse other questions tagged .