forked from nicoptere/physarum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
259 lines (198 loc) · 7.35 KB
/
index.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
import 'file-loader?name=[name].[ext]!./src/html/index.html';
import {
Scene,
OrthographicCamera,
WebGLRenderer,
Mesh,
DataTexture,
RGBAFormat,
FloatType,
PlaneBufferGeometry,
ShaderMaterial,
Vector2,
AudioListener,
Audio,
AudioAnalyser,
AudioLoader,
LuminanceFormat
} from 'three';
import PingpongRenderTarget from "./src/PingpongRenderTarget"
import RenderTarget from "./src/RenderTarget"
import dat from "dat.gui";
import Controls from "./src/Controls";
import soundfile from "./src/song.mp3"
// 0 configure scene
//////////////////////////////////////
let w = window.innerWidth
let h = window.innerHeight
const renderer = new WebGLRenderer({
alpha: true
});
document.body.appendChild(renderer.domElement);
renderer.setSize(w, h);
const scene = new Scene();
const camera = new OrthographicCamera(-w / 2, w / 2, h / 2, -h / 2, 0.1, 100);
camera.position.z = 1
// AUDIO
var fftSize = 128;
var listener = new AudioListener();
/*
var audio = new Audio( listener );
navigator.mediaDevices.getUserMedia( { audio: true, video: false } ).then( handleSuccess );
function handleSuccess( stream ) {
var audio = new Audio( listener );
var context = listener.context;
context.resume();
var source = context.createMediaStreamSource( stream );
audio.setNodeSource( source );
}
var analyser = new AudioAnalyser( audio, fftSize );
*/
// create an Audio source
var sound = new Audio( listener );
// load a sound and set it as the Audio object's buffer
var audioLoader = new AudioLoader();
audioLoader.load( soundfile, function( buffer ) {
sound.setBuffer( buffer );
sound.setLoop(true);
sound.setVolume(0.5);
sound.play();
});
// create an AudioAnalyser, passing in the sound and desired fftSize
var analyser = new AudioAnalyser( sound, fftSize);
// get the average frequency of the sound
var data = analyser.getAverageFrequency();
console.log(data)
// 1 init buffers
//////////////////////////////////////
let size = 512 // particles amount = ( size ^ 2 )
let count = size * size;
let pos = new Float32Array(count * 3)
let uvs = new Float32Array(count * 2)
let ptexdata = new Float32Array(count * 4)
let id = 0, u,v;
for (let i = 0; i < count; i++) {
//point cloud vertex
id = i * 3
pos[id++] = pos[id++] = pos[id++] = 0;
//computes the uvs
u = (i % size) / size;
v = ~~(i / size) / size;
id = i * 2
uvs[id++] = u
uvs[id] = v
//particle texture values (agents)
id = i * 4
ptexdata[id++] = Math.random() // normalized pos x
ptexdata[id++] = Math.random() // normalized pos y
ptexdata[id++] = Math.random() // normalized angle
ptexdata[id++] = 1
}
// 2 data & trails
//////////////////////////////////////
//performs the diffusion and decay
let diffuse_decay = new ShaderMaterial({
uniforms: {
points: { value: null },
decay: {value: .9 }
},
vertexShader: require('./src/glsl/quad_vs.glsl'),
fragmentShader: require('./src/glsl/diffuse_decay_fs.glsl')
})
let trails = new PingpongRenderTarget(w, h, diffuse_decay)
// 3 agents
//////////////////////////////////////
//moves agents around
let update_agents = new ShaderMaterial({
uniforms: {
data: { value: null },
sa: { value: 2 },
ra: { value: 4 },
so: { value: 12 },
ss: { value: 1.1 }
},
vertexShader: require('./src/glsl/quad_vs.glsl'),
fragmentShader: require('./src/glsl/update_agents_fs.glsl')
})
let agents = new PingpongRenderTarget(size, size, update_agents, ptexdata)
// 4 point cloud
//////////////////////////////////////
//renders the updated agents as red dots
let render_agents = new ShaderMaterial({
vertexShader: require('./src/glsl/render_agents_vs.glsl'),
fragmentShader: require('./src/glsl/render_agents_fs.glsl')
})
let render = new RenderTarget(w,h,render_agents, pos, uvs)
// 5 post process
//////////////////////////////////////
//post process the result of the trails (render the trails as greyscale)
let postprocess = new ShaderMaterial({
uniforms: {
data: {
value: null
},
time: {
value: 0.0
},
tAudioData: {
value: new DataTexture( analyser.data, fftSize / 2, 1, LuminanceFormat )
}
},
vertexShader: require('./src/glsl/quad_vs.glsl'),
fragmentShader: require('./src/glsl/postprocess_fs.glsl')
})
let postprocess_mesh = new Mesh(new PlaneBufferGeometry(), postprocess)
postprocess_mesh.scale.set(w, h, 1)
scene.add(postprocess_mesh)
// 6 interactive controls
//////////////////////////////////////
let controls = new Controls( renderer, agents )
controls.count = ~~(size * size * .05)
// animation loop
//////////////////////////////////////
function raf(){
requestAnimationFrame(raf)
time = (Date.now() - start) * 0.001
trails.material.uniforms.points.value = render.texture
trails.render( renderer, time )
agents.material.uniforms.data.value = trails.texture
agents.render(renderer, time)
//agents.material.uniforms.ra.value.needsUpdate = true;
//agents.material.uniforms.ra.value = (new DataTexture( analyser.data, fftSize / 2, 1, LuminanceFormat )).image.data[0]/1
//agents.material.uniforms.sa.value = (new DataTexture( analyser.data, fftSize / 2, 1, LuminanceFormat )).image.data[10]/10
//agents.material.uniforms.so.value = (new DataTexture( analyser.data, fftSize / 2, 1, LuminanceFormat )).image.data[20]/1
agents.material.uniforms.ra.value = 60 + Math.min(Math.max(0,new DataTexture( analyser.data, fftSize / 2, 1, LuminanceFormat ).image.data[50]/5),60*0.25)
agents.material.uniforms.sa.value = 60 + Math.min(Math.max(0,new DataTexture( analyser.data, fftSize / 2, 1, LuminanceFormat ).image.data[50]/5),60*0.25)
agents.material.uniforms.ss.value = 2 + Math.min(Math.max(0,new DataTexture( analyser.data, fftSize / 2, 1, LuminanceFormat ).image.data[50]/5),10)
console.log((new DataTexture( analyser.data, fftSize / 2, 1, LuminanceFormat )).image.data[50]/1)
render.render( renderer, time )
postprocess_mesh.material.uniforms.data.value = render.texture
postprocess_mesh.material.uniforms.time.value = time
analyser.getFrequencyData();
//console.log(new DataTexture( analyser.data, fftSize / 2, 1, LuminanceFormat ).image.data)
//postprocess_mesh.material.uniforms.tAudioData.value = new DataTexture( analyser.data, fftSize / 2, 1, LuminanceFormat )
postprocess_mesh.material.uniforms.tAudioData.value.needsUpdate = true;
renderer.setSize(w,h)
renderer.clear()
renderer.render(scene, camera)
}
//////////////////////////////////////////////////
let materials = [
diffuse_decay, update_agents, render_agents
]
let resolution = new Vector2(w,h);
materials.forEach( (mat)=>{mat.uniforms.resolution.value = resolution})
let start = Date.now();
let time = 0;
raf()
// settings
//////////////////////////////////////////////////
let gui = new dat.GUI()
gui.add(diffuse_decay.uniforms.decay, "value", 0.01, .99, .01).name("decay")
gui.add(update_agents.uniforms.sa, "value", 1, 90, .1).name("sa")
gui.add(update_agents.uniforms.ra, "value", 1, 90, .1).name("ra")
gui.add(update_agents.uniforms.so, "value", 1, 90, .1).name("so")
gui.add(update_agents.uniforms.ss, "value", 0.1, 10, .1).name("ss")
gui.add(controls, "random")
gui.add(controls, "radius",.001,.25)
gui.add(controls, "count", 1,size*size, 1)