Last active
August 12, 2019 23:19
-
-
Save wraikny/245c77bc1819e8626fcadf4b4409be1b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifdef GL_ES | |
precision mediump float; | |
#endif | |
// --- Utils --- | |
const float PI = 3.14159265; | |
vec2 fromRad(float rad) { | |
return vec2( | |
cos(rad), sin(rad) | |
); | |
} | |
float circle(vec2 p, float r) { | |
return (length(p) - r); | |
} | |
float rand(vec2 co, vec2 t) { | |
return fract(sin(dot(co.xy , t + vec2(12.9898, 78.233))) * (43758.5453)); | |
} | |
vec3 lerp(vec3 a, vec3 b, float t) { | |
return (1.0 - t) * a + t * b; | |
} | |
float max3(float a, float b, float c) {return max(max(a, b), c);} | |
// --- Property --- | |
// Looks | |
const float timeSpeed = 3.0; | |
const float zoom = 7.0; | |
const vec3 color1 = vec3(30, 30, 100); | |
const vec3 color2 = vec3(200, 255, 200); | |
// Move | |
const float ySpeed = 0.05; | |
const vec2 randomSpeed = vec2(0.2, 0.5); | |
const vec2 randomRange = vec2(0.2, 0.05); | |
const float patternSpeed = 0.2; | |
// Shape | |
const float repeatRate = 1.15; // Spread repeated interval and reduce failure | |
const float patternInterval = 0.05; | |
const float patternThreshold = 0.6; | |
// ---------------- | |
const float radius = 1.0; | |
float repeat = radius * repeatRate; | |
bool calc(float time, vec2 p0, float rnd) { | |
float r3 = 1.7320508; | |
float randN; | |
{ | |
float xn = floor((p0.x) / repeat / r3); | |
float yn = floor((p0.y + ySpeed * time) / repeat); | |
randN = | |
// Magic Numbers for pseudo random | |
rand(vec2(abs(xn), sin(yn)), vec2(59.0 - rnd, -124.4 + rnd)) | |
+ rand(vec2(abs(xn), sin(yn)), vec2(200.9 + rnd, 24.5 - rnd)) | |
+ rand(vec2(abs(xn), sin(yn)), vec2(63.5 - rnd, 124.2 + rnd)) | |
; | |
} | |
vec2 p; | |
{ | |
vec2 p_tmp = p0 + randomRange * sin(randN + time * randomSpeed); | |
float x = mod(p_tmp.x, repeat * r3) - repeat * r3 / 2.0; | |
float y = mod(p_tmp.y + ySpeed * time, repeat); | |
p = vec2(x, y); | |
} | |
vec2 a = fromRad(PI/6.0); | |
float d = max3 | |
( circle(p, radius) | |
, -circle(p - vec2(a.x, -a.y) * radius, radius) | |
, -circle(p - vec2(-a.x, -a.y) * radius, radius) | |
); | |
return ( | |
(d < 0.0) | |
&& ( | |
sin(length(p) / radius / patternInterval - time * patternSpeed) | |
> patternThreshold | |
) | |
); | |
} | |
vec4 main_(vec2 p, float time) { | |
float result; | |
{ | |
bool f1 = calc(time, p, 10.0); | |
bool f2 = calc(time, p + fromRad(PI/6.0) * repeat, 30.0); | |
result = (f1 || f2) ? 1.0 : 0.0; | |
} | |
return vec4(lerp(color2 / 255.0, color1 / 255.0, result), 1.0); | |
} | |
// --- System --- | |
uniform float time; | |
uniform vec2 resolution; | |
void main() { | |
vec2 p0 = gl_FragCoord.xy / min(resolution.x, resolution.y); | |
gl_FragColor = main_(p0 * zoom, time * timeSpeed); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment