Skip to content

Instantly share code, notes, and snippets.

@daniellanner
Last active April 22, 2020 02:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daniellanner/e05bb789a4ff3c72a40af459a5940195 to your computer and use it in GitHub Desktop.
Save daniellanner/e05bb789a4ff3c72a40af459a5940195 to your computer and use it in GitHub Desktop.
Contracting Button - Unity shader done for a dribbble shot.
/**
(c) 2020 Daniel Lanner
This code is licensed under MIT license, do with it as you will
This shader creates a contracting button effect I used for a loading animation
*/
Shader "InDiversity/Unlit Frag/Contracting Button"
{
Properties
{
_MainTex("Main Tex", 2D) = "white" {} // this does nothing but is needed for Unity UI shader
_LeftColor("Left Color", Color) = (.2, .2, .2, 1) // screen space interpolated gradient, x = 0 Color
_RightColor("Right Color", Color) = (.3, .3, .3, 1) // screen space interpolated gradient, x = 1 Color
_BackColor("Background Color", Color) = (.1, .1, .1, 1) // paints background color, avoids transparency
_Contraction("Contraction", Range(0.0, 0.5)) = 0.2 // button contraction painted in uv.y axis
_SinOffset("Offset", float) = 1
_SinAmplitude("Amplitude", Range(-0.5, 0.5)) = 0.1
_SinFrequency("Frequency", Range(1.0, 24.0)) = 1.0
_EaseAmplitude("Ease Amplitude", Range(0.0, 1.0)) = 1.0 // fades out amplitude towards the horizontal sides of the button
_EaseContraction("Ease Contraction", Range(0.0, 1.0)) = 0.125 // fades out contraction towards the horizontal sides of the button
}
SubShader
{
Tags
{
"RenderType" = "Transparent"
"Queue" = "Transparent"
}
LOD 100
ZWrite Off
Blend SrcAlpha OneMinusSrcAlpha
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
#define PI 3.14159265
#define HALF_PI 1.57079632
#define TWO_PI 6.28318530
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float4 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
sampler2D _MainTex;
float4 _MainTex_ST;
fixed4 _LeftColor;
fixed4 _RightColor;
fixed4 _BackColor;
float _Contraction;
float _SinOffset;
float _SinAmplitude;
float _SinFrequency;
float _EaseAmplitude;
float _EaseContraction;
float GreaterThan(float a, float b)
{
return (sign(a - b) + 1.0) / 2.0;
}
float LessThan(float a, float b)
{
return 1.0 - GreaterThan(a, b);
}
v2f vert(appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv.xy = v.uv;
o.uv.zw = ComputeScreenPos(o.vertex);
return o;
}
fixed4 frag(v2f i) : SV_Target
{
float offset = saturate(_Contraction);
float sinwave = sin(i.uv.x * TWO_PI * _SinFrequency + _SinOffset);
sinwave *= _SinAmplitude;
fixed4 col = lerp(_LeftColor, _RightColor, (i.uv.z + i.uv.w) * .5);
float easeAmplitude = (sin(-HALF_PI + i.uv.x * TWO_PI) + 1) * .5;
float ease = lerp(1.0, easeAmplitude, _EaseAmplitude);
offset += offset * -easeAmplitude * _EaseContraction;
col.rgb = lerp(_BackColor, col.rgb, GreaterThan(i.uv.y + sinwave * ease, offset));
col.rgb = lerp(_BackColor, col.rgb, LessThan(i.uv.y + sinwave * ease, 1.0 - offset));
col.a = tex2D(_MainTex, i.uv).a;
col.a = saturate(col.a - (1 - GreaterThan(i.uv.y + sinwave * ease, offset)));
col.a = saturate(col.a - (1 - LessThan(i.uv.y + sinwave * ease, 1.0 - offset)));
return col;
}
ENDCG
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment