Tweak to wrapdistf
[SugarCubes.git] / TestPatterns.pde
1 /**
2 * Simplest demonstration of using the rotating master hue.
3 * All pixels are full-on the same color.
4 */
5 class TestHuePattern extends SCPattern {
6 public TestHuePattern(GLucose glucose) {
7 super(glucose);
8 }
9
10 public void run(int deltaMs) {
11 // Access the core master hue via this method call
12 float hv = lx.getBaseHuef();
13 for (int i = 0; i < colors.length; ++i) {
14 colors[i] = color(hv, 100, 100);
15 }
16 }
17 }
18
19 /**
20 * Test of a wave moving across the X axis.
21 */
22 class TestXPattern extends SCPattern {
23 private final SinLFO xPos = new SinLFO(0, model.xMax, 4000);
24 public TestXPattern(GLucose glucose) {
25 super(glucose);
26 addModulator(xPos).trigger();
27 }
28 public void run(int deltaMs) {
29 float hv = lx.getBaseHuef();
30 for (Point p : model.points) {
31 // This is a common technique for modulating brightness.
32 // You can use abs() to determine the distance between two
33 // values. The further away this point is from an exact
34 // point, the more we decrease its brightness
35 float bv = max(0, 100 - abs(p.fx - xPos.getValuef()));
36 colors[p.index] = color(hv, 100, bv);
37 }
38 }
39 }
40
41 /**
42 * Test of a wave on the Y axis.
43 */
44 class TestYPattern extends SCPattern {
45 private final SinLFO yPos = new SinLFO(0, model.yMax, 4000);
46 public TestYPattern(GLucose glucose) {
47 super(glucose);
48 addModulator(yPos).trigger();
49 }
50 public void run(int deltaMs) {
51 float hv = lx.getBaseHuef();
52 for (Point p : model.points) {
53 float bv = max(0, 100 - abs(p.fy - yPos.getValuef()));
54 colors[p.index] = color(hv, 100, bv);
55 }
56 }
57 }
58
59 /**
60 * Test of a wave on the Z axis.
61 */
62 class TestZPattern extends SCPattern {
63 private final SinLFO zPos = new SinLFO(0, model.zMax, 4000);
64 public TestZPattern(GLucose glucose) {
65 super(glucose);
66 addModulator(zPos).trigger();
67 }
68 public void run(int deltaMs) {
69 float hv = lx.getBaseHuef();
70 for (Point p : model.points) {
71 float bv = max(0, 100 - abs(p.fz - zPos.getValuef()));
72 colors[p.index] = color(hv, 100, bv);
73 }
74 }
75 }
76
77 /**
78 * This shows how to iterate over towers, enumerated in the model.
79 */
80 class TestTowerPattern extends SCPattern {
81 private final SawLFO towerIndex = new SawLFO(0, model.towers.size(), 1000*model.towers.size());
82
83 public TestTowerPattern(GLucose glucose) {
84 super(glucose);
85 addModulator(towerIndex).trigger();
86 }
87
88 public void run(int deltaMs) {
89 int ti = 0;
90 for (Tower t : model.towers) {
91 for (Point p : t.points) {
92 colors[p.index] = color(
93 lx.getBaseHuef(),
94 100,
95 max(0, 100 - 80*LXUtils.wrapdistf(ti, towerIndex.getValuef(), model.towers.size()))
96 );
97 }
98 ++ti;
99 }
100 }
101
102 }
103
104 /**
105 * This is a demonstration of how to use the projection library. A projection
106 * creates a mutation of the coordinates of all the points in the model, creating
107 * virtual x,y,z coordinates. In effect, this is like virtually rotating the entire
108 * art car. However, since in reality the car does not move, the result is that
109 * it appears that the object we are drawing on the car is actually moving.
110 *
111 * Keep in mind that what we are creating a projection of is the view coordinates.
112 * Depending on your intuition, some operations may feel backwards. For instance,
113 * if you translate the view to the right, it will make it seem that the object
114 * you are drawing has moved to the left. If you scale the view up 2x, objects
115 * drawn with the same absolute values will seem to be half the size.
116 *
117 * If this feels counterintuitive at first, don't worry. Just remember that you
118 * are moving the pixels, not the structure. We're dealing with a finite set
119 * of sparse, non-uniformly spaced pixels. Mutating the structure would move
120 * things to a space where there are no pixels in 99% of the cases.
121 */
122 class TestProjectionPattern extends SCPattern {
123
124 private final Projection projection;
125 private final SawLFO angle = new SawLFO(0, TWO_PI, 9000);
126 private final SinLFO yPos = new SinLFO(-20, 40, 5000);
127
128 public TestProjectionPattern(GLucose glucose) {
129 super(glucose);
130 projection = new Projection(model);
131 addModulator(angle).trigger();
132 addModulator(yPos).trigger();
133 }
134
135 public void run(int deltaMs) {
136 // For the same reasons described above, it may logically feel to you that
137 // some of these operations are in reverse order. Again, just keep in mind that
138 // the car itself is what's moving, not the object
139 projection.reset(model)
140
141 // Translate so the center of the car is the origin, offset by yPos
142 .translateCenter(model, 0, yPos.getValuef(), 0)
143
144 // Rotate around the origin (now the center of the car) about an X-vector
145 .rotate(angle.getValuef(), 1, 0, 0)
146
147 // Scale up the Y axis (objects will look smaller in that access)
148 .scale(1, 1.5, 1);
149
150 float hv = lx.getBaseHuef();
151 for (Coord c : projection) {
152 float d = sqrt(c.x*c.x + c.y*c.y + c.z*c.z); // distance from origin
153 // d = abs(d-60) + max(0, abs(c.z) - 20); // life saver / ring thing
154 d = max(0, abs(c.y) - 10 + .3*abs(c.z) + .08*abs(c.x)); // plane / spear thing
155 colors[c.index] = color(
156 (hv + .6*abs(c.x) + abs(c.z)) % 360,
157 100,
158 constrain(140 - 10*d, 0, 100)
159 );
160 }
161 }
162 }
163
164 class TestCubePattern extends SCPattern {
165
166 private SawLFO index = new SawLFO(0, Cube.POINTS_PER_CUBE, Cube.POINTS_PER_CUBE*60);
167
168 TestCubePattern(GLucose glucose) {
169 super(glucose);
170 addModulator(index).start();
171 }
172
173 public void run(int deltaMs) {
174 for (Cube c : model.cubes) {
175 int i = 0;
176 for (Point p : c.points) {
177 colors[p.index] = color(
178 lx.getBaseHuef(),
179 100,
180 max(0, 100 - 80.*abs(i - index.getValuef()))
181 );
182 ++i;
183 }
184 }
185 }
186 }
187
188 class MappingTool extends SCPattern {
189
190 private int cubeIndex = 0;
191 private int stripIndex = 0;
192 private int channelIndex = 0;
193
194 public final int MAPPING_MODE_ALL = 0;
195 public final int MAPPING_MODE_CHANNEL = 1;
196 public final int MAPPING_MODE_SINGLE_CUBE = 2;
197 public int mappingMode = MAPPING_MODE_ALL;
198
199 public final int CUBE_MODE_ALL = 0;
200 public final int CUBE_MODE_SINGLE_STRIP = 1;
201 public final int CUBE_MODE_STRIP_PATTERN = 2;
202 public int cubeMode = CUBE_MODE_ALL;
203
204 public boolean channelModeRed = true;
205 public boolean channelModeGreen = false;
206 public boolean channelModeBlue = false;
207
208 private final int numChannels;
209
210 private final PandaMapping[] pandaMappings;
211 private PandaMapping activeMapping;
212 private int mappingChannelIndex;
213
214 MappingTool(GLucose glucose, PandaMapping[] pandaMappings) {
215 super(glucose);
216 this.pandaMappings = pandaMappings;
217 numChannels = pandaMappings.length * PandaMapping.CHANNELS_PER_BOARD;
218 setChannel();
219 }
220
221 private void setChannel() {
222 mappingChannelIndex = channelIndex % PandaMapping.CHANNELS_PER_BOARD;
223 activeMapping = pandaMappings[channelIndex / PandaMapping.CHANNELS_PER_BOARD];
224 }
225
226 private int cubeInChannel(Cube c) {
227 int i = 1;
228 for (int index : activeMapping.channelList[mappingChannelIndex]) {
229 if (c == model.getCubeByRawIndex(index)) {
230 return i;
231 }
232 ++i;
233 }
234 return 0;
235 }
236
237 private void printInfo() {
238 println("Cube:" + cubeIndex + " Strip:" + (stripIndex+1));
239 }
240
241 public void cube(int delta) {
242 int len = model.cubes.size();
243 cubeIndex = (len + cubeIndex + delta) % len;
244 printInfo();
245 }
246
247 public void strip(int delta) {
248 int len = Cube.STRIPS_PER_CUBE;
249 stripIndex = (len + stripIndex + delta) % len;
250 printInfo();
251 }
252
253 public void run(int deltaMs) {
254 color off = color(0, 0, 0);
255 color c = off;
256 color r = #FF0000;
257 color g = #00FF00;
258 color b = #0000FF;
259 if (channelModeRed) c |= r;
260 if (channelModeGreen) c |= g;
261 if (channelModeBlue) c |= b;
262
263 int ci = 0;
264 for (Cube cube : model.cubes) {
265 boolean cubeOn = false;
266 int channelIndex = cubeInChannel(cube);
267 switch (mappingMode) {
268 case MAPPING_MODE_ALL: cubeOn = true; break;
269 case MAPPING_MODE_SINGLE_CUBE: cubeOn = (cubeIndex == ci); break;
270 case MAPPING_MODE_CHANNEL: cubeOn = (channelIndex > 0); break;
271 }
272 if (cubeOn) {
273 if (mappingMode == MAPPING_MODE_CHANNEL) {
274 color cc = off;
275 switch (channelIndex) {
276 case 1: cc = r; break;
277 case 2: cc = r|g; break;
278 case 3: cc = g; break;
279 case 4: cc = b; break;
280 case 5: cc = r|b; break;
281 }
282 setColor(cube, cc);
283 } else if (cubeMode == CUBE_MODE_STRIP_PATTERN) {
284 int si = 0;
285 color sc = off;
286 for (Strip strip : cube.strips) {
287 int faceI = si / Face.STRIPS_PER_FACE;
288 switch (faceI) {
289 case 0: sc = r; break;
290 case 1: sc = g; break;
291 case 2: sc = b; break;
292 case 3: sc = r|g|b; break;
293 }
294 if (si % Face.STRIPS_PER_FACE == 2) {
295 sc = r|g;
296 }
297 setColor(strip, sc);
298 ++si;
299 }
300 } else if (cubeMode == CUBE_MODE_SINGLE_STRIP) {
301 setColor(cube, off);
302 setColor(cube.strips.get(stripIndex), c);
303 } else {
304 setColor(cube, c);
305 }
306 } else {
307 setColor(cube, off);
308 }
309 ++ci;
310 }
311
312 }
313
314 public void incCube() {
315 cubeIndex = (cubeIndex + 1) % model.cubes.size();
316 }
317
318 public void decCube() {
319 --cubeIndex;
320 if (cubeIndex < 0) {
321 cubeIndex += model.cubes.size();
322 }
323 }
324
325 public void incChannel() {
326 channelIndex = (channelIndex + 1) % numChannels;
327 setChannel();
328 }
329
330 public void decChannel() {
331 --channelIndex;
332 if (channelIndex < 0) {
333 channelIndex += numChannels;
334 }
335 setChannel();
336 }
337
338 public void incStrip() {
339 int stripsPerCube = Cube.FACES_PER_CUBE * Face.STRIPS_PER_FACE;
340 stripIndex = (stripIndex + 1) % stripsPerCube;
341 }
342
343 public void decStrip() {
344 int stripsPerCube = Cube.FACES_PER_CUBE * Face.STRIPS_PER_FACE;
345 --stripIndex;
346 if (stripIndex < 0) {
347 stripIndex += stripsPerCube;
348 }
349 }
350
351 public void keyPressed() {
352 switch (keyCode) {
353 case UP: if (mappingMode == MAPPING_MODE_CHANNEL) incChannel(); else incCube(); break;
354 case DOWN: if (mappingMode == MAPPING_MODE_CHANNEL) decChannel(); else decCube(); break;
355 case LEFT: decStrip(); break;
356 case RIGHT: incStrip(); break;
357 }
358 switch (key) {
359 case 'r': channelModeRed = !channelModeRed; break;
360 case 'g': channelModeGreen = !channelModeGreen; break;
361 case 'b': channelModeBlue = !channelModeBlue; break;
362 }
363 }
364 }