Search Unity

Sprite Outline Shader

Discussion in 'Shaders' started by hublard, Mar 17, 2018.

  1. hublard

    hublard

    Joined:
    Aug 19, 2015
    Posts:
    79
    Hello there,

    im looking for a sprite outline diffuse shader. When a player is behind a wall then you only see the outline shader.

    can someone help me? i have no experience in shaders.
     
  2. rakkarage

    rakkarage

    Joined:
    Feb 3, 2014
    Posts:
    683
  3. hublard

    hublard

    Joined:
    Aug 19, 2015
    Posts:
    79
    Thank you, but i need something where the outline is not always showing only there where a bodypart or a sprite is behind an other specific sprite
     
  4. rakkarage

    rakkarage

    Joined:
    Feb 3, 2014
    Posts:
    683
    i think it might be your best shot. not many good free open source outline shaders for unity that i could find. from page 4 of that thread:

     
  5. hublard

    hublard

    Joined:
    Aug 19, 2015
    Posts:
    79
    thank you for your help. its not exactly what i need. i hope i will find a solution
     
  6. Zee_pso

    Zee_pso

    Joined:
    Nov 12, 2016
    Posts:
    31
    test.png

    I managed to use this shader as a base for the effect you wanted using stencils. It's not perfect (I made it in a rush), and could be fixed up, but it should help you get started. Let me know if you need any more help.

    First Shader
    Code (csharp):
    1.  
    2. Shader "Outlined/Blocked Silhouette Only ZTest Fail" {
    3.     Properties{
    4.         _OutlineColor("Outline Color", Color) = (0,0,0,1)
    5.         _Outline("Outline width", Range(0.0, 0.5)) = .005
    6.     }
    7.  
    8.         CGINCLUDE
    9.         #include "UnityCG.cginc"
    10.  
    11.         struct appdata {
    12.         float4 vertex : POSITION;
    13.         float3 normal : NORMAL;
    14.     };
    15.  
    16.     struct v2f {
    17.         float4 pos : POSITION;
    18.         float4 color : COLOR;
    19.     };
    20.  
    21.     uniform float _Outline;
    22.     uniform float4 _OutlineColor;
    23.  
    24.     v2f vert(appdata v) {
    25.         // just make a copy of incoming vertex data but scaled according to normal direction
    26.         v2f o;
    27.         o.pos = UnityObjectToClipPos(v.vertex);
    28.  
    29.         float3 norm = mul((float3x3)UNITY_MATRIX_IT_MV, v.normal);
    30.         float2 offset = TransformViewToProjection(norm.xy);
    31.  
    32.         o.pos.xy += offset * o.pos.z * _Outline;
    33.         o.color = _OutlineColor;
    34.         return o;
    35.     }
    36.     ENDCG
    37.  
    38.     SubShader{
    39.         Name "FULL"
    40.         Tags { "Queue" = "Transparent" }
    41.         ztest equal
    42.  
    43.         Pass {
    44.             Name "BASE"
    45.             Cull Back
    46.             Blend Zero One
    47.             ztest always
    48.             Stencil {
    49. Ref 1
    50. Comp always
    51. Pass replace
    52. }
    53.          
    54.  
    55.         // uncomment this to hide inner details:
    56.         //Offset -8, -8
    57.  
    58.             SetTexture[_OutlineColor] {
    59.                 ConstantColor(0,0,0,0)
    60.                 Combine constant
    61.             }
    62.         }
    63.  
    64.         // note that a vertex shader is specified here but its using the one above
    65.         Pass {
    66.             Name "BLOCK"
    67.             Tags { "LightMode" = "Always" "Queue" = "Transparent"}
    68.             Cull Front
    69.             Ztest greater
    70.             Stencil {
    71. Ref 1
    72. Comp notEqual
    73. }
    74.  
    75.         // you can choose what kind of blending mode you want for the outline
    76.         //Blend SrcAlpha OneMinusSrcAlpha // Normal
    77.         //Blend One One // Additive
    78.         Blend One OneMinusDstColor // Soft Additive
    79.         //Blend DstColor Zero // Multiplicative
    80.         //Blend DstColor SrcColor // 2x Multiplicative
    81.  
    82.         CGPROGRAM
    83.         #pragma vertex vert
    84.         #pragma fragment frag
    85.  
    86.         half4 frag(v2f i) :COLOR {
    87.             return i.color;
    88.         }
    89.         ENDCG
    90.         }
    91.  
    92.         Pass {
    93.             Name "OUTLINE"
    94.             Tags { "LightMode" = "Always" "Queue" = "Transparent"}
    95.             Cull Front
    96.             Ztest greater
    97.             Stencil {
    98. Ref 1
    99. Comp notEqual
    100. }
    101.  
    102.         // you can choose what kind of blending mode you want for the outline
    103.         //Blend SrcAlpha OneMinusSrcAlpha // Normal
    104.         //Blend One One // Additive
    105.         Blend One OneMinusDstColor // Soft Additive
    106.         //Blend DstColor Zero // Multiplicative
    107.         //Blend DstColor SrcColor // 2x Multiplicative
    108.  
    109.         CGPROGRAM
    110.         #pragma vertex vert
    111.         #pragma fragment frag
    112.  
    113.         half4 frag(v2f i) :COLOR {
    114.             return i.color;
    115.         }
    116.         ENDCG
    117.         }
    118.     }
    119.  
    120.         Fallback "Diffuse"
    121. }
    122.  
    Second Shader
    Code (csharp):
    1.  
    2. Shader "Unlit/UnlitBasic"
    3. {
    4.     Properties
    5.     {
    6.         _MainTex ("Texture", 2D) = "white" {}
    7.     }
    8.     SubShader
    9.     {
    10.         Tags { "RenderType"="Opaque" }
    11.         LOD 100
    12.         //ztest always
    13.  
    14.         Pass
    15.         {
    16.             Name "BASIC"
    17.             CGPROGRAM
    18.             #pragma vertex vert
    19.             #pragma fragment frag
    20.             // make fog work
    21.             #pragma multi_compile_fog
    22.          
    23.             #include "UnityCG.cginc"
    24.  
    25.             struct appdata
    26.             {
    27.                 float4 vertex : POSITION;
    28.                 float2 uv : TEXCOORD0;
    29.             };
    30.  
    31.             struct v2f
    32.             {
    33.                 float2 uv : TEXCOORD0;
    34.                 UNITY_FOG_COORDS(1)
    35.                 float4 vertex : SV_POSITION;
    36.             };
    37.  
    38.             sampler2D _MainTex;
    39.             float4 _MainTex_ST;
    40.          
    41.             v2f vert (appdata v)
    42.             {
    43.                 v2f o;
    44.                 o.vertex = UnityObjectToClipPos(v.vertex);
    45.                 o.uv = TRANSFORM_TEX(v.uv, _MainTex);
    46.                 UNITY_TRANSFER_FOG(o,o.vertex);
    47.                 return o;
    48.             }
    49.          
    50.             fixed4 frag (v2f i) : SV_Target
    51.             {
    52.                 // sample the texture
    53.                 fixed4 col = tex2D(_MainTex, i.uv);
    54.                 // apply fog
    55.                 UNITY_APPLY_FOG(i.fogCoord, col);
    56.                 return col;
    57.             }
    58.             ENDCG
    59.         }
    60.     }
    61. }
    62.  
    Third shader combining the two (this is the one you'll use on a material)
    Code (csharp):
    1.  
    2. Shader "Outline/Full" {
    3.     Properties {
    4.         _MainTex ("Base (RGB)", 2D) = "white" {}
    5.  
    6.         _OutlineColor("Outline Color", Color) = (0,0,0,1)
    7.         _Outline("Outline width", Range(0.0, 0.5)) = .005
    8.     }
    9.  
    10.     SubShader {
    11.         Tags { "Queue" = "Transparent" }
    12.         UsePass "Unlit/UnlitBasic/BASIC"
    13.         UsePass "Outlined/Blocked Silhouette Only ZTest Fail/BASE"
    14.         UsePass "Outlined/Blocked Silhouette Only ZTest Fail/BLOCK"
    15.     }
    16.  
    17.     Fallback "Diffuse"
    18. }
    19.  
    20.  
     
    rakkarage likes this.