Game object sprite clipping/stencil (SOLVED)

Hi there! I’m trying to (once again) figure out the best way to dynamically “cut off” parts of the sprite (effectively a stencil). For example if there’s a portal you want the sprite to disappear into it (not just disappear completely in 1 frame - that looks weird and cheap).

I’ve previously done something similar by having a condition in the fp/vp that doesn’t render anything outside of a hardcoded y range. The issue is that the value was hand-picker and it introduced a conditional in the fp which I believe is bad practice.

What I want to do now now is to be able to dynamically place these “portals” or “doors” and have the sprite “disappear” and “appear” from them (while still rendering the sprite as usual if the door or portal isn’t used) - looking for any ideas on how to approach this.

Hi! If I understand correctly, you want to block parts of the sprite by the portal or door so it’s only visible on the area that the sprite is rendering on? Sounds like you probably could be using the stencil buffer to achieve that. I have an example here that might help you:

demo:
https://jhonnyking.github.io/my-public-defold-examples/sprite-masking

4 Likes

My previous experience on similar subject:

It is better to use mesh shape as @Pkeod suggested:

@jhonny.goransson 's example multiplies with black which is not very suitable for every use case:

My conclusion is; simply there no better way of masking/clipping build in sprite without using second texture which is not possible right now.

3 Likes

Another approach to mask a sprite with arbitrary mask:
Desktop game.zip (978.6 KB)

5 Likes

I’ve actually ended up going another way of adding a variable to the fragment program which makes everything to the side of a line alpha = 0 :smiley: (thanks to everyone here and @JCash and @roccosaienz on Discord for inspiration!)

1 Like

Hi @Alesis you have an example, or can you explain your approach?

1 Like