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