Guest User

SpriteOutlineOverlay.shader

a guest
May 20th, 2020
473
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.  
  8.         [Header(Outline)]
  9.         [MaterialToggle] JustOutline("JustOutline", Float) = 0
  10.         [MaterialToggle] TexturedOutline("TexturedOutline", Float) = 0
  11.         _Brightness("Outline Brightness", Range(0,8)) = 3.2
  12.         _Width("Outline Width", Range(0,0.05)) = 0.003
  13.         _OutlineTex("Outline Texture", 2D) = "white" {}
  14.         _SpeedX("Scroll Speed X", Range(-20,20)) = 0.003
  15.         _SpeedY("Scroll Speed Y", Range(-20,20)) = 0.003
  16.         _OutlineColor("OutlineColor", Color) = (1,1,1,1)
  17.  
  18.         [Header(Overlay)]
  19.         _OverlayTex("Overlay Texture", 2D) = "white" {}
  20.         _OverlayScale("Overlay Scale", Range(0,0.5)) = 0.003
  21.         _Intensity("Overlay Intensity", Range(0,4)) = 0.003
  22.         _Tint("Overlay Tint", Color) = (1,1,1,1)
  23.        
  24.         // stencil for (UI) Masking
  25.         _StencilComp("Stencil Comparison", Float) = 8
  26.         _Stencil("Stencil ID", Float) = 0
  27.         _StencilOp("Stencil Operation", Float) = 0
  28.         _StencilWriteMask("Stencil Write Mask", Float) = 255
  29.         _StencilReadMask("Stencil Read Mask", Float) = 255
  30.         _ColorMask("Color Mask", Float) = 15
  31.     }
  32.  
  33.         SubShader
  34.         {
  35.             Tags
  36.             {
  37.                 "Queue" = "Transparent"
  38.                 "IgnoreProjector" = "True"
  39.                 "RenderType" = "Transparent"
  40.                 "PreviewType" = "Plane"
  41.                 "CanUseSpriteAtlas" = "True"
  42.             }
  43.             // stencil for (UI) Masking
  44.              Stencil
  45.             {
  46.                 Ref[_Stencil]
  47.                 Comp[_StencilComp]
  48.                 Pass[_StencilOp]
  49.                 ReadMask[_StencilReadMask]
  50.                 WriteMask[_StencilWriteMask]
  51.             }
  52.             Cull Off
  53.             Lighting Off
  54.             ZWrite Off
  55.             Blend One OneMinusSrcAlpha
  56.  
  57.  
  58.             Pass
  59.             {
  60.             CGPROGRAM
  61.                 #pragma vertex vert
  62.                 #pragma fragment frag
  63.                 #pragma multi_compile _ JUSTOUTLINE_ON
  64.                 #pragma multi_compile _ TEXTUREDOUTLINE_ON
  65.                 #include "UnityCG.cginc"
  66.  
  67.                 struct appdata_t
  68.                 {
  69.                     float4 vertex   : POSITION;
  70.                     float4 color    : COLOR;
  71.                     float2 texcoord : TEXCOORD0;
  72.                 };
  73.  
  74.                 struct v2f
  75.                 {
  76.                     float4 vertex   : SV_POSITION;
  77.                     fixed4 color : COLOR;
  78.                     float2 texcoord  : TEXCOORD0;
  79.                     float4 screenPos : TEXCOORD1;
  80.                 };
  81.  
  82.  
  83.                 v2f vert(appdata_t IN)
  84.                 {
  85.                     v2f OUT;
  86.                      UNITY_INITIALIZE_OUTPUT(v2f,OUT)
  87.                     OUT.vertex = UnityObjectToClipPos(IN.vertex);
  88.                     OUT.texcoord = IN.texcoord;
  89.                     OUT.color = IN.color;
  90.                     OUT.screenPos = ComputeScreenPos(IN.vertex);
  91.                     return OUT;
  92.                 }
  93.  
  94.                 fixed4 _Color;
  95.                 fixed4 _OutlineColor;
  96.                 float4 _Tint;
  97.                 sampler2D _MainTex;
  98.                 sampler2D _OutlineTex, _OverlayTex;
  99.                 float _Brightness;
  100.                 float _Width;
  101.                 float _SpeedX, _SpeedY, _OverlayScale, _Intensity;
  102.  
  103.                 fixed4 frag(v2f IN) : SV_Target
  104.                 {
  105.                     float4 screenPos = IN.screenPos / IN.screenPos.w;
  106.                     fixed4 c = tex2D(_MainTex, IN.texcoord) * _Color;
  107.                     fixed4 t = tex2D(_OutlineTex, float2(IN.texcoord.x + (_Time.x * _SpeedX), IN.texcoord.y + (_Time.x * _SpeedY))) * _Color;
  108.                     c.rgb *= c.a;
  109.  
  110.                     // Move sprite in 4 directions according to width, we only care about the alpha
  111.                     float spriteLeft = tex2D(_MainTex, IN.texcoord + float2(_Width, 0)).a;
  112.                     float spriteRight = tex2D(_MainTex, IN.texcoord - float2(_Width,  0)).a;
  113.                     float spriteBottom = tex2D(_MainTex, IN.texcoord + float2(0 ,_Width)).a;
  114.                     float spriteTop = tex2D(_MainTex, IN.texcoord - float2(0 , _Width)).a;
  115.  
  116.                     // then combine
  117.                     float result = (spriteRight + spriteLeft + spriteTop + spriteBottom);
  118.                     // delete original alpha to only leave outline
  119.                     result *= (1 - c.a);
  120.                     // add color and brightness
  121.                     float4 outlines = result * _OutlineColor* _Brightness;
  122.  
  123.                     #ifdef TEXTUREDOUTLINE_ON
  124.                     outlines *= t;
  125.                     #endif
  126.                     #ifdef JUSTOUTLINE_ON
  127.                     // only show outlines
  128.                     c = outlines;
  129.                     #else
  130.                     // show outlines +sprite
  131.                     c.rgb = c.rgb  + outlines;
  132.                     #endif
  133.  
  134.  
  135.                     float3 overlay = tex2D(_OverlayTex, float2(screenPos.x * _OverlayScale, screenPos.y* _OverlayScale));
  136.  
  137.                     overlay *= _Tint;
  138.                     overlay *= c.a;
  139.  
  140.                     c.rgb += (overlay * _Intensity);
  141.                     c *= IN.color.a;
  142.  
  143.                     return  c;
  144.                 }
  145.             ENDCG
  146.             }
  147.         }
  148. }
Add Comment
Please, Sign In to add comment