-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
363 lines (322 loc) · 11.6 KB
/
main.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
// Variables which the user can edit via dat.GUI
let springs = new function() {
this.numSamples = 65;
this.nodesPerLine = 150;
this.springHeight = 14;
this.springPhase = 13;
}
let spiral = new function() {
this.inOutScale = 1.8;
this.phase = 5.7;
}
let segments = new function() {
this.segBrightness = 34;
this.segBaseRadius = 40;
}
//The presets loaded into dat GUI.
const presetJSON = {
"preset": "Segments Only",
"remembered": {
"Default": {
"0": {
"numSamples": 65,
"nodesPerLine": 150,
"springPhase": 13,
"springHeight": 14
},
"1": {
"inOutScale": 1.599332944113884,
"phase": 5.186617184989917
},
"2": {
"segBrightness": 37.146978910639895,
"segBaseRadius": 40
}
},
"Triangles": {
"0": {
"numSamples": 65,
"nodesPerLine": 150,
"springPhase": 100,
"springHeight": 37.775509121533496
},
"1": {
"inOutScale": 0.5102239600941513,
"phase": 20.81918149097012
},
"2": {
"segBrightness": 37.146978910639895,
"segBaseRadius": 48.148079759324055
}
},
"Flower": {
"0": {
"numSamples": 219.01266597081658,
"nodesPerLine": 144.96275848037894,
"springPhase": 83.36825087263828,
"springHeight": 18.171547409178306
},
"1": {
"inOutScale": 0.6191348584961245,
"phase": 31.061206381095083
},
"2": {
"segBrightness": 0,
"segBaseRadius": 0
}
},
"Segments Only": {
"0": {
"numSamples": 256,
"nodesPerLine": 1,
"springPhase": 0.1,
"springHeight": 1
},
"1": {
"inOutScale": 0.1,
"phase": 1
},
"2": {
"segBrightness": 50.34829992906089,
"segBaseRadius": 49.24818984419248
}
}
},
"closed": true,
"folders": {
"Springs": {
"preset": "Default",
"closed": true,
"folders": {}
},
"Spiral": {
"preset": "Default",
"closed": true,
"folders": {}
},
"Segments": {
"preset": "Default",
"closed": true,
"folders": {}
}
}
}
let gui = new dat.GUI({
load: presetJSON,
preset: 'Default'
});
gui.remember(springs);
gui.remember(spiral);
gui.remember(segments);
//Creating the folder structure to display all the GUI elements.
let f1 = gui.addFolder('Springs');
let numSamplesController = f1.add(springs,'numSamples',0,256).name("Number");
let nodesPerLineController = f1.add(springs,'nodesPerLine',1,300).name("Smoothness");
f1.add(springs,'springPhase',0.1,100).name("Springiness");
f1.add(springs,'springHeight',1,100).name("Height");
let f2 = gui.addFolder('Spiral');
f2.add(spiral,'inOutScale',0.1,10).name("Move In/Out");
f2.add(spiral,'phase',1,50).name("Phase");
let f3 = gui.addFolder('Segments');
let segBrightnessController = f3.add(segments,'segBrightness',0,100).name("Brightness");
let segBaseRadiusController = f3.add(segments,'segBaseRadius',0,100).name("Radius");
//Declaring the controllers which trigger reset of the visualisers arrays.
numSamplesController.onChange(function(val) {
resetArrays();
});
nodesPerLineController.onChange(function(val) {
resetArrays();
});
segBrightnessController.onChange(function(val) {
resetArrays();
});
segBaseRadiusController.onChange(function(val) {
resetArrays();
});
//Code begins for WebAudioAPI.
let context = new (window.AudioContext || window.webkitAudioContext)();
let analyser = context.createAnalyser();
let soundDataArray;
const MAX_SOUND_VALUE = 256;
//When the user chooses audio to visualise, this function is called.
//It starts playing the music, hides the input, and calls createAudioObjects.
audioInput.onchange = function() {
let sound = document.getElementById("sound");
let reader = new FileReader();
reader.onload = function(e) {
sound.src = this.result;
sound.controls = true;
sound.play();
};
reader.readAsDataURL(this.files[0]);
createAudioObjects();
document.getElementById('audioInput').style.visibility = 'hidden';
document.getElementById('audioInputLabel').style.visibility = 'hidden';
};
//Connects the audio source to the analyser and creating a suitably sized array to hold the frequency data.
function createAudioObjects() {
source = context.createMediaElementSource(document.getElementById("sound"));
source.connect(analyser);
analyser.connect(context.destination);
analyser.fftSize = 1024; //128, 256, 512, 1024 and 2048 are valid values.
let bufferLength = analyser.frequencyBinCount;
soundDataArray = new Uint8Array(bufferLength);
}
//Returns the overall average sound of the soundDataArray, normalized between 0 and 1;
function getAverageOfDataArray(soundDataArray){
let sum = 0;
for (let i = 0; i < soundDataArray.length; i++){
sum += soundDataArray[i];
}
let average = sum / soundDataArray.length;
return average / MAX_SOUND_VALUE;
}
//Returns the average of a small sample of the array. Index declares which sample you want, ideal for iteration.
function getSampleOfSoundData(index, noSampleSections, soundDataArray){
let sampleSize = Math.floor((soundDataArray.length/2) / noSampleSections);
//Note division by 2. I think I accidently initalize the soundDataArray as twice as long as it needs to be?
let minBound = index * sampleSize;
let maxBound = (index + 1) * sampleSize;
let sum = 0;
for (let i = minBound; i < maxBound; i++){
sum += soundDataArray[i];
}
let average = sum / sampleSize;
return average / MAX_SOUND_VALUE;
}
//Code begins for THREE js scene setup.
let scene = new THREE.Scene();
let camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
let controls = new THREE.OrbitControls(camera);
//Note that I use a customised THREE.OrbitControls class, which can be found at https://codepen.io/jhancock532/pen/VdLmvN
//I commented out the line event.preventDefault(); in the mouse down function, fixing select preset for the dat.GUI (See line 661)
controls.dampingFactor = 0.1;
controls.rotateSpeed = 0.005;
controls.target = new THREE.Vector3(0,10,0);
controls.enableDamping = true;
controls.enableKeys = false;
controls.enablePan = false;
controls.maxDistance = 400;
let renderer = new THREE.WebGLRenderer();
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );
camera.position.z = 0;
camera.position.y = 60;
controls.update();
//Creation of the geometry, material and line arrays
let springMaterialArray = [];
let springGeometryArray = [];
let springsArray = [];
let segmentGeometryArray = [];
let segmentMaterialArray = [];
let segmentsArray = [];
setUpAllArrays();
createSkybox();
//Makes a subtle grey background.
function createSkybox(){
let sphereBox = new THREE.SphereGeometry( 500, 32, 32 );
let sphereMaterial = new THREE.MeshBasicMaterial( {color: 0x111111, side: 2} );
let sphere = new THREE.Mesh( sphereBox, sphereMaterial );
scene.add( sphere );
}
//Creates all the geometries, materials and meshes, properties of which are animated later.
function setUpAllArrays() {
for (let i = 0; i < springs.numSamples; i++){
segmentGeometryArray.push(new THREE.CircleGeometry(segments.segBaseRadius, 64, i*2*Math.PI/springs.numSamples, 2*Math.PI/springs.numSamples));
springGeometryArray.push(new THREE.Geometry()); //Vertices are added iteratively.
springMaterialArray.push(new THREE.LineBasicMaterial({
color: getRainbowColour(i,springs.numSamples, 50)
}));
segmentMaterialArray.push(new THREE.MeshBasicMaterial( {
color: getRainbowColour(i,springs.numSamples, segments.segBrightness),
side: 2
}));
let XBaseline = getSpiralPosition("X",i);
let ZBaseline = getSpiralPosition("Z",i);
//The height (y value) of the vertices is determined by audio data, so we don't have to worry about it here.
for (let j = 0; j < springs.nodesPerLine; j++){
springGeometryArray[i].vertices.push(new THREE.Vector3(XBaseline, 0, ZBaseline));
}
//Combine together the geometry and material to create the mesh, store this in the global array.
springsArray.push(new THREE.Line( springGeometryArray[i], springMaterialArray[i]));
segmentsArray.push(new THREE.Mesh(segmentGeometryArray[i], segmentMaterialArray[i]));
segmentsArray[i].rotateX( Math.PI / 2 ); //A circle is initally created in the wrong plane, so we rotate it here.
//We add the contents of the global arrays into the scene.
scene.add(springsArray[i]);
scene.add(segmentsArray[i]);
}
}
//A small mathematical function to give X and Z coords of a spiral.
function getSpiralPosition(axis, i){
let t = 1.5 + i*Math.PI*spiral.phase/(springs.numSamples);
//1.5 is an offset to stop a spiral being created at 0,0.
if (axis == "X"){
return t * spiral.inOutScale * Math.cos(t);
} else {
return t * spiral.inOutScale * Math.sin(t);
}
}
//Creates a clean slate and repopulates all arrays with new values. Resets the scene as well, while creating a new skybox.
function resetArrays() {
springMaterialArray = [];
springGeometryArray = [];
springsArray = [];
segmentGeometryArray = [];
segmentMaterialArray = [];
segmentsArray = [];
//Code to remove all children from a scene.
for(var i = scene.children.length - 1; i >= 0; i--){
obj = scene.children[i];
scene.remove(obj);
}
createSkybox();
setUpAllArrays();
}
//Returns a rainbow spectrum when iterated through with i, going from 0 to maxValue, with brightness as specified.
function getRainbowColour(i, maxValue, brightness){
return new THREE.Color("hsl("+(i*359)/maxValue+", 100%, "+String(Math.floor(brightness))+"%)");
}
//Code to animate the visualisation begins.
function updateMeshes(){
let volumeBoost = 0; //VolumeBoost is used to speed up the segments spinning.
if ((soundDataArray === undefined) == false) {
volumeBoost = getAverageOfDataArray(soundDataArray);
}
for (let i = 0; i < springs.numSamples; i++){
let sampleLevel = 1;
//Carefully access the soundDataArray, as it doesn't exist until the user selects a sound file.
if ((soundDataArray === undefined) == false) {
sampleLevel = getSampleOfSoundData(i, springs.numSamples, soundDataArray);
}
//Update the springs.
for (let j = 0; j < springs.nodesPerLine; j++){
//Wave factor is a simple trig representation of a circle. You could make this any lissajous figure.
//It's the amount the line point is offset by from the center line in the middle in of the spring.
let waveFactorOne = Math.sin(Math.PI*springs.springPhase*(j/springs.nodesPerLine)) - 0.5;
let waveFactorTwo = Math.cos(Math.PI*springs.springPhase*(j/springs.nodesPerLine)) - 0.5;
let XBaseline = getSpiralPosition("X",i);
let ZBaseline = getSpiralPosition("Z",i);
springsArray[i].geometry.vertices[j] = new THREE.Vector3(
XBaseline+waveFactorOne,
sampleLevel*springs.springHeight*(j/springs.nodesPerLine),
ZBaseline+waveFactorTwo);
}
springsArray[i].geometry.verticesNeedUpdate = true;
//This line is vital for animation of updated geometries.
//Update the segments.
segmentsArray[i].scale.set(0.01+sampleLevel,0.01+sampleLevel,1);
segmentsArray[i].rotateZ(0.003+0.01*volumeBoost);
//In this case, we have transformed on the mesh directly, so it knows to update already.
}
}
function animate() {
requestAnimationFrame(animate);
controls.update();
//Update the soundDataArray with the new wave frequency.
if((soundDataArray === undefined) == false){
analyser.getByteFrequencyData(soundDataArray);
}
updateMeshes();
renderer.render(scene, camera);
}
animate();