-
Notifications
You must be signed in to change notification settings - Fork 69
/
mandlebrot-shader.html
203 lines (160 loc) · 4.45 KB
/
mandlebrot-shader.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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
<!DOCTYPE html>
<html>
<head>
<title>A-Frame - custom shaders</title>
<meta name="description" content="A-Frame - custom shaders">
<script src="js/aframe-master-v1.3.0.min.js"></script>
<script src="js/aframe-environment-component.js"></script>
<script src="js/controller-listener.js"></script>
<script src="js/player-move.js"></script>
<script src="js/raycaster-extras.js"></script>
</head>
<body>
<script>
AFRAME.registerShader('mandlebrot', {
schema:
{
timeMsec: {type: 'time', is: 'uniform'},
tex: {type: 'map', is: 'uniform'},
},
vertexShader: `
varying vec2 vUv;
void main()
{
vUv = uv;
gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
}
`,
fragmentShader: `
varying vec2 vUv;
uniform sampler2D tex;
uniform float timeMsec;
void main()
{
vec2 c = 2.0 * (vUv - 0.5) - vec2(0.5, 0);
vec2 v = vec2(0,0);
float m = 0.0;
const float r = 5.0;
for (int n = 0; n < 128; ++n)
{
v = vec2(v.x * v.x - v.y * v.y, v.x * v.y * 2.0) + c;
// measure how quickly point escapes
if (dot(v, v) < (r * r - 1.0))
{
m++;
}
v = clamp(v, -r, r);
}
if (m > 127.0)
gl_FragColor = vec4(0.06, 0, 0.125, 0.80);
else
gl_FragColor = texture2D(tex, vec2(0, 1.0 - m / 128.0));
}
`,
});
AFRAME.registerShader('julia', {
schema:
{
timeMsec: {type: 'time', is: 'uniform'},
tex: {type: 'map', is: 'uniform'},
c: {type: 'vec2', is: 'uniform', default: {x:0.0, y:0.0}},
},
vertexShader: `
varying vec2 vUv;
void main()
{
vUv = uv;
gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
}
`,
fragmentShader: `
varying vec2 vUv;
uniform vec2 c;
uniform sampler2D tex;
uniform float timeMsec;
void main()
{
vec2 v = 3.0 * (vUv - 0.5) - vec2(0, 0);
float m = 0.0;
const float r = 5.0;
for (int n = 0; n < 128; ++n)
{
v = vec2(v.x * v.x - v.y * v.y, v.x * v.y * 2.0) + c;
// measure how quickly point escapes
if (dot(v, v) < (r * r - 1.0))
{
m++;
}
v = clamp(v, -r, r);
}
if (m > 127.0)
gl_FragColor = vec4(0.06, 0, 0.125, 0.80);
else
gl_FragColor = texture2D(tex, vec2(0, 1.0 - m / 128.0));
}
`,
});
AFRAME.registerComponent("c-mover", {
init: function()
{
this.clock = new THREE.Clock();
this.sphere = document.getElementById("cSphere");
this.juliaPlane = document.getElementById("julia-plane");
},
tick: function()
{
let amplitude = 0.7;
let speedMult = 0.1;
this.clock.getDelta();
let time = this.clock.elapsedTime;
let x = amplitude * Math.sin(1.32 * time * speedMult);
let y = amplitude * Math.cos(1.00 * time * speedMult);
this.sphere.object3D.position.set(x,y,0);
this.juliaPlane.object3D.children[0].material.uniforms.c.value.set(x - 0.5, y); // -0.5: mandlebrot image shifted left
},
});
</script>
<a-scene environment>
<a-assets>
<img id="gradientRainbow" src="images/gradient-rainbow.png"/>
<img id="gradient" src="images/gradient-fade.png" />
</a-assets>
<a-sky color = "#000337"></a-sky>
<a-entity
id="player"
position="0 0 0"
player-move="controllerListenerId: #controller-data;
navigationMeshClass: environmentGround;">
<a-camera></a-camera>
<a-entity
id="controller-data"
controller-listener="leftControllerId: #left-controller;
rightControllerId: #right-controller;">
</a-entity>
<a-entity
id="left-controller"
oculus-touch-controls="hand: left">
</a-entity>
<a-entity
id="right-controller"
oculus-touch-controls="hand: right"
raycaster="objects: .raycaster-target, .environmentGround;"
raycaster-extras="controllerListenerId: #controller-data;
beamImageSrc: #gradient; beamLength: 0.5;">
</a-entity>
</a-entity>
<a-plane
position = "-1.1 1 -2"
width="2" height="2"
material = "shader: mandlebrot; tex: #gradientRainbow; transparent: true; side: double;">
<a-sphere id="cSphere" radius="0.02" c-mover></a-sphere>
</a-plane>
<a-plane
id="julia-plane"
position = "1.1 1 -2"
width="2" height="2"
material = "shader: julia; tex: #gradientRainbow; transparent: true; side: double;">
</a-plane>
</a-scene>
</body>
</html>