Search Unity

Help with Outline view dependent shader

Discussion in 'Shaders' started by Eriks-indesign, Jun 22, 2017.

  1. Eriks-indesign

    Eriks-indesign

    Joined:
    Aug 15, 2012
    Posts:
    50
    Hey,

    I'm trying to create a view dependent outline shader, where the outline appears if the camera looks straight at the item. If the camera is angled or if you look away the outline disappears. I want to do this all in the shader so I don't have to create a script that gives me the vectors.

    Here is what I have so far, but it doesn't work correctly like when I orbit the object it is like it acts in world space.

    Code (CSharp):
    1.  
    2.  
    3. Shader "FantasticYes/Effect/Outline _ ITEM OUTLINE"
    4. {
    5.     Properties
    6.     {
    7.         [HDR]_TintColor("Tint Color",                    Color) = (0.5,0.5,0.5,0.5)
    8.     }
    9.  
    10.         SubShader
    11.     {
    12.         Tags { "RenderType" = "Transparent" "Queue" = "Geometry"}
    13.  
    14.         Pass
    15.         {
    16.             ZTest Less
    17.             ZWrite Off
    18.             Blend One One
    19.            
    20.             CGPROGRAM
    21.             #pragma vertex vert
    22.             #pragma fragment frag
    23.  
    24.             #include "UnityCG.cginc"
    25.            
    26.  
    27.             uniform half4 _TintColor;
    28.  
    29.             struct v2f
    30.             {
    31.                 float4 pos        : SV_POSITION;
    32.                 float2 uv        : TEXCOORD0;
    33.                 float3 color    : COLOR;
    34.             };
    35.             v2f vert (appdata_full v)
    36.             {
    37.                 v2f o;
    38.                 o.pos = UnityObjectToClipPos(v.vertex);
    39.  
    40.  
    41.                 float3 viewDir = normalize(UNITY_MATRIX_V[2].xyz);
    42.                 float4 objectOrigin = unity_ObjectToWorld[3];
    43.                 float3 normal = normalize(objectOrigin - _WorldSpaceCameraPos);
    44.  
    45.                 float dotProduct = 1 - dot(normal, viewDir);
    46.                 o.color = smoothstep(1, 0.9, dotProduct);
    47.                
    48.                
    49.                 return o;
    50.             }
    51.             fixed4 frag(v2f i) : SV_Target
    52.             {
    53.                 float4 col = _TintColor;
    54.                 col.rgb *= i.color;
    55.  
    56.                 return col;
    57.             }
    58.             ENDCG
    59.         }
    60.     }
    61. }
    62.