Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

2D Silhouette Shader

Discussion in 'Shaders' started by charles_xl, Jul 13, 2019.

  1. charles_xl

    charles_xl

    Joined:
    Oct 4, 2016
    Posts:
    87
    Hey all!

    I try not to post unless I'm really stuck on something, I'm trying to create a shader for 2D sprites similar to http://i2.wp.com/ruinofthereckless.com/wp-content/uploads/2016/04/silhouette2.gif. A good example can also be found in Hyper Light Drifter when traveling behind an object that is meant to be "see-through"

    I currently have it working for 3D but can't figure out why it won't also work for 2D. Here is the code:

    Code (CSharp):
    1. Shader "Sprites/Silhouette"
    2. {
    3.     Properties
    4.     {
    5.         [PerRendererData] _MainTex ("Texture", 2D) = "white" {}
    6.         _StencilComp ("Stencil Comparison", Float) = 8
    7.         _Stencil ("Stencil ID", Float) = 0
    8.         _StencilOp ("Stencil Operation", Float) = 0
    9.         _StencilWriteMask ("Stencil Write Mask", Float) = 255
    10.         _StencilReadMask ("Stencil Read Mask", Float) = 255
    11.  
    12.         _ColorMask ("Color Mask", Float) = 15
    13.     }
    14.  
    15.     SubShader
    16.     {
    17.         Tags
    18.         {
    19.             "Queue"="Transparent"
    20.             "IgnoreProjector"="True"
    21.             "RenderType"="Transparent"
    22.             "PreviewType"="Plane"
    23.             "CanUseSpriteAtlas"="True"
    24.         }
    25.  
    26.         Stencil
    27.         {
    28.             Ref [_Stencil]
    29.             Comp [_StencilComp]
    30.             Pass [_StencilOp]
    31.             ReadMask [_StencilReadMask]
    32.             WriteMask [_StencilWriteMask]
    33.         }
    34.  
    35.         Cull Off
    36.         Lighting Off
    37.         ZWrite Off
    38.         Blend SrcAlpha OneMinusSrcAlpha
    39.         ColorMask [_ColorMask]
    40.  
    41.         Pass
    42.         {
    43.         CGPROGRAM
    44.             #pragma vertex vert
    45.             #pragma fragment frag
    46.             #pragma target 2.0
    47.             #include "UnityCG.cginc"
    48.        
    49.             struct appdata_t
    50.             {
    51.                 float4 vertex   : POSITION;
    52.                 float4 color    : COLOR;
    53.                 float2 texcoord : TEXCOORD0;
    54.             };
    55.  
    56.             struct v2f
    57.             {
    58.                 float4 vertex   : SV_POSITION;
    59.                 fixed4 color    : COLOR;
    60.                 float2 texcoord  : TEXCOORD0;
    61.             };
    62.  
    63.             v2f vert(appdata_t IN)
    64.             {
    65.                 v2f OUT;
    66.                 OUT.vertex = UnityObjectToClipPos(IN.vertex);
    67.                 OUT.texcoord = IN.texcoord;
    68.                 OUT.color = IN.color;
    69.  
    70.                 return OUT;
    71.             }
    72.  
    73.             sampler2D _MainTex;
    74.  
    75.             fixed4 frag(v2f IN) : SV_Target
    76.             {
    77.                 half4 c = tex2D(_MainTex, IN.texcoord) * IN.color;
    78.                 return c;
    79.             }
    80.         ENDCG
    81.         }
    82.  
    83.         Pass
    84.         {
    85.             ZTest Greater
    86.  
    87.         CGPROGRAM
    88.             #pragma vertex vert
    89.             #pragma fragment frag
    90.             #pragma target 2.0
    91.             #include "UnityCG.cginc"
    92.        
    93.             struct appdata_t
    94.             {
    95.                 float4 vertex   : POSITION;
    96.             };
    97.  
    98.             struct v2f
    99.             {
    100.                 float4 vertex   : SV_POSITION;
    101.             };
    102.  
    103.             v2f vert(appdata_t IN)
    104.             {
    105.                 v2f OUT;
    106.                 OUT.vertex = UnityObjectToClipPos(IN.vertex);
    107.  
    108.                 return OUT;
    109.             }
    110.  
    111.             sampler2D _MainTex;
    112.  
    113.             fixed4 frag(v2f IN) : SV_Target
    114.             {
    115.                 return fixed4(0, 1, 0, 1);
    116.             }
    117.         ENDCG
    118.         }
    119.     }
    120. }
    121.  
    Any help on this would be much appreciated!
     
  2. CaptainJeoy

    CaptainJeoy

    Joined:
    Sep 20, 2017
    Posts:
    2
    Hey buddy! Check out this tutorial I made on a simpler trick (without shaders) to achieving this effect in 2d:
     
  3. rubendariohh

    rubendariohh

    Joined:
    Oct 14, 2017
    Posts:
    32
    Hi, did you managed to make the shader works for 2D?
    I have tried everything without luck.