Skip to content

Instantly share code, notes, and snippets.

@JLChnToZ
Created March 10, 2024 05:52
Show Gist options
  • Star 30 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save JLChnToZ/4e1486734b633faa22cb364bd20856ef to your computer and use it in GitHub Desktop.
Save JLChnToZ/4e1486734b633faa22cb364bd20856ef to your computer and use it in GitHub Desktop.
Shader "Unlit/PolyRhythmVisualizer" {
Properties {
_TimeCode ("Input Time", Float) = 0
_OuterRingFreq ("Outer Ring Frequency", Float) = 1
_InnerRingFreq ("Inner Ring Frequency", Float) = 0.922
_RingCount ("Ring Count", Int) = 35
_VibrantFreq ("Vibrant Frequency", Float) = 2
_Vibrant ("Vibrant", Range(0, 1)) = 0.5
_Decay ("Decay", Range(0, 10)) = 2
[Header(Cosine Gradiant)]
_Offset ("Offset", Vector) = (0.5, 0.5, 0.5, 0)
_Amp ("Amplitude", Vector) = (0.25, 0.25, 0.25, 0)
_Freq ("Frequency", Vector) = (0.7, 0.7, 0.7, 0)
_Phase ("Phase", Vector) = (0, 0.75, 0.25, 0)
}
SubShader {
Tags { "RenderType"="Opaque" }
LOD 100
Pass {
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile_fog
#include "UnityCG.cginc"
#define PI2 6.28318530718
struct appdata {
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f {
float2 uv : TEXCOORD0;
UNITY_FOG_COORDS(1)
float4 vertex : SV_POSITION;
};
float _TimeCode;
float _OuterRingFreq, _InnerRingFreq;
int _RingCount;
float _VibrantFreq;
float3 _Offset;
float3 _Amp;
float3 _Freq;
float3 _Phase;
float _Vibrant;
float _Decay;
half3 cosineGradiant(float v, float3 offset, float3 amp, float3 freq, float3 phase) {
return offset + amp * cos(PI2 * (freq * v + phase));
}
float signedCircularDistance(float a, float b) {
a = frac(a);
b = frac(b);
if (b < a) b++;
return b - a;
}
v2f vert (appdata v) {
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = v.uv;
UNITY_TRANSFER_FOG(o,o.vertex);
return o;
}
half3 calcColor (float2 uv) {
uv -= 0.5;
float angle = atan2(uv.x, uv.y) / PI2;
float distance = length(uv) * 2;
float steppedDistance = floor(distance * _RingCount) / _RingCount;
float t = lerp(_OuterRingFreq, _InnerRingFreq, steppedDistance) * _TimeCode;
float f = floor(_RingCount * 8 * steppedDistance + 4);
t = signedCircularDistance(t, angle);
return cosineGradiant(steppedDistance, _Offset, _Amp, _Freq, _Phase) *
max(0.01, pow(t, 4 + 4 * (steppedDistance + 1) * _Decay)) *
step(distance, 1) *
step(0.2, frac(distance * _RingCount)) *
(4 - _Vibrant * frac(_TimeCode * _VibrantFreq) * 3);
}
half4 frag (v2f i) : SV_Target {
half4 col = half4(0, 0, 0, 1);
float2 dd = float2(ddx(i.uv.x), ddy(i.uv.y));
col.rgb = (
calcColor(i.uv) + (
calcColor(i.uv + float2( dd.x, 0)) +
calcColor(i.uv + float2(-dd.x, 0)) +
calcColor(i.uv + float2(0, dd.y)) +
calcColor(i.uv + float2(0, -dd.y))
) * 0.5 + (
calcColor(i.uv + dd * 0.707) +
calcColor(i.uv - dd * 0.707) +
calcColor(i.uv + float2(dd.x, -dd.y) * 0.707) +
calcColor(i.uv + float2(-dd.x, dd.y) * 0.707)
) * 0.25
) * 0.333333;
return col;
}
ENDCG
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment