Merge branch 'master' of https://github.com/jackstah/SugarCubes into jackstah-master
[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 int beat = 0;
36 float prevRamp = 0;
37 void run(int deltaMs) {
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;
45 float phase = (beat+ramp) / 2.0 * 2 * PI;
46
47 float denominator = max(xAngle.getValuef() + yAngle.getValuef() + zAngle.getValuef(), 1);
48
49 projection.reset(model)
50 // Swim around the world
51 .rotate(rotation.getValuef(), xAngle.getValuef() / denominator, yAngle.getValuef() / denominator, zAngle.getValuef() / denominator)
52 .translateCenter(model, 0, 50 + yPos.getValuef(), 0);
53
54 float model_height = model.yMax - model.yMin;
55 float model_width = model.xMax - model.xMin;
56 for (Coord p : projection) {
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;
67 colors[p.index] = color(hue_color, 70, v1);
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.
74 * This is not done.
75 */
76 class Breathe extends SCPattern {
77
78 final BasicParameter hueScale = new BasicParameter("HUE", 0.3);
79
80 class Sphere {
81 float x, y, z;
82 }
83
84
85 // Projection stuff
86 private final Projection projection;
87
88 SinLFO sphere1Z = new SinLFO(0, 80, 15323);
89 SinLFO sphere2Z = new SinLFO(-80, 0, 8323);
90 SawLFO rotation = new SawLFO(- PI / 16, PI / 16, 7334);
91
92
93 private final Sphere[] spheres;
94 private final float centerX, centerY, centerZ, modelHeight, modelWidth, modelDepth;
95 SinLFO heightMod = new SinLFO(0.6, 1.85, 17298);
96
97 public Breathe(GLucose glucose) {
98 super(glucose);
99
100 // Unused for now
101 projection = new Projection(model);
102
103 addParameter(hueScale);
104
105 spheres = new Sphere[2];
106 centerX = (model.xMax + model.xMin) / 2;
107 centerY = (model.yMax + model.yMin) / 2;
108 centerZ = (model.zMax + model.zMin) / 2;
109 modelHeight = model.yMax - model.yMin;
110 modelWidth = model.xMax - model.xMin;
111 modelDepth = model.zMax - model.zMin;
112
113 spheres[0] = new Sphere();
114 spheres[0].x = 3*modelWidth/8;
115 spheres[0].y = centerY + 10;
116 spheres[0].z = centerZ;
117
118 spheres[1] = new Sphere();
119 spheres[1].x = 7*modelWidth/8;
120 spheres[1].y = centerY - 20;
121 spheres[1].z = centerZ;
122
123
124 addModulator(sphere1Z).trigger();
125 addModulator(sphere2Z).trigger();
126
127 addModulator(heightMod).trigger();
128 }
129
130 int beat = 0;
131 float prevRamp = 0;
132 void run(int deltaMs) {
133
134 // Sync to the beat
135 float ramp = (float)lx.tempo.ramp();
136 if (ramp < prevRamp) {
137 beat = (beat + 1) % 4;
138 }
139 prevRamp = ramp;
140 float phase = (beat+ramp) * PI % (2 * PI);
141
142 projection.reset(model)
143 .rotate(rotation.getValuef(), 0, 1, 0);
144
145 for (Coord p : projection) {
146 float x_percentage = (p.x - model.xMin)/modelWidth;
147
148 float y_in_range = heightMod.getValuef() * (2*p.y - model.yMax - model.yMin) / modelHeight;
149 float sin_x = sin(PI / 2 + phase + 2 * PI * x_percentage);
150
151 // Color fade near the top of the sin wave
152 float v1 = sin_x > y_in_range ? (100 + 100*(y_in_range - sin_x)) : 0;
153
154 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;
155 color c = color(hue_color, 60, v1);
156
157 // Now draw the spheres
158 for (Sphere s : spheres) {
159 float phase_x = (s.x - phase * modelWidth / ( 2 * PI)) % modelWidth;
160 float x_dist = LXUtils.wrapdistf(p.x, phase_x, modelWidth);
161
162 float sphere_z = (s == spheres[0]) ? (s.z + sphere1Z.getValuef()) : (s.z - sphere2Z.getValuef());
163
164 float d = sqrt(pow(x_dist, 2) + pow(p.y - s.y, 2) + pow(p.z - sphere_z, 2));
165 float r = 25;
166
167 float distance_value = max(0, 1 - max(0, d - r) / 10);
168 float beat_value = 1.0;
169 if (s == spheres[0]) {
170 // beat_value = .2 + ((beat % 4 >= 2) ? ((4 - (ramp + beat)) / 2) *.8 : 0);
171 }
172 else {
173 // beat_value = .2 + ((beat % 4 < 2) ? ((2 - (ramp + beat)) / 2) *.8 : 0);
174 }
175
176 float value = min(beat_value, distance_value);
177
178 c = blendColor(c, color((hue_color + 180) % 360, 100, min(1, value) * 100), ADD);
179 }
180 colors[p.index] = c;
181 }
182 }
183 }
184