-
Notifications
You must be signed in to change notification settings - Fork 11
/
cube-visualizer.js
145 lines (109 loc) · 3.76 KB
/
cube-visualizer.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
// cube-visualizer.js
function AudioVisualizer(element) {
//constants
this.numberOfCubes = 40;
this.delta = 0
//Rendering
this.scene;
this.camera;
this.renderer;
this.animationId = 0;
this.activeColorIdx = 0;
this.colorSelections = [
0x82b1ff,
0xff2b60,
0x42f462,
0xe842ff,
0xffb938
];
this.element = element;
this.cubes = new Array();
this.cubesSides = new Array();
}
AudioVisualizer.prototype.initialize = function () {
this.scene = new THREE.Scene();
this.camera = new THREE.PerspectiveCamera( 75, this.element.clientWidth / this.element.clientHeight, 0.1, 1000);
this.createCubes();
// Base lighting
var light = new THREE.PointLight(0x9E15EC, 1, 100, 2);
light.position.set(-4, 5, -1);
this.scene.add(light);
var light = new THREE.PointLight(0x0B8AE2, 1, 100, 2);
light.position.set(6, 5, 1);
this.scene.add(light);
// Ambient light
this.scene.add(new THREE.AmbientLight(0xbfbfbf));
this.renderer = new THREE.WebGLRenderer();
this.renderer.setSize( this.element.clientWidth, this.element.clientHeight );
this.element.appendChild(this.renderer.domElement);
var self = this;
window.addEventListener( 'resize', function() {
self.camera.aspect = self.element.clientWidth / self.element.clientHeight;
self.camera.updateProjectionMatrix();
self.renderer.setSize( self.element.clientWidth, self.element.clientHeight );
}, false );
}
AudioVisualizer.prototype.createCubes = function () {
for (var x = -3; x <= 3; x++) {
for (var z = -3; z <= 3; z++) {
var geometry = new THREE.BoxGeometry(1, 1, 1);
for (var i = geometry.faces.length - 1; i >= 0; i--) {
if (i !== 4 && i !== 5) {
geometry.faces[i].color.setHex(this.colorSelections[this.activeColorIdx]);
this.cubesSides.push(geometry.faces[i]);
}
}
var material = new THREE.MeshLambertMaterial({color: 0xEEEEEE, vertexColors: THREE.FaceColors});
var cube = new THREE.Mesh(geometry, material);
cube.position.x = 1.2 * x;
cube.position.z = 1.2 * z;
console.log(cube)
this.cubes.push(cube);
this.scene.add(cube);
}
}
}
AudioVisualizer.prototype.setColor = function(color) {
for (var i = 0; i<this.cubesSides.length; i++) {
this.cubesSides[i].color.setHex(color);
}
for (var j = 0; j<this.cubes.length; j++) {
this.cubes[j].geometry.colorsNeedUpdate = true;
}
}
AudioVisualizer.prototype.toggle = function() {
this.activeColorIdx++;
this.activeColorIdx = this.activeColorIdx % this.colorSelections.length;
this.setColor(this.colorSelections[this.activeColorIdx])
}
AudioVisualizer.prototype.animate = function(audioSource) {
this.render(audioSource);
var that = this;
this.animationId = requestAnimationFrame(function() {
that.animate(audioSource);
});
}
AudioVisualizer.prototype.stop = function() {
cancelAnimationFrame(this.animationId);
}
AudioVisualizer.prototype.render = function(audioSource, clamp=96) {
var noSignal = 50
const data = audioSource.streamData.slice(20, 70)
var step = Math.floor(data.length / this.numberOfCubes)
for (var i = 0, n = 0; i < this.cubes.length; i++, n+=step) {
var val = Math.abs(audioSource[n]) / noSignal;
this.cubes[i].scale.y = Math.abs(data[n]) / noSignal;
if (this.cubes[i].scale.y <= 1) {
this.cubes[i].scale.y = 1;
}
this.cubes[i].position.y = (this.cubes[i].scale.y / 2) - 0.5;
}
this.delta += 0.006;
var dist = 15;
this.camera.lookAt(new THREE.Vector3(0,5,0));
this.camera.position.x = Math.sin(this.delta) * dist;
this.camera.position.y = Math.cos(62) * dist;
this.camera.position.z = Math.cos(this.delta) * dist;
this.renderer.render(this.scene, this.camera);
}
module.exports = AudioVisualizer;