-
Notifications
You must be signed in to change notification settings - Fork 1
/
index_Demo1.html
424 lines (321 loc) · 11.6 KB
/
index_Demo1.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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Interactive Graphics - Final Project</title>
<link rel="stylesheet" href="style/style.css">
<meta name="description" content="Interactive Graphics - Final Project - 2019">
<meta name="keywords" content="HTML, CSS, JavaScript, GLSL">
<meta name="author" content="JohnnyMDA">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="shortcut icon" href="ig_cube.ico">
<link href="https://fonts.googleapis.com/css?family=Droid+Serif:400,400i,700|PT+Serif:400,400i&subset=latin-ext" rel="stylesheet">
<link rel="stylesheet" type="text/css" href="style/style.css">
<script src="Common/jquery-3.4.0.min.js"></script>
<script src="Common/general_purpose_scripts.js"></script>
<script src="Common/three.min.js"></script>
<script src="Common/Stats.js"></script> <!-- JavaScript Performance Monitor -->
</head>
<body>
<div id="animate"> </div>
<div align="center">
<audio id="my_audio" src="audio/Me & My Toothbrush - Push the Tempo.mp3" autobuffer autoloop loop controls>Your browser does not support the audio element.</audio>
</div>
<script>
// set variables
var audio,
audioContext,
frameBufferLength,
bufferSize,
signal,
peak,
fft,
display,
stats,
renderer,
scene,
sceneHeight,
sceneWidth,
camera,
cameraFar,
cameraNear,
cameraAspect,
cameraViewAngle,
sphere,
cubes,
numCubes;
// Initialize the visualization
window.onload=function()
{
init();
}
function init()
{
// enable THREE.js logs
// console.log(THREE);
// Get the audio element and a new Audio Context (Web Audio API)
audio = document.getElementById("my_audio");
audioContext = newAudioContext();
// Get the DOM element where to display
display = document.getElementById("animate");
console.log("display = " + display);
// Set up the scene dimensions
sceneHeight = window.innerHeight;
sceneWidth = window.innerWidth;
// Set the camera properties
cameraFar = 10000;
cameraNear = 0.1;
cameraAspect = sceneWidth / sceneHeight;
cameraViewAngle = 65;
// Create the renderer and camera (the WebGL one)
renderer = new THREE.WebGLRenderer( { antialias: true } );
camera = new THREE.PerspectiveCamera(
cameraViewAngle,
cameraAspect,
cameraNear,
cameraFar
);
// Create the scene
scene = new THREE.Scene();
// Set the scene's background color
//scene.background = new THREE.Color( '#FCFCFC' );
// add some fog in the scene (black fog)
scene.fog = new THREE.FogExp2( 0x000000 , 0.00075);
// Set the Camera position (initial)
camera.position.z = 560; // move backward
// START the (WebGL) renderer
renderer.setSize(sceneWidth, sceneHeight);
// Append the renderer DOM element on 'display'
display.appendChild(renderer.domElement);
// Create the (main) sphere
createSphere();
// set up the Stats.js for statistic display and add them to the DOM
stats = new Stats();
stats.showPanel( 0 ); // 0: fps, 1: ms, 2: mb, 3+: custom
stats.dom.style.top = '10px';
stats.dom.style.left = '10px';
document.body.appendChild( stats.dom );
//#########################//
//#### EVENT LISTENERS ####//
//#########################//
if(true)
{
window.addEventListener("resize", function()
{
// Set up the scene dimensions
sceneHeight = window.innerHeight;
sceneWidth = window.innerWidth;
camera.aspect = sceneWidth / sceneHeight;
camera.updateProjectionMatrix();
renderer.setSize(sceneWidth, sceneHeight);
renderer.render( scene, camera );
console.log("DEBUG: window resized !\n");
});
document.addEventListener('visibilitychange', function()
{
if (document.hidden)
{
// stop rendering && stop audio
// ... TODOS ...
}else{
// resume rendering && audio
// ... TODOS ...
}
});
audio.addEventListener("loadeddata", onLoadedMetadata);
audio.addEventListener("MozAudioAvailable", onAudioAvailable);
// Call the loop() routine
loop();
}
function createSphere()
{
var radius = 100,
segments = 32,
rings = 16,
sphereFaces,
sphereMaterial,
cubeSize = 10,
cubeHalfSize = cubeSize / 2,
particleGeometry,
particles,
numParticles = 4000,
spaceRangePS = 1000;
//#########################//
//###### MAIN SPHERE ######//
//#########################//
sphere = new THREE.Mesh(
new THREE.SphereGeometry( radius, segments, rings),
new THREE.MeshNormalMaterial() // remove it later
// we don't want to see it
);
// Add the sphere to the scene
scene.add(sphere);
//###################//
//###### CUBES ######//
//###################//
// Get the faces of the sphere
sphereFaces = sphere.geometry.faces;
// New cubes array
cubes = [];
// Loop through every sphere's face
var cube, sphereFace, vertices, scaleVector, faceCentroid;
for (var i=0, len=sphereFaces.length; i < len; i++)
{
// Get the i-th sphere's face
sphereFace = sphereFaces[i];
// Create the i-th cube
cube = new THREE.Mesh(
new THREE.BoxGeometry(cubeSize, cubeSize, cubeSize),
new THREE.MeshNormalMaterial()
);
// Get the cube vertexes (corners)
vertices = cube.geometry.vertices;
for (var v=0; v < vertices.length; v++)
{
// Shift each vertex in order to e able to scale
// the cube from the bottom rather then the center
vertices[v].z -= cubeHalfSize;
}
// Move the v-th cube to the i-th face's center
faceCentroid = new THREE.Vector3( 0, 0, 0 );
faceCentroid.add( sphere.geometry.vertices[ sphereFace.a ] );
faceCentroid.add( sphere.geometry.vertices[ sphereFace.b ] );
faceCentroid.add( sphere.geometry.vertices[ sphereFace.c ] );
faceCentroid.divideScalar( 3 );
cube.position.x = faceCentroid.x;
cube.position.y = faceCentroid.y;
cube.position.z = faceCentroid.z;
// Scale the cube so it's smaller near the poles of the sphere (visual effect)
scaleValue = Math.abs(cube.position.y) / 100;
cube.scale.x = 1 - (scaleValue * 0.7);
cube.scale.y = 1 - (scaleValue * 0.7);
cube.scale.z = 1 - (scaleValue * 0.7);
cube.scaleValue = 1 - (scaleValue * 0.7);
// Make the cube look at the center of the sphere
cube.lookAt(sphere.position);
// Push the new cube in the cubes' array
cubes.push(cube);
// Add the new cube to the scene
sphere.add(cube);
};
numCubes = cubes.length;
//#########################//
//#### PARTICLE SYSTEM ####//
//#########################//
var v,
color,
size,
materials = [ ],
particlesParamsCollection = [ [0xfcfcfc, 1.75],
[0xcccccc, 1.50],
[0xaaaaaa, 1.25],
[0x888888, 0.75],
[0x666666, 0.50] ]; // [hex color, size]
// New geometry for the particles
particleGeometry = new THREE.Geometry();
// Create and add the particles' vectors to the geometry
for (var i=0; i < numParticles; i++)
{
v = new THREE.Vector3( Math.random() * 2*spaceRangePS - spaceRangePS,
Math.random() * 2*spaceRangePS - spaceRangePS,
Math.random() * 2*spaceRangePS - spaceRangePS
); // (random points around 0 in spaceRangePS)
particleGeometry.vertices.push(v);
};
// create ONE PARTICLE SYSTEM for each [size, color]
for (var i=0, len=particlesParamsCollection.length; i < len; i++)
{
color = particlesParamsCollection[i][0];
size = particlesParamsCollection[i][1];
materials[i] = new THREE.PointsMaterial( {size : size} );
materials[i].color.setHex(color);
particles = new THREE.Points( particleGeometry, materials[i] );
particles.rotation.x = Math.random() * 5;
particles.rotation.y = Math.random() * 5;
particles.rotation.z = Math.random() * 5;
scene.add(particles);
};
};
function loop() {
// START STATS.js monitoring
stats.begin();
var cube;
for (var i=0; i < numCubes; i++)
{
cube = cubes[i];
// Scale the cube high as the i-th peak amplitude (demo 1)
// cube.scale.z = cube.scaleValue + ( (peak[i]) ? 1 + (3 * (fft.spectrum[i] / peak[i])) : 0);
};
// Sphere rotation (all the hierarchical structure of the sphere will rotate)
sphere.rotation.x += 0.01;
sphere.rotation.y += 0.01;
sphere.rotation.z -= 0.0085;
// Particle system rotation
// ...TODO...
// Render the scene
renderer.render(scene, camera);
// END STATS.js monitoring
stats.end();
// new Animation Frame Request
window.requestAnimationFrame(loop);
}
// Run when metadata has loaded on the audio to be played
function onLoadedMetadata()
{
console.log("<<< start --- onLoadedMetadata() >>>");
// Get the # of channels of the audio (1-mono, 2-stereo)
channels = audio.mozChannels;
// Set up the frame buffer length
frameBufferLength = audio.mozFrameBufferLength;
// We will use mono for dsp.js, so the buffer size is given by:
bufferSize = frameBufferLength / channels;
// Set up dsp.js for audio analysis (ready to use)
signal = new Float32Array(bufferSize);
peak = new Float32Array(bufferSize);
//fft = new FFT(bufferSize, 44100);
};
// Run when audio data is available
function onAudioAvailable(event)
{
// // event.frameBuffer is a Float32Array with the raw audio data (32-bit float values) obtained from decoding the audio;
// console.log("Audio available, processing...");
// // Audio FFT visualization from dsp.js;
// // Deinterleave and mix down to <mono> channel audio;
// signal = DSP.getChannel(DSP.MIX, event.frameBuffer);
// // Perform forward transform of the signal
// fft.forward(signal);
// // Compute and get the peak values of the signal
// for (var i=0; i < bufferSize; i++)
// {
// // Equalizer: attenuate low frequents and boost the high ones
// fft.spectrum[i] *= -1 * Math.log((fft.bufferSize / 2 - i) * (0.5 / fft.bufferSize / 2)) * fft.bufferSize;
// if (peaak[i] < fft.spectrum[i])
// {
// // Assign a new peak value
// peak[i] = fft.spectrum[i];
// }
// else
// {
// // decreasing the peak slowly till a new peak is found
// peak[i] *= 0.987;
// }
// }
}; // --- end onAudioAvailable(event) --- //
function newAudioContext()
{
var contextClass = ( window.AudioContext || window.webkitAudioContext ||
window.mozAudioContext || window.oAudioContext ||
window.msAudioContext );
if (contextClass)
{ // Web Audio API is available.
console.log("DEBUG: New audioContext acquired !");
return new contextClass();
}else{
// No Web Audio API support on the current browser
window.location.href = 'alert.html';
}
};
}; // --- end init() --- //
</script>
</body>
</html>