Send normalized values back to APC40
[SugarCubes.git] / JackStahl.pde
CommitLineData
677f5e15
JS
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 */
9class Swim extends SCPattern {
10
11 // Projection stuff
9fa29818 12 private final LXProjection projection;
71515698
JS
13 SinLFO rotationX = new SinLFO(-PI/16, PI/8, 9000);
14 SinLFO rotationY = new SinLFO(-PI/8, PI/8, 7000);
15 SinLFO rotationZ = new SinLFO(-PI/8, PI/16, 11000);
16 SinLFO yPos = new SinLFO(-1, 1, 13234);
17 SinLFO sineHeight = new SinLFO(1, 2.5, 13234);
18 SawLFO phaseLFO = new SawLFO(0, 2 * PI, 15000 - 13000 * 0.5);
19 final BasicParameter phaseParam = new BasicParameter("Spd", 0.5);
20 final BasicParameter crazyParam = new BasicParameter("Crzy", 0.5);
7e052aba 21
677f5e15
JS
22 final BasicParameter hueScale = new BasicParameter("HUE", 0.3);
23
24 public Swim(GLucose glucose) {
25 super(glucose);
9fa29818 26 projection = new LXProjection(model);
677f5e15 27 addParameter(hueScale);
71515698
JS
28 addParameter(crazyParam);
29 addParameter(phaseParam);
677f5e15 30
71515698
JS
31 addModulator(rotationX).trigger();
32 addModulator(rotationY).trigger();
33 addModulator(rotationZ).trigger();
677f5e15 34 addModulator(yPos).trigger();
71515698
JS
35 addModulator(phaseLFO).trigger();
36 }
37
38 public void onParameterChanged(LXParameter parameter) {
39 if (parameter == phaseParam) {
40 phaseLFO.setDuration(5000 - 4500 * parameter.getValuef());
41 }
677f5e15 42 }
677f5e15
JS
43
44 int beat = 0;
45 float prevRamp = 0;
34327c96 46 void run(double deltaMs) {
677f5e15 47
71515698
JS
48 float phase = phaseLFO.getValuef();
49
50 float up_down_range = (model.yMax - model.yMin) / 4;
677f5e15 51
71515698
JS
52 // Swim around the world
53 float crazy_factor = crazyParam.getValuef() / 0.2;
2bb56822 54 projection.reset()
71515698
JS
55 .rotate(rotationZ.getValuef() * crazy_factor, 0, 1, 0)
56 .rotate(rotationX.getValuef() * crazy_factor, 0, 0, 1)
57 .rotate(rotationY.getValuef() * crazy_factor, 0, 1, 0)
58 .translate(0, up_down_range * yPos.getValuef(), 0);
59
677f5e15
JS
60
61 float model_height = model.yMax - model.yMin;
62 float model_width = model.xMax - model.xMin;
9fa29818 63 for (LXVector p : projection) {
677f5e15
JS
64 float x_percentage = (p.x - model.xMin)/model_width;
65
71515698
JS
66 // Multiply by sineHeight to shrink the size of the sin wave to be less than the height of the cubes.
67 float y_in_range = sineHeight.getValuef() * (2*p.y - model.yMax - model.yMin) / model_height;
677f5e15
JS
68 float sin_x = sin(phase + 2 * PI * x_percentage);
69
71515698
JS
70 float size_of_sin_wave = 0.4;
71
72 float v1 = (abs(y_in_range - sin_x) > size_of_sin_wave) ? 0 : abs((y_in_range - sin_x + size_of_sin_wave) / size_of_sin_wave / 2 * 100);
73
677f5e15
JS
74
75 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;
71515698 76 colors[p.index] = lx.hsb(hue_color, 100, v1);
677f5e15
JS
77 }
78 }
79}
80
81/**
82 * 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.
7e052aba
JS
83 * This is not done.
84 */
8185ec8f 85class Balance extends SCPattern {
677f5e15 86
8185ec8f 87 final BasicParameter hueScale = new BasicParameter("Hue", 0.4);
677f5e15 88
9b37e50c
JS
89 class Sphere {
90 float x, y, z;
9b37e50c 91 }
7e052aba
JS
92
93
94 // Projection stuff
9fa29818 95 private final LXProjection projection;
7e052aba 96
8185ec8f
JS
97 SinLFO sphere1Z = new SinLFO(0, 0, 15323);
98 SinLFO sphere2Z = new SinLFO(0, 0, 8323);
99 SinLFO rotationX = new SinLFO(-PI/32, PI/32, 9000);
100 SinLFO rotationY = new SinLFO(-PI/16, PI/16, 7000);
101 SinLFO rotationZ = new SinLFO(-PI/16, PI/16, 11000);
102 SawLFO phaseLFO = new SawLFO(0, 2 * PI, 5000 - 4500 * 0.5);
103 final BasicParameter phaseParam = new BasicParameter("Spd", 0.5);
104 final BasicParameter crazyParam = new BasicParameter("Crzy", 0.2);
7e052aba
JS
105
106
9b37e50c 107 private final Sphere[] spheres;
7e052aba 108 private final float centerX, centerY, centerZ, modelHeight, modelWidth, modelDepth;
8185ec8f 109 SinLFO heightMod = new SinLFO(0.8, 1.9, 17298);
9b37e50c 110
8185ec8f 111 public Balance(GLucose glucose) {
677f5e15
JS
112 super(glucose);
113
9fa29818 114 projection = new LXProjection(model);
7e052aba 115
677f5e15 116 addParameter(hueScale);
8185ec8f
JS
117 addParameter(phaseParam);
118 addParameter(crazyParam);
7e052aba 119
9b37e50c
JS
120 spheres = new Sphere[2];
121 centerX = (model.xMax + model.xMin) / 2;
122 centerY = (model.yMax + model.yMin) / 2;
123 centerZ = (model.zMax + model.zMin) / 2;
7e052aba
JS
124 modelHeight = model.yMax - model.yMin;
125 modelWidth = model.xMax - model.xMin;
126 modelDepth = model.zMax - model.zMin;
9b37e50c 127
9b37e50c 128 spheres[0] = new Sphere();
8185ec8f
JS
129 spheres[0].x = 1*modelWidth/2 + model.xMin;
130 spheres[0].y = centerY + 20;
9b37e50c 131 spheres[0].z = centerZ;
7e052aba 132
9b37e50c 133 spheres[1] = new Sphere();
8185ec8f 134 spheres[1].x = model.xMin;
7e052aba 135 spheres[1].y = centerY - 20;
9b37e50c 136 spheres[1].z = centerZ;
9b37e50c 137
8185ec8f
JS
138 addModulator(rotationX).trigger();
139 addModulator(rotationY).trigger();
140 addModulator(rotationZ).trigger();
141
677f5e15 142
7e052aba
JS
143 addModulator(sphere1Z).trigger();
144 addModulator(sphere2Z).trigger();
8185ec8f 145 addModulator(phaseLFO).trigger();
7e052aba
JS
146
147 addModulator(heightMod).trigger();
148 }
677f5e15 149
8185ec8f
JS
150 public void onParameterChanged(LXParameter parameter) {
151 if (parameter == phaseParam) {
152 phaseLFO.setDuration(5000 - 4500 * parameter.getValuef());
153 }
154 }
155
677f5e15
JS
156 int beat = 0;
157 float prevRamp = 0;
34327c96 158 void run(double deltaMs) {
677f5e15
JS
159
160 // Sync to the beat
161 float ramp = (float)lx.tempo.ramp();
162 if (ramp < prevRamp) {
163 beat = (beat + 1) % 4;
164 }
165 prevRamp = ramp;
8185ec8f 166 float phase = phaseLFO.getValuef();
677f5e15 167
8185ec8f 168 float crazy_factor = crazyParam.getValuef() / 0.2;
2bb56822 169 projection.reset()
8185ec8f
JS
170 .rotate(rotationZ.getValuef() * crazy_factor, 0, 1, 0)
171 .rotate(rotationX.getValuef() * crazy_factor, 0, 0, 1)
172 .rotate(rotationY.getValuef() * crazy_factor, 0, 1, 0);
677f5e15 173
9fa29818 174 for (LXVector p : projection) {
7e052aba
JS
175 float x_percentage = (p.x - model.xMin)/modelWidth;
176
177 float y_in_range = heightMod.getValuef() * (2*p.y - model.yMax - model.yMin) / modelHeight;
178 float sin_x = sin(PI / 2 + phase + 2 * PI * x_percentage);
677f5e15
JS
179
180 // Color fade near the top of the sin wave
8185ec8f 181 float v1 = max(0, 100 * (1 - 4*abs(sin_x - y_in_range)));
677f5e15 182
8185ec8f 183 float hue_color = (lx.getBaseHuef() + hueScale.getValuef() * (abs(p.x-model.xMax/2.) + abs(p.y-model.yMax/2)*.2 + abs(p.z - model.zMax/2.)*.5)) % 360;
a41f334c 184 color c = lx.hsb(hue_color, 80, v1);
7e052aba 185
9b37e50c
JS
186 // Now draw the spheres
187 for (Sphere s : spheres) {
8185ec8f 188 float phase_x = (s.x - phase / (2 * PI) * modelWidth) % modelWidth;
7e052aba
JS
189 float x_dist = LXUtils.wrapdistf(p.x, phase_x, modelWidth);
190
191 float sphere_z = (s == spheres[0]) ? (s.z + sphere1Z.getValuef()) : (s.z - sphere2Z.getValuef());
192
8185ec8f 193
7e052aba 194 float d = sqrt(pow(x_dist, 2) + pow(p.y - s.y, 2) + pow(p.z - sphere_z, 2));
8185ec8f
JS
195
196 float distance_from_beat = (beat % 2 == 1) ? 1 - ramp : ramp;
197
198 min(ramp, 1-ramp);
199
200 float r = 40 - pow(distance_from_beat, 0.75) * 20;
7e052aba
JS
201
202 float distance_value = max(0, 1 - max(0, d - r) / 10);
203 float beat_value = 1.0;
7e052aba
JS
204
205 float value = min(beat_value, distance_value);
206
b0be384f
JS
207 float sphere_color = (lx.getBaseHuef() - (1 - hueScale.getValuef()) * d/r * 45) % 360;
208
a41f334c 209 c = blendColor(c, lx.hsb((sphere_color + 270) % 360, 60, min(1, value) * 100), ADD);
9b37e50c
JS
210 }
211 colors[p.index] = c;
677f5e15
JS
212 }
213 }
214}