Advertisement

How to use custom shader as a Mask?

Started by March 31, 2020 09:02 AM
3 comments, last by Juliean 4 years, 10 months ago

Hi guys. In my game I want to use some SpriteMask. I wander if I can use my custom shader as a Mask?

In my custom shader I draw a shape that changes over time and I want to use this shader(drawn shape) as a mask.

How can I achive this effect?

P.S. I'm working on 2D game.

Answer is yes you can….

There are infinite possible ways of doing this, be more specific

Advertisement

In my custom shader I draw a shape that changes over time and I want to use this shader(drawn shape) as a mask.

@_WeirdCat_ this line is specifies what I want. I want to have a sprite like background. Also I have another sprite with custom shader which draws some shapes. I want that the background sprite will be visible online inside the shape that is drawn over second sprite and NOT visible beyond this shape

Alexander Nazarov said:
@_WeirdCat_ this line is specifies what I want. I want to have a sprite like background. Also I have another sprite with custom shader which draws some shapes. I want that the background sprite will be visible online inside the shape that is drawn over second sprite and NOT visible beyond this shape

Some ways you could do it:

  1. Use a stencil-buffer. Clear the stencil before drawing the shape-sprite, and when drawing fill all the pixels that belong to the shape. When wanting the shape applied, configure stencil-test so that it will fail for all pixels outside of that shape. Plus-Side: Almost no need to modify the shaders. Can be controlled with stencil-states from application side (which might be a downside depending on your seput).
  2. Draw shape-sprite into separate render-target (0/1 depending on in shape or not), bind this shape-texture to the target sprite, sample and check for the value. Discard/modify alpha to 0 when the pixel is not in shape.

This topic is closed to new replies.

Advertisement