-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinstrument.js
668 lines (457 loc) · 16.8 KB
/
instrument.js
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
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
var texture = new THREE.ImageUtils.loadTexture('gold.jpg');
var texture2 = new THREE.ImageUtils.loadTexture('steel2.jpg');
var texture3 = new THREE.ImageUtils.loadTexture('bronze.jpg');
var cylinderMaterial = new THREE.MeshPhongMaterial( { specular: 0xD1F5FD, shininess: 500, map: texture } );
var baseMaterial = new THREE.MeshPhongMaterial( { specular: 0xD1F5FD, shininess: 500, map: texture } );
var blackMaterial = new THREE.MeshPhongMaterial( {color:0x000000, specular: 0xD1F5FD, shininess: 500, map: texture } );
var steelMaterial = new THREE.MeshPhongMaterial( {specular: 0xD1F5FD, shininess: 500, map: texture2 } );
var pinObject = new THREE.Object3D();
var revolvingCylinder = new THREE.Object3D();
var combFingerObejct = new THREE.Object3D();
var combFingerAarry = [];
var combAssem = new THREE.Object3D();
var handleAssem = new THREE.Object3D();
var comnbFingerExtrudeSettings = {
amount : 0.2, //combFinger thickness
steps : 1,
bevelEnabled : false,
};
var combMaterial = new THREE.MeshPhongMaterial( {specular: 0xD1F5FD, shininess: 500, map: texture3 } );
var extrudeSettings = {
amount : 0.4, //base thickness
steps : 1,
bevelEnabled : false,
};
var renderer, scene, camera, controls;
var cylinderRadius = 20;
var keyInterval = 0.5;
var keyWidth = 1;
var cylinderLength = 88 * keyWidth + 87 * keyInterval;
var pinBaseRaidus = 0.15;
var pinTopRaidus = 0.03;
var numKeys = 88;
var keyRadius = 100;
var bucketRadiusToCannon = 140;
var bucketPositionOffset = 0.2;
var bucketBaseRadius = 3;
var bucketTopRadius = 5;
var bucketHeight = 12;
var bucketThickness = 1;
var darkBaseRadius = bucketBaseRadius + 0.5;
var darkBaseHeight = 1;
var blackColor = 0x000000;
var tubeRadius = 3;
var numCentralizingPipes = 7;
var ballRadius = 2;
var ballHangtime = 100;
var G = -0.1;
var initVelocity = -G * ballHangtime / 2;
var firingAngle = Math.acos(keyRadius / (ballHangtime * initVelocity));
var velocityOut = Math.cos(firingAngle) * initVelocity;
var bounceHangtime = 48;
var bounceVelocity = -G * bounceHangtime / 2;
var bounceAngle = Math.acos((bucketRadiusToCannon - keyRadius) / (bounceHangtime * bounceVelocity));
var bounceOut = Math.cos(bounceAngle) * bounceVelocity;
var keys = [];
var balls = [];
var queue = []; // pronounced: kWEH.
var ballHeadstart = 1730;
var timeInSong = -startDelay;
var lastUpdatedTime = 0;
function init() {
var WIDTH = $('.rest').width(),
HEIGHT = $('.rest').height(),
VIEW_ANGLE = 45,
ASPECT = WIDTH / HEIGHT,
NEAR = 0.1,
FAR = 10000;
console.log('Size: ' + WIDTH + ' ' + HEIGHT);
// create a WebGL renderer, camera
// and a scene
renderer = new THREE.WebGLRenderer( { antialias: true } );
renderer.gammaInput = true;
renderer.gammaOutput = true;
renderer.setSize(WIDTH, HEIGHT);
renderer.setClearColorHex( 0xb0c4de, 1.0 );
camera = new THREE.PerspectiveCamera(VIEW_ANGLE, ASPECT, NEAR, FAR);
scene = new THREE.Scene();
camera.position.x = 285;
camera.position.y = 108;
camera.position.z = 163;
camera.lookAt(new THREE.Vector3(0, 0, 0));
// start the renderer
renderer.setSize(WIDTH, HEIGHT);
// attach the render-supplied DOM element
$('#MusicBox').append(renderer.domElement);
// and the camera
scene.add(camera);
addControls();
// draw!
renderer.render(scene, camera);
}
function fillScene() {
addLighting(); //add the light
addbase();
addParts(); //add the main part: comb, cylinder, etc.
var cameraCube;
}
function addbase(){
var extrudeSettings = {
amount : 5, //combFinger thickness
steps : 1,
bevelEnabled : true,
bevelSize : 1
};
var boxBaseShape = new THREE.Shape();
boxBaseShape.moveTo( -70, -60 );
boxBaseShape.lineTo( 180, -60 );
boxBaseShape.lineTo( 180, 50 );
boxBaseShape.lineTo( -70, 50 );
boxBaseShape.lineTo( -70, -60 );
var boxBaseGeo = new THREE.ExtrudeGeometry( boxBaseShape, extrudeSettings );
var boxBase = new THREE.Mesh( boxBaseGeo, baseMaterial);
boxBase.rotation.x = 90 / 180 * Math.PI;
boxBase.position.y = -30;
boxBase.position.z = 20;
scene.add( boxBase );
var geometry = new THREE.CylinderGeometry( 21, 21, 5, 32 );
var cylinder = new THREE.Mesh( geometry, blackMaterial);
cylinder.rotation.z = 0.5 * Math.PI;
cylinder.position.x = 131.5 + 2.5;
scene.add( cylinder );
//free end , the last big black cylinder;
var geometry = new THREE.CylinderGeometry( 22, 22, 10, 32 );
var cylinder = new THREE.Mesh( geometry, blackMaterial);
cylinder.rotation.z = 0.5 * Math.PI;
cylinder.position.x = 131.5 + 2.5 + 7.5;
scene.add( cylinder );
//add a box to finish the fix work.
var geometry = new THREE.CubeGeometry( 32, 10, 44);
var box = new THREE.Mesh(geometry, blackMaterial);
box.rotation.z = 0.5 * Math.PI;
box.position.x = 131.5 + 2.5 + 7.5;
box.position.y = - 18;
scene.add( box );
var geometry = new THREE.CylinderGeometry( 22, 22, 4, 32 );
var cylinder = new THREE.Mesh( geometry, blackMaterial);
cylinder.rotation.z = 0.5 * Math.PI;
cylinder.position.x = -2;
scene.add( cylinder );
var geometry = new THREE.CylinderGeometry( 23, 23, 15, 32 );
var cylinder = new THREE.Mesh( geometry, blackMaterial);
cylinder.rotation.z = 0.5 * Math.PI;
cylinder.position.x = -2 - 4;
scene.add( cylinder );
var geometry = new THREE.CylinderGeometry( 4, 4, 60, 32 );
var cylinder = new THREE.Mesh( geometry, steelMaterial);
cylinder.position.x = -2 - 4;
cylinder.position.z = -30;
scene.add( cylinder );
var cylinder = new THREE.Mesh( geometry, steelMaterial);
cylinder.position.x = -2 - 4 - 30;
cylinder.position.z = -30;
scene.add( cylinder );
var cylinder = new THREE.Mesh( geometry, steelMaterial);
cylinder.position.x = -2 - 4;
cylinder.position.z = 30;
scene.add( cylinder );
var cylinder = new THREE.Mesh( geometry, steelMaterial);
cylinder.position.x = -2 - 4 - 30;
cylinder.position.z = 30;
scene.add( cylinder );
var boxGeometry = new THREE.CubeGeometry( 8, 59.9, 32 );
var box = new THREE.Mesh( boxGeometry, steelMaterial);
box.position.x = -6 - 15;
box.position.z = -30;
box.rotation.y =0.5 * Math.PI;
scene.add( box );
var box = new THREE.Mesh( boxGeometry, steelMaterial);
box.position.x = -6 - 15;
box.position.z = 30;
box.rotation.y =0.5 * Math.PI;
scene.add( box );
//rear small support
var geometry = new THREE.CylinderGeometry( 4, 4, 4, 32 );
var cylinder = new THREE.Mesh( geometry, combMaterial);
cylinder.rotation.z = 0.5 * Math.PI;
cylinder.position.x = 131.5 + 2.5 + 7.5 + 7;
scene.add( cylinder );
//driven rings on the cylinder
var geometry = new THREE.SphereGeometry( 6, 32, 32 );
for (var i = 0; i <= 15; i ++) {
var rings = new THREE.Mesh( geometry, combMaterial);
rings.scale.z = 0.3;
rings.position.x = -20;
rings.position.y = 10;
rings.position.z = -15 + 2 * i;
scene.add(rings);
}
//handle
var geometry = new THREE.CylinderGeometry( 4, 4, 110, 32 );
var cylinder = new THREE.Mesh( geometry, combMaterial);
cylinder.rotation.x = 0.5 * Math.PI;
cylinder.position.x = -20;
cylinder.position.y = 10;
cylinder.position.z = - 20;
handleAssem.add(cylinder);
//scene.add( cylinder );
//handle ending
var geometry = new THREE.CylinderGeometry( 4, 7, 15, 32 );
var cylinder = new THREE.Mesh( geometry, blackMaterial);
cylinder.rotation.x = 0.5 * Math.PI;
cylinder.position.x = -20;
cylinder.position.y = 10;
cylinder.position.z = - 74;
handleAssem.add(cylinder);
//scene.add( cylinder );
//handle neck
var neckMaterial = new THREE.MeshPhongMaterial( {color:0xFF0000, specular: 0xD1F5FD, shininess: 500 } );
var geometry = new THREE.CylinderGeometry( 5, 5, 5, 32 );
var cylinder = new THREE.Mesh( geometry, blackMaterial);
cylinder.rotation.x = 0.5 * Math.PI;
cylinder.position.x = -20;
cylinder.position.y = 10;
cylinder.position.z = 35;
scene.add( cylinder );
scene.add (handleAssem);
var extrudeSettings = {
amount : 25, //combFinger thickness
steps : 1,
bevelEnabled : false,
bevelSize : 1
};
var combBaseShape = new THREE.Shape();
combBaseShape.moveTo( 0, 0 );
combBaseShape.lineTo( 0, 10 );
combBaseShape.lineTo( 131.5, 30 );
combBaseShape.lineTo( 131.5, 0 );
combBaseShape.lineTo( 0, 0 );
var combBaseGeo = new THREE.ExtrudeGeometry( combBaseShape, extrudeSettings );
var combBase = new THREE.Mesh( combBaseGeo, steelMaterial);
combBase.rotation.x = - 0.5 * Math.PI;
combBase.position.y = - 25.3;
combBase.position.z = 61;
scene.add(combBase);
var geometry = new THREE.SphereGeometry( 3, 32, 32 );
var sphere = new THREE.Mesh( geometry, blackMaterial );
sphere.position.z = 55;
sphere.position.x = 13;
scene.add( sphere );
var sphere = new THREE.Mesh( geometry, blackMaterial );
sphere.position.z = 55;
sphere.position.x = 68;
scene.add( sphere );
var sphere = new THREE.Mesh( geometry, blackMaterial );
sphere.position.z = 55;
sphere.position.x = 120;
scene.add( sphere );
}
function addNotePins(){
scene.remove(pinObject); //remove the notespin from scene
revolvingCylinder.remove(pinObject); //remove the notes pin from cylinder object;
pinObject = new THREE.Object3D();
var pinGeo = new THREE.CylinderGeometry( pinTopRaidus, pinBaseRaidus, 3, 32 );
var songFullTime = notes[notes.length - 1].time;
for ( var i = 0; i < notes.length; i++) { //potential problem: the last one is the fist one.
var pinPosition = notes[i].note - MIDI.pianoKeyOffset
var noteTime = notes[i].time;
console.log(noteTime, pinPosition);
var pin = new THREE.Mesh( pinGeo, cylinderMaterial );
var pinTheta = noteTime / songFullTime * 2 * Math.PI;
pin.rotation.x = - pinTheta;
pin.position.x = (pinPosition - 1 ) * 1.5 + 0.5;
if (pinTheta >= 0 && pinTheta < 90) {
pin.position.z = cylinderRadius * Math.sin (- pinTheta);
pin.position.y = cylinderRadius * Math.cos (- pinTheta);
}else if (pinTheta >= 90 && pinTheta < 180){
pinTheta = pinTheta - 90;
pin.position.z = - cylinderRadius * Math.cos ( pinTheta);
pin.position.y = - cylinderRadius * Math.sin ( pinTheta);
}else if (pinTheta >= 180 && pinTheta < 270){
pinTheta = pinTheta - 180;
pin.position.z = cylinderRadius * Math.sin ( pinTheta);
pin.position.y = - cylinderRadius * Math.cos ( pinTheta);
}else {
pinTheta = pinTheta - 270;
pin.position.z = cylinderRadius * Math.cos ( pinTheta);
pin.position.y = cylinderRadius * Math.sin ( pinTheta);
}
//pin.position.z = cylinderRadius * Math.sin (- pinTheta);
//pin.position.y = cylinderRadius * Math.cos (- pinTheta);
pinObject.add(pin);
}
pinObject.rotation.x = 90 / 180 * Math.PI; //change the start point from left level;
revolvingCylinder.add(pinObject);
scene.add(pinObject);
}
function updateComb(){
if (notes.length === 0) {
return;
}
if (notes[0].time <= player.currentTime) {
for (var i = 0; i < 88; i++){
combFingerAarry[i].material = combMaterial;
}
}
while (notes[0].time <= player.currentTime) {
var combMaterial2 = new THREE.MeshPhongMaterial( { color: 0xCCFFCC, pecular: 0xD1F5FD, shininess: 500 } );
combFingerAarry[notes[0].note - MIDI.pianoKeyOffset - 1].material = combMaterial2;
notes.splice(0, 1);
if (notes.length === 0) {
return;
}
}
//combAssem.add( combFingerObejct );
}
var cylinderGeo = new THREE.CylinderGeometry( cylinderRadius, cylinderRadius, cylinderLength, 32 );
function addParts(){
var cylinder = new THREE.Mesh( cylinderGeo, cylinderMaterial );
cylinder.rotation.z = 90 / 180 * Math.PI;
cylinder.position.x = cylinderLength / 2;
revolvingCylinder.add(cylinder);
scene.add( revolvingCylinder );
var combBaseShape = new THREE.Shape();
combBaseShape.moveTo( 0, 0 );
combBaseShape.lineTo( 0, 10 );
combBaseShape.lineTo( 131.5, 30 );
combBaseShape.lineTo( 131.5, 0 );
combBaseShape.lineTo( 0, 0 );
var combBaseGeo = new THREE.ExtrudeGeometry( combBaseShape, extrudeSettings );
var combBase = new THREE.Mesh( combBaseGeo, combMaterial);
combAssem.add( combBase );
for (var i = 1; i < numKeys + 1; i++){
var baseFingerShape = new THREE.Shape();
var startPointX = 1.5 * (i - 1);
var startPointY = 10;
baseFingerShape.moveTo( startPointX, startPointY );
baseFingerShape.lineTo( startPointX, startPointY + 30);
baseFingerShape.lineTo( startPointX + 1, startPointY + 30 );
baseFingerShape.lineTo( startPointX + 1, startPointY );
baseFingerShape.lineTo( startPointX, startPointY );
var combFingerGeo = new THREE.ExtrudeGeometry( baseFingerShape, comnbFingerExtrudeSettings );
var combFinger = new THREE.Mesh( combFingerGeo, combMaterial);
combFingerObejct.add(combFinger);
combFingerAarry.push(combFinger);
}
combAssem.add( combFingerObejct );
scene.add( combAssem );
combAssem.rotation.x = -90 / 180 * Math.PI;
combAssem.position.z = cylinderRadius + 40 + 1; //the cylinder R + width comb + gap
combAssem.position.y = -0.35; //makesure the pin above the comb
}
function addLighting(){
scene.fog = new THREE.Fog( 0x808080, 2000, 4000 );
// LIGHTS
var ambientLight = new THREE.AmbientLight( 0x222222 );
var light = new THREE.DirectionalLight( 0xFFFFFF, 1.0 );
light.position.set( 200, 400, 500 );
var light2 = new THREE.DirectionalLight( 0xFFFFFF, 1.0 );
light2.position.set( -500, 250, -200 );
scene.add(ambientLight);
scene.add(light);
scene.add(light2);
}
function animate() {
window.requestAnimationFrame(animate);
timeDelta = Date.now() - lastUpdatedTime;
controls.update();
if (MIDI.Player.playing) {
pinObject.rotation.x += timeDelta / notes[notes.length-1].time * 360 / 180 * Math.PI;
revolvingCylinder.rotation.x += timeDelta / notes[notes.length-1].time * 360 / 180 * Math.PI;
// rotateCylinder();
}
//moveBalls();
//darkenKeys();
lastUpdatedTime = Date.now();
renderer.render(scene, camera);
}
function rotateCylinder(){
var delta = clock.getDelta();
//revolvingCylinder.rotation.x +=
pinObject.rotation.x += delta * 1000 / notes[notes.length-1].time * 360 / 180 * Math.PI;
}
// where the magic happens
function throwBallsToMusic() {
if (notes.length == 0)
return;
lastUpdatedTime = lastUpdatedTime || Date.now();
var delta = 0.01;
var interpolatedTime = Date.now() - lastUpdatedTime;
var currTime = timeInSong + interpolatedTime;
while (notes[0].time < currTime + ballHeadstart) {
//console.log(notes[0].note - MIDI.pianoKeyOffset);
addBall(notes[0].note - MIDI.pianoKeyOffset);
notes.splice(0, 1);
if (notes.length === 0) {
return;
}
}
}
function resetTimer(songTime) {
timeInSong = songTime;
lastUpdatedTime = Date.now();
}
function moveBalls() {
for (var i = balls.length - 1; i >= 0; i--) {
var ball = balls[i];
if (ball.cannon.position.y < 0) {
if (ball.cannon.position.x >= bucketRadiusToCannon) {
scene.remove(ball.object);
balls.splice(i, 1);
continue;
} else {
ballHeadstart = Date.now() - queue.shift(1);
makeKeyGlow(ball.target);
// and bounce back up!
ball.velocityUp = bounceVelocity * Math.sin(bounceAngle);
ball.cannon.position.y = 0;
}
}
ball.velocityUp += G;
if (ball.cannon.position.x < bucketRadiusToCannon) {
ball.cannon.position.y += ball.velocityUp;
ball.cannon.position.x += velocityOut;
} else {
ball.cannon.position.y += ball.velocityUp;
ball.cannon.position.x += bounceOut;
}
}
}
function makeKeyGlow(key) {
keys[key].material.color.setHex(transitionColors[transitionColors.length - 1]);
}
function darkenKeys() {
for (var i = 0; i < numKeys; i++) {
var key = keys[i];
var state = transitionColors.indexOf(key.material.color.getHex());
if (state === 0) {
continue;
} else {
key.material.color.setHex(transitionColors[state - 1]);
}
}
}
function addControls() {
controls = new THREE.TrackballControls(camera, renderer.domElement);
var radius = 100 * 0.75; // scalar value used to determine relative zoom distances
controls.rotateSpeed = 1;
controls.zoomSpeed = 2;
controls.panSpeed = 1;
controls.noZoom = false;
controls.noPan = false;
controls.staticMoving = false;
controls.dynamicDampingFactor = 0.3;
controls.minDistance = radius * 1.1;
controls.maxDistance = radius * 25;
controls.keys = [65, 83, 68]; // [ rotateKey, zoomKey, panKey ]
}
try {
init();
fillScene();
//var axisHelper = new THREE.AxisHelper( 500 );
//scene.add( axisHelper );
animate();
} catch(e) {
var errorReport = "Your program encountered an unrecoverable error, can not draw on canvas. Error was:<br/><br/>";
$('#container').append(errorReport+e);
}