forked from l8on/dome
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cackler.pde
383 lines (345 loc) · 12.7 KB
/
Cackler.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
public class Ring extends LEDomeLayer {
private final float MIN_THICKNESS = 0.5 * FEET;
private final float MAX_THICKNESS = 1.0 * FEET;
private final float MIN_PERIOD = 5.0 * SECONDS;
private final float MAX_PERIOD = 15.0 * SECONDS;
private float hue;
private float thickness;
private LXProjection projection;
private SinLFO heightMod;
public Ring(LX lx) {
this(lx, random(0, 360));
}
public Ring(LX lx, float hue) {
super(lx);
this.hue = hue;
this.thickness = random(MIN_THICKNESS, MAX_THICKNESS);
this.projection = new LXProjection(lx.model);
this.heightMod = new SinLFO(lx.model.yMin, lx.model.yMax, random(MIN_PERIOD, MAX_PERIOD));
addModulator(this.heightMod).start();
}
public void run(double deltaMs) {
this.projection.reset();
for (LXVector p : this.projection) {
float pointDistance = dist(p.x, p.y, p.z, p.x, this.heightMod.getValuef(), p.z);
if (pointDistance <= this.thickness) {
float brightness = 50 + 50 * (this.thickness - pointDistance) / this.thickness;
setColor(p.index, LX.hsb(this.hue, 100, brightness));
}
}
}
}
public class Rings extends LEDomePattern {
private final float BACKGROUND_PERIOD = 30.0 * SECONDS;
private final int MIN_BACKGROUND_BRIGHTNESS = 10;
private final int DEFAULT_BACKGROUND_BRIGHTNESS = 25;
private final int MAX_BACKGROUND_BRIGHTNESS = 40;
private final int MIN_RING_COUNT = 1;
private final int DEFAULT_RING_COUNT = 3;
private final int MAX_RING_COUNT = 5;
private final float MIN_HUE_DIFF = 30.0;
private SawLFO backgroundHueMod = new SawLFO(0, 360, BACKGROUND_PERIOD);
private LEDomeAudioParameterFull brightnessParam = new LEDomeAudioParameterFull(
"BRT", DEFAULT_BACKGROUND_BRIGHTNESS, MIN_BACKGROUND_BRIGHTNESS, MAX_BACKGROUND_BRIGHTNESS);
private DiscreteParameter ringCountParam = new DiscreteParameter(
"NRINGS", DEFAULT_RING_COUNT, MIN_RING_COUNT, MAX_RING_COUNT + 1);
private List<Ring> rings = new ArrayList<Ring>();
private Random hueRandomizer = new Random();
public Rings(LX lx) {
super(lx);
addModulator(backgroundHueMod).start();
addParameter(brightnessParam);
addParameter(ringCountParam);
}
public float getNextRingHue() {
// randomize ring hues but enforce variety
List availableHues = new ArrayList<Float>();
for (int hue = 0; hue < 360; hue++) {
boolean isAvailable = true;
for (Ring ring: this.rings) {
float diff = abs(ring.hue - hue);
if (diff < MIN_HUE_DIFF || diff > (360 - MIN_HUE_DIFF)) {
isAvailable = false;
break;
}
}
if (isAvailable) {
availableHues.add((float)hue);
}
}
if (availableHues.isEmpty()) {
return random(0, 360);
}
return (float)availableHues.get(this.hueRandomizer.nextInt(availableHues.size()));
}
public void updateRings() {
int ringCount = ringCountParam.getValuei();
while (this.rings.size() < ringCount) {
Ring ring = new Ring(lx, this.getNextRingHue());
this.rings.add(ring);
addLayer(ring);
}
while (this.rings.size() > ringCount) {
removeLayer(this.rings.get(0));
this.rings.remove(0);
}
}
public void run(double deltaMs) {
updateRings();
setColors(LX.hsb(backgroundHueMod.getValuef(), 50, brightnessParam.getValuef()));
}
}
public class ColorSpiral extends LEDomePattern {
private final int FACE_COUNT = model.faces.size();
private final float MIN_PERIOD = 1.0 * SECONDS;
private final float DEFAULT_PERIOD = 5.0 * SECONDS;
private final float MAX_PERIOD = 10.0 * SECONDS;
private SawLFO currIndex = new SawLFO(0, FACE_COUNT, DEFAULT_PERIOD);
private LEDomeAudioParameterFull brightnessParam = new LEDomeAudioParameterFull("BRT", 50, 0, 100);
private BoundedParameter saturationParam = new BoundedParameter("SAT", 75, 50, 100);
private BoundedParameter speedParam = new BoundedParameter(
"SPD", DEFAULT_PERIOD, MAX_PERIOD, MIN_PERIOD);
public ColorSpiral(LX lx) {
super(lx);
addParameter(brightnessParam);
addParameter(saturationParam);
addParameter(speedParam);
addModulator(currIndex).start();
}
public void run(double deltaMs) {
int index = (int)currIndex.getValuef();
int effectiveIndex;
float hue;
float brightness = brightnessParam.getValuef();
float saturation = saturationParam.getValuef();
currIndex.setPeriod(speedParam.getValuef());
for (int i = 0; i < FACE_COUNT; i++) {
LEDomeFace face = model.faces.get(i);
if(!face.hasLights()) {
continue;
}
effectiveIndex = (i + index) % FACE_COUNT;
for (LXPoint p : face.points) {
hue = effectiveIndex / float(FACE_COUNT) * 360;
this.colors[p.index] = LX.hsb(hue, saturation, brightness);
}
}
}
}
public class Meteor extends LXLayer {
private final int METEOR_HUE = 60;
private final int METEOR_SAT = 40;
private final int DEFAULT_RATE = 6;
private final float DEFAULT_SPEED = 75.0;
private final float SPEED_RAND = 15.0;
private final int DELAY_MAX = 60000;
private final int DELAY_RAND = 2000;
private final int MIN_COUNT = 6;
private final int MAX_COUNT = 18;
private final float MAX_ANGLE = PI / 6;
private final int FADE_TIME = 50;
private LEDome dome = (LEDome)model;
private LXRangeModulator xMod = new LinearEnvelope(0);
private LXRangeModulator yMod = new LinearEnvelope(0);
private LXRangeModulator zMod = new LinearEnvelope(0);
private LXRangeModulator delayMod = new LinearEnvelope(0);
private List<LEDomeEdge> edges = new ArrayList<LEDomeEdge>();
private List<LXPoint> points = new ArrayList<LXPoint>();
private List<LXPoint> queue = new ArrayList<LXPoint>();
private int count = 0;
private int maxCount = 0;
private int rate = DEFAULT_RATE;
private float speed = DEFAULT_SPEED;
private boolean autoRestart = false;
public Meteor(LX lx) {
this(lx, true);
}
public Meteor(LX lx, boolean autoRestart) {
super(lx);
addModulator(this.xMod);
addModulator(this.yMod);
addModulator(this.zMod);
addModulator(this.delayMod);
this.autoRestart = autoRestart;
}
private void restart() {
this.restart(DELAY_MAX / this.rate + (int)random(-1 * DELAY_RAND, DELAY_RAND));
}
private void restart(float delayDuration) {
this.delayMod.setRange(0, 1, delayDuration).trigger();
this.speed = DEFAULT_SPEED + random(-1 * SPEED_RAND, SPEED_RAND);
this.edges.clear();
this.points.clear();
this.queue.clear();
this.count = 0;
this.maxCount = (int)random(MIN_COUNT, MAX_COUNT);
LEDomeEdge firstEdge = dome.randomEdge();
this.edges.add(firstEdge);
this.queue.add(firstEdge.points.get(0));
this.queue.add(firstEdge.points.get(1));
this.queue.add(firstEdge.points.get(2));
}
private void addEdge() {
LXPoint origin = this.queue.get(0);
LEDomeEdge currEdge = this.edges.get(this.edges.size() - 1);
HE_Vertex originVertex = dome.closestVertex(origin);
List<HE_Halfedge> neighbors = originVertex.getHalfedgeStar();
float bestAngle = PI;
LEDomeEdge bestEdge = null;
for (HE_Halfedge halfEdge : neighbors) {
HE_Halfedge pairedHalfEdge = halfEdge.getPair();
int pairedHalfEdgeLabel = pairedHalfEdge.getLabel();
if (pairedHalfEdgeLabel < 0) {
continue;
}
LEDomeEdge edge = dome.edges.get(pairedHalfEdgeLabel);
if (edge == null || this.edges.contains(edge)) {
continue;
}
float angle = dome.angleBetweenEdges(currEdge, edge);
if (angle < MAX_ANGLE && angle < bestAngle) {
bestEdge = edge;
bestAngle = angle;
}
}
if (bestEdge == null) {
return;
}
this.edges.add(bestEdge);
LXPoint closestPoint = bestEdge.closestVertexPoint(origin.x, origin.y, origin.z);
if (closestPoint != origin) {
this.queue.add(closestPoint);
}
this.queue.add(bestEdge.points.get(1));
if (closestPoint == bestEdge.points.get(0)) {
this.queue.add(bestEdge.points.get(2));
} else {
this.queue.add(bestEdge.points.get(0));
}
}
private void move() {
if (this.queue.size() < 2 && this.count < this.maxCount) {
this.addEdge();
}
if (this.queue.size() > 1) {
LXPoint origin = this.queue.get(0);
LXPoint target = this.queue.get(1);
float travelDist = dist(origin.x, origin.y, origin.z, target.x, target.y, target.z);
double travelTime = max(travelDist / (this.speed / SECONDS), 0.0);
this.xMod.setRange((double)origin.x, (double)target.x, travelTime).start();
this.yMod.setRange((double)origin.y, (double)target.y, travelTime).start();
this.zMod.setRange((double)origin.z, (double)target.z, travelTime).start();
}
this.queue.remove(0);
}
public void setRate(int rate) {
this.rate = rate;
}
public Integer getColor(LXPoint point) {
int index = this.points.indexOf(point);
if (index < 0) {
return null;
}
float brightness = 100.0 * (index + 1) / this.count;
return LX.hsb(METEOR_HUE, METEOR_SAT, brightness);
}
public void run(double deltaMs) {
if (this.delayMod.isRunning()) {
return;
}
if (!this.xMod.isRunning() && !this.yMod.isRunning() || !this.zMod.isRunning()) {
if (this.queue.size() > 0) {
this.points.add(this.queue.get(0));
this.count += 1;
this.move();
} else if (this.count < 3 * this.points.size()) {
this.count += 1;
this.delayMod.setRange(0, 1, FADE_TIME).trigger();
} else if (this.autoRestart) {
this.restart();
}
}
}
}
public class Stargaze extends LXPattern {
private final int SKY_COLOR = LX.hsb(240, 80, 40);
private final int STAR_HUE = 60;
private final int STAR_SAT = 20;
private final int TWINKLE_MIN = 1000;
private final int TWINKLE_MAX = 5000;
private LEDome dome = (LEDome)model;
private List<LEDomeFace> faces = new ArrayList<LEDomeFace>(dome.faces);
private List<LXPoint> stars = new ArrayList<LXPoint>();
private List<SinLFO> twinklers = new ArrayList<SinLFO>();
private Meteor autoMeteor = new Meteor(lx, true);
private Meteor clapMeteor = new Meteor(lx);
private BoundedParameter brightnessParam = new BoundedParameter("BRT", 70, 40, 100);
private BoundedParameter numStarsParam = new BoundedParameter("STAR", 40, 10, 90);
private BoundedParameter meteorRateParam = new BoundedParameter("MET", 6, 1, 20);
private LEDomeAudioClapGate clapGate = new LEDomeAudioClapGate("XCLAP", lx);
private BooleanParameter triggerParameter;
public Stargaze(LX lx) {
super(lx);
addParameter(this.brightnessParam);
addParameter(this.numStarsParam);
addParameter(this.meteorRateParam);
Collections.shuffle(this.faces);
for (LEDomeFace face : this.faces) {
if (!face.hasLights()) {
continue;
}
this.stars.add(face.points.get((int)random(0, 6)));
SinLFO twinkler = new SinLFO(0, 1, random(TWINKLE_MIN, TWINKLE_MAX));
this.twinklers.add(twinkler);
addModulator(twinkler).trigger();
}
addLayer(this.autoMeteor);
addLayer(this.clapMeteor);
addModulator(this.clapGate).start();
this.triggerParameter = this.clapGate.gate;
this.triggerParameter.addListener(this);
}
public void onParameterChanged(LXParameter parameter) {
if (parameter == this.meteorRateParam) {
this.autoMeteor.setRate((int)this.meteorRateParam.getValue());
}
if (parameter == this.triggerParameter) {
this.clapMeteor.restart(0);
}
}
public void blendColor(int index, int newColor) {
int existingColor = this.colors[index];
float h1 = LXColor.h(existingColor);
float s1 = LXColor.s(existingColor);
float b1 = LXColor.b(existingColor);
float h2 = LXColor.h(newColor);
float s2 = LXColor.s(newColor);
float b2 = LXColor.b(newColor);
float b3 = max(b1, b2);
float h3 = h2 + (h1 - h2) * (1 - (b3 - b1) / (101 - b1));
float s3 = s2 + (s1 - s2) * (1 - (b3 - b1) / (101 - b1));
this.colors[index] = LX.hsb(h3, s3, b3);
}
public void run(double deltaMs) {
float brightness = this.brightnessParam.getValuef();
for (LXPoint point : model.points) {
this.colors[point.index] = SKY_COLOR;
}
for (int i = 0; i < this.numStarsParam.getValue(); i++) {
LXPoint point = this.stars.get(i);
SinLFO twinkler = this.twinklers.get(i);
twinkler.setRange(brightness / 2, brightness);
this.colors[point.index] = LX.hsb(STAR_HUE, STAR_SAT, twinkler.getValuef());
}
for (LXPoint point : model.points) {
Integer autoMeteorColor = this.autoMeteor.getColor(point);
if (autoMeteorColor != null) {
this.blendColor(point.index, (int)autoMeteorColor);
}
Integer clapMeteorColor = this.clapMeteor.getColor(point);
if (clapMeteorColor != null) {
this.blendColor(point.index, (int)clapMeteorColor);
}
}
}
}