Skip to content

Instantly share code, notes, and snippets.

@o8que
Created July 13, 2018 03:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save o8que/7306c4505ffcacda0e50f529c7d4d064 to your computer and use it in GitHub Desktop.
Save o8que/7306c4505ffcacda0e50f529c7d4d064 to your computer and use it in GitHub Desktop.
Shader "Sprites/OutlineMeter"
{
Properties
{
[PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
_Color ("Tint", Color) = (1,1,1,1)
[MaterialToggle] PixelSnap ("Pixel snap", Float) = 0
[HideInInspector] _RendererColor ("RendererColor", Color) = (1,1,1,1)
[HideInInspector] _Flip ("Flip", Vector) = (1,1,1,1)
[PerRendererData] _AlphaTex ("External Alpha", 2D) = "white" {}
[PerRendererData] _EnableExternalAlpha ("Enable External Alpha", Float) = 0
[Header(OutlineMeterProperties)]
_MeterValue ("Meter Value", Range(0, 1)) = 1
_OutlineColor ("Outline Color", Color) = (1,1,0,1)
}
SubShader
{
Tags
{
"Queue"="Transparent"
"IgnoreProjector"="True"
"RenderType"="Transparent"
"PreviewType"="Plane"
"CanUseSpriteAtlas"="True"
}
Cull Off
Lighting Off
ZWrite Off
Blend One OneMinusSrcAlpha
Pass
{
CGPROGRAM
#pragma vertex SpriteVert
#pragma fragment frag
#pragma target 2.0
#pragma multi_compile_instancing
#pragma multi_compile _ PIXELSNAP_ON
#pragma multi_compile _ ETC1_EXTERNAL_ALPHA
#include "UnitySprites.cginc"
float4 _MainTex_TexelSize;
float _MeterValue;
fixed4 _OutlineColor;
fixed4 frag(v2f IN) : SV_Target
{
fixed4 t = SampleSpriteTexture(IN.texcoord);
float y = IN.texcoord.y - fmod(IN.texcoord.y, _MainTex_TexelSize.y);
if (t.a == 0 && y < _MeterValue) {
fixed4 l = SampleSpriteTexture(IN.texcoord - fixed2(_MainTex_TexelSize.x, 0));
fixed4 r = SampleSpriteTexture(IN.texcoord + fixed2(_MainTex_TexelSize.x, 0));
fixed4 u = SampleSpriteTexture(IN.texcoord + fixed2(0, _MainTex_TexelSize.y));
fixed4 d = SampleSpriteTexture(IN.texcoord - fixed2(0, _MainTex_TexelSize.y));
if ((l.a + r.a + u.a + d.a) > 0) {
return _OutlineColor;
}
}
fixed4 c = t * IN.color;
c.rgb *= c.a;
return c;
}
ENDCG
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment