Search Unity

Question need help with this deferred rendering shader code

Discussion in 'Shaders' started by valveCorpHype, Aug 12, 2022.

  1. valveCorpHype

    valveCorpHype

    Joined:
    Jul 5, 2018
    Posts:
    14
    I am trying to create an outline shader in deferred rendering.

    Code (CSharp):
    1.  
    2. Shader "OutlineShader"
    3. {
    4.     Properties
    5.     {
    6.         _MainColor("Main Color", Color)=(1,1,1,1)
    7.         _OutlineColor("Outline Color", Color)=(1,1,1,1)
    8.         _OutlineSize("OutlineSize", Range(0.5,1.5))=1.1
    9.  
    10.     }
    11.     SubShader
    12.     {
    13.         CGPROGRAM
    14.         #pragma surface surf Lambert
    15.         struct Input {
    16.           float4 color : COLOR;
    17.         };
    18.         fixed4 _MainColor;
    19.         void surf (Input IN, inout SurfaceOutput o) {
    20.           o.Albedo = _MainColor.rgb;
    21.         }
    22.         ENDCG
    23.        
    24.         Pass
    25.         {
    26.             Cull Front
    27.             Tags {"LightMode"="Deferred"}
    28.            
    29.             CGPROGRAM
    30.             #pragma vertex vert
    31.             #pragma fragment frag
    32.             #include "UnityCG.cginc"
    33.            
    34.             fixed4 _OutlineColor;
    35.             float _OutlineSize;
    36.            
    37.             struct appdata
    38.             {
    39.                 float4 vertex:POSITION;
    40.             };
    41.             struct v2f
    42.             {
    43.                 float4 clipPos:SV_POSITION;
    44.             };
    45.             v2f vert (appdata v)
    46.             {
    47.                 v2f o;
    48.                 o.clipPos=UnityObjectToClipPos(v.vertex*_OutlineSize);
    49.                 return o;
    50.             }
    51.             fixed4 frag (v2f i) : SV_Target
    52.             {
    53.                 return _OutlineColor;
    54.             }
    55.             ENDCG
    56.         }
    57.    }
    58.  
    59.    
    60. }
    61.  
    My understanding is that the surface shader fills the G-buffer, and the second deferred pass renders to SV_Target0. The outline shows up, but the color is not what I set it to at all. There are also unexpected behaviors when I move the camera around. What did I do wrong?