-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathFluid.pde
484 lines (418 loc) · 16 KB
/
Fluid.pde
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
import ddf.minim.*;
class Fluid extends Visualizer {
@Override
int getOptimalFrameRate() {
return 40;
}
final int SPEC_SIZE = 30;
final float SPEC_WIDTH = 5;
final int HORIZ_SAMPLE_NUM = 80;
final int VERT_SAMPLE_NUM = 30;
final int REFRESH = 3;
final float ANGLE_INC = 0.001;
final float MIN_PARTICLE_SIZE = 2;
final float MAX_PARTICLE_SIZE = 20;
// since we need 4 different color trackers -- base and peak colors for both
// bottom and top halves -- stored all dem in an array
// colorTrackers[0] -> base tracker for bottom half
// colorTrackers[1] -> peak tracker for bottom half
// colorTrackers[2] -> base tracker for top half
// colorTrackers[3] -> peak tracker for top half
ColorTracker[] colorTrackers;
HorizSample[] horizSamples;
VertSample[] vertSamples;
float fluidXRot, fluidYRot;
float currRot = 0;
int particleDetailLoss = 1;
Fluid(AudioInput input) {
super(input, "TERRAIN");
colorTrackers = new ColorTracker[4];
for (int i = 0; i < colorTrackers.length; i++) {
colorTrackers[i] = new ColorTracker(0.5, 4);
}
camera.setCenter(SPEC_SIZE * SPEC_WIDTH, 0, 0);
horizSamples = new HorizSample[HORIZ_SAMPLE_NUM];
vertSamples = new VertSample[VERT_SAMPLE_NUM];
for (int i = 0; i < horizSamples.length; i++) {
horizSamples[i] = new HorizSample(i * REFRESH, REFRESH, HORIZ_SAMPLE_NUM * REFRESH);
}
for (int i = 0; i < vertSamples.length; i++) {
vertSamples[i] = new VertSample(i * REFRESH, REFRESH, VERT_SAMPLE_NUM * REFRESH);
}
camera.viewingMode = false;
camera.pos = new PVector(SPEC_SIZE * SPEC_WIDTH, 0, -130);
camera.setOuterBounds(0, -200, -200, SPEC_SIZE * SPEC_WIDTH * 2, 200, REFRESH * HORIZ_SAMPLE_NUM);
// noFill();
}
class Point {
float x, y, z, intensity;
// we are re-using the same samples to draw both bottom and top - but bottom and top need
// different NON-COMPLEMENTARY colors. so each point keeps track of the two set of colors
// it will display as
float[] topColors;
float[] botColors;
public Point(float x, float y, float z) {
this.x = x;
this.y = y;
this.z = z;
topColors = new float[4];
botColors = new float[4];
}
}
class HorizSample {
float pos, speed, stop;
int index;
Point[] points;
HorizSample(float initPos, float speed, float stop) {
this.speed = speed;
this.stop = stop;
index = (int) (initPos / speed);
pos = initPos;
points = new Point[SPEC_SIZE * 2];
for (int i = 0; i < points.length; i++) {
points[i] = new Point(i * SPEC_WIDTH, 0, 0);
}
}
void setColor(float fade, float[] colors) {
stroke(colors[0] * fade, colors[1] * fade, colors[2] * fade);
// fill(colors[0] * fade*.1, colors[1] * fade*.1, colors[2] * fade*.1);
}
void update() {
pos += speed;
if (expand) {
for (int i = 0; i < points.length; i++) {
points[i].y += pos / 40;
}
}
if (pos >= stop) {
for (int i = 0; i < points.length; i++) {
int fftIndex = (int)round(abs(points.length / 2.0 - i));
points[i].y = getIntensity(fftIndex);
points[i].intensity = getIntensity(fftIndex);
// see comment inside Point (above botColors and topColors)
// for explanation on wtf is going on here
points[i].botColors = getColor(points[i].intensity, 40, colorTrackers[0], colorTrackers[1]);
points[i].topColors = getColor(points[i].intensity, 40, colorTrackers[2], colorTrackers[3]);
}
pos = 0;
}
}
void drawLines(int ydir, float fade) {
pushMatrix();
if (pos > 0) {
HorizSample currSample = this;
int prevIndex;
if (index == 0) {
prevIndex = horizSamples.length - 1;
} else {
prevIndex = index - 1;
}
HorizSample prevSample = horizSamples[prevIndex];
// strokeWeight cannot being changed while inside beginShape/endShape,
// so we must use point() instead of vertex() when drawing particles
if (!particles) {
beginShape(QUAD_STRIP);
}
float zEnd = prevSample.pos;
float zStart = currSample.pos;
float tempFade = fade;
for (int i = 0; i < points.length; i++) {
float xStart = currSample.points[i].x;
float xEnd = prevSample.points[i].x;
float yStart = currSample.points[i].y * ydir;
float yEnd = prevSample.points[i].y * ydir;
if(!expand) {
if (abs(yEnd - yStart) <= 1)
tempFade = 0.1;
else
tempFade = fade * abs(1-(yEnd / volumeScale / (PHI-1) - yStart / volumeScale / (PHI-1))/5.0);
}
if (ydir > 0) {
setColor(tempFade, points[i].botColors);
} else {
setColor(tempFade, points[i].topColors);
}
if (!particles) {
vertex(xStart, yStart, zStart);
vertex(xEnd, yEnd, zEnd);
} else if (i % particleDetailLoss == 0) {
if(!expand) {
strokeWeight(bindRange(currSample.points[i].intensity, MIN_PARTICLE_SIZE, MAX_PARTICLE_SIZE));
}
spriteShader.set("weight", bindRange(currSample.points[i].intensity, MIN_PARTICLE_SIZE, MAX_PARTICLE_SIZE));
point(xStart, yStart, zStart);
strokeWeight(bindRange(prevSample.points[i].intensity, MIN_PARTICLE_SIZE, MAX_PARTICLE_SIZE));
point(xEnd, yEnd, zEnd);
// } else if (i % particleDetailLoss == 0) {
// strokeWeight(bindRange(currSample.points[i].intensity, MIN_PARTICLE_SIZE, MAX_PARTICLE_SIZE));
// point(xStart, yStart, zStart);
}
}
if (!particles) {
endShape();
}
}
popMatrix();
}
}
class VertSample {
float pos, stop, speed;
PVector[] points;
boolean continueSampling;
VertSample(float initPos, float speed, float stop) {
pos = initPos;
this.speed = speed;
this.stop = stop;
points = new PVector[SPEC_SIZE * 2];
for (int i = 0; i < points.length; i++) {
points[i] = new PVector(i * SPEC_WIDTH, 0);
}
continueSampling = false;
}
void update() {
pos += speed;
if (pos >= stop) {
for (int i = 0; i < points.length; i++) {
int fftIndex = abs(points.length / 2 - i);
points[i].y = getIntensity(fftIndex);
}
pos = 0;
if (highlight) {
continueSampling = true;
} else {
continueSampling = false;
}
}
}
void drawLines(int ydir) {
pushMatrix();
translate(0, pos * ydir, 0);
if (!particles) {
beginShape(LINES);
}
for (int i = 0; i < points.length - 1; i++) {
float weight = (!particles)
? bindRange((points[i].y + points[i + 1].y) / 20, 1, 6)
: bindRange(points[i].y / 2, 1, MAX_PARTICLE_SIZE);
strokeWeight(weight);
if (!particles) {
vertex(points[i].x, points[i].y * ydir);
vertex(points[i + 1].x, points[i + 1].y * ydir);
} else if (i % particleDetailLoss == 0) {
strokeWeight(bindRange(weight, MIN_PARTICLE_SIZE, MAX_PARTICLE_SIZE));
point(points[i].x, points[i].y * ydir);
}
}
float weight = min((points[points.length - 2].y + points[points.length - 1].y) / 20, 6);
strokeWeight(weight);
if (!particles) {
vertex(points[points.length - 2].x, points[points.length - 2].y * ydir);
vertex(points[points.length - 1].x, points[points.length - 1].y * ydir);
} else {
strokeWeight(bindRange(weight, MIN_PARTICLE_SIZE, MAX_PARTICLE_SIZE));
point(points[points.length - 2].x, points[points.length - 2].y * ydir);
}
if (!particles) {
endShape();
}
popMatrix();
}
}
@Override
void draw() {
if (blur) {
setBackground(contrast, 80);
} else {
setBackground(contrast, 255);
// setBackground(contrast, 150);
}
hint(DISABLE_DEPTH_MASK);
camera.update();
// --------------------------------------------------- Rotate Fluid
if(revolve) {
translate(0, 0, HORIZ_SAMPLE_NUM * REFRESH/2);
} else {
translate(SPEC_SIZE*SPEC_WIDTH, 0, HORIZ_SAMPLE_NUM * REFRESH/2);
}
if (followMouse) {
fluidXRot = lerp(fluidXRot, map(mouseY/2, 0, height/2, -PI, PI), .05);
fluidYRot = lerp(fluidYRot, map(mouseX/2, 0, width/2, -PI, PI), .05);
} else {
fluidXRot = lerp(fluidXRot, 0, .05);
fluidYRot = lerp(fluidYRot, 0, .05);
}
rotateX(fluidXRot);
rotateY(fluidYRot);
if(revolve) {
translate(0, 0, -HORIZ_SAMPLE_NUM * REFRESH/2);
} else {
translate(-SPEC_SIZE*SPEC_WIDTH, 0, -HORIZ_SAMPLE_NUM * REFRESH/2);
}
noFill();
pushMatrix();
// makes sure vertical samples appear at the front of the figure
if (revolve) {
translate(0, 0, 170);
}
if (!pause) {
for (ColorTracker ct : colorTrackers) {
ct.incrementColor();
}
if (revolve) {
currRot += ANGLE_INC;
} else {
if(currRot > 0){
currRot -= ANGLE_INC;
currRot = max(0, currRot);
}
}
for (int i = 0; i < VERT_SAMPLE_NUM; i++) {
vertSamples[i].update();
}
}
for (int i = 0; i < VERT_SAMPLE_NUM; i++) {
VertSample s = vertSamples[i];
if (s.continueSampling) {
rotateZ(currRot);
float fade = 1 - s.pos / (VERT_SAMPLE_NUM * REFRESH);
setComplementaryColor(fade, colorTrackers[0]);
s.drawLines(1);
setComplementaryColor(fade, colorTrackers[2]);
s.drawLines(-1);
}
}
popMatrix();
pushMatrix();
strokeWeight(1);
if (!pause){
for (int i = 0; i < HORIZ_SAMPLE_NUM; i++) {
horizSamples[i].update();
}
}
for (int i = 0; i < HORIZ_SAMPLE_NUM; i++) {
HorizSample s = horizSamples[i];
int relativeIndex = (int) (s.pos / REFRESH);
rotateZ(currRot * relativeIndex);
if (expand) {
float weight = map(s.pos, 0, s.stop, 0.8, 5);
strokeWeight(weight);
}
float fade;
if (expand) {
fade = 1 - s.pos / (HORIZ_SAMPLE_NUM * REFRESH) / 2;
} else {
fade = min(1 - s.pos / (HORIZ_SAMPLE_NUM * REFRESH), .3);
// if(1-s.pos == 1 || s.pos < 5) //sets only the front to full color
// fade = 1;
}
// for (int j = 0; j < s.points.length; j++) {
// if(s.points[j].y >= mag)
// fade = 1;
// }
s.drawLines(1, fade);
s.drawLines(-1, fade);
rotateZ(-currRot * relativeIndex);
}
popMatrix();
}
void setComplementaryColor(float fade, ColorTracker tracker) {
stroke((255 - tracker.red) * fade, (255 - tracker.green) * fade, (255 - tracker.blue) * fade);
}
@Override
void adjustDetail(float avgFr) {
if (avgFr < 25) {
particleDetailLoss = 5;
} else if (avgFr < 30) {
particleDetailLoss = 4;
} else if (avgFr < 35) {
particleDetailLoss = 3;
} else if (avgFr < 38) {
particleDetailLoss = 2;
}
// println(particleDetailLoss);
}
@Override
void particles() {
particles = !particles;
blur = particles;
}
@Override
void highlight() {
highlight = !highlight;
}
@Override
void expand() {
expand = !expand;
}
@Override
void revolve() {
revolve = !revolve;
if (!revolve && currRot >= .082) {
currRot = .082; //sets revolve to 1 full rotation
}
if(revolve) {
camera.setOuterBounds(-SPEC_SIZE * SPEC_WIDTH, -200, -200, SPEC_SIZE * SPEC_WIDTH, 200, REFRESH * HORIZ_SAMPLE_NUM);
} else {
camera.setOuterBounds(0, -200, -200, SPEC_SIZE * SPEC_WIDTH * 2, 200, REFRESH * HORIZ_SAMPLE_NUM);
}
fPressed();
frontView();
}
@Override
void pause() {
pause = !pause;
}
@Override
void frontView() {
float camX = SPEC_SIZE * SPEC_WIDTH;
if (revolve) {
camera.initMoveCenter(0, 0, 0, (int)frameRate);
camX = 0;
} else {
camera.initMoveCenter(SPEC_SIZE * SPEC_WIDTH, 0, 0, (int)frameRate);
}
camera.initMoveCamera(new PVector(camX, 0, -130), (int)frameRate);
camera.initMoveDir(new PVector(0, 1, 0), (int) frameRate);
}
@Override
void rearView() {
float camX = SPEC_SIZE * SPEC_WIDTH;
if (revolve) {
camera.initMoveCenter(0, 0, 0, (int)frameRate);
camX = 0;
}
camera.initMoveCamera(new PVector(camX, 0, 300), (int)frameRate);
camera.initMoveDir(new PVector(0, 1, 0), (int) frameRate);
}
@Override
void topView() {
float camZ = HORIZ_SAMPLE_NUM * REFRESH/ 1.99;
float camY = -150;
if (frontView) {
camZ = HORIZ_SAMPLE_NUM * REFRESH / 2.1;
camY = 160;
}
if (revolve) {
camera.initMoveCamera(new PVector(-150, camY, camZ), (int) frameRate * 2);
camera.initMoveCenter(0, 0, HORIZ_SAMPLE_NUM * REFRESH / 2, (int) frameRate / 2);
} else {
camera.initMoveCamera(new PVector(150, camY, camZ), (int) frameRate * 2);
camera.initMoveCenter(SPEC_SIZE * SPEC_WIDTH, 0, HORIZ_SAMPLE_NUM * REFRESH / 2, (int) frameRate);
}
camera.initMoveDir(new PVector(0, 1, 0), (int) frameRate);
}
@Override
void autoPan() {
float camZ = HORIZ_SAMPLE_NUM * REFRESH/ 1.99;
float camY = -150;
if (frontView) {
camZ = HORIZ_SAMPLE_NUM * REFRESH / 2.1;
camY = 160;
}
if (revolve) {
camera.initMoveCenter(0, 0, HORIZ_SAMPLE_NUM * REFRESH / 2, (int) frameRate / 2);
} else {
camera.initMoveCenter(SPEC_SIZE * SPEC_WIDTH, 0, HORIZ_SAMPLE_NUM * REFRESH / 2, (int) frameRate);
}
}
}