-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path88.html
372 lines (287 loc) · 9.59 KB
/
88.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
<!DOCTYPE html>
<html>
<head>
<title>Rockstar</title>
</head>
<style>
body{
margin: 0;
overflow: hidden;
}
#loading, #start{
height: 100vh;
width: 100vw;
position: fixed;
}
#loading{
background-color: black;
z-index: 2000;
}
#start{
z-index: 1000;
opacity: 1;
background: rgba(0, 0, 0, .3);
transition: all .5s ;
}
#start .startButton{
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
color: white;
font-family: sans-serif;
font-size: 35px;
font-weight: lighter;
text-align: center;
text-decoration: none;
}
#start .startButton a:hover{
cursor: pointer;
}
#loading h1 {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
color: white;
font-family: sans-serif;
font-size: 1em;
font-weight: lighter;
text-align: center;
}
#description{
position: absolute;
bottom: 0;
left: 0;
margin: 25px;
color: white;
font-family: Helvetica, sans-serif;
}
#play{
font-family: Helvetica;
font-size: 1em;
position: absolute;
margin: 25px;
color: white;
bottom: 0;
right: 0;
}
#play:hover{
cursor: pointer;
}
a{
text-decoration: underline;
color: white;
}
</style>
<body>
<div id="loading"><h1>LOADING</h1></div>
<div id="start">
<div class="startButton">
<a>START</a>
</div>
</div>
<div id="container"></div>
<div id="play" onclick="toggleplay()">play</div>
<div id="description">
Visualizing audio using frequency data and instancing. Model by <a target="_blank" href="https://aprilding.myportfolio.com/">April Ding</a>.
</div>
</body>
<script src="bower_components/three.js/build/three.js"></script>
<script src="bower_components/three.js/examples/js/controls/TrackballControls.js"></script>
<script src="bower_components/dat.gui/dat.gui.min.js"></script>
<script src="bower_components/tween.js/src/Tween.js"></script>
<script>
var camera, scene, renderer, controls;
var mat, grid, WIDTH, HEIGHT;
var audioLoader, analyser, listener, sound;
var volumeThreshold = 1, decay = .1, hold = 0;
var time = 0;
var playing = false;
var modelGeom;
function resize(){
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
}
function init() {
var container = document.getElementById( 'container' );
renderer = new THREE.WebGLRenderer({antialias: true});
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCSoftShadowMap;
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight);
renderer.setClearColor(0x0c0c0c, 1.);
container.appendChild( renderer.domElement );
camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, .001, 10000 );
controls = new THREE.TrackballControls(camera, renderer.domElement);
controls.rotateSpeed = 2.0;
controls.panSpeed = 0.8;
controls.zoomSpeed = 1.5;
scene = new THREE.Scene();
scene.fog = new THREE.Fog(0x0c0c0c, 1, 50);
var hemisphereLight = new THREE.HemisphereLight(0xfceafc, 0x000000, .8)
scene.add(hemisphereLight);
var vertexShader = [
"attribute vec3 translate;",
"uniform float freqData[16];",
"uniform float width;",
"uniform float height;",
"uniform float magnitude;",
"varying float vScale;",
"varying float p;",
"void main(){",
"p = position.x;",
"vec4 mvPosition = modelViewMatrix * vec4( translate, 1.0 );",
"int index = int(floor(16.*(abs(translate.x)/width)));",
"float scale = freqData[index];",
"vScale = float(index)/width;",
"vec3 p = vec3(position.x, position.y * scale * magnitude, position.z);",
"mvPosition.xyz += p;",
"gl_Position = projectionMatrix * mvPosition;",
"}"
].join('\n');
var fragmentShader = [
"precision highp float;",
"uniform float max;",
"varying float vScale;",
"varying float p;",
"vec3 HUEtoRGB(float H){",
"H = mod(H,1.0);",
"float R = abs(H * 6.0 - 3.0) - 1.0;",
"float G = 2.0 - abs(H * 6.0 - 2.0);",
"float B = 2.0 - abs(H * 6.0 - 4.0);",
"return clamp(vec3(R,G,B),0.0,1.0);",
"}",
"vec3 HSLtoRGB(vec3 HSL){",
"vec3 RGB = HUEtoRGB(HSL.x);",
"float C = (1.0 - abs(2.0 * HSL.z - 1.0)) * HSL.y;",
"return (RGB - 0.5) * C + HSL.z;",
"}",
"void main() {",
// "float h = clamp(vScale, 0., .14);",
// "vec3 hsl = vec3(h, 50., .9);",
// "vec3 color = HSLtoRGB(hsl);",
// "gl_FragColor = vec4(color, .2);",
// "gl_FragColor = vec4(vec3(.3+vScale), .2);",
"gl_FragColor = vec4(HUEtoRGB(.66 + p+vScale*.2), .2);",
"}",
].join('\n');
var dataLength = 16;
var arr = Array.apply(null, Array(dataLength)).map(Number.prototype.valueOf,1);
var boxX = .1, boxY = .1;
var boxGeom = new THREE.BoxBufferGeometry(boxX, boxY, 1);
var geometry = new THREE.InstancedBufferGeometry();
geometry.copy(boxGeom);
var planeGeom = modelGeom,
vertices = planeGeom.vertices,
instances = vertices.length;
var typedArray = new Float32Array( instances * 3);
var index = 0;
for (var i=0; i<instances; i++){
var pos = vertices[i];
typedArray[index] = pos.x;
typedArray[index+1] = pos.y;
typedArray[index+2] = pos.z;
index += 3;
}
var translateAttribute = new THREE.InstancedBufferAttribute(typedArray, 3, 1);
translateAttribute.dynamic = true;
geometry.addAttribute('translate', translateAttribute);
WIDTH = HEIGHT = 5;
mat = new THREE.ShaderMaterial({
vertexShader : vertexShader,
fragmentShader : fragmentShader,
uniforms : {
'freqData': { 'value': arr },
'width': { 'value': WIDTH },
'height': { 'value': HEIGHT },
'magnitude': { 'value': .04 }
},
transparent: true
});
grid = new THREE.Mesh(geometry, mat);
scene.add(grid);
camera.add(listener);
grid.add(sound);
grid.rotation.y = -Math.PI;
grid.rotation.z = Math.PI;
grid.rotation.x = Math.PI;
camera.position.set(-2.5, 4, 7);
window.addEventListener('resize', resize);
}
function toggleplay(){
if(playing){
sound.pause();
playing = false;
document.getElementById('play').innerHTML = 'play';
}
else{
sound.play();
playing = true;
document.getElementById('play').innerHTML = 'pause';
}
}
function update(){
var freqDataInt = analyser.getFrequencyData();
var freqData = Array.from(freqDataInt);
grid.material.uniforms['freqData'].value = freqData;
var volume = 0;
for(var i=0; i<freqData.length; i++){
volume += freqData[i];
}
volume = volume/freqData.length;
if(volume>volumeThreshold && !hold){
volumeThreshold = volume;
hold = 1;
}
volumeThreshold = (volumeThreshold <= 0) ? 0 : volumeThreshold-decay;
hold = (hold <= 0) ? 0 : hold-.005;
grid.material.uniforms['magnitude'].value = .01+.05*(volume/256);
grid.rotation.y += .0005;
controls.update();
}
function animate(){
update();
renderer.render(scene, camera);
window.requestAnimationFrame(animate);
}
var modelManager = new THREE.LoadingManager();
var audioManager = new THREE.LoadingManager();
var loader = new THREE.JSONLoader(modelManager);
// create an AudioListener and add it to the camera
listener = new THREE.AudioListener();
// create an Audio source
sound = new THREE.PositionalAudio( listener );
// load a sound and set it as the Audio object's buffer
audioLoader = new THREE.AudioLoader(audioManager);
audioLoader.load( 'assets/rockstar.mp3', function( buffer ) {
sound.setBuffer( buffer );
sound.setVolume(0.7);
sound.setRefDistance( 20 );
});
// create an AudioAnalyser, passing in the sound and desired fftSize
analyser = new THREE.AudioAnalyser( sound, 32 );
audioManager.onLoad = function(){
loader.load(
'assets/88.json',
function(geometry){
modelGeom = geometry;
}
)
}
modelManager.onLoad = function(){
document.getElementById('loading').style.display = 'none';
init();
animate();
var b = document.getElementsByClassName('startButton')[0];
b.addEventListener('mousedown', function(){
toggleplay();
document.getElementById('start').style.opacity = 0;
setTimeout(function(){
document.getElementById('start').style.display = 'none';
}, 500);
})
}
</script>
</html>