Is there a way to mask a a paper 2D sprite with another sprite or material?

I am building a 2D game with a character that consists of outlines. I need to block the sprites behind the character (for example the horizon you see in this picture, but not the background image. I know how to do it with a ‘hold out’ material, but that needs meshes and a lit scene. I would prefer to work in an unlit scene with simple sprites and flipbooks.
here is an example of a character and an asset against a horizon that needs to be blocked: https://www.janheinarens.com/wp-content/uploads/2020/04/IMG_20200420_171606_583-scaled.jpg
Any help would be greatly appreciated.

By default when you Apply Paper 2D Settings to a texture before creating your Spite, they use the TranslucentUnlitSpriteMaterial which is part of the engine content - specifically Paper2D Content. You can swap this out for any of the other types in the Paper2D Content folder (Masked Unlit, Masked Lit, etc.) which all derive from (IE, they are instances of) the DefaultLitSpriteMaterial or the DefaultSpriteMaterial for unlit, but you can also create your own base material.

DefaultSpriteMaterials

The Default materials use the alpha of a texture as the mask, so if you import a png on a transparent background, it will automatically mask out the transparent parts of the texture. If you make a copy of the DefaultLitSpriteMaterial you can modify it to allow you to specify an additional mask by multiplying another texture that will act as another mask to the entire sprite. Here’s an example of what this would look like:

You don’t have to use the RGB Switch Parameters as I have, but it’s an example of a more versatile material as you could store 3 alpha masks in each texture to save memory and then just specify which channel you want to use in the Material Instance.

Either way, masking an already masked material is trivial and I’m sorry no one answered your question sooner.

The benefit of using the DefaultLit material as your master material is you also get to use Physical Materials on your sprites (Metallic, Roughness, AO, Normals etc.) and you get shadowed sprites too so your game will look way better!

Also, I just realized you may want to have the mask remain stationary in some cases where you want to rotate the sprite. If that’s the case, use this setup in a separate material:

This will cause the sprite to rotate, but the mask to remain stationary. In my case I used it for a spinning saw blade where I wanted the bottom of the blade to not show through the ground.

I also added a switch to invert the mask to make it even more versatile.

Oh, and if you’re rotating a circular sprite, you’ll run into an issue where it tiles the texture and you see the edges of the circle in the corners. To fix this, just open your texture and set its X and Y tiling method to Clamp. This means it won’t tile the edges and will fix that issue.

I hope this helps someone :slight_smile: