-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathpiano.html
272 lines (236 loc) · 11.8 KB
/
piano.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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>PianoQuest!</title>
<meta name="description" content="A-Frame - PianoQuest">
<script src="https://aframe.io/releases/1.4.2/aframe.min.js"></script>
<script src="https://unpkg.com/aframe-cubemap-component@2.1.1/dist/aframe-cubemap-component.min.js"></script>
<script src="https://code.createjs.com/1.0.0/soundjs.min.js"></script>
<script src="../dist/aframe-hand-tracking-controls-extras.js"></script>
<script>
AFRAME.registerComponent("touchy-fingertips", {
init: function () {
let left = document.getElementById("left-hand")
let right = document.getElementById("right-hand")
this.fingertips = {};
this.fingertipsInit = this.fingertipsInit.bind(this);
// grab the joint references
left.addEventListener("hand-tracking-extras-ready", this.fingertipsInit);
right.addEventListener("hand-tracking-extras-ready", this.fingertipsInit);
this.piano = document.querySelector("[piano]")
},
fingertipsInit: function (evt) {
var side = evt.detail.data.getSide();
var joints = evt.detail.data.joints;
var fingertips = {};
fingertips[side + "Thumb"] = { joint: joints.T_Tip };
fingertips[side + "Index"] = { joint: joints.I_Tip };
fingertips[side + "Middle"] = { joint: joints.M_Tip };
fingertips[side + "Ring"] = { joint: joints.R_Tip };
fingertips[side + "Little"] = { joint: joints.L_Tip };
// get ready for the lines!
const material = new THREE.LineBasicMaterial({
color: 0x0000ff,
opacity: 0.75
});
for (let finger in fingertips) {
let points = [new THREE.Vector3(0, 1, -1), new THREE.Vector3(0, 0, -1)];
const geometry = new THREE.BufferGeometry().setFromPoints(points);
const line = new THREE.Line(geometry, material);
line.frustumCulled = false;
line.visible = false;
this.el.object3D.add(line)
fingertips[finger].line = line;
fingertips[finger].points = points;
}
Object.assign(this.fingertips, fingertips)
},
tick: (function () {
const posV = new THREE.Vector3();
const dirV = new THREE.Vector3();
const normV = new THREE.Vector3();
const angE = new THREE.Euler();
const quatQ = new THREE.Quaternion();
const raycaster = new THREE.Raycaster();
const rayDir = new THREE.Vector3(0, -1, 0);
var intersected = {};
return function () {
// wait for the fingertips
if (Object.keys(this.fingertips).length === 0) return
var newIntersections = {};
for (let name in this.fingertips) {
let finger = this.fingertips[name]
if (finger.joint.isValid()) {
// debug
finger.joint.getPosition(posV)
finger.points[0].copy(posV);
finger.points[1].copy(finger.points[0]);
finger.points[1].y -= 0.005;
finger.line.geometry.setFromPoints(finger.points)
finger.line.visible = true;
posV.y += 0.1;
raycaster.set(posV, rayDir)
// check out the far ones
raycaster.far = 0.105;
let obj = raycaster.intersectObject(this.piano.object3D, true)
if (obj.length) {
let item = obj[0];
if (newIntersections[item.object.uuid]) continue;
newIntersections[item.object.uuid] = item.object;
}
/* cant press black so hard that white is pressed as well
for (let item of obj) {
if (newIntersections[item.object.uuid]) continue;
newIntersections[item.object.uuid] = item.object;
}*/
} else {
finger.line.visible = false;
}
}
for (let UUID in intersected) {
// still intersecting - do nothing
if (newIntersections[UUID]) {
delete newIntersections[UUID];
continue;
};
// intersection cleared
if (!newIntersections[UUID]) {
intersected[UUID].onIntersectionCleared();
delete intersected[UUID];
}
}
// play new items, and store them for later comparison
for (let newUUID in newIntersections) {
if (!newIntersections[newUUID].intersected)
newIntersections[newUUID].onIntersected();
intersected[newUUID] = newIntersections[newUUID];
}
}
})()
})
AFRAME.registerComponent("piano", {
init: function () {
/* Natural Keys */
let naturalKeyGeometry = new THREE.BoxGeometry(0.02, 0.005, 0.1);
const naturalRange = ["A3", "B3", "C4", "D4", "E4", "F4", "G4", "A4", "B4", "C5", "D5", "E5"];
const pianoPath = "assets/piano/";
const extension = ".mp3";
naturalRange.forEach((note, index) => {
// load the audio elements
createjs.Sound.registerSound(pianoPath + note + extension, note);
// create the 3js object;
let key = this.createKey(naturalKeyGeometry, new THREE.MeshBasicMaterial({ color: 0xfafafa }), note, 0.0225 * index, 0, 0);
this.el.object3D.add(key);
})
/* Flat keys */
let flatKeyGeometry = new THREE.BoxGeometry(0.0175, 0.005, 0.075);
const flatrange = ["Bb3", null, "Db4", "Eb4", null, "Gb4", "Ab4", "Bb4", null, "Db5", "Eb5"];
flatrange.forEach((note, index) => {
// skip the nulls
if (!note) return;
// load the audio elements
createjs.Sound.registerSound(pianoPath + note + extension, note);
// create the 3js object;
let key = this.createKey(flatKeyGeometry, new THREE.MeshBasicMaterial({ color: 0x0a0a0a }), note, 0.022 * index + 0.0125, 0.005, -0.04);
this.el.object3D.add(key);
})
},
createKey(geometry, material, name, xPos, yPos, zPos) {
let key = new THREE.Mesh(geometry, material);
key.name = name;
key.position.set(xPos, yPos, zPos);
key.originalColor = material.color.getHex();
key.onIntersected = function () {
// handle audio
if (!key.audioInstance) {
key.audioInstance = createjs.Sound.play(key.name);
key.audioInstance.duration = 750;
}
if (key.audioInstance.position) key.audioInstance.stop();
key.audioInstance.play();
key.intersected = true;
key.material.color.setHex(0x00ff00);
}
key.onIntersectionCleared = function () {
key.intersected = false;
key.material.color.setHex(key.originalColor);
}
return key;
},
remove: function () {
this.el.object3D.children.forEach(node => {
// get rid of the sound, geometry and material
node.parent.remove(node);
node.audioInstance.destroy();
node.geometry.dispose();
node.material.dispose();
})
}
})
AFRAME.registerComponent("start-the-show", {
init: function () {
let left = document.getElementById("left-hand")
let right = document.getElementById("right-hand")
this.fingertips = {};
this.setRWrist = this.setWrist.bind(this, "Right");
this.setLWrist = this.setWrist.bind(this, "Left");
this.cleanup = this.cleanup.bind(this);
// grab the joint references
left.addEventListener("hand-tracking-extras-ready", this.setLWrist);
right.addEventListener("hand-tracking-extras-ready", this.setRWrist);
this.el.addEventListener("animationcomplete", this.cleanup)
},
setWrist: function (side, evt) {
this[side + "Wrist"] = evt.detail.data.joints.Wrist;
},
cleanup: function () {
this.el.sceneEl.removeChild(this.el)
},
tick: (function () {
let lWristPos = new THREE.Vector3();
let rWristPos = new THREE.Vector3();
let camPos = new THREE.Vector3();
let animation = false;
return function () {
if (animation) return;
if (! (this["RightWrist"] && this["LeftWrist"])) return;
let cam = document.querySelector("[camera]")
camPos.copy(cam.object3D.position);
this.RightWrist.getPosition(rWristPos);
this.RightWrist.getPosition(lWristPos);
if (rWristPos.y - camPos.y > 0.1 || lWristPos.y - camPos.y > 0.1) {
animation = true;
this.el.emit("woopwoop")
}
}
})(),
remove: function () {
this.el.removeEventListener("animationcomplete", this.cleanup)
let left = document.getElementById("left-hand")
let right = document.getElementById("right-hand")
left.removeEventListener("hand-tracking-extras-ready", this.setLWrist);
right.removeEventListener("hand-tracking-extras-ready", this.setRWrist);
}
})
</script>
</head>
<body>
<a-scene touchy-fingertips>
<a-assets>
<img id="title-img" src="assets/images/PianoQuest.png">
</a-assets>
<a-entity position="0 1 -0.25" piano></a-entity>
<a-entity id="left-hand" hand-tracking-controls="hand: left; " hand-tracking-extras>
</a-entity>
<a-entity id="right-hand" hand-tracking-controls="hand: right" hand-tracking-extras>
</a-entity>
<a-plane material="src: #title-img; transparent: true" width="2" height="0.5" position="0 1 -3"></a-plane>
<!-- Sky + surprise sky -->
<a-sky color="#26184a" start-the-show
animation="property: material.opacity; to: 0.0; from: 1.0; dur: 1000; easing: easeOutQuad; startEvents: woopwoop">
</a-sky>
<a-entity cubemap="folder: assets/cubemaps/SwedishRoyalCastle/; edgeLength:500"></a-entity>
</a-scene>
</body>
</html>