Merge branch 'master' into jack-breathe
[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 * 2 * 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 class Sphere {
82 float x, y, z;
83 float radius;
84 float hue;
85 }
86
87 private final Sphere[] spheres;
88 private final float centerX, centerY, centerZ;
89
90 public Breathe(GLucose glucose) {
91 super(glucose);
92
93 addParameter(hueScale);
94
95 spheres = new Sphere[2];
96 centerX = (model.xMax + model.xMin) / 2;
97 centerY = (model.yMax + model.yMin) / 2;
98 centerZ = (model.zMax + model.zMin) / 2;
99
100
101 spheres[0] = new Sphere();
102 spheres[0].x = model.xMin + 50;
103 spheres[0].y = centerY;
104 spheres[0].z = centerZ;
105 spheres[0].radius = 25;
106
107 spheres[1] = new Sphere();
108 spheres[1].x = model.xMax - 50;
109 spheres[1].y = centerY;
110 spheres[1].z = centerZ;
111 spheres[1].radius = 25;
112
113 }
114
115
116 int beat = 0;
117 float prevRamp = 0;
118 void run(int deltaMs) {
119
120 // Sync to the beat
121 float ramp = (float)lx.tempo.ramp();
122 if (ramp < prevRamp) {
123 beat = (beat + 1) % 4;
124 }
125 prevRamp = ramp;
126 float phase = (beat+ramp) / 2.0 * 2 * PI;
127
128 float model_height = model.yMax - model.yMin;
129 float model_width = model.xMax - model.xMin;
130 for (Point p : model.points) {
131 float x_percentage = (p.x - model.xMin)/model_width;
132
133 // Multiply by 1.4 to shrink the size of the sin wave to be less than the height of the truck.
134 float y_in_range = 1.4 * (2*p.y - model.yMax - model.yMin) / model_height;
135 // xcxc add back phase
136 float sin_x = sin(phase + 2 * PI * x_percentage);
137
138 // Color fade near the top of the sin wave
139 float v1 = sin_x > y_in_range ? (100 + 100*(y_in_range - sin_x)) : 0;
140
141 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;
142 color c = color(hue_color, 70, v1);
143
144 // Now draw the spheres
145 for (Sphere s : spheres) {
146 float phase_x = (s.x - phase * model_width / ( 2 * PI)) % model_width;
147 float d = sqrt(pow(p.x - phase_x, 2) + pow(p.y - s.y, 2) + pow(p.z - s.z, 2));
148 float r = (s.radius);
149 float value = max(0, 1 - max(0, d - r) / 10);
150
151 c = blendColor(c, color(hue_color + 180 % 360, 70, min(1, value) * 100), ADD);
152 }
153 colors[p.index] = c;
154 }
155 }
156 }
157
158