-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathTapestryGenerator.pde
99 lines (83 loc) · 2.55 KB
/
TapestryGenerator.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
import java.util.Collections;
public class TapestryGenerator extends Visulization {
int blockSize, squareBlocks, bucket;
float[][] tapestry;
color from = color(300, 90, 90),
to = color((hue(from) - 150) % 360 , 90, 90);
public TapestryGenerator() {
super();
squareBlocks = 35;
blockSize = width / squareBlocks;
tapestry = new float[squareBlocks][squareBlocks];
}
@Override
public void update() {
super.fft.forward(player.mix);
// shift forward previous state
for (int i=0; i < tapestry.length; i++) {
for (int j=tapestry[i].length - 1; j > 0; j--) {
tapestry[i][j] = 0.94 * tapestry[i][j-1]; // decay
}
}
// load new fft into tapestry
for (int i=0; i < tapestry.length; i++) {
bucket = floor(map(i, 0, squareBlocks, 0, 14));
tapestry[i][0] = -1.5 * super.fft.getAvg(bucket);
}
background(127);
if (newColor) {
from = color(random(0, 360), 90, 90);
to = color((hue(from) + 120) % 360 , 90, 90);
newColor = false;
}
// 1st person field of view
PVector eyes = new PVector(
width/2,
-height/2,
height/2 / tan(30 * PI / 180.0)
);
// reference point
PVector anchor = new PVector(
width/2,
-height,
height * 1.5
);
// set the view static
camera(
anchor.x, anchor.y, anchor.z, // reversed from default...
eyes.x, eyes.y, eyes.z, // ...3rd person -> 1st person
0, 1, 0 // upwards direction is Y
);
frame.setTitle(floor(frameRate) + " fps");
stroke(0);
strokeWeight(2);
// render quilt
for (int i=0; i < tapestry.length - 1; i++) {
for (int j=0; j < tapestry[i].length - 1; j++) {
drawQuadrant(i, j);
}
}
}
public PVector[] getCorners(int i, int j) {
return new PVector[] {
new PVector(i, tapestry[i][j], j), // upper left
new PVector(i, tapestry[i][j+1], j+1), // upper right
new PVector(i+1, tapestry[i+1][j+1], j+1), // lower right
new PVector(i+1, tapestry[i+1][j], j) // lower left
};
}
public void drawQuadrant(int i, int j) {
PVector[] corners = getCorners(i, j);
float alpha = (float) (i * squareBlocks + j) / (float) (squareBlocks * squareBlocks);
fill(lerpColor(from, to, alpha));
beginShape(QUAD);
for (int k=0; k < corners.length; k++) {
vertex(
(corners[k].x + 1) * blockSize,
corners[k].y,
(corners[k].z + 1) * blockSize
);
}
endShape(CLOSE);
}
}