Search Unity

How to modify shader so it works with deferred rendering? (Help)

Discussion in 'Shaders' started by AylaBVR, Aug 20, 2019.

  1. AylaBVR

    AylaBVR

    Joined:
    Jun 21, 2019
    Posts:
    4
    I have a shader for highlighting an object but since changing the camera to use deferred rather than forward rendering, it doesn't work any more. Could someone explain how I would need to modify the shader so that it does work? Thanks in advance.

    This is the shader:

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

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    Not possible I'm afraid. Unity disables the use of stencils when using the deferred rendering path, so that shader is unusable and no amount of minor or significant modification will fix it while still retaining the original functionality.

    If you want to use deferred, you will have to choose a different visual style for highlights, or use a shell based toon outline, or do significantly more c# coding.
     
  3. AylaBVR

    AylaBVR

    Joined:
    Jun 21, 2019
    Posts:
    4
    Oh dear that's annoying. I will have to look for a different method then. But that is very useful to know, thank you very much!