-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
228 lines (180 loc) · 7.21 KB
/
app.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
class HSLHelper {
constructor(color) {
this.color = color;
this.temp = {h: 0, s: 0, l: 0};
}
get h() { return this.color.getHSL(this.temp).h; }
set h(h) {
const {s, l} = this.color.getHSL(this.temp);
this.color.setHSL(h, s, l);
}
get s() { return this.color.getHSL(this.temp).s; }
set s(s) {
const {h, l} = this.color.getHSL(this.temp);
this.color.setHSL(h, s, l);
}
get l() { return this.color.getHSL(this.temp).l; }
set l(l) {
const {h, s} = this.color.getHSL(this.temp);
this.color.setHSL(h, s, l);
}
}
var noise = new SimplexNoise();
var vizInit = function (){
var file = document.getElementById("thefile");
var audio = document.getElementById("audio");
var fileLabel = document.querySelector("label.file");
document.onload = function(e){
console.log(e);
audio.play();
play();
}
file.onchange = function(){
fileLabel.classList.add('normal');
audio.classList.add('active');
var files = this.files;
audio.src = URL.createObjectURL(files[0]);
audio.load();
audio.play();
play();
}
function play() {
audio.style.visibility = "visible";
var context = new AudioContext();
var src = context.createMediaElementSource(audio);
var analyser = context.createAnalyser();
src.connect(analyser);
analyser.connect(context.destination);
analyser.fftSize = 512;
var bufferLength = analyser.frequencyBinCount;
var dataArray = new Uint8Array(bufferLength);
var scene = new THREE.Scene();
var aspect = window.innerWidth / window.innerHeight;
var camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 1000);
camera.position.set( 20, 40, 20 );
camera.lookAt( scene.position );
scene.add(camera);
var renderer = new THREE.WebGLRenderer({ alpha: true, antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
var geometry = new THREE.PlaneGeometry( 400, 400, 40, 40);
var material = new THREE.MeshStandardMaterial( { wireframe: true, opacity: 0.3, transparent: true, side: THREE.DoubleSide} );
var grid = new THREE.Mesh( geometry, material );
grid.rotation.order = 'YXZ';
grid.material.color.setHex(0x00ff00);
grid.rotation.x = - Math.PI / 2;
grid.position.set(0,-20,0);
scene.add( grid );
var geometry1 = new THREE.IcosahedronGeometry(4, 4);
var material1 = new THREE.MeshLambertMaterial( {wireframe: true , transparent: false} );
var ball = new THREE.Mesh( geometry1, material1 );
ball.position.set(0,5,0);
scene.add( ball);
var ambientLight = new THREE.AmbientLight(
0x00ff00, 0.15
);
scene.add(ambientLight);
var spotLight = new THREE.SpotLight(0x00ff00);
spotLight.intensity = 1;
spotLight.position.set(-80, 100, 60);
spotLight.lookAt(ball);
spotLight.power = 5;
spotLight.castShadow = true;
scene.add(spotLight);
const helper1 = new HSLHelper(ambientLight.color);
const tl1 = new TimelineMax();
tl1.fromTo(helper1, {h: 0, s: 1, l: 0.5}, {duration: 25, h: 1, s: 1, l: 0.5, ease:Linear.easeNone});
tl1.repeat(-1);
const helper2 = new HSLHelper(spotLight.color);
const tl2 = new TimelineMax();
tl2.fromTo(helper2, {h: 0, s: 1, l: 0.5}, {duration: 25, h: 1, s: 1, l: 0.5, ease:Linear.easeNone});
tl2.repeat(-1);
const helper3 = new HSLHelper(grid.material.color);
const tl3 = new TimelineMax();
tl3.fromTo(helper3, {h: 0, s: 1, l: 0.5}, {duration: 25, h: 1, s: 1, l: 0.5, ease:Linear.easeNone});
tl3.repeat(-1);
document.getElementById('out').appendChild(renderer.domElement);
window.addEventListener('resize', onWindowResize, false);
render();
function render() {
analyser.getByteFrequencyData(dataArray);
var lowerHalfArray = dataArray.slice(0, (dataArray.length/2) - 1);
var upperHalfArray = dataArray.slice((dataArray.length/2) - 1, dataArray.length - 1);
var overallAvg = avg(dataArray);
var lowerMax = max(lowerHalfArray);
var lowerAvg = avg(lowerHalfArray);
var upperMax = max(upperHalfArray);
var upperAvg = avg(upperHalfArray);
var lowerMaxFr = lowerMax / lowerHalfArray.length;
var lowerAvgFr = lowerAvg / lowerHalfArray.length;
var upperMaxFr = upperMax / upperHalfArray.length;
var upperAvgFr = upperAvg / upperHalfArray.length;
makeRoughGround(grid, modulate(lowerMaxFr, 0, 0.5, 0.5, 2));
makeRoughBall(ball, modulate(Math.pow(lowerMaxFr, 0.4), 0, 1, 0, 6), modulate(upperAvgFr, 0, 1, 0, 3));
ball.rotation.y += 0.003;
renderer.render(scene, camera);
requestAnimationFrame(render);
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
function makeRoughBall(mesh, bassFr, treFr) {
mesh.geometry.vertices.forEach(function (vertex, i) {
var offset = mesh.geometry.parameters.radius;
var amp = 5;
var time = window.performance.now();
vertex.normalize();
var rf = 0.00001;
var distance = (offset+bassFr) + noise.noise3D(vertex.x + time *rf*7, vertex.y + time*rf*8, vertex.z + time*rf*9) * amp * treFr;
vertex.multiplyScalar(distance);
});
mesh.geometry.verticesNeedUpdate = true;
mesh.geometry.normalsNeedUpdate = true;
mesh.geometry.computeVertexNormals();
mesh.geometry.computeFaceNormals();
}
function makeRoughGround(mesh, distortionFr) {
mesh.geometry.vertices.forEach(function (vertex, i) {
var amp = 0.3;
var time = Date.now();
var distance = (noise.noise2D(vertex.x + time * 0.0003, vertex.y + time * 0.0001) + 0) * distortionFr * amp;
vertex.z = distance;
});
mesh.geometry.verticesNeedUpdate = true;
mesh.geometry.normalsNeedUpdate = true;
mesh.geometry.computeVertexNormals();
mesh.geometry.computeFaceNormals();
}
function makeRoughPlane(mesh, distortionFr) {
mesh.geometry.vertices.forEach(function (vertex, i) {
var amp = 2;
var time = Date.now();
var distance = (noise.noise2D(vertex.x + time * 0.0001, (Math.sin(Math.PI * 8 ) ) / 20 + time * 0.0001) + 0) * distortionFr * amp;
vertex.z = distance;
});
mesh.geometry.verticesNeedUpdate = true;
mesh.geometry.normalsNeedUpdate = true;
mesh.geometry.computeVertexNormals();
mesh.geometry.computeFaceNormals();
}
audio.play();
};
}
window.onload = vizInit();
document.body.addEventListener('touchend', function(ev) { context.resume(); });
function fractionate(val, minVal, maxVal) {
return (val - minVal)/(maxVal - minVal);
}
function modulate(val, minVal, maxVal, outMin, outMax) {
var fr = fractionate(val, minVal, maxVal);
var delta = outMax - outMin;
return outMin + (fr * delta);
}
function avg(arr){
var total = arr.reduce(function(sum, b) { return sum + b; });
return (total / arr.length);
}
function max(arr){
return arr.reduce(function(a, b){ return Math.max(a, b); })
}