Advertisement
Guest User

Untitled

a guest
Aug 13th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Shader "Sprites/Outline"
  2. {
  3.     Properties
  4.     {
  5.         [PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
  6.         _Color ("Tint", Color) = (1,1,1,1)
  7.         [MaterialToggle] PixelSnap ("Pixel snap", Float) = 0
  8.         [HideInInspector] _RendererColor ("RendererColor", Color) = (1,1,1,1)
  9.         [HideInInspector] _Flip ("Flip", Vector) = (1,1,1,1)
  10.         [PerRendererData] _AlphaTex ("External Alpha", 2D) = "white" {}
  11.         [PerRendererData] _EnableExternalAlpha ("Enable External Alpha", Float) = 0
  12.  
  13.         // Add values to determine if outlining is enabled and outline color.
  14.         [PerRendererData] _Outline("Outline", Float) = 0
  15.         [PerRendererData] _OutlineColor("Outline Color", Color) = (1,1,1,1)
  16.         [PerRendererData] _OutlineSize("Outline Size", int) = 1
  17.     }
  18.  
  19.     SubShader
  20.     {
  21.         Tags
  22.         {
  23.             "Queue"="Transparent"
  24.             "IgnoreProjector"="True"
  25.             "RenderType"="Transparent"
  26.             "PreviewType"="Plane"
  27.             "CanUseSpriteAtlas"="True"
  28.         }
  29.  
  30.         Cull Off
  31.         Lighting Off
  32.         ZWrite Off
  33.         Blend One OneMinusSrcAlpha
  34.  
  35.         Pass
  36.         {
  37.         CGPROGRAM
  38.             #pragma vertex SpriteVert
  39.             #pragma fragment frag
  40.             #pragma target 2.0
  41.             #pragma multi_compile_instancing
  42.             #pragma multi_compile _ PIXELSNAP_ON
  43.             #pragma multi_compile _ ETC1_EXTERNAL_ALPHA
  44.             #include "UnitySprites.cginc"
  45.  
  46.             float _Outline;
  47.             fixed4 _OutlineColor;
  48.             int _OutlineSize;
  49.             float4 _MainTex_TexelSize;
  50.  
  51.             fixed4 frag(v2f IN) : SV_Target
  52.             {
  53.                 fixed4 c = SampleSpriteTexture(IN.texcoord) * IN.color;
  54.  
  55.                 // If outline is enabled and there is a pixel, try to draw an outline.
  56.                 if (_Outline > 0 && c.a != 0) {
  57.                     float totalAlpha = 1.0;
  58.  
  59.                     [unroll(16)]
  60.                     for (int i = 1; i < _OutlineSize + 1; i++) {
  61.                         fixed4 pixelUp = tex2D(_MainTex, IN.texcoord + fixed2(0, i * _MainTex_TexelSize.y));
  62.                         fixed4 pixelDown = tex2D(_MainTex, IN.texcoord - fixed2(0,i *  _MainTex_TexelSize.y));
  63.                         fixed4 pixelRight = tex2D(_MainTex, IN.texcoord + fixed2(i * _MainTex_TexelSize.x, 0));
  64.                         fixed4 pixelLeft = tex2D(_MainTex, IN.texcoord - fixed2(i * _MainTex_TexelSize.x, 0));
  65.  
  66.                         totalAlpha = totalAlpha * pixelUp.a * pixelDown.a * pixelRight.a * pixelLeft.a;
  67.                     }
  68.  
  69.                     if (totalAlpha == 0) {
  70.                         c.rgba = fixed4(1, 1, 1, 1) * _OutlineColor;
  71.                     }
  72.                 }
  73.  
  74.                 c.rgb *= c.a;
  75.  
  76.                 return c;
  77.             }
  78.         ENDCG
  79.         }
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement