Search Unity

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

Discussion in 'Shaders' started by PhilippLe, Sep 5, 2016.

  1. PhilippLe

    PhilippLe

    Joined:
    Sep 10, 2015
    Posts:
    28
    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!
     
    Jason_Hipkins likes this.
  2. Namey5

    Namey5

    Joined:
    Jul 5, 2013
    Posts:
    188
    JamesArndt likes this.
  3. Thomas-Mountainborn

    Thomas-Mountainborn

    Joined:
    Jun 11, 2015
    Posts:
    501
    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.
     
  4. PhilippLe

    PhilippLe

    Joined:
    Sep 10, 2015
    Posts:
    28
    Thank you. I solved my issue! This video is very good. The source code is in the decription:
     
  5. caleb424242

    caleb424242

    Joined:
    Jan 21, 2020
    Posts:
    1
    hi
     
    azevedco and twiggs1 like this.