How to mask a mask?

I need to use a sprite that masks another sprite that is then used to mask a 3rd sprite( by using the combination of the 2 masks)

Here is a picture of what Im trying to do:


I understand that this is not possible using sprite masks, because it would need to use a combination of both “visible inside mask” and “visible outside mask”, which is not a valid option on sprite renderers.

So it seems I need a shader to accomplish this?

How would I go about creating a shader that makes this possible?

Here are the requirements:

  • the shader needs to be able to receive the inputs of both mask 1 and mask 2(which are opaque color with transparent pixels on borders) and the sprite subject to be masked

  • the transform.position of mask 1 and mask 2 and the sprite must be considered for the masks to work correctly

  • the alpha=1 pixels of the masks must render as transparent even though they are a solid color( like the sprite mask component)

  • there will be multiple instances of this character, so there must be a way for the masks to apply only locally to the object they are attached to. ( this is achieved with the sprite mask component by creating a parent with a sorting group component, essentially to avoid masks from different characters to interfere with eachother )

I’m currently using the sprites/default material, so maybe I should start from there?

Should I use shader graph? Is there a tutorial to something that fits the situation that you could point me to?

Any advice would be greatly appreciated. Thanks in advance!

Instead of making new threads, why don’t you stay in the same thread you already started.

I suggested a valid, possibly functioning solution (depending on your use-case). If you were looking for ready to use code, that’s something I did not provide.

Makes me wonder if you even bothered to check the answers.

2 Likes

it was your answer that led me to make this thread, thanks for your reply and I did read it, however the resources you provided did not contain the information to solve 3 of the needs of my usecase:

1)how to make the stencil apply only to a given sprite ( if 2 characters are close to one another and have the same material, how do i prevent the mask in one to affect the other )

2)how to input 2 different sprites to be used as masks

3)how to take into account the world positions of the inputted sprite masks

Thanks!

1 Like