Search Unity

Depth test against a previous pass

Discussion in 'Shaders' started by customphase, Aug 19, 2017.

  1. customphase

    customphase

    Joined:
    Aug 19, 2012
    Posts:
    246
    Hey everybody, is there a way to do depth testing against a previous pass? I need to render a mesh in such a way that backfaces are one color and front faces are the other, i do it in two passes, but passes are drawn on top of each other and they dont do any depth testing, so it causes backfaces, that shouldnt be visible, to be drawn on top of the front faces (and vice versa, if i change the order of passes). Maybe theres some other way of achieving this effect? Tried searching and couldnt find anything regarding that.
     
  2. DominoM

    DominoM

    Joined:
    Nov 24, 2016
    Posts:
    460
    You could use stencil.

    Code (csharp):
    1.            // 1st pass
    2.             Stencil {
    3.                 Ref 0
    4.                 Comp Always
    5.                 Pass IncrSat
    6.             }
    7.            Cull Back
    8.  
    9.            // 2nd pass
    10.             Stencil {
    11.                 Ref 1
    12.                 Comp Greater
    13.                 Pass IncrSat
    14.             }
    15.            Cull Front
    16.  
     
  3. customphase

    customphase

    Joined:
    Aug 19, 2012
    Posts:
    246
    Thanks, i tried stencils, but couldnt make it work. Your code seem to do nothing as well :(
     
  4. customphase

    customphase

    Joined:
    Aug 19, 2012
    Posts:
    246
    I forgot to mention that i displace vertices in the shader (they are displaced exactly the same in both passes), i think thats the reason stencil buffers dont do anything
     
  5. DominoM

    DominoM

    Joined:
    Nov 24, 2016
    Posts:
    460
    I've used it in a toon shader to do an outline pass after the normal pass, but I've not tested with displaced vertices so I can't confirm it would still work then.

    Is the material transparent? I can't picture how you expect backfaces to be seen unless the displacement make them visible when they normally would not be..

    Edit: Past my bedtime correction: Of course the outline pass in my toon shader is a displaced version of the mesh used in 1st pass, so at least in my case, it does still work. I used stencil so only the displaced edges get drawn on the back face outline pass.
     
    Last edited: Aug 20, 2017
  6. customphase

    customphase

    Joined:
    Aug 19, 2012
    Posts:
    246
    Im a goddamn moron. The reason it didnt work was because i was rendering it into a renderTarget without a depth buffer. As soon as i enable it, it actually works like i wanted it by default now, without the need to use stencils. Thanks for the help!