Skip to content

Instantly share code, notes, and snippets.

@samloeschen
Last active November 15, 2019 16:25
Show Gist options
  • Save samloeschen/04d48f41351e86eff0b9c7b23f88d56b to your computer and use it in GitHub Desktop.
Save samloeschen/04d48f41351e86eff0b9c7b23f88d56b to your computer and use it in GitHub Desktop.
CrescentAnimation
Shader "Custom/Unlit/CrescentAnimation"
{
Properties
{
[PerInstanceData] _Anim ("Animation", Range(0, 1)) = 0
[PerInstanceData] _Flash ("Flash", Range(0, 1)) = 0
[PerInstanceData] _Color ("Color", Color) = (1,1,1,1)
[PerInstanceData] _OffsetDir ("Offset Direction", Vector) = (0.5, 0.5, 0, 0)
_Displacement ("Displacement", Float) = 0.65
_Softness ("Softness", Float) = 0.02
_MinMaskRadius ("Min Mask Radius", Float) = 0.15
_MaxMaskRadius ("Max Mask Radius", Float) = 0.5
}
SubShader
{
Tags { "RenderType"="Transparent" }
LOD 100
Blend One OneMinusSrcAlpha
Pass
{
HLSLPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile_instancing
#include "UnityCG.cginc"
#define PI 3.14159265359
UNITY_INSTANCING_BUFFER_START(Props)
UNITY_DEFINE_INSTANCED_PROP(half, _Anim)
UNITY_DEFINE_INSTANCED_PROP(half, _Flash)
UNITY_DEFINE_INSTANCED_PROP(half2, _OffsetDir)
UNITY_DEFINE_INSTANCED_PROP(half3, _Color)
UNITY_INSTANCING_BUFFER_END(Props)
uniform half _Displacement;
uniform half _Softness;
uniform half _MinMaskRadius;
uniform half _MaxMaskRadius;
struct appdata
{
float4 vertex : POSITION;
half2 uv : TEXCOORD0;
UNITY_VERTEX_INPUT_INSTANCE_ID
};
struct v2f
{
float4 vertex : SV_POSITION;
half2 uv : TEXCOORD0;
half3 offsetRadius : TEXCOORD1;
half3 color : TEXCOORD2;
};
half unlerp(half a, half b, half x)
{
return (x - a) / (b - a);
}
v2f vert (appdata v)
{
v2f o;
UNITY_SETUP_INSTANCE_ID(v);
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = v.uv;
half anim = UNITY_ACCESS_INSTANCED_PROP(Props, _Anim);
half t = sin(anim * PI);
o.offsetRadius = half3(
normalize(UNITY_ACCESS_INSTANCED_PROP(Props, _OffsetDir)) * lerp(-_Displacement, _Displacement, anim),
lerp(_MinMaskRadius, _MaxMaskRadius, saturate(unlerp(0.1, 0.9, t * t)))
);
half flash = UNITY_ACCESS_INSTANCED_PROP(Props, _Flash);
half3 mainColor = UNITY_ACCESS_INSTANCED_PROP(Props, _Color);
o.color = lerp(mainColor, step(0.66, flash), step(0.33, flash));
return o;
}
fixed4 frag (v2f i) : SV_Target
{
half circ1 = smoothstep(1.0, 1.0 - _Softness, length(i.uv - 0.5) * 2.0);
half2 offset = i.offsetRadius.xy;
half radius = i.offsetRadius.z;
half circ2 = 1.0 - smoothstep(radius, radius + _Softness, length(i.uv - 0.5 + offset));
return half4(i.color, 1.0) * circ1 - circ2;
}
ENDHLSL
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment