Render an object only if the object is behind a specific object

Hello community,

I want to render an object (particles) only if they are behind an specific object. For example: I want to render bubbles in a liquid. The bubbles should be only visible if they are behind (or within) the “liquid mesh”. I don’t want to see the bubbles if I’m looking from the top through the liquid-surface (liquid-surface and liquid-mesh are different objects/materials in my scene). I’m well aware of the ZTest GEqual, but if I’m using this, the bubbles will be visible from everywhere. Therefore I adjusted the RenderQueue of some other objects. The renderqueue of my liquid surface is now 3100 and the renderqueue of the bubbles is 2500. Unfortunately if I’m looking from a specific angle (the camera is under the water-level) the surface is rendered in front of the bubbles.
Is there anyone out there who can point me in the right direction?
Help is very appreciated!

1 Like

The stencil buffer is normally used for things like this.

1 Like

Indeed, the stencil buffer is the way to go. This great post by supernat helped me make sense of them, hopefully it’ll do the same for you.

Thank you. I solved my issue! This video is very good. The source code is in the decription:

hi

2 Likes