-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGeometrical Element (Plate 4 - 12) c.frag
182 lines (154 loc) · 4.78 KB
/
Geometrical Element (Plate 4 - 12) c.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/* Main function, uniforms & utils */
#ifdef GL_ES
precision highp float;
#endif
uniform vec2 u_resolution;
uniform vec2 u_mouse;
uniform float u_time;
#define HALF_PI 1.5707963267948966
#define PI 3.14159265358979323846
#define TWO_PI 6.28318530718
/* Coordinate and unit utils */
vec2 coord(in vec2 p) {
p = p / u_resolution.xy;
// correct aspect ratio
if (u_resolution.x > u_resolution.y) {
p.x *= u_resolution.x / u_resolution.y;
p.x += (u_resolution.y - u_resolution.x) / u_resolution.y / 2.0;
} else {
p.y *= u_resolution.y / u_resolution.x;
p.y += (u_resolution.x - u_resolution.y) / u_resolution.x / 2.0;
}
// centering
p -= 0.5;
p *= vec2(-1.0, 1.0);
return p;
}
#define rx 1.0 / min(u_resolution.x, u_resolution.y)
#define uv gl_FragCoord.xy / u_resolution.xy
#define st coord(gl_FragCoord.xy)
#define mx coord(u_mouse)
/* Signed distance drawing methods */
float fill(in float d) { return 1.0 - smoothstep(0.0, rx * 2.0, d); }
float stroke(in float d, in float t) { return 1.0 - smoothstep(t - rx * 1.5, t + rx * 1.5, abs(d)); }
vec3 draw(in sampler2D t, in vec2 pos, in vec2 w) { vec2 s = w / 1.0; s.x *= -1.0; return texture2D(t, pos / s + 0.5).rgb; }
/* Tiling function */
vec2 tile(in vec2 p, vec2 w) { return fract(mod(p + w / 2.0, w)) - (w / 2.0); }
vec2 tile(in vec2 p, float w) { return tile(p, vec2(w)); }
/* Shape 2D segment */
float sSegment(in vec2 a, in vec2 b) {
vec2 ba = a - b;
float d = clamp(dot(a, ba) / dot(ba, ba), 0.0, 1.0);
return length(a - ba * d) * 2.0;
}
float segment(in vec2 a, in vec2 b, float t) {
float d = sSegment(a, b);
return stroke(d, t);
}
/* Shape 2D line */
float sLine(in vec2 a, in vec2 b) {
vec2 p = b - a;
float d = abs(dot(normalize(vec2(p.y, -p.x)), a));
return d * 2.0;
}
float line(in vec2 a, in vec2 b) {
float d = sLine(a, b);
return fill(d);
}
float line(in vec2 a, in vec2 b, in float t) {
float d = sLine(a, b);
return stroke(d, t);
}
float line(in vec2 p, in float a, in float t) {
vec2 b = p + vec2(sin(a), cos(a));
return line(p, b, t);
}
/* Shape 2D grid */
float grid(in vec2 p, in float w) {
vec2 l = tile(p, w);
float d = 0.0;
d += line(l, l + vec2(0.0, 0.1), 0.002);
d += line(l, l + vec2(0.1, 0.0), 0.002);
d *= 0.2;
p = tile(p, vec2(w * 5.0));
float s = w / 10.0;
float g = 0.0;
g += segment(p + vec2(-s, 0.0), p + vec2(s, 0.0), 0.004);
g += segment(p + vec2(0.0, -s), p + vec2(0.0, s), 0.004);
return d + g;
}
/* Shape 2D arc */
float sArc(in vec2 p, in float w, in float s, in float e) {
float a = distance(p, w * 0.5 * vec2(cos(s), sin(s)));
float x = -PI;
p *= mat2(cos(x - s), -sin(x - s), sin(x - s), cos(x - s));
float b = clamp(atan(p.y, p.x), x, x + e);
b = distance(p, w * 0.5 * vec2(cos(b), sin(b)));
return min(a, b) * 2.0;
}
float arc(in vec2 p, in float w, in float s, in float e, in float t) {
float d = sArc(p, w, s, e);
return stroke(d, t);
}
/* Shape 2D rect */
float sRect(in vec2 p, in vec2 w) {
float d = max(abs(p.x / w.x), abs(p.y / w.y)) * 2.0;
float m = max(w.x, w.y);
return d * m - m;
}
float rect(in vec2 p, in vec2 w) {
float d = sRect(p, w);
return fill(d);
}
float rect(in vec2 p, in vec2 w, in float t) {
float d = sRect(p, w);
return stroke(d, t);
}
/* Rotate 2D */
vec2 rotate(vec2 _st, float _angle) {
// _st -= 0.5;
_st = mat2(cos(_angle), -sin(_angle),
sin(_angle), cos(_angle)) * _st;
// _st += 0.5;
return _st;
}
/* Geometrical Element */
float wave(vec2 p, float d, float width) {
float w = 0.0;
p.y -= 0.1;
w += arc(p, d, HALF_PI, HALF_PI, width);
p.x += 0.1;
w += arc(p, d, HALF_PI + PI, HALF_PI, width);
p.y += 0.1;
w += arc(p, d, HALF_PI, HALF_PI, width);
return w;
}
float twelve(vec2 p, float width) {
float m = 0.0;
float d = fract(PI) / (1.41 + width);
for(int i = 0; i < 4; i++) {
m += wave(p, d, width);
p = rotate(p, HALF_PI);
}
return m;
}
void main() {
// Use polar coordinates instead of cartesian
vec2 toCenter = st * vec2(PI);
float angle = atan(toCenter.y, toCenter.x);
float radius = length(toCenter) * 2.0;
vec2 p = toCenter;
float n = HALF_PI/(TWO_PI - HALF_PI);
p = rotate(toCenter, TWO_PI + abs(u_time / 2.0));
p = tile(p, vec2(n));
vec3 color = vec3(0.0);
color -= rect(p, vec2(0.2), 0.01);
color -= twelve(p, 0.01 / cos(abs(radius - u_time * 2.)));
color.r += sin(p.x + angle);
color.g += cos(p.y);
color.b += .7;
mat3 rgb2yuv = mat3(0.2126, 0.7152, 0.0722,
-0.09991, -0.33609, 0.43600,
0.615, -0.5586, -0.05639);
gl_FragColor = vec4(color + rgb2yuv[2], 1.0);
}