L8on Strips 3d Life
[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 LXProjection projection;
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);
21
22 final BasicParameter hueScale = new BasicParameter("HUE", 0.3);
23
24 public Swim(GLucose glucose) {
25 super(glucose);
26 projection = new LXProjection(model);
27 addParameter(hueScale);
28 addParameter(crazyParam);
29 addParameter(phaseParam);
30
31 addModulator(rotationX).trigger();
32 addModulator(rotationY).trigger();
33 addModulator(rotationZ).trigger();
34 addModulator(yPos).trigger();
35 addModulator(phaseLFO).trigger();
36 }
37
38 public void onParameterChanged(LXParameter parameter) {
39 if (parameter == phaseParam) {
40 phaseLFO.setDuration(5000 - 4500 * parameter.getValuef());
41 }
42 }
43
44 int beat = 0;
45 float prevRamp = 0;
46 void run(double deltaMs) {
47
48 float phase = phaseLFO.getValuef();
49
50 float up_down_range = (model.yMax - model.yMin) / 4;
51
52 // Swim around the world
53 float crazy_factor = crazyParam.getValuef() / 0.2;
54 projection.reset()
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
60
61 float model_height = model.yMax - model.yMin;
62 float model_width = model.xMax - model.xMin;
63 for (LXVector p : projection) {
64 float x_percentage = (p.x - model.xMin)/model_width;
65
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;
68 float sin_x = sin(phase + 2 * PI * x_percentage);
69
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
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;
76 colors[p.index] = lx.hsb(hue_color, 100, v1);
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.
83 * This is not done.
84 */
85 class Balance extends SCPattern {
86
87 final BasicParameter hueScale = new BasicParameter("Hue", 0.4);
88
89 class Sphere {
90 float x, y, z;
91 }
92
93
94 // Projection stuff
95 private final LXProjection projection;
96
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);
105
106
107 private final Sphere[] spheres;
108 private final float centerX, centerY, centerZ, modelHeight, modelWidth, modelDepth;
109 SinLFO heightMod = new SinLFO(0.8, 1.9, 17298);
110
111 public Balance(GLucose glucose) {
112 super(glucose);
113
114 projection = new LXProjection(model);
115
116 addParameter(hueScale);
117 addParameter(phaseParam);
118 addParameter(crazyParam);
119
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;
124 modelHeight = model.yMax - model.yMin;
125 modelWidth = model.xMax - model.xMin;
126 modelDepth = model.zMax - model.zMin;
127
128 spheres[0] = new Sphere();
129 spheres[0].x = 1*modelWidth/2 + model.xMin;
130 spheres[0].y = centerY + 20;
131 spheres[0].z = centerZ;
132
133 spheres[1] = new Sphere();
134 spheres[1].x = model.xMin;
135 spheres[1].y = centerY - 20;
136 spheres[1].z = centerZ;
137
138 addModulator(rotationX).trigger();
139 addModulator(rotationY).trigger();
140 addModulator(rotationZ).trigger();
141
142
143 addModulator(sphere1Z).trigger();
144 addModulator(sphere2Z).trigger();
145 addModulator(phaseLFO).trigger();
146
147 addModulator(heightMod).trigger();
148 }
149
150 public void onParameterChanged(LXParameter parameter) {
151 if (parameter == phaseParam) {
152 phaseLFO.setDuration(5000 - 4500 * parameter.getValuef());
153 }
154 }
155
156 int beat = 0;
157 float prevRamp = 0;
158 void run(double deltaMs) {
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;
166 float phase = phaseLFO.getValuef();
167
168 float crazy_factor = crazyParam.getValuef() / 0.2;
169 projection.reset()
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);
173
174 for (LXVector p : projection) {
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);
179
180 // Color fade near the top of the sin wave
181 float v1 = max(0, 100 * (1 - 4*abs(sin_x - y_in_range)));
182
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;
184 color c = lx.hsb(hue_color, 80, v1);
185
186 // Now draw the spheres
187 for (Sphere s : spheres) {
188 float phase_x = (s.x - phase / (2 * PI) * modelWidth) % modelWidth;
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
193
194 float d = sqrt(pow(x_dist, 2) + pow(p.y - s.y, 2) + pow(p.z - sphere_z, 2));
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;
201
202 float distance_value = max(0, 1 - max(0, d - r) / 10);
203 float beat_value = 1.0;
204
205 float value = min(beat_value, distance_value);
206
207 float sphere_color = (lx.getBaseHuef() - (1 - hueScale.getValuef()) * d/r * 45) % 360;
208
209 c = blendColor(c, lx.hsb((sphere_color + 270) % 360, 60, min(1, value) * 100), ADD);
210 }
211 colors[p.index] = c;
212 }
213 }
214 }