Only render mesh over other mesh

Heyho there friends.

So heres my situation.

Im making a 2D games, which out of various reasons uses 2D meshes instead of sprites. I use them to make polygons.

Now lets say I have these 2 polygons:


Basically what I want to achieve is that only the the part of the red pentagon that is OVER the yellow rectangle gets rendered. So like this:


Until now I did this by calculating the union of both meshes and then changing them accordingly. But this is not only prone to errors but also sometimes performance heavy.

There must be a more elegant way. You know of any?
I assume there is a shader or something that will do this with ease, right?

Thanks in advance guys. :smiley:

Found it myself. The solution is called “Stencil Buffers”. If you have the same problem, do some research on them and you will have a working shader in no time :slight_smile: