-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlowField.pde
More file actions
165 lines (144 loc) · 3.89 KB
/
FlowField.pde
File metadata and controls
165 lines (144 loc) · 3.89 KB
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
class FlowField{
ArrayList<ArrayList<PVector>>field;
int cols, rows;
int r; //resolution
boolean isVisible;
boolean isActivated;
float strength, speed, noise;
float noiseScale;
float zoff;
PImage img;
int type;
int index;
boolean[] apply;
FlowField(int index){
this.index = index;
r = int(cf.controllerTool.getController("ff_resolution"+index).getValue());
strength = cf.controllerTool.getController("ff_strength"+index).getValue();
speed = cf.controllerTool.getController("ff_speed"+index).getValue();
noise = cf.controllerTool.getController("ff_noise"+index).getValue();
isVisible = cf.controllerTool.get(Button.class,"Show tools").isOn();
isActivated = true;
type = NOISE;
initArray();
zoff = 0;
img = texture[0];
noiseSeed((int)random(10000));
apply = new boolean[N_FLOCK_MAX];
for (int i = 0; i< apply.length; i++) {
apply[i] = i<flocks.size();
}
}
void initArray(){
try{
cols = OUTPUT_WIDTH/r;
rows = OUTPUT_HEIGHT/r;
field = new ArrayList<ArrayList<PVector>>(cols);
for (int i = 0; i< cols; i++){
field.add(new ArrayList<PVector>(rows));
}
for (int i = 0; i< cols; i++){
for (int j = 0; j< rows; j++)
field.get(i).add(j, new PVector());
}
}
catch (NullPointerException e) {
println("Issue during initialisation of Flowfield");
}
}
PVector computeNoiseCell(int x, int y){
float noiseX = map(noise(noise*x,noise*y,zoff),0,1,-strength,strength);
float noiseY = map(noise(1000+noise*x,noise*y,zoff),0,1,-strength,strength);
//if (noiseX*noiseX + noiseY*noiseY <1)
return new PVector(noiseX,noiseY);
}
void setNoiseCell(int i, int j){
if(isActivated)
field.get(i).set(j, computeNoiseCell(i,j));
}
void update(){
switch(type){
case NOISE :
for (int i = 0; i<cols; i++){
if(isActivated){
for (int j = 0; j<rows; j++)
setNoiseCell(i,j);
}
}
zoff+=0.001*speed;
break;
case IMAGE :
for (int i = 0; i < cols; i++) {
for (int j = 0; j < rows; j++) {
field.get(i).set(j,new PVector(width/2-i*r,height/2-j*r));
field.get(i).get(j).normalize();
}
}
for (int i = 1; i < cols-1; i++) {
for (int j = 1; j < rows-1; j++) {
int x = i*r;
int y = j*r;
int c = 0;
if (x<img.width && y<img.height){
c = img.pixels[x + y*img.width];
float theta = map(brightness(c), 0, 255, 0, TWO_PI);
field.get(i).set(j,new PVector(cos(theta),sin(theta)));
}
}
}
break;
}
}
PVector getVector(PVector pos){
PVector particlePosition = PVector.div(pos,DISPLAY_SCALE);
if(isActivated){
int column = int(constrain(particlePosition.x/r, 0, cols-1));
int row = int(constrain(particlePosition.y/r, 0, rows-1));
return field.get(column).get(row).copy();
}
else return new PVector();
}
void drawCell(PGraphics pg, int i, int j){
if(isActivated){
pg.pushMatrix();
pg.translate((0.5+i)*r,(0.5+j)*r);
pg.line(0,0,0.5*r*field.get(i).get(j).x,0.5*r*field.get(i).get(j).y);
pg.popMatrix();
}
}
void render(PGraphics pg){
pg.stroke(255);
for (int i = 0; i<cols; i++){
if(isActivated){
for (int j = 0; j<rows; j++)
drawCell(pg,i,j);
}
}
}
void updateRes(int res){
field.clear();
r = res;
initArray();
isActivated = true;
}
void apply(){
for (int i = 0; i< flocks.size(); i++){
if (apply[i]){
for (Boid b : flocks.get(i).boids)
b.follow(this);
}
}
}
void run(PGraphics pg){
if (isActivated)
{
update();
apply();
if (isVisible){
pg.beginDraw();
render(pg);
pg.endDraw();
}
}
}
}