-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
96 lines (73 loc) · 2.26 KB
/
index.html
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>2D Julia set on the GPU</title>
<style>
body { background-color: #000; margin: 0; }
#main { background: #000; margin: auto auto; display: block; }
</style>
</head>
<body>
<canvas id="main"></canvas>
<script id="vs-copy-position" type="x-shader/x-vertex">
// Attributes
attribute vec3 a_VertexPosition;
// Varyings
varying vec2 v_Position;
void main()
{
v_Position = vec2(a_VertexPosition.xy);
gl_Position = vec4(v_Position.xy, 0.0, 1.0);
}
</script>
<script id="fs-julia" type="x-shader/x-fragment">
#ifdef GL_ES
precision highp float;
#endif
// Uniforms
uniform vec2 u_JuliaConstant;
uniform vec2 u_Offset;
uniform float u_Zoom;
uniform float u_ColorShift;
// Varyings
varying vec2 v_Position;
// Constants
const int MAX_ITERATIONS = 1000;
const float ESCAPE_THRESHOLD = 4.0;
void main()
{
// Offset and scale the current point
vec2 z = vec2(v_Position.x, v_Position.y) * u_Zoom + u_Offset;
// Starting color
vec4 color = vec4(0.0, 0.0, 0.0, 1.0);
for (int i = 0; i < MAX_ITERATIONS; ++i)
{
// z^2 = (a + bi)^2 = (a^2 - b^2) + (2ab)i
z = vec2(z.x * z.x - z.y * z.y, 2.0 * z.x * z.y) + u_JuliaConstant;
// |z| > ESCAPE_THRESHOLD is unbound
if (dot(z, z) > ESCAPE_THRESHOLD)
{
// Bail
break;
}
if (i == (MAX_ITERATIONS - 1))
{
color = vec4(0.05, 0.05, 0.05, 1.0);
}
else
{
color.r = abs((0.7 - u_ColorShift)) * 125.0 * float(i) / float(MAX_ITERATIONS);
color.g = abs((0.2 - u_ColorShift)) * 210.0 * float(i) / float(MAX_ITERATIONS);
color.b = abs((0.6 - u_ColorShift)) * 225.0 * float(i) / float(MAX_ITERATIONS);
color.a = 1.0;
}
}
gl_FragColor = color;
}
</script>
<script src="main.js"></script>
</body>
</html>