-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGeometricalElement1b.frag
119 lines (106 loc) · 3.34 KB
/
GeometricalElement1b.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
/* 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);
}
void main() {
vec3 color = vec3(
abs(cos(st.x + mx.x)),
abs(sin(st.y + mx.y)),
abs(sin(u_time))
);
vec2 p = st;
color += grid(p, .1);
float d = 0.4;
float s = 0.004;
p = tile(st, vec2(d, d + (s / d))); //TODO: add stroke options [center, inner]
color += arc(p, d, 0.0, PI, s);
gl_FragColor = vec4(color, 1.0);
}