summaryrefslogtreecommitdiffstats
path: root/examples/square-in-square.frag
blob: 07343d88802e8a02c712d9c576fc840c49c2f05a (plain)
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
#version 330

out vec4 fragColor;

uniform vec2 res;
uniform float time;

const float sharpness = 300;

const float PI = 3.14159265358979323846;


mat2 rot(float a) {
	return mat2(
		cos(a), -sin(a),
		sin(a), cos(a)
	);
}

float circle(vec2 m, float r, vec2 p) {
	vec2 diff = m - p;
	float d = sqrt(dot(diff, diff));

	return clamp((r - d)*sharpness, 0.0, 1.0);
}

float circle_d(vec2 m, float r, vec2 p) {
	return circle(m, r, p) - circle(m, r - 1/sharpness, p);
}

float square(vec2 m, float a, float r, vec2 p) {
	vec2 diff = abs((m - p) * rot(r));
	float d = max(diff.x, diff.y);

	return clamp((a - d)*sharpness, 0.0, 1.0);
}

float square_d(vec2 m, float a, float r, vec2 p) {
	return square(m, a, r, p) - square(m, a - 1/sharpness, r, p);
}

void main(void) {
	vec2 m = res/2;
	float s = min(m.x, m.y);
	vec2 p = (gl_FragCoord.xy - m) / s;

	float scene = 0;

	int n = 20;
	float b = 1;
	float alpha = 0;
	for (int i = 0; i < n; i++) {
		float delta = mod(time/5000, PI/2);
		alpha = mod(alpha + delta, PI/2);
		b /= (sin(delta) + cos(delta));

		float c = square(vec2(0, 0), b, alpha, p);
		scene = abs(scene - c);
	}


	b = 1;
	alpha = 0;
	for (int i = 0; i < n; i++) {
		float delta = mod(-time/5000, PI/2);
		alpha = mod(alpha + delta, PI/2);
		b /= (sin(delta) + cos(delta));

		float c = square(vec2(0, 0), b, alpha, p);
		scene = abs(scene - c);
	}

	fragColor = vec4(vec3(1, 1, 1) * scene, 1);
}