How to mask/clipping a sprite?

Alpha version scene setup:

  • One or more ‘masking’ sprites that just draws black and white textures to the backbuffer
  • One or more ‘should-be-masked’ sprites that has their alpha mode set to ‘multiply’

Stencil version setup:

  • One or more ‘masking’ sprites that has a pass-through shader that basically just transforms, writes to backbuffer doesn’t matter
  • One or more ‘should-be-masked’ sprites that will be affected by the stencil buffer
  • Render ‘masking’ sprites to stencil buffer with color and depth writes off
  • Render ‘should-be-masked’ sprites with stencil testing on and stencil writes off

Not sure what your requirements are in terms of what you want to do with the masking, but either setup above is very lightweight (no extra framebuffers etc).

HTML5: https://jhonnyking.github.io/my-public-defold-examples/sprite-masking/
Project: https://github.com/jhonnyking/my-public-defold-examples/tree/master/sprite-masking

2 Likes