-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfrag.glsl
78 lines (56 loc) · 1.94 KB
/
frag.glsl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/* Fragment shader that draws a fancy animation */
precision highp float;
varying vec2 vPosition;
uniform float uTime;
/* colors used in the shader */
uniform vec4 uColPrimary;
uniform vec4 uColPop;
#define M_PI 3.1415926535897932384626433832795
/* Returns the color (RGBA) for the fragment, at time "t" (in seconds) */
vec4 getColor() {
float t = uTime;
const float PERIOD_SHAPE = 10.0; // seconds
const float PERIOD_ROTATE = 40.0; // seconds
const float ZOOM = 0.8;
// Convert to polar
float r = length(vPosition.xy);
float theta = atan(vPosition.y, vPosition.x);
// Rotate with time
theta += 2.0 * M_PI * t/PERIOD_ROTATE;
// If we're outside a disk of radius 1, leave pixel transparent
if (r >= 1.0) {
return vec4(0.0);
}
// give it a spherical look by taking r as being the angle of a point
// on a sphere
r = asin(r);
// Pattern zoom
r = r / ZOOM;
// Convert back to cartesian
vec2 p = vec2(r * cos(theta), r * sin(theta));
// delta in the animation found empirically (though with known period)
float delta = 2.095 + 0.030 * sin(t * 2.0 * M_PI / PERIOD_SHAPE);
// Adapted from https://youtu.be/8bbTkNZYdQ8
for (float i = 0.0; i < 128.0; i += 1.0) {
p = 1.03 * (abs(p) - 0.6);
p *= mat2(cos(delta), -sin(delta), sin(delta), cos(delta));
}
// Initialize the pixel as transparent
vec3 rgb = vec3(0.0);
float alpha = 0.0;
// Find some nice spots (empirically) & make output only 2 colors
if(1.2 * length(p) >= 0.8) {
rgb = uColPrimary.rgb;
alpha = uColPrimary.a;
} else if (length(p + vec2(0.2, -0.1)) <= 0.5) {
rgb = uColPop.rgb;
alpha = uColPop.a;
}
// Add subtle shading
// (light in top-left and dark in bottom right)
rgb -= cos(atan(vPosition.y, vPosition.x) + M_PI/4.0) * r / 15.;
return vec4(alpha * rgb, alpha);
}
void main() {
gl_FragColor = getColor();
}