Merge branch 'newlayout' of https://github.com/sugarcubes/SugarCubes
[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 is a demonstration of how to use the projection library. A projection
79 * creates a mutation of the coordinates of all the points in the model, creating
80 * virtual x,y,z coordinates. In effect, this is like virtually rotating the entire
81 * art car. However, since in reality the car does not move, the result is that
82 * it appears that the object we are drawing on the car is actually moving.
83 *
84 * Keep in mind that what we are creating a projection of is the view coordinates.
85 * Depending on your intuition, some operations may feel backwards. For instance,
86 * if you translate the view to the right, it will make it seem that the object
87 * you are drawing has moved to the left. If you scale the view up 2x, objects
88 * drawn with the same absolute values will seem to be half the size.
89 *
90 * If this feels counterintuitive at first, don't worry. Just remember that you
91 * are moving the pixels, not the structure. We're dealing with a finite set
92 * of sparse, non-uniformly spaced pixels. Mutating the structure would move
93 * things to a space where there are no pixels in 99% of the cases.
94 */
95 class TestProjectionPattern extends SCPattern {
96
97 private final Projection projection;
98 private final SawLFO angle = new SawLFO(0, TWO_PI, 9000);
99 private final SinLFO yPos = new SinLFO(-20, 40, 5000);
100
101 public TestProjectionPattern(GLucose glucose) {
102 super(glucose);
103 projection = new Projection(model);
104 addModulator(angle).trigger();
105 addModulator(yPos).trigger();
106 }
107
108 public void run(int deltaMs) {
109 // For the same reasons described above, it may logically feel to you that
110 // some of these operations are in reverse order. Again, just keep in mind that
111 // the car itself is what's moving, not the object
112 projection.reset(model)
113
114 // Translate so the center of the car is the origin, offset by yPos
115 .translateCenter(model, 0, yPos.getValuef(), 0)
116
117 // Rotate around the origin (now the center of the car) about an X-vector
118 .rotate(angle.getValuef(), 1, 0, 0)
119
120 // Scale up the Y axis (objects will look smaller in that access)
121 .scale(1, 1.5, 1);
122
123 float hv = lx.getBaseHuef();
124 for (Coord c : projection) {
125 float d = sqrt(c.x*c.x + c.y*c.y + c.z*c.z); // distance from origin
126 // d = abs(d-60) + max(0, abs(c.z) - 20); // life saver / ring thing
127 d = max(0, abs(c.y) - 10 + .3*abs(c.z) + .08*abs(c.x)); // plane / spear thing
128 colors[c.index] = color(
129 (hv + .6*abs(c.x) + abs(c.z)) % 360,
130 100,
131 constrain(140 - 10*d, 0, 100)
132 );
133 }
134 }
135 }
136
137 class TestCubePattern extends SCPattern {
138
139 private int POINTS_PER_CUBE = Cube.FACES_PER_CUBE * Face.STRIPS_PER_FACE * Strip.POINTS_PER_STRIP;
140 private SawLFO index = new SawLFO(0, POINTS_PER_CUBE, POINTS_PER_CUBE*60);
141
142 TestCubePattern(GLucose glucose) {
143 super(glucose);
144 addModulator(index).start();
145 }
146
147 public void run(int deltaMs) {
148 for (Cube c : model.cubes) {
149 int i = 0;
150 for (Point p : c.points) {
151 colors[p.index] = color(
152 lx.getBaseHuef(),
153 100,
154 max(0, 100 - 80.*abs(i - index.getValuef()))
155 );
156 ++i;
157 }
158 }
159 }
160 }
161
162 class MappingTool extends SCPattern {
163
164 private int cubeIndex = 0;
165 private int stripIndex = 0;
166 private int channelIndex = 0;
167
168 public final int MAPPING_MODE_ALL = 0;
169 public final int MAPPING_MODE_CHANNEL = 1;
170 public final int MAPPING_MODE_SINGLE_CUBE = 2;
171 public int mappingMode = MAPPING_MODE_ALL;
172
173 public final int CUBE_MODE_ALL = 0;
174 public final int CUBE_MODE_SINGLE_STRIP = 1;
175 public final int CUBE_MODE_STRIP_PATTERN = 2;
176 public int cubeMode = CUBE_MODE_ALL;
177
178 public boolean channelModeRed = true;
179 public boolean channelModeGreen = false;
180 public boolean channelModeBlue = false;
181
182 private final static int NUM_CHANNELS = 16;
183
184 private final int[][] frontChannels;
185 private final int[][] rearChannels;
186 private int[] activeChannels;
187
188 MappingTool(GLucose glucose, int[][]frontChannels, int[][]rearChannels) {
189 super(glucose);
190 this.frontChannels = frontChannels;
191 this.rearChannels = rearChannels;
192 setChannel();
193 }
194
195 private void setChannel() {
196 if (channelIndex < frontChannels.length) {
197 activeChannels = frontChannels[channelIndex];
198 } else {
199 activeChannels = rearChannels[channelIndex - frontChannels.length];
200 }
201 }
202
203 private int cubeInChannel(Cube c) {
204 int i = 1;
205 for (int index : activeChannels) {
206 if (c == model.getCubeByRawIndex(index)) {
207 return i;
208 }
209 ++i;
210 }
211 return 0;
212 }
213
214 private void printInfo() {
215 println("Cube:" + cubeIndex + " Strip:" + (stripIndex+1));
216 }
217
218 public void cube(int delta) {
219 int len = model.cubes.size();
220 cubeIndex = (len + cubeIndex + delta) % len;
221 printInfo();
222 }
223
224 public void strip(int delta) {
225 int len = Cube.FACES_PER_CUBE * Face.STRIPS_PER_FACE;
226 stripIndex = (len + stripIndex + delta) % len;
227 printInfo();
228 }
229
230 public void run(int deltaMs) {
231 color off = color(0, 0, 0);
232 color c = off;
233 color r = #FF0000;
234 color g = #00FF00;
235 color b = #0000FF;
236 if (channelModeRed) c |= r;
237 if (channelModeGreen) c |= g;
238 if (channelModeBlue) c |= b;
239
240 int ci = 0;
241 for (Cube cube : model.cubes) {
242 boolean cubeOn = false;
243 int channelIndex = cubeInChannel(cube);
244 switch (mappingMode) {
245 case MAPPING_MODE_ALL: cubeOn = true; break;
246 case MAPPING_MODE_SINGLE_CUBE: cubeOn = (cubeIndex == ci); break;
247 case MAPPING_MODE_CHANNEL: cubeOn = (channelIndex > 0); break;
248 }
249 if (cubeOn) {
250 if (mappingMode == MAPPING_MODE_CHANNEL) {
251 color cc = off;
252 switch (channelIndex) {
253 case 1: cc = r; break;
254 case 2: cc = r|g; break;
255 case 3: cc = g; break;
256 case 4: cc = b; break;
257 case 5: cc = r|b; break;
258 }
259 setColor(cube, cc);
260 } else if (cubeMode == CUBE_MODE_STRIP_PATTERN) {
261 int si = 0;
262 color sc = off;
263 for (Strip strip : cube.strips) {
264 int faceI = si / Face.STRIPS_PER_FACE;
265 switch (faceI) {
266 case 0: sc = r; break;
267 case 1: sc = g; break;
268 case 2: sc = b; break;
269 case 3: sc = r|g|b; break;
270 }
271 if (si % Face.STRIPS_PER_FACE == 2) {
272 sc = r|g;
273 }
274 setColor(strip, sc);
275 ++si;
276 }
277 } else if (cubeMode == CUBE_MODE_SINGLE_STRIP) {
278 setColor(cube, off);
279 setColor(cube.strips.get(stripIndex), c);
280 } else {
281 setColor(cube, c);
282 }
283 } else {
284 setColor(cube, off);
285 }
286 ++ci;
287 }
288
289 }
290
291 public void incCube() {
292 cubeIndex = (cubeIndex + 1) % model.cubes.size();
293 }
294
295 public void decCube() {
296 --cubeIndex;
297 if (cubeIndex < 0) {
298 cubeIndex += model.cubes.size();
299 }
300 }
301
302 public void incChannel() {
303 channelIndex = (channelIndex + 1) % NUM_CHANNELS;
304 setChannel();
305 }
306
307 public void decChannel() {
308 --channelIndex;
309 if (channelIndex < 0) {
310 channelIndex += NUM_CHANNELS;
311 }
312 setChannel();
313 }
314
315 public void incStrip() {
316 int stripsPerCube = Cube.FACES_PER_CUBE * Face.STRIPS_PER_FACE;
317 stripIndex = (stripIndex + 1) % stripsPerCube;
318 }
319
320 public void decStrip() {
321 int stripsPerCube = Cube.FACES_PER_CUBE * Face.STRIPS_PER_FACE;
322 --stripIndex;
323 if (stripIndex < 0) {
324 stripIndex += stripsPerCube;
325 }
326 }
327
328 public void keyPressed() {
329 switch (keyCode) {
330 case UP: if (mappingMode == MAPPING_MODE_CHANNEL) incChannel(); else incCube(); break;
331 case DOWN: if (mappingMode == MAPPING_MODE_CHANNEL) decChannel(); else decCube(); break;
332 case LEFT: decStrip(); break;
333 case RIGHT: incStrip(); break;
334 }
335 switch (key) {
336 case 'r': channelModeRed = !channelModeRed; break;
337 case 'g': channelModeGreen = !channelModeGreen; break;
338 case 'b': channelModeBlue = !channelModeBlue; break;
339 }
340 }
341 }