Search Unity

Outline shader where only the outline renders and the mesh is invisible ?

Discussion in 'General Graphics' started by kingbaggot, Jan 4, 2022.

  1. kingbaggot

    kingbaggot

    Joined:
    Jun 6, 2013
    Posts:
    51
    I've looked at a lot of outline shaders this afternoon and tried amending them to get this effect - but no dice - I always end up with the block colour vertex-fattened inital pass showing. Wheras I just want a nice outline with *nothing* inside it.

    Could someone point me in the right direction ?

    thanks X
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,342
    The easiest way to do this is with stencils.

    Most outline shaders require two passes, one for the object and one for the outline. This would be no different. However the first pass would be the object writing to the stencil buffer, but otherwise invisible. And the second pass would be the outline set to not draw anywhere the stencil has been written to.

    Code (csharp):
    1. Pass {
    2.   // write 1 to the stencil buffer only where it's not already set
    3.   Stencil {
    4.     Ref 1
    5.     Comp NotEqual
    6.     Pass Replace
    7.   }
    8.   ColorMask 0 // don't render to color
    9.   ZWrite Off // don't render to depth
    10.   // ZTest Always // if you want the outline to appear through walls
    11.  
    12.   CGPROGRAM
    13.   #pragma vertex vert
    14.   #pragma fragment frag
    15.  
    16.   #include "UnityCG.cginc"
    17.  
    18.   float4 vert (float4 vertex : POSITION) : SV_POSITION { return UnityObjectToClipPos(vertex); }
    19.   fixed4 frag() : SV_Target { return 0; }
    20.   ENDCG
    21. }
    22. Pass {
    23.   // only render where the stencil hasn't been set to 1
    24.   Stencil {
    25.     Ref 1
    26.     Comp NotEqual
    27.   }
    28.   // ZTest Always // if you want the outline to appear through walls
    29.  
    30.   // the rest of the outline shader pass
    31. }
     
    PF-Sebastien likes this.