Search Unity

Write pushed vertices to stencil buffer?

Discussion in 'Shaders' started by BottleEngineer, Jul 15, 2018.

  1. BottleEngineer

    BottleEngineer

    Joined:
    Jul 10, 2018
    Posts:
    12
    To the best of my knowledge, writing to the stencil buffer is allowed only for pixels covered by original geometry. However, if I move my vertices around in a vert shader, is there a way to write those changes into the stencil buffer in a followup pass? I've done some testing and it seems like this might not be possible. I would love to be proven wrong!

    My goal:

    Code (CSharp):
    1.  Pass {
    2.      // Push vertices around
    3.  }
    4.  
    5.  Pass {
    6.     // Write to stencil buffer using altered vertices
    7.  }
     
  2. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    3,028
    Hi!
    The stencil buffer gets updated with whatever is being rendered to it, the same way the other buffers work (color, for instance). If you modify the positions of the vertices in the vertex shader, the stencil buffer should be updated with the altered geometry.
     
  3. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    To add to the above:

    Code (CSharp):
    1. Pass {
    2.     // Set Stencil stuff
    3.     Stencil {
    4.         Ref 1
    5.         Comp Always
    6.         Pass Replace
    7.     }
    8.  
    9.     CGPROGRAM
    10.     #pragma vertex vert
    11.     #pragma fragment frag
    12.  
    13.     v2f vert(appdata_full v) {
    14.         // Push vertices around
    15.     }
    16.  
    17.     // etc
    18. }
     
  4. BottleEngineer

    BottleEngineer

    Joined:
    Jul 10, 2018
    Posts:
    12
    Ok, thanks for the tip.