-
Notifications
You must be signed in to change notification settings - Fork 0
/
viz.js
191 lines (154 loc) · 5.03 KB
/
viz.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
const backgroundColor = 0x00000F;
var noise = new SimplexNoise();
/*////////////////////////////////////////*/
var renderCalls = [];
function render () {
requestAnimationFrame( render );
renderCalls.forEach((callback)=>{ callback(); });
}
render();
/*////////////////////////////////////////*/
var scene = new THREE.Scene();
var clock = new THREE.Clock();
var camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.1, 1000 );
camera.position.set(30, 80, 70);
camera.lookAt(new THREE.Vector3(0, 0, 0));
var renderer = new THREE.WebGLRenderer( { antialias: true } );
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
renderer.setClearColor( backgroundColor );
window.addEventListener( 'resize', function () {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
uniforms.u_resolution.value.x = renderer.domElement.width;
uniforms.u_resolution.value.y = renderer.domElement.height;
}, false );
document.body.appendChild( renderer.domElement);
function renderScene(){ renderer.render( scene, camera ); }
renderCalls.push(renderScene);
/* ////////////////////////////////////////////////////////////////////////// */
var controls = new THREE.OrbitControls( camera, renderer.domElement );
controls.listenToKeyEvents( window ); // optional
//controls.enableZoom = false;
//controls.autoRotate = true;
//
//controls.rotateSpeed = 0.01;
//controls.zoomSpeed = 0.3;
//
//controls.enablePan = false;
controls.enableDamping = true;
controls.dampingFactor = 0.05;
controls.screenSpacePanning = false;
controls.minDistance = 100;
controls.maxDistance = 500;
controls.maxPolarAngle = Math.PI / 2;
controls.update();
renderCalls.push(function(){
controls.update()
});
/* ////////////////////////////////////////////////////////////////////////// */
var spotLight = new THREE.SpotLight( 0xffffff, 1.25 );
spotLight.position.set( -200, 200, 600 );
camera.add( spotLight );
spotLight.position.set(-5, 2, 0);
scene.add(camera);
var pointLight = new THREE.PointLight(0xffffff, 0.25);
scene.add(pointLight);
var stats = initStats();
renderCalls.push(function(){
stats.update()
});
var datcontrols = {
Amplitude : 3,
}
var gui = new dat.GUI();
gui.add(datcontrols, 'Amplitude', 0, 10);
/* ////////////////////////////////////////////////////////////////////////// */
let dummy_array = new Uint8Array(512);
const uniforms = {
u_time: {
type: "f",
value: 1.0,
},
u_resolution: {
type: "v2",
value: new THREE.Vector2(),
},
u_mouse: {
type: "v2",
value: new THREE.Vector2(),
},
u_amplitude: {
type: "f",
value: 3.0,
},
u_data_arr: {
type: "float[64]",
value: dummy_array,
},
// u_black: { type: "vec3", value: new THREE.Color(0x000000) },
// u_white: { type: "vec3", value: new THREE.Color(0xffffff) },
};
const planeGeometry = new THREE.PlaneGeometry(72, 72, 72, 72);
//const planeMaterial = new THREE.MeshNormalMaterial({ wireframe: true });
var vertShader = document.getElementById("vertex_shader").innerHTML;
var fragShader = document.getElementById("fragment_shader").innerHTML;
const planeMaterial = new THREE.ShaderMaterial({
uniforms: uniforms,
vertexShader: vertShader,
fragmentShader: fragShader,
wireframe:true,
});
var plane = new THREE.Mesh(planeGeometry, planeMaterial);
plane.rotation.x = -0.5 * Math.PI;
plane.position.set(0, 10, 0);
scene.add(plane);
var audioContext;
var audioElement;
var audioSource;
var audioAnalyser;
var FFTBufferLength;
var FFTDataArray;
let audio_initialized = false;
function init_audio() {
audioContext = new AudioContext();
audioElement = document.querySelector('audio');
audioSource = audioContext.createMediaElementSource(audioElement);
audioAnalyser = audioContext.createAnalyser();
audioSource.connect(audioAnalyser);
audioAnalyser.connect(audioContext.destination)
audioAnalyser.fftSize = 1024;
FFTBufferLength = audioAnalyser.frequencyBinCount;
FFTDataArray = new Uint8Array(FFTBufferLength);
audio_initialized = true;
}
renderCalls.push(function(){
uniforms.u_resolution.value.x = renderer.domElement.width;
uniforms.u_resolution.value.y = renderer.domElement.height;
uniforms.u_time.value += clock.getDelta();
uniforms.u_amplitude.value = datcontrols.Amplitude;
if (audio_initialized) {
audioAnalyser.getByteFrequencyData(FFTDataArray);
uniforms.u_data_arr.value = FFTDataArray;
}
});
// select our play button
const playButton = document.querySelector('button');
playButton.addEventListener('click', function() {
if (!audio_initialized) {
init_audio();
}
// check if context is in suspended state (autoplay policy)
if (audioContext.state === 'suspended') {
audioContext.resume();
}
// play or pause track depending on state
if (this.dataset.playing === 'false') {
audioElement.play();
this.dataset.playing = 'true';
} else if (this.dataset.playing === 'true') {
audioElement.pause();
this.dataset.playing = 'false';
}
}, false);