-
-
Notifications
You must be signed in to change notification settings - Fork 116
/
Copy pathpiano.html
452 lines (410 loc) · 13.6 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
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>MathBox - Piano</title>
<script
type="text/javascript"
src="https://cdn.jsdelivr.net/npm/three@0.137.0/build/three.min.js"
></script>
<script
type="text/javascript"
src="https://cdn.jsdelivr.net/npm/three@0.137.0/examples/js/controls/OrbitControls.js"
></script>
<!--
- a minified version mathbox.min.js is also available;
- recommend using a specific version (not @latest) in public sites
-->
<script
type="text/javascript"
src="https://cdn.jsdelivr.net/npm/mathbox@latest/build/bundle/mathbox.js"
></script>
<link
rel="stylesheet"
href="https://cdn.jsdelivr.net/npm/mathbox@latest/build/mathbox.css"
/>
<meta name="viewport" content="initial-scale=1, maximum-scale=1" />
</head>
<body>
<script>
/*
{DocComment} Piano
Generates 5 black and 7 white keys of a piano keyboard, and then uses repeat
and spread to make multiple copies. There is a wavy-shader that makes the
keyboard ripple, and there is a keytilt-shader that takes the stream of points
for the keyboard and a separate stream indicating which keys are down, and
combines them to 'play' the piano.
*/
</script>
<script type="application/glsl" id="wavy-shader">
// Warps coordinates. Makes the whole keyboard wave.
uniform float time; // Additional declaration to import time.
vec4 getSample(vec4 xyz);
vec4 getFramesSample(vec4 wxyz) {
vec4 v= getSample(wxyz);
float t = v.x + time ;
// Use 'beat frequencies' between two close frequencies to make a warping
// in y and z that comes and goes.
// We are making a very small adjustment on the coordinate, v.
v = v + 0.003*vec4(
0.3*cos( 4.4 * t ),
cos( 4.3 * t )-cos( 4.2 * t ),
cos( 4.1 * t )-cos( 4.0 * t )+2.5,
0.0);
return( v );
}
</script>
<script type="application/glsl" id="keytilt-shader">
// Moves selected keys down.
// We stream data in from two sources....
vec4 getSampleA(vec4 xyzw); // External source, key movement.
vec4 getSample(vec4 xyzw); // Original source, points of all keys.
vec4 getFramesSample(vec4 xyzw) {
vec4 v = getSample(xyzw);
float a = getSampleA(xyzw).x;
//This commented out line for 'u' is an alternative that just moves the whole key down.
//vec4 u = vec4( 0.0, 0.0, a, 0.0 );
//This value for 'u' moves the front down more than the back.
//As the angle we are tilting through is small, that's good enough.
vec4 u = vec4( 0.0, 0.0, a * (v.y +0.2)* 3.5, 0.0 );
return vec4( v + u);
}
</script>
<script>
mathbox = MathBox.mathBox({
plugins: ["core", "controls", "cursor"],
controls: {
klass: THREE.OrbitControls,
},
});
three = mathbox.three;
three.renderer.setClearColor(new THREE.Color(0x6060a0), 1.0);
var view = mathbox.camera({
lookAt: [0, 0, 0],
position: [0.7, 0.4, -0.2],
up: [0, 1, 0],
proxy: true,
});
// Wrap the view inside a presentation slide to control scripts
view = view
.present({
index: 1,
})
.slide({
steps: 2,
});
// Add a translucent grid
view
.grid({
width: 5,
opacity: 0.5,
axes: [1, 3],
color: 0x2020a0,
blending: "add",
zOrder: 1,
zWrite: false,
})
// Fade out on step
.step({
pace: 3,
script: [{ props: { opacity: 0.5 } }, { props: { opacity: 0 } }],
});
// Emits the four points of one face of a cuboid.
// The faces are numbered 0..5.
// The cuboid has dimensions w,h,d and is positioned at x,y,z.
function cuboid_face(emit, w, h, d, x, y, z, face) {
// faces 3,4,5 are displaced relative to their partner
// faces 1,2,3.
if (face == 3) x += w;
if (face == 4) y += h;
if (face == 5) z += d;
// It's now as if we are drawing face 0,1 or 2.
face = face % 3;
// Each face lacks one of the 3 dimensions.
// The conditionals ensure we generate 4 points, not 6.
// We are just stepping in two of the dimensions.
if (face != 0) {
x += w;
emit(x, y, z);
}
if (face != 1) {
y += h;
emit(x, y, z);
}
if (face != 2) {
z += d;
emit(x, y, z);
}
if (face != 0) {
x -= w;
emit(x, y, z);
}
if (face != 1) {
y -= h;
emit(x, y, z);
}
if (face != 2) {
z -= d;
emit(x, y, z);
}
}
// The white keys of a piano repeat after 7
// We could set nKeys to 56 here. Instead we set it low
// and then repeat on the GPU.
// This example was originally built without nRepeats, and
// then adapted to repeat on the GPU.
nKeys = 7; // White keys generated in javascript
nRepeats = 8; // Number of repeats on GPU. (usually 8, can be as high as 800)
// Fewer black keys.
nBlackKeys = Math.floor((nKeys * 5) / 7 + 0.5);
var keyDepth1 = 1.5; // Distance front edge to black key.
var keyDepth2 = 3; // Distance from start of black key to back.
var keyGap = 0.1; // Gap between keys.
var keyPlayInterval = 2; // How often we have a key that's down.
// r is the width of one white key, and is used to scale all the other
// measurements.
var r = 2 / (nKeys * nRepeats * (1 + keyGap));
// This array with an item per key can set which keys are down, individually.
// Our formula for delta is setting every 2nd key.
view.array({
width: nKeys * nRepeats,
channels: 1,
expr: function (emit, key, t) {
adjustedTime = t * (5 + (key % 7) / 10) + key;
delta = (0.25 / nRepeats) * Math.max(0, Math.sin(adjustedTime) - 0.7);
emit(key % keyPlayInterval == 0 ? -delta : 0);
},
});
// The next four nodes convert our
// (nKeys x nRepeats) array into the shape
// (4) * (12 * nKeys) * (nRepeats)
view.repeat({
items: 4 * 12, // 4 points per face, 12 faces per key.
});
// The splits and joins don't affect the number of points.
view.split({
axis: "width",
length: nKeys,
});
view.split({
axis: "items",
length: 4,
});
view.join({
id: "whitekeydown",
axis: "width",
});
// The white keys.
view.array({
// 12 faces per white key.
// (Because we draw each white key as two cuboids).
// We are drawing one more face than we need to for each white key.
// It is worth it for the convenience of simpler building blocks.
id: "whitekeys",
width: 12 * nKeys,
items: 4, // Four points per face
channels: 3, // Three dimensions per point
expr: function (emit, i, t) {
var key = Math.floor(i / 12); // Key number 0..nKeys-1 in this repetition
var cuboid = Math.floor(i / 6); // Cuboid number 0..2*nKeys-1 in this repetition
var face = i % 6; // Face number 0..5 for this cuboid.
// Keys start on the left
var xDisplace = -1 / (r * nRepeats);
// Keys are 1 unit apart, plus a gap
xDisplace += key * (1 + keyGap);
// Settings for even numbered cuboids, the front of the keys.
var yDisplace = 0;
var keyWidth = 1.0;
var keyDepth = keyDepth1;
// Odd numbered cuboids have black keys and are longer, further back,
// displaced and narrower than the cuboid at the 'front' of the key.
if (cuboid % 2 == 1) {
keyDepth = keyDepth2; // longer
yDisplace = keyDepth1; // further back
// All but 0 and 4 are displaced half a black key width to the right.
if (key % 4 != 0) xDisplace += 0.25 + keyGap / 2;
// 0,3,4 and 6 are slightly wider than 1,2 and 5.
if (key == 0 || key == 3 || key == 4 || key == 6)
keyWidth = 0.75 - keyGap / 2;
else {
keyWidth = 0.5 - keyGap;
}
}
// r is the unit of size.
cuboid_face(
emit,
-r * keyWidth,
-r * keyDepth,
r / 4,
-r * xDisplace,
-r * yDisplace,
r / 4,
face
);
},
});
// 'repeat' replicates in one of the dimensions.
// We were only using the width and items dimensions, so this
// repeat on 'height' adds a new dimension to our array of points.
view.repeat({
height: nRepeats,
});
// 'spread' transfers information from our array dimensions into
// the vec4's actually in the array.
// We're spreading based on the newly added dimension.
// That's quite a common pattern.
var whitekeys = view.spread({
unit: "absolute",
height: [-2 / nRepeats, 0, 0, 0], // Affect only the 'x' of the vec4s.
});
// This shader is merging two streams of information.
// The whitekeydown source has one float item per key.
// It's being merged with 48x vec4 for each key that give the faces.
// This only defines the shader.
view.shader({
sources: ["#whitekeydown"], // extra sources, in addition to whitekeys.
code: "#keytilt-shader",
});
// This actually invokes the shader.
// The shader is implicitly bound.
// Leave this out, and the keys won't play.
view.resample({
width: 12 * nKeys,
height: nRepeats,
});
// These two steps warp the keyboard.
view.shader(
{
code: "#wavy-shader",
},
// The second argument to shader (or indeed any node) says what properties
// are recalculated every tick. Exception: <expr> is re-evaluated every tick,
// by default, even though in the first argument.
{
time: function (t) {
return t;
},
}
);
view.resample({
width: 12 * nKeys,
height: nRepeats,
});
// The earlier emits put values in to x,y,z, but the way we are viewing this,
// our 'y' goes into the page and our z goes up. We wouldn't need this
// swizzle if we'd emitted xyz in a different order.
view.swizzle({
order: "yzxw",
});
// Explode the geometry on step
view
.spread({
unit: "relative",
})
.step({
pace: 3,
script: [
{ props: { width: [0, 0, 0], height: [0, 0, 0] } },
{ props: { width: [0, 1, 0], height: [0, 0, -1] } },
],
});
// Draw the white keys.
// Take groups of four items, from the full width x height array of points,
// and treat those groups as faces (rectangles).
view.face({
color: 0xffffff,
shaded: true,
});
// The black keys
view.array({
width: 6 * nBlackKeys,
expr: function (emit, i, t) {
var face = i % 6; // Face number 0..5 within cuboid
var key = Math.floor(i / 6); // Key number 0..nBlackKeys-1
var yDisplace = keyDepth1 + keyGap;
var keyWidth = 0.5; // Half the width of a white key
var keyDepth = keyDepth2;
var xDisplace = -1 / (r * nRepeats); // start on the left
// The 7/5 indicates 5 black keys need to be spread out to the space that
// 7 white keys take. The first black key starts 0.75 of a key over, plus
// half a key gap.
xDisplace +=
Math.floor((key * 7) / 5) * (1 + keyGap) + 0.75 + keyGap / 2;
cuboid_face(
emit,
-r * keyWidth,
-r * keyDepth,
r / 2,
-r * xDisplace,
-r * yDisplace,
r / 4,
face
);
},
items: 4, // 4 points per face.
channels: 3, // xyz values for each point.
});
view.repeat({
height: nRepeats,
});
view.spread({
unit: "absolute",
height: [-2 / nRepeats, 0, 0, 0],
});
view.shader(
{
code: "#wavy-shader",
},
{
time: function (t) {
return t;
},
}
);
view.resample({
width: 6 * nBlackKeys,
height: nRepeats,
});
view.swizzle({
order: "yzxw",
});
// Explode the geometry on step
view
.spread({
unit: "relative",
})
.step({
pace: 3,
script: [
{ props: { width: [0, 0, 0], height: [0, 0, 0] } },
{ props: { width: [0, 1, 0], height: [1, 0, 0] } },
],
});
// Draw the black keys.
view.face({
color: 0x000000,
shaded: true,
});
// Control presentation with button, swap between slide 1 and 2.
var present = mathbox.select("present");
var toggle = false;
var explode = function () {
toggle = !toggle;
present.set("index", 1 + toggle);
};
</script>
<button
style="
position: absolute;
bottom: 15px;
left: 50%;
margin-left: -60px;
width: 120px;
font-size: 20px;
"
onclick="explode()"
>
Explode
</button>
</body>
</html>