Search Unity

Outline Shader Rendering Behind Everything

Discussion in 'Shaders' started by halecc, Jul 16, 2017.

  1. halecc

    halecc

    Joined:
    Dec 17, 2016
    Posts:
    33
    Hello guys,

    I really have not messed with shaders but i tried combining the effects of unlit and outlining which seemed to work at first like it was intended but now it is not and i am not sure why. I am sure it is a pretty simple fix but i have been stuck on this for too long.

    The outline of the shader only renders in the background, i have tried switching RenderQueue but that does not do anything. Please help!


    Code (CSharp):
    1. Shader "OutlineAndUnlitFill"
    2. {
    3.     Properties
    4.     {
    5.         _OutlineColor("Outline Color", Color) = (1, 0, 0, 1)
    6.         _Thickness("Thickness", float) = 1
    7.         _Color("Color Tint", Color) = (1, 1, 1, 1)
    8.         _MainTex("Base (RGB) Alpha (A)", 2D) = "white"
    9.     }
    10.  
    11.     SubShader
    12.     {
    13.         Tags
    14.         {
    15.             "Queue" = "Transparent"
    16.             "IgnoreProjector" = "True"
    17.             "RenderType" = "Transparent"
    18.         }
    19.  
    20.        
    21.         //Unlit
    22.         Pass
    23.         {
    24.             SetTexture[_MainTex]
    25.             {
    26.                 ConstantColor[_Color]
    27.                 Combine Texture * constant
    28.             }
    29.         }
    30.        
    31.         // Fill the stencil buffer
    32.         Pass
    33.         {
    34.             Stencil
    35.             {
    36.                 Ref 1
    37.                 Comp Always
    38.                 Pass Replace
    39.                 ZFail Replace
    40.             }
    41.  
    42.             ColorMask 0
    43.         }
    44.  
    45.         // Draw the outline
    46.         Pass
    47.         {
    48.             Blend SrcAlpha OneMinusSrcAlpha
    49.             ZWrite Off // On (default) = Ignore lights etc. Should this be a property?
    50.             Stencil
    51.             {
    52.                 Ref 0
    53.                 Comp Equal
    54.             }
    55.  
    56.             CGPROGRAM
    57.                 #pragma vertex vert
    58.                 #pragma geometry geom
    59.                 #pragma fragment frag
    60.  
    61.                 #include "UnityCG.cginc"
    62.                 half4 _OutlineColor;
    63.                 float _Thickness;
    64.  
    65.                 struct appdata
    66.                 {
    67.                     float4 vertex : POSITION;
    68.                 };
    69.  
    70.                 struct v2g
    71.                 {
    72.                     float4 pos : SV_POSITION;
    73.                 };
    74.  
    75.                 v2g vert(appdata IN)
    76.                 {
    77.                     v2g OUT;
    78.                     OUT.pos = UnityObjectToClipPos(IN.vertex);
    79.                     return OUT;
    80.                 }
    81.  
    82.                 void geom2(v2g start, v2g end, inout TriangleStream<v2g> triStream)
    83.                 {
    84.                     float width = _Thickness / 100;
    85.                     float4 parallel = (end.pos - start.pos) * width;
    86.                     float4 perpendicular = normalize(float4(parallel.y, -parallel.x, 0, 0)) * width;
    87.                     float4 v1 = start.pos - parallel;
    88.                     float4 v2 = end.pos + parallel;
    89.                     v2g OUT;
    90.                     OUT.pos = v1 - perpendicular;
    91.                     triStream.Append(OUT);
    92.                     OUT.pos = v1 + perpendicular;
    93.                     triStream.Append(OUT);
    94.                     OUT.pos = v2 - perpendicular;
    95.                     triStream.Append(OUT);
    96.                     OUT.pos = v2 + perpendicular;
    97.                     triStream.Append(OUT);
    98.                 }
    99.  
    100.                 [maxvertexcount(12)]
    101.                 void geom(triangle v2g IN[3], inout TriangleStream<v2g> triStream)
    102.                 {
    103.                     geom2(IN[0], IN[1], triStream);
    104.                     geom2(IN[1], IN[2], triStream);
    105.                     geom2(IN[2], IN[0], triStream);
    106.                 }
    107.  
    108.                 half4 frag(v2g IN) : COLOR
    109.                 {
    110.                     _OutlineColor.a = 1;
    111.                     return _OutlineColor;
    112.                 }
    113.             ENDCG
    114.         }
    115.     }
    116.  
    117.     FallBack "Diffuse"
    118. }