-
Notifications
You must be signed in to change notification settings - Fork 0
/
gradient resonance.frag
102 lines (79 loc) · 2.61 KB
/
gradient resonance.frag
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#ifdef GL_ES
precision mediump float;
#endif
uniform vec2 u_resolution;
uniform vec2 u_mouse;
uniform float u_time;
// From https://github.com/Jam3/glsl-hsl2rgb
float hue2rgb(float f1, float f2, float hue) {
if (hue < 0.0)
hue += 1.0;
else if (hue > 1.0)
hue -= 1.0;
float res;
if ((6.0 * hue) < 1.0)
res = f1 + (f2 - f1) * 6.0 * hue;
else if ((2.0 * hue) < 1.0)
res = f2;
else if ((3.0 * hue) < 2.0)
res = f1 + (f2 - f1) * ((2.0 / 3.0) - hue) * 6.0;
else
res = f1;
return res;
}
vec3 hsl2rgb(vec3 hsl) {
vec3 rgb;
if (hsl.y == 0.0) {
rgb = vec3(hsl.z); // Luminance
} else {
float f2;
if (hsl.z < 0.5)
f2 = hsl.z * (1.0 + hsl.y);
else
f2 = hsl.z + hsl.y - hsl.y * hsl.z;
float f1 = 2.0 * hsl.z - f2;
rgb.r = hue2rgb(f1, f2, hsl.x + (1.0/3.0));
rgb.g = hue2rgb(f1, f2, hsl.x);
rgb.b = hue2rgb(f1, f2, hsl.x - (1.0/3.0));
}
return rgb;
}
vec3 hsl2rgb(float h, float s, float l) {
return hsl2rgb(vec3(h, s, l));
}
// Gradient Resonance
// The background is a simple gradient from top to bottom.
vec4 bg(vec2 st) {
vec2 mouse_uv = u_mouse.xy / u_resolution.xy;
float hue_top = mod(mouse_uv.x, 1.);
float hue_bottom = mod(mouse_uv.x + mouse_uv.y, 1.);
vec3 color_bottom = hsl2rgb(vec3(hue_bottom, 1., 0.5));
vec3 color_top = hsl2rgb(vec3(hue_top, 1., 0.5));
vec3 color = st.y * color_top + (1. - st.y) * color_bottom;
return vec4(color, 1.);
}
// We draw a grid of squares spaced by unit width. Each column is slightly
// offset along the vertical. Each square has the same gradient as the
// background.
vec4 square(vec2 st) {
float scale = 0.15;
float offset_x = 0.106;
float column = (st.x + offset_x) / scale;
float offset_y = offset_x + 0.09 * floor(column);
float row = (st.y + offset_y) / scale;
// TODO: recalc as inset squares over whole grid. experiment with spacing
vec2 square_st = vec2(column - floor(column), row - floor(row));
float a = floor((mod(floor(column), 2.) + mod(floor(row), 2.)) / 2.);
return vec4(bg(square_st).xyz, a);
}
// The painting is a composition of the squares on top of the background.
void main() {
vec2 st = gl_FragCoord.xy/u_resolution.y;
if (u_resolution.x < u_resolution.y) {
st = gl_FragCoord.xy/u_resolution.x;
}
vec4 bg = bg(st);
vec4 square = square(st);
vec3 color = (bg.xyz * (1. - square.a)) + (square.xyz * square.a);
gl_FragColor = vec4(color, 1.);
}