-
Notifications
You must be signed in to change notification settings - Fork 5
/
monkeys-hyp.html
457 lines (380 loc) · 13 KB
/
monkeys-hyp.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
<!DOCTYPE html>
<html lang="en">
<head>
<title>Quaternion Monkeys</title>
<!--
4D Monkeys
interactive webVR version (by Marc ten Bosch and Vi Hart) of a 4d monkey sculpture (by Henry Segerman and Will Segerman) inspired by work on 4 dimensional symmetry groups (by Henry Segerman and Vi Hart) inspired by work on 4d graphics (by Marc ten Bosch) and 3d modeling (by Will Segerman).
http://www.marctenbosch.com
http://vihart.com
http://www.segerman.org/
http://www.willsegerman.com/
It has oculus support for webVR browsers (thanks Mozilla!)
https://github.com/MozVR/vr-web-examples/tree/master/threejs-vr-boilerplate
And WASD + E/Q navigation support both in and out of VR (thanks, Andrea Hawksley!)
Enter to go into VR mode, S for sound,
Space to click links based on camera rotation
https://github.com/hawksley
-->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<style>
body {
background-color: #000;
color: #fff;
margin: 0px;
padding: 0;
overflow: hidden;
}
</style>
</head>
<body>
<audio id='music' src="media/monkeygif.mp3"/>
</body>
<!--
three.js 3d library
-->
<script src="js/lib/three.min.js"></script>
<!--
library for fast quaternion rotation
-->
<script src="js/lib/gl-matrix.js"></script>
<!--
VRControls.js acquires positional information from connected VR devices and applies the transformations to a three.js camera object.
-->
<script src="js/vr/PhoneVR.js"></script>
<script src="js/vr/VRControls.js"></script>
<!--
VREffect.js handles stereo camera setup and rendering.
-->
<script src="js/vr/VREffect.js"></script>
<script src="js/loaders/OBJLoader.js"></script>
<script type="x-shader/x-vertex" id="vertexShader">
// This shader moves vertices around
// Quaternion Multiplication
vec4 quatMult( in vec4 p, in vec4 q )
{
vec4 r;
r.w = + p.w*q.w - p.x*q.x - p.y*q.y - p.z*q.z;
r.x = + p.w*q.x + p.x*q.w + p.y*q.z - p.z*q.y;
r.y = + p.w*q.y - p.x*q.z + p.y*q.w + p.z*q.x;
r.z = + p.w*q.z + p.x*q.y - p.y*q.x + p.z*q.w;
return r;
}
// input
uniform float time; // global time in seconds
uniform vec4 quatPerMonkey; // quaternion that moves this monkey into 4-space, set once per monkey
uniform int fogType; // which type of fog to use
uniform vec2 mousePos;
uniform mat4 boost;
// Hopf fibration coloring
// returns a color based on the 4D normal
vec3 HopfColor( in vec4 nBase )
{
// first rotate the 4D normal to a space aligned with the 120-cell
float cosx = -sqrt( 0.1*(5.+sqrt(5.)) );
float sinx = sqrt( 0.1*(5.-sqrt(5.)) );
mat4 mat = mat4( vec4(1.,0.,0.,0.), vec4(0.,1.,0.,0.), vec4(0.,0.,cosx,-sinx), vec4(0.,0.,sinx,cosx) );
vec4 n = mat * nBase;
// compute the color
float y = n.x;
float u = n.y;
float v = n.z;
float x = n.w;
float r = 2. * (u*x + v*y);
float g = 2. * (u*y - v*x);
float b = x*x + y*y - u*u - v*v;
return vec3(r*0.5 + 0.5,g*0.5 + 0.5,b*0.5 + 0.5);
}
// output
varying vec3 vColor; // this shader computes the color of each vertex
float cosh(float x) {
float t = exp(x);
return (t + 1.0 / t) / 2.0;
}
float sinh(float x) {
float t = exp(x);
return (t - 1.0 / t) / 2.0;
}
vec4 projectToHyperboloid(vec4 v) {
float scaleFactor = sqrt(v.w * v.w * 5.0 - v.x * v.x - v.y * v.y - v.z * v.z);
vec4 result;
result.x = v.x / scaleFactor;
result.y = v.y / scaleFactor;
result.z = v.z / scaleFactor;
result.w = v.w / scaleFactor;
return result;
}
// this gets called once per vertex of the monkey mesh (and 8 times since there are 8 monkeys)
void main()
{
// base position
// turn a 3D position of a monkey into a 4D position by adding a 1 as the w component then normalizing To get onto the unit 3-sphere
vec4 p = projectToHyperboloid( vec4(position.zyx, 1.0) );
p = boost * p;
// this is the normal to the point
// same concept as for the position, but we add a 0 as the w component
vec4 n3sphere = vec4( normal.zyx, 0.0);
// compute the color from the normal
vColor = HopfColor(n3sphere);
// take the final 3D position and project it onto the screen
gl_Position = projectionMatrix * modelViewMatrix * p; //vec4( pos3 + vec3(0.0,-0.6,-1.5), 1.0 );
// do fog
if ( fogType == 1 )
{
// ramp fog
// compute distance to camera from 0 to 1
float zz = gl_Position.z / gl_Position.w;
// go from 1 to 0 instead (0 is furthest and 1 is where the camera is )
// ( note that the computed distance is not linear )
float fogScale = 1. - zz;
// anything closer than 0.1 gets regular color
if ( fogScale > 0.1 )
fogScale = 1.0;
// everything else ramps from 0 to 1
else
fogScale = fogScale / 0.1;
// mutliply color by this value to make it go to black
vColor *= fogScale;
}
else if ( fogType == 2 )
{
// near fog
float zz = gl_Position.z / gl_Position.w;
// go from 1 to 0, and make the curve less straight
float fogScale = pow( 1. - zz, 0.7 );
// everything closer than 0.2 gets regular color
// but everything else stays the same, creating a discontinuity
if ( fogScale > 0.2 ) fogScale = 1.0;
// mutliply color by this value to make it go to black
vColor *= fogScale;
} else if (fogType == 3 ){
vColor.r *= mousePos.x/1000.;
vColor.g *= mousePos.y/1000.;
vColor.b *= abs(1. - (mousePos.x + mousePos.y)/1000.);
}
}
</script>
<script type="x-shader/x-vertex" id="fragmentShader">
// this gets called once per pixel
varying vec3 vColor;
void main()
{
// just use the color we computed and assign it to this pixel
gl_FragColor = vec4( vColor, 1. );
}
</script>
<script type="text/javascript" id="mainCode">
var camera;
var scene;
var renderer;
var mesh;
var matArray = new Array(8);
var effect;
var controls;
var clicky = 0;
var mouseX = 1;
var mouseY = 1;
var currentBoost = new THREE.Matrix4().set(1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1);
// one quaternion per monkey
// these are just the 8 sides of the hypercube
var quatPerMonkeyArray = [
new THREE.Vector4(1, 0, 0, 0),
new THREE.Vector4(0, 1, 0, 0),
new THREE.Vector4(0, 0, 1, 0),
new THREE.Vector4(0, 0, 0, 1),
new THREE.Vector4(-1, 0, 0, 0),
new THREE.Vector4(0, -1, 0, 0),
new THREE.Vector4(0, 0, -1, 0),
new THREE.Vector4(0, 0, 0, -1)
];
init();
animate();
function onkey(event) {
event.preventDefault();
if (event.keyCode == 90) { // z
controls.zeroSensor();
}
};
window.addEventListener("keydown", onkey, true);
document.body.addEventListener('dblclick', function () {
effect.setFullScreen(true);
});
function init()
{
start = Date.now();
scene = new THREE.Scene();
camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 0.1, 100);
camera.position.x = 0;
camera.position.z = 0;
// -----
// vr stuff
renderer = new THREE.WebGLRenderer({ antialias: true });
document.body.appendChild(renderer.domElement);
controls = new THREE.VRControls(camera);
effect = new THREE.VREffect(renderer);
effect.setSize(window.innerWidth, window.innerHeight);
// -----
// material for the monkeys is a shader
materialBase = new THREE.ShaderMaterial({
// these are the parameters for the shader
uniforms: {
// global time
time: {
type: "f",
value: 0.0
},
// quaternion that moves this monkey into 4-space, set once per monkey
quatPerMonkey: {
type: "v4",
value: new THREE.Vector4( 0, 0, 0, 0 )
},
fogType: {
type: "i",
value: 0
},
mousePos: {
type: "v2",
value: new THREE.Vector2(0,0)
},
boost: {
type: "m4",
value: new THREE.Matrix4().set(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)
}
},
vertexShader: document.getElementById('vertexShader').textContent,
fragmentShader: document.getElementById('fragmentShader').textContent
});
materialBase.side = THREE.FrontSide;
// one material per monkey, since they have a different quaternion
for (var i = 0; i < 8; i++)
{
matArray[i] = materialBase.clone();
}
// load the monkey mesh
var manager = new THREE.LoadingManager();
var loader = new THREE.OBJLoader(manager);
loader.load('media/monkey.obj', function (object) {
// make 8 copies of the mesh and assign them a unique material out of the 8 we created previously
for (var i = 0; i < 8; i++)
{
var newObject = object.clone();
newObject.traverse(function (child) {
if (child instanceof THREE.Mesh) {
child.material = matArray[i];
}
});
scene.add(newObject);
}
});
window.addEventListener('resize', onWindowResize, false);
effect.render(scene, camera);
}
function animate() {
// send the time every frame so that we can rotate the monkeys over time
for (var i = 0; i < 8; i++)
{
matArray[i].uniforms['quatPerMonkey'].value = quatPerMonkeyArray[i];
matArray[i].uniforms['time'].value = .00025 * (Date.now() - start);
matArray[i].uniforms['fogType'].value = clicky;
matArray[i].uniforms['mousePos'].value = new THREE.Vector2(mouseX, mouseY);
matArray[i].uniforms['boost'].value = currentBoost;
}
controls.update();
effect.render(scene, camera);
requestAnimationFrame(animate);
}
//links
function link(){
window.location="http://vihart.github.io/webVR-playing-with/index.html";
}
//listen for mouse movement to get mouseX and mouseY
document.body.addEventListener( 'mousemove', function (event) {
mouseY = event.clientY;
mouseX = event.clientX;
});
//listen for click
document.body.addEventListener( 'click', function(){
clicky = (clicky + 1) % 4;
})
/*
Listen for double click event to enter full-screen VR mode
*/
document.body.addEventListener( 'dblclick', function() {
effect.setFullScreen( true );
});
/*
Listen for keyboard events
*/
function onkey(event) {
event.preventDefault();
if (event.keyCode == 90) { // z
controls.zeroSensor(); //zero rotation
} else if (event.keyCode == 70 || event.keyCode == 13) { //f or enter
effect.setFullScreen(true) //fullscreen
} else if (event.keyCode == 32) {//space
link();
} else if (event.keyCode === 73){ //i
infoSign.material.visible = !infoSign.material.visible;
} else if (event.keyCode == 80) {//p
var music = document.querySelector('#music');
if (music.paused){
music.play();
} else{
music.pause();
}
}
};
window.addEventListener("keydown", onkey, true);
var step = 0.05;
var ud = new THREE.Matrix4().set(1,0,0,0,
0,1,0,0,
0,0,Math.cosh(step),Math.sinh(step),
0,0,Math.sinh(step),Math.cosh(step));
var lr = new THREE.Matrix4().set(1,0,0,0,
0,Math.cosh(step),0,Math.sinh(step),
0,0,1,0,
0,Math.sinh(step),0,Math.cosh(step))
var fb = new THREE.Matrix4().set(Math.cosh(step),0,0,Math.sinh(step),
0,1,0,0,
0,0,1,0,
Math.sinh(step),0,0,Math.cosh(step));
var udi = new THREE.Matrix4().getInverse(ud);
var lri = new THREE.Matrix4().getInverse(lr);
var fbi = new THREE.Matrix4().getInverse(fb);
var infinitesimalBoosts = {
3: ud,
4: udi,
5: lr,
6: lri,
7: fb,
8: fbi
};
//hold down keys to do rotations and stuff
function key(event, sign) {
var letter = String.fromCharCode(event.keyCode).toLowerCase();
var control = controls.manualControls[letter];
if (!control) {
if (infinitesimalBoosts[letter]) {
currentBoost.multiply(infinitesimalBoosts[letter]);
}
return;
}
if (sign === 1 && control.active || sign === -1 && !control.active)
return;
control.active = (sign === 1);
controls.manualRotateRate[control.index] += sign * control.sign;
}
document.addEventListener('keydown', function(event) { key(event, 1); }, false);
document.addEventListener('keyup', function(event) { key(event, -1); }, false);
/*
Handle window resizes
*/
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
effect.setSize( window.innerWidth, window.innerHeight );
}
window.addEventListener( 'resize', onWindowResize, false );
</script>
</html>