Merge branch 'master' of github.com:sugarcubes/SugarCubes
[SugarCubes.git] / JackStahl.pde
1 /**
2 * A Projection of sin wave in 3d space.
3 * It sort of looks like an animal swiming around in water.
4 * Angle sliders are sort of a work in progress that allow yo to change the crazy ways it moves around.
5 * Hue slider allows you to control how different the colors are along the wave.
6 *
7 * This code copied heavily from Tim and Slee.
8 */
9 class Swim extends SCPattern {
10
11 // Projection stuff
12 private final Projection projection;
13 SawLFO rotation = new SawLFO(0, TWO_PI, 19000);
14 SinLFO yPos = new SinLFO(-25, 25, 12323);
15 final BasicParameter xAngle = new BasicParameter("XANG", 0.9);
16 final BasicParameter yAngle = new BasicParameter("YANG", 0.3);
17 final BasicParameter zAngle = new BasicParameter("ZANG", 0.3);
18
19 final BasicParameter hueScale = new BasicParameter("HUE", 0.3);
20
21 public Swim(GLucose glucose) {
22 super(glucose);
23 projection = new Projection(model);
24
25 addParameter(xAngle);
26 addParameter(yAngle);
27 addParameter(zAngle);
28 addParameter(hueScale);
29
30 addModulator(rotation).trigger();
31 addModulator(yPos).trigger();
32
33 }
34
35
36 int beat = 0;
37 float prevRamp = 0;
38 void run(int deltaMs) {
39
40 // Sync to the beat
41 float ramp = (float)lx.tempo.ramp();
42 if (ramp < prevRamp) {
43 beat = (beat + 1) % 4;
44 }
45 prevRamp = ramp;
46 float phase = (beat+ramp) / 2.0 * 4 * PI;
47
48 float denominator = max(xAngle.getValuef() + yAngle.getValuef() + zAngle.getValuef(), 1);
49
50 projection.reset(model)
51 // Swim around the world
52 .rotate(rotation.getValuef(), xAngle.getValuef() / denominator, yAngle.getValuef() / denominator, zAngle.getValuef() / denominator)
53 .translateCenter(model, 0, 50 + yPos.getValuef(), 0);
54
55 float model_height = model.yMax - model.yMin;
56 float model_width = model.xMax - model.xMin;
57 for (Coord p : projection) {
58 float x_percentage = (p.x - model.xMin)/model_width;
59
60 // Multiply by 1.4 to shrink the size of the sin wave to be less than the height of the cubes.
61 float y_in_range = 1.4 * (2*p.y - model.yMax - model.yMin) / model_height;
62 float sin_x = sin(phase + 2 * PI * x_percentage);
63
64 // Color fade near the top of the sin wave
65 float v1 = sin_x > y_in_range ? (100 + 100*(y_in_range - sin_x)) : 0;
66
67 float hue_color = (lx.getBaseHuef() + hueScale.getValuef() * (abs(p.x-model.xMax/2.)*.3 + abs(p.y-model.yMax/2)*.9 + abs(p.z - model.zMax/2.))) % 360;
68 colors[p.index] = color(hue_color, 70, v1);
69 }
70 }
71 }
72
73 /**
74 * The idea here is to do another sin wave pattern, but with less rotation and more of a breathing / heartbeat affect with spheres above / below the wave.
75 * TODO
76 */
77 class Breathe extends SCPattern {
78
79 final BasicParameter hueScale = new BasicParameter("HUE", 0.3);
80
81 public Breathe(GLucose glucose) {
82 super(glucose);
83
84 addParameter(hueScale);
85 }
86
87
88 int beat = 0;
89 float prevRamp = 0;
90 void run(int deltaMs) {
91
92 // Sync to the beat
93 float ramp = (float)lx.tempo.ramp();
94 if (ramp < prevRamp) {
95 beat = (beat + 1) % 4;
96 }
97 prevRamp = ramp;
98 float phase = (beat+ramp) / 2.0 * 2 * PI;
99
100 float model_height = model.yMax - model.yMin;
101 float model_width = model.xMax - model.xMin;
102 for (Point p : model.points) {
103 float x_percentage = (p.x - model.xMin)/model_width;
104
105 // Multiply by 1.4 to shrink the size of the sin wave to be less than the height of the truck.
106 float y_in_range = 1.4 * (2*p.y - model.yMax - model.yMin) / model_height;
107 // xcxc add back phase
108 float sin_x = sin(phase + 2 * PI * x_percentage);
109
110 // Color fade near the top of the sin wave
111 float v1 = sin_x > y_in_range ? (100 + 100*(y_in_range - sin_x)) : 0;
112
113 float hue_color = (lx.getBaseHuef() + hueScale.getValuef() * (abs(p.x-model.xMax/2.)*.6 + abs(p.y-model.yMax/2)*.9 + abs(p.z - model.zMax/2.))) % 360;
114 colors[p.index] = color(hue_color, 70, v1);
115 }
116 }
117 }
118
119