-
Notifications
You must be signed in to change notification settings - Fork 0
/
workshop_resonate2012.pde
217 lines (176 loc) · 5.08 KB
/
workshop_resonate2012.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
import toxi.color.*;
import toxi.color.theory.*;
import toxi.data.csv.*;
import toxi.data.feeds.*;
import toxi.data.feeds.util.*;
import processing.opengl.*;
import toxi.processing.*;
import toxi.physics3d.*;
import toxi.physics2d.constraints.*;
import toxi.physics2d.behaviors.*;
import toxi.physics3d.constraints.*;
import toxi.physics2d.*;
import toxi.physics3d.behaviors.*;
import toxi.math.conversion.*;
import toxi.geom.*;
import toxi.math.*;
import toxi.geom.mesh2d.*;
import toxi.geom.nurbs.*;
import toxi.util.datatypes.*;
import toxi.util.events.*;
import toxi.geom.mesh.subdiv.*;
import toxi.math.waves.*;
import toxi.geom.mesh.*;
import toxi.util.*;
import toxi.math.noise.*;
final int RESX = 40;
final int RESY = 30;
final int TWEET_DELAY = 50;
final int BOX_X_SIZE = 5;
final int BOX_Y_SIZE = 5;
final int BOX_Z_SIZE = 5;
final int PLUCK_STRENGTH = 10;
final float STIFFNESS = 0.5;
boolean ONLINE = false;
final String QUERY = "resonate_io";
VerletPhysics3D physics;
ToxiclibsSupport gfx;
List<TweetPoint> tweets = new ArrayList<TweetPoint>();
TweetPoint selection = null;
int tweetId;
int progress;
float scaleX;
float scaleY;
void setup() {
size(1200, 800, OPENGL);
background(0);
noStroke();
physics = new VerletPhysics3D();
gfx = new ToxiclibsSupport(this);
scaleX = (float) width / (RESX - 1);
scaleY = (float) height / (RESY - 1);
for (int y = 0; y < RESY; y++) {
for (int x = 0; x < RESX; x++) {
// create a particle
VerletParticle3D p = new VerletParticle3D(new Vec3D(x - RESX / 2, y - RESY / 2, 0).scaleSelf(scaleX, scaleY, 1));
physics.addParticle(p);
if (x > 0) {
// add a horizontal connection
VerletParticle3D q = physics.particles.get(index(x - 1, y));
VerletSpring3D s = new VerletSpring3D(p, q, p.distanceTo(q), STIFFNESS);
physics.addSpring(s);
}
if (y > 0) {
// add a vertical connection
VerletParticle3D q = physics.particles.get(index(x, y - 1));
VerletSpring3D s = new VerletSpring3D(p, q, p.distanceTo(q), STIFFNESS);
physics.addSpring(s);
}
}
}
// lock corners
physics.particles.get(index(0, 0)).lock();
physics.particles.get(index(RESX - 1, 0)).lock();
physics.particles.get(index(RESX - 1, RESY - 1)).lock();
physics.particles.get(index(0, RESY - 1)).lock();
initTwitter();
}
void draw() {
hint(ENABLE_DEPTH_TEST);
background(0);
// fade
// fill(0, 20);
// rect(0, 0, width, height);
// fill(255);
pushMatrix();
translate(width/2, height/2, 0);
rotateX(radians(60));
// randomJitter();
progress++;
if (progress == TWEET_DELAY) {
progress = 0;
if (tweetId < tweets.size() - 1) {
tweetId++;
jitterGrid(tweets.get(tweetId).pos);
}
}
// update physics
physics.update();
// draw springs
stroke(255);
for (VerletSpring3D s : physics.springs) {
gfx.line(s.a, s.b);
}
// draw particles
int c = 0;
noStroke();
for (VerletParticle3D p : physics.particles) {
// stroke(c % 255, 255 - c % 255, 255);
// fill(c % 255, 255 - c % 255, 255);
// fill(c % 255, 255 - c % 255, 255 - (((int)abs(p.z) * 4) % 255));
gfx.fill(TColor.newHSV(map(p.z, -50, 50, 0, 1), 1, 1));
// gfx.fill(TColor.newHSV(0.58, map(p.z, -50, 50, 0, 1), 1));
gfx.box(new AABB(p, new Vec3D(BOX_X_SIZE, BOX_Y_SIZE, BOX_Z_SIZE)));
c++;
}
// draw tweets
fill(255, 0, 0);
for (int i = 0; i <= tweetId; i++) {
TweetPoint tp = tweets.get(i);
// find related particle for tweet
int idx = index((int)tp.pos.x + RESX / 2, (int)tp.pos.y + RESY / 2);
// offset z position
Vec3D pos = physics.particles.get(idx).add(0, 0, 10);
// check if a tweet is selected
if (selection != null && selection == tp) {
gfx.fill(TColor.YELLOW);
}
else {
gfx.fill(TColor.RED);
}
// draw as sphere
gfx.sphere(new Sphere(pos, 8), 10);
text(tp.tweet.author.name, pos.x, pos.y - 10);
}
// handle mouse
handleMouseMoved();
// back to 2D
popMatrix();
hint(DISABLE_DEPTH_TEST);
if (selection != null) {
String txt = selection.tweet.title;
fill(0, 200);
float h = textWidth(txt) / 200 * 48;
rect(mouseX - 10, mouseY - 10, 220, h);
fill(255);
text(txt, mouseX, mouseY, 200, 1000);
}
}
int index(int x, int y) {
return y * RESX + x;
}
void randomJitter() {
if (MathUtils.randomChance(0.5)) {
physics.particles.get((int)random(RESX * RESY)).jitter(50);
// tweets.get((int)random(tweets.size())).pos.jitter(50);
}
}
void jitterGrid(Vec2D gp) {
int idx = index((int)gp.x + RESX / 2, (int)gp.y + RESY / 2);
physics.particles.get(idx).jitter(PLUCK_STRENGTH);
}
void handleMouseMoved() {
Vec2D mpos = new Vec2D(mouseX, mouseY);
for (int i = 0; i <= tweetId; i++) {
TweetPoint tp = tweets.get(i);
int idx = index((int)tp.pos.x + RESX / 2, (int)tp.pos.y + RESY / 2);
Vec3D pos = physics.particles.get(idx);
float sx = screenX(pos.x, pos.y, pos.z);
float sy = screenY(pos.x, pos.y, pos.z);
Vec2D spos = new Vec2D(sx, sy);
if (spos.distanceTo(mpos) < 20) {
selection = tp;
break;
}
}
}