-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
600 lines (452 loc) · 18.5 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
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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
<!DOCTYPE html>
<html lang="en">
<head>
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-WTY198QNED"></script>
<script src="https://davidlyons.dev/sketches/ga.js"></script>
<meta charset="utf-8" />
<title>Sound Pyramid</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="description" content="" />
<meta name="author" content="" />
<link href="https://davidlyons.dev/sketches/info.css" rel="stylesheet">
<style>
* {
box-sizing: border-box;
}
html {
overflow: hidden;
}
body {
margin: 0;
padding: 0;
overflow: hidden;
font-family: Arial, sans-serif;
font-size: 12px;
background: #e3cab3;
}
canvas {
outline: none;
}
#loading {
padding: 10px;
font-weight: bold;
}
#webglmessage {
position: absolute;
top: 0;
left: 50%;
margin-left: -200px !important;
}
</style>
</head>
<body>
<div id="container"></div>
<div id="loading">Loading...</div>
<script src="https://cdn.jsdelivr.net/gh/mrdoob/three.js@r112/build/three.js"></script>
<script src="https://cdn.jsdelivr.net/gh/mrdoob/three.js@r112/examples/js/controls/OrbitControls.js"></script>
<script src="https://cdn.jsdelivr.net/gh/mrdoob/three.js@r112/examples/js/libs/inflate.min.js"></script>
<script src="https://cdn.jsdelivr.net/gh/mrdoob/three.js@r112/examples/js/loaders/FBXLoader.js"></script>
<script src="https://cdn.jsdelivr.net/gh/mrdoob/three.js@r112/examples/js/modifiers/SubdivisionModifier.js"></script>
<script src="https://cdn.jsdelivr.net/gh/mrdoob/three.js@r112/examples/js/WebGL.js"></script>
<!-- <script src="https://cdnjs.cloudflare.com/ajax/libs/dat-gui/0.7.6/dat.gui.min.js"></script> -->
<script src="https://cdn.jsdelivr.net/gh/tweenjs/[email protected]/src/Tween.js"></script>
<script src="https://davidlyons.dev/sketches/info.js"></script>
<script>
if (THREE.WEBGL.isWebGLAvailable() === false) {
document.body.appendChild(THREE.WEBGL.getWebGLErrorMessage());
}
if (Info) {
var info = new Info(
'Sound Pyramid',
[
'<a href="https://davidlyons.dev">David Lyons</a>',
'<a href="pyramid-flash.jpg">Flash version screenshot</a>',
'Concept by Melody Neumann',
'Sand from <a href="http://www.textures.com/" target="_blank">textures.com</a>',
'Left mouse orbit, middle mouse zoom',
'Q,A,W,S keys to play notes',
].join('<br>')
);
}
var container;
var camera, controls, scene, renderer;
var pyramid;
var points;
var sprite;
var sounds = [];
var mouse = new THREE.Vector2();
var INTERSECTED, SELECTED;
var raycaster = new THREE.Raycaster();
var FLOOR = -100;
// ---------------------------------------------
var Sound = function (sources) {
this.audio = document.createElement('audio');
for (var i = 0; i < sources.length; i++) {
this.source = document.createElement('source');
this.source.src = sources[i];
this.audio.appendChild(this.source);
}
};
Sound.prototype.play = function () {
this.audio.load();
this.audio.play();
};
// ---------------------------------------------
init();
function init() {
container = document.getElementById('container');
scene = new THREE.Scene();
scene.fog = new THREE.Fog(0xe3cab3, 500, 1000);
renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setClearColor(0xe3cab3, 1);
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize(window.innerWidth, window.innerHeight);
camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 1, 100000);
camera.position.set(90, 150, 400);
controls = new THREE.OrbitControls(camera, renderer.domElement);
controls.minPolarAngle = Math.PI / 8;
controls.maxPolarAngle = Math.PI / 2;
controls.enablePan = false;
controls.minDistance = 200;
controls.maxDistance = 700;
// THREE.DefaultLoadingManager.onProgress = function ( url, itemsLoaded, itemsTotal ) {
// console.log( 'Loading file: ' + url + '.\nLoaded ' + itemsLoaded + ' of ' + itemsTotal + ' files.' );
// };
THREE.DefaultLoadingManager.onLoad = function () {
document.body.removeChild(document.querySelector('#loading'));
container.appendChild(renderer.domElement);
sand.receiveShadow = true;
pyramid.traverse(function (obj) {
if (obj.isMesh) {
obj.castShadow = true;
obj.receiveShadow = true;
}
});
loop();
};
var textureLoader = new THREE.TextureLoader();
var fbxLoader = new THREE.FBXLoader();
// var axisHelper = new THREE.AxesHelper( 100 );
// scene.add( axisHelper );
var alight = new THREE.AmbientLight(0x404040); // soft white light
scene.add(alight);
var keyLight = new THREE.DirectionalLight(0xffffff, 0.3);
keyLight.position.set(1, 1, 1).multiplyScalar(100);
scene.add(keyLight);
var fillLight = new THREE.DirectionalLight(0xffffff, 0.3);
fillLight.position.set(-1, 0, -1).multiplyScalar(100);
scene.add(fillLight);
// shadows
// renderer.outputEncoding = THREE.GammaEncoding;
// renderer.shadowMap.enabled = true;
// renderer.shadowMap.type = THREE.PCFSoftShadowMap; // default THREE.PCFShadowMap
// function shadow(light) {
// light.castShadow = true;
// light.shadow.mapSize.width = 2048;
// light.shadow.mapSize.height = 2048;
// var d = 100;
// light.shadow.camera.left = -d;
// light.shadow.camera.right = d;
// light.shadow.camera.top = d;
// light.shadow.camera.bottom = -d;
// light.shadow.camera.far = 3500;
// light.shadow.bias = -0.0001;
// // var camHelper = new THREE.CameraHelper( light.shadow.camera );
// // scene.add( camHelper );
// }
// shadow(keyLight);
// shadow(fillLight);
// ------------------------------------------------------------
// dome
var sphereMat = new THREE.MeshBasicMaterial({
color: 0x5a4d3e,
wireframe: true,
transparent: true,
opacity: 0.6,
});
var sphereGeo1 = new THREE.SphereGeometry(600, 20, 10);
var sphereGeo2 = new THREE.IcosahedronGeometry(700, 2);
var sphere1 = new THREE.Mesh(sphereGeo1, sphereMat);
var sphere2 = new THREE.Mesh(sphereGeo2, sphereMat);
scene.add(sphere1);
scene.add(sphere2);
// ------------------------------------------------------------
// Dust Particles
// maybe try this instead
// http://www.html5rocks.com/en/tutorials/casestudies/oz/
var pointCount = 400;
var pointsGeo = new THREE.Geometry();
var pointsMat = new THREE.PointsMaterial({
color: 0x7a5d43,
size: 2,
});
for (var p = 0; p < pointCount; p++) {
var point = new THREE.Vector3(
THREE.Math.randFloatSpread(500),
THREE.Math.randFloat(0, 300 + FLOOR),
THREE.Math.randFloatSpread(500)
);
pointsGeo.vertices.push(point);
}
points = new THREE.Points(pointsGeo, pointsMat);
points.sortParticles = true;
scene.add(points);
// ------------------------------------------------------------
// Sprites / Hieroglyphs
// var map = textureLoader.load('textures/eye-of-horus.png');
var map = textureLoader.load('textures/hieroglyphs.png');
var material = new THREE.SpriteMaterial({
map: map,
color: 0xffffff,
fog: true,
transparent: true,
opacity: 0.5,
});
sprite = new THREE.Sprite(material);
sprite.scale.setScalar(80);
sprite.position.set(50, 80, 50);
scene.add(sprite);
// ------------------------------------------------------------
// Sand / Ground
var sandColor = textureLoader.load('textures/sand-color.jpg');
sandColor.wrapS = sandColor.wrapT = THREE.RepeatWrapping;
sandColor.repeat.set(8, 8);
var sandMat = new THREE.MeshLambertMaterial({
color: 0xcfbb9a,
map: sandColor,
});
var sandGeo = new THREE.PlaneBufferGeometry(4096, 4096, 1, 1);
var sand = new THREE.Mesh(sandGeo, sandMat);
scene.add(sand);
sand.rotation.x = -Math.PI / 2;
sand.position.y = FLOOR;
// ------------------------------------------------------------
var modifier = new THREE.SubdivisionModifier(1);
fbxLoader.load('models/pyramid.fbx', function (fbx) {
pyramid = fbx;
pyramid.scale.setScalar(2);
pyramid.position.y = FLOOR;
scene.add(pyramid);
var pLight2 = new THREE.PointLight(0x00ffcc, 0.6, 500);
pLight2.position.set(0, 130, 0);
pyramid.add(pLight2);
// var plh2 = new THREE.PointLightHelper( pLight2, 25 );
// scene.add( plh2 );
var speaker1 = pyramid.getObjectByName('speaker1');
speaker1.material.specular.setHex(0xffffff);
var base = pyramid.getObjectByName('pyramidBase');
base.material.specular.setHex(0xffffff);
base.side = THREE.DoubleSide;
var borders = pyramid.getObjectByName('borders');
borders.material.specular.setHex(0xffffff);
borders.side = THREE.DoubleSide;
borders.geometry = modifier.modify(borders.geometry);
// --------------------------------------------------------
// top
var pyramidTop = pyramid.getObjectByName('pyramidTop');
var pLight = new THREE.PointLight(0x00ffcc, 0.6, 500);
pLight.position.y = -28;
pyramidTop.add(pLight);
// var plh = new THREE.PointLightHelper( pLight, 25 );
// scene.add( plh );
// var ah = new THREE.AxesHelper( 50 );
// pyramidTop.add( ah );
var tween = new TWEEN.Tween(pyramidTop.position)
.to({ y: pyramidTop.position.y + 15 }, 1500)
.easing(TWEEN.Easing.Cubic.InOut);
var tween2 = new TWEEN.Tween(pyramidTop.position)
.to({ y: pyramidTop.position.y + 3 }, 1500)
.easing(TWEEN.Easing.Cubic.InOut);
tween.chain(tween2);
tween2.chain(tween);
tween.start();
// --------------------------------------------------------
// speakers
for (var i = 1; i <= 8; i++) {
var speaker = pyramid.getObjectByName('speaker' + i);
speaker.material = speaker.material.clone();
speaker.geometry = modifier.modify(speaker.geometry);
// assign sound to speaker
var sound;
switch (i) {
// front : back :
case 1:
case 5:
sound = sounds[0];
break;
case 2:
case 6:
sound = sounds[1];
break;
case 3:
case 7:
sound = sounds[2];
break;
case 4:
case 8:
sound = sounds[3];
break;
default:
sound = sounds[0];
break;
}
speaker.userData.sound = sound;
// -------------------------------
// direction / axis / face normal of pyramid base side
var normal = new THREE.Vector3(0.6457077325259278, 0.40832318480986296, 0.6452392586498092);
speaker.geometry.computeBoundingBox();
var boundingBox = speaker.geometry.boundingBox;
var centerX = 0.5 * (boundingBox.max.x + boundingBox.min.x);
var centerZ = 0.5 * (boundingBox.max.z + boundingBox.min.z);
normal.x *= centerX > 0 ? 1 : -1;
normal.z *= centerZ > 0 ? 1 : -1;
// arrow helpers
// var centerY = 0.5 * ( boundingBox.max.y + boundingBox.min.y );
// var center = new THREE.Vector3( centerX, centerY, centerZ);
// var arrow = new THREE.ArrowHelper(normal, center, 100, 0xff0000);
// pyramid.add( arrow );
// ------------------------------
var origin = (speaker.userData.origin = new THREE.Group());
origin.position.copy(speaker.position);
var target1 = (speaker.userData.target1 = new THREE.Group());
target1.position.copy(speaker.position);
target1.translateOnAxis(normal, 4);
var target2 = (speaker.userData.target2 = new THREE.Group());
target2.position.copy(speaker.position);
target2.translateOnAxis(normal, -2);
}
// ---------------------------------------------------
document.addEventListener('mousemove', mousemove, false);
document.addEventListener('mousedown', mousedown, false);
document.addEventListener('mouseup', mouseup, false);
document.addEventListener('touchstart', mousedown, false);
document.addEventListener('touchend', mouseup, false);
});
// --------------------------------------------------------------
var soundFiles = ['Ab', 'Eb', 'A', 'E'];
for (var i = 0; i < soundFiles.length; i++) {
var sound = new Sound(['sounds/square/' + soundFiles[i] + '.mp3']);
sounds.push(sound);
}
// --------------------------------------------------------
// var gui = new dat.GUI();
// var opts = {
// spriteScale: sprite.scale.x
// };
// gui.add(opts, 'spriteScale', 20, 200).onChange(function(val){
// sprite.scale.setScalar(val);
// });
// gui.add(camera, 'fov', 10, 135).onChange(function(){
// camera.updateProjectionMatrix();
// });
// gui.add(brickMaterial, 'shininess', 1, 30);
// var f1 = gui.addFolder('fog');
// f1.add(scene.fog, 'near', 1, 1000);
// f1.add(scene.fog, 'far', 500, 1500);
// f1.open();
// --------------------------------------------------------
//
window.addEventListener('resize', resize, false);
}
// --------------------------------------------------------
function resize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
function mousemove(event) {
event.preventDefault();
var e = (event.touches && event.touches[0]) || event;
// mouse x and y are between -1 and 1 (normalized?)
mouse.x = (e.clientX / window.innerWidth) * 2 - 1;
mouse.y = -(e.clientY / window.innerHeight) * 2 + 1;
findIntersections();
}
function speakerActivated(speaker) {
speaker.userData.sound.play();
var tween1 = new TWEEN.Tween(speaker.position)
.to(speaker.userData.target1.position, 60)
.easing(TWEEN.Easing.Cubic.Out);
var tween2 = new TWEEN.Tween(speaker.position)
.to(speaker.userData.target2.position, 60)
.delay(60)
.easing(TWEEN.Easing.Cubic.In);
var tween3 = new TWEEN.Tween(speaker.position)
.to(speaker.userData.origin.position, 60)
.delay(60)
.easing(TWEEN.Easing.Cubic.Out);
tween1.chain(tween2);
tween2.chain(tween3);
tween1.start();
}
function mousedown(event) {
event.preventDefault();
mousemove(event);
var intersects = raycaster.intersectObjects(pyramid.children);
if (intersects.length > 0) {
SELECTED = intersects[0].object;
if (SELECTED.name.startsWith('speaker')) {
controls.enabled = false;
speakerActivated(SELECTED);
}
}
}
function mouseup(event) {
event.preventDefault();
controls.enabled = true;
if (INTERSECTED) {
SELECTED = null;
}
}
function findIntersections() {
raycaster.setFromCamera(mouse, camera);
var intersects = raycaster.intersectObjects(pyramid.children);
if (intersects.length > 0) {
if (INTERSECTED != intersects[0].object) {
if (INTERSECTED) {
INTERSECTED.material.emissive.setHex(INTERSECTED.currentHex);
container.style.cursor = 'auto';
}
INTERSECTED = intersects[0].object;
// console.log(INTERSECTED.name);
// if ( INTERSECTED.name.indexOf('speaker') >= 0 ) {
if (INTERSECTED.name.startsWith('speaker')) {
INTERSECTED.currentHex = INTERSECTED.material.emissive.getHex();
INTERSECTED.material.emissive.setHex(0x004f3f);
container.style.cursor = 'pointer';
}
}
} else {
if (INTERSECTED) {
INTERSECTED.material.emissive.setHex(INTERSECTED.currentHex);
container.style.cursor = 'auto';
}
INTERSECTED = null;
}
}
//
function loop() {
requestAnimationFrame(loop);
points.rotation.y += 0.005;
TWEEN.update();
controls.update();
renderer.render(scene, camera);
}
window.addEventListener('keydown', function (e) {
// play note and speaker animation
// - move into mesh.blast() function on speakers
if (e.keyCode == 83) speakerActivated(scene.getObjectByName('speaker4')); // s
if (e.keyCode == 81) speakerActivated(scene.getObjectByName('speaker1')); // q
if (e.keyCode == 65) speakerActivated(scene.getObjectByName('speaker2')); // a
if (e.keyCode == 87) speakerActivated(scene.getObjectByName('speaker3')); // w
});
// Ideas:
// Egyptian Hieroglyph Iconography
// Anubis, Jackal, Scarab Beetle, Cobra, Mummy, Falcon / Vulture
// Sphynx, Pharoah
// Eye of Horus / Ra
// http://en.wikipedia.org/wiki/Egyptian_hieroglyphs
</script>
</body>
</html>