Performance improvements to SineSpheres and 's' to toggle simulation
[SugarCubes.git] / AlexGreen.pde
1 class SineSphere extends SCPattern {
2 private SawLFO yrot = new SawLFO(0, TWO_PI, 3000);
3 private SawLFO yrot2 = new SawLFO(0, -TWO_PI, 8000);
4 public final Projection sinespin;
5 public final Projection sinespin2;
6 private BasicParameter rotation = new BasicParameter("rotation", 0);
7 float modelrad = sqrt((model.xMax)*(model.xMax) + (model.yMax)*(model.yMax) + (model.zMax)*(model.zMax));
8 Pick Sshape;
9 public final PVector P = new PVector();
10
11 class Sphery {
12 float f1xcenter, f1ycenter, f1zcenter, f2xcenter , f2ycenter, f2zcenter; //second three are for an ellipse with two foci
13 private SinLFO vibration;
14 private SinLFO surface;
15 private SinLFO vx;
16 private SinLFO xbounce;
17 public SinLFO ybounce;
18 private SinLFO zbounce;
19 float vibration_min, vibration_max, vperiod;
20 public BasicParameter widthparameter;
21 public BasicParameter huespread;
22 public BasicParameter bouncerate;
23 public BasicParameter bounceamp;
24 public final PVector circlecenter = new PVector();
25
26 public Sphery(float f1xcenter, float f1ycenter, float f1zcenter, float vibration_min, float vibration_max, float vperiod)
27 {
28 this.f1xcenter = f1xcenter;
29 this.f1ycenter = f1ycenter;
30 this.f1zcenter = f1zcenter;
31 this.vibration_min = vibration_min;
32 this.vibration_max = vibration_max;
33 this.vperiod = vperiod;
34 //addParameter(bounceamp = new BasicParameter("Amp", .5));
35 //addParameter(bouncerate = new BasicParameter("Rate", .5)); //ybounce.modulateDurationBy(bouncerate);
36 addParameter(widthparameter = new BasicParameter("Width", .1));
37 addParameter(huespread = new BasicParameter("Hue", .5, 10));
38
39 addModulator( vx = new SinLFO(-4000, 10000, 100000)).trigger() ;
40 //addModulator(xbounce = new SinLFO(model.xMax/3, 2*model.yMax/3, 2000)).trigger();
41 addModulator(ybounce= new SinLFO(model.yMax/3, 2*model.yMax/3, 240000./lx.tempo.bpm())).trigger(); //ybounce.modulateDurationBy
42
43 //addModulator(bounceamp); //ybounce.setMagnitude(bouncerate);
44 addModulator( vibration = new SinLFO(vibration_min , vibration_max, 240000./lx.tempo.bpm())).trigger(); //vibration.modulateDurationBy(vx);
45
46 }
47 public Sphery(float f1xcenter, float f1ycenter, float f1zcenter, float f2xcenter, float f2ycenter, float f2zcenter,
48 float vibration_min, float vibration_max, float vperiod)
49
50 {
51 this.f1xcenter = f1xcenter;
52 this.f1ycenter = f1ycenter;
53 this.f1zcenter = f1zcenter;
54 this.f2xcenter = f2xcenter;
55 this.f2ycenter = f2ycenter;
56 this.f2zcenter = f2zcenter;
57 this.vibration_min = vibration_min;
58 this.vibration_max = vibration_max;
59 this.vperiod = vperiod;
60 //addModulator(xbounce = new SinLFO(model.xMax/3, 2*model.yMax/3, 2000)).trigger();
61 addModulator(ybounce).trigger();
62 addModulator( vibration = new SinLFO(vibration_min , vibration_max, lx.tempo.rampf())).trigger(); //vibration.modulateDurationBy(vx);
63 addParameter(widthparameter = new BasicParameter("Width", .1));
64 addParameter(huespread = new BasicParameter("Hue", .2));
65
66 }
67
68
69
70
71
72 float distfromcirclecenter(float px, float py, float pz, float f1x, float f1y, float f1z)
73 {
74 return dist(px, py, pz, f1x, f1y, f1z);
75 }
76 //void updatespherey(deltaMs, )
77
78 float quadrant(PVector q) {
79 float qtheta = atan2( (q.x-f1xcenter) , (q.z - f1zcenter) );
80 //println( "qtheta " + qtheta);
81
82 return map(qtheta, -PI/2, PI/2, 140, 240);
83 //if (q.x > f1xcenter ) {return 140 ;}
84 //else {return 250;}
85 }
86 color spheryvalue (PVector p, float f1xcenter, float f1ycenter, float f1zcenter) {
87 circlecenter.set(f1xcenter, f1ycenter, f1zcenter);
88
89 // circlecenter = new PVector(f1xcenter, f1ycenter, f1zcenter);
90 //switch(sShpape.cur() ) {}
91
92 float b = max(0, 100 - 100*widthparameter.getValuef()*abs(p.dist(circlecenter)
93 - vibration.getValuef() ) );
94
95 if (b <= 0) {
96 return 0;
97 }
98
99 return lx.hsb(
100 constrain(huespread.getValuef()*5*quadrant(p), 0, 360),
101 80,
102 b
103 );
104 }
105 color ellipsevalue(float px, float py, float pz , float f1xc, float f1yc, float f1zc, float f2xc, float f2yc, float f2zc)
106 {
107 //switch(sShpape.cur() ) {}
108 return lx.hsb(huespread.getValuef()*5*px, dist(model.xMax-px, model.yMax-py, model.zMax-pz, f1xc, f1yc, f1zc) ,
109 max(0, 100 - 100*widthparameter.getValuef() *
110 abs( (dist(px, py, pz, f1xc, ybounce.getValuef(), f1zc) +
111 (dist(px, py , pz, f2xc, ybounce.getValuef(), f2zc) ) )/2
112 - 1.2*vibration.getValuef() ) ) ) ;
113 }
114
115 void run(double deltaMs) {
116 float vv = vibration.getValuef();
117 float ybv = ybounce.getValuef();
118
119 }
120
121 }
122
123
124 final Sphery[] spherys;
125
126 SineSphere(GLucose glucose)
127 {
128 super(glucose);
129 sinespin = new Projection(model);
130 sinespin2 = new Projection(model);
131 addModulator(yrot).trigger();
132 addModulator(yrot2).trigger();
133 addParameter(rotation);
134 //Sshape = addPick("Shape", , 1);
135 spherys = new Sphery[] {
136 new Sphery(model.xMax/4, model.yMax/2, model.zMax/2, modelrad/16, modelrad/8, 3000),
137 new Sphery(.75*model.xMax, model.yMax/2, model.zMax/2, modelrad/20, modelrad/10, 2000),
138 new Sphery(model.xMax/2, model.yMax/2, model.zMax/2, modelrad/4, modelrad/8, 2300),
139
140 new Sphery(.3*model.xMax, .4*model.yMax, .6*model.zMax, modelrad/16, modelrad/8, 4000),
141 new Sphery(.75*model.xMax, model.yMax/2, model.zMax/2, modelrad/20, modelrad/10, 2000),
142 new Sphery(model.xMax/2, model.yMax/2, model.zMax/2, modelrad/4, modelrad/8, 2300),
143
144 };
145 }
146
147 // public void onParameterChanged(LXParameter parameter)
148 // {
149
150
151 // for (Sphery s : spherys) {
152 // if (s == null) continue;
153 // double bampv = s.bounceamp.getValue();
154 // double brv = s.bouncerate.getValue();
155 // double tempobounce = lx.tempo.bpm();
156 // if (parameter == s.bounceamp)
157 // {
158 // s.ybounce.setRange(bampv*model.yMax/3 , bampv*2*model.yMax/3, brv);
159 // }
160 // else if ( parameter == s.bouncerate )
161 // {
162 // s.ybounce.setDuration(120000./tempobounce);
163 // }
164 // }
165 // }
166
167 public void run( double deltaMs) {
168 float t = lx.tempo.rampf();
169 float bpm = lx.tempo.bpmf();
170 spherys[0].run(deltaMs);
171 spherys[1].run(deltaMs);
172 spherys[2].run(deltaMs);
173 spherys[3].run(deltaMs);
174
175 sinespin.reset(model)
176
177
178 // Translate so the center of the car is the origin, offset
179 .translateCenter(model, 0, 0, 0)
180 // .scale(1.3,1.3,1.3)
181 // Rotate around the origin (now the center of the car) about an y-vector
182 .rotate(yrot.getValuef(), 0, 1 , 0)
183 .translate(model.cx, model.cy, model.cz);
184
185
186
187
188
189 for (Coord p: sinespin)
190 // for (Point p: model.points)
191 {
192 P.set(p.x, p.y, p.z);
193 // PVector P = new PVector(p.x, p.y, p.z);
194 color c = #000000;
195 c = blendIfColor(c, spherys[1].spheryvalue(P, .75*model.xMax, model.yMax/2, model.zMax/2), ADD);
196 c = blendIfColor(c, spherys[0].spheryvalue(P, model.xMax/4, model.yMax/4, model.zMax/2), ADD);
197 c = blendIfColor(c, spherys[2].spheryvalue(P, model.xMax/2, model.yMax/2, model.zMax/2),ADD);
198
199
200 colors[p.index] = c;
201
202
203 }
204 sinespin2.reset(model).
205 translateCenter(model,0,0,0).
206 rotate(yrot2.getValuef(), 0, 1, 0).
207 translate(model.cx,model.cy,model.cz);
208
209 for (Coord p: sinespin2)
210 { color c = 0;
211 // PVector P = new PVector(p.x, p.y, p.z);
212 P.set(p.x, p.y, p.z);
213 c = blendIfColor(c, spherys[3].spheryvalue(P, .3*model.xMax, .7*model.yMax, .6*model.zMax),ADD);
214
215 colors[p.index] = blendIfColor(colors[p.index], c , ADD);
216
217 }
218
219
220
221 }
222
223 color blendIfColor(color c1, color c2, int mode) {
224 if (c2 != 0) {
225 return blendColor(c1, c2, mode);
226 }
227 return c1;
228 }
229
230
231 // color c = 0;
232 // c = blendColor(c, spherys[3].ellipsevalue(Px.x, Px.y, Px.z, model.xMax/4, model.yMax/4, model.zMax/4, 3*model.xMax/4, 3*model.yMax/4, 3*model.zMax/4),ADD);
233 // return c;
234 // }
235 // return lx.hsb(0,0,0);
236 // // else if(spheremode ==2)
237 // { color c = 0;
238 // return lx.hsb(CalcCone( (xyz by = new xyz(0,spherys[2].ybounce.getValuef(),0) ), Px, mid) );
239
240 // }
241
242
243 // }
244
245 }
246
247 class CubeCurl extends SCPattern{
248 float CH, CW, diag;
249 ArrayList<PVector> cubeorigin = new ArrayList<PVector>();
250 ArrayList<PVector> centerlist = new ArrayList<PVector>();
251 private SinLFO curl = new SinLFO(0, Cube.EDGE_HEIGHT, 5000 );
252
253 private SinLFO bg = new SinLFO(180, 220, 3000);
254
255 CubeCurl(GLucose glucose){
256 super(glucose);
257 addModulator(curl).trigger();
258 addModulator(bg).trigger();
259 this.CH = Cube.EDGE_HEIGHT;
260 this.CW = Cube.EDGE_WIDTH;
261 this.diag = sqrt(CW*CW + CW*CW);
262
263
264 ArrayList<PVector> centerlistrelative = new ArrayList<PVector>();
265 for (int i = 0; i < model.cubes.size(); i++){
266 Cube a = model.cubes.get(i);
267 cubeorigin.add(new PVector(a.x, a.y, a.z));
268 centerlist.add(new PVector(a.cx, a.cy, a.cz) );
269
270 }
271
272 }
273 //there is definitely a better way of doing this!
274 PVector centerofcube(int i) {
275 Cube c = model.cubes.get(i);
276
277 println(" cube #: " + i + " c.x " + c.x + " c.y " + c.y + " c.z " + c.z );
278 // PVector cubeangle = new PVector(c.rx, c.ry, c.rz);
279 println("raw x angle: " + c.rx + "raw y angle: " + c.ry + "raw z angle: " + c.rz);
280 PVector cubecenter = new PVector(c.x + CW/2, c.y + CH/2, c.z + CW/2);
281 println("cubecenter unrotated: " + cubecenter.x + " " +cubecenter.y + " " +cubecenter.z );
282 PVector centerrot = new PVector(cos(c.rx)*CW/2 - sin(c.rx)*CW/2, cubecenter.y, cos(c.rz)*CW/2 + sin(c.rz)*CW/2);
283 // nCos*(y-o.y) - nSin*(z-o.z) + o.y
284 cubecenter = PVector.add(new PVector(c.x, c.y, c.z), centerrot);
285 println( " cubecenter.x " + cubecenter.x + " cubecenter.y " + cubecenter.y + " cubecenter.z " + cubecenter.z + " ");
286
287
288 return cubecenter;
289 }
290
291
292 void run(double deltaMs){
293 for (int i =0; i < model.cubes.size(); i++) {
294 Cube c = model.cubes.get(i);
295 float cfloor = c.y;
296
297 // if (i%3 == 0){
298
299 // for (Point p : c.points ){
300 // // colors[p.index]=color(0,0,0);
301 // //float dif = (p.y - c.y);
302 // //colors[p.index] = color( bg.getValuef() , 80 , dif < curl.getValuef() ? 80 : 0, ADD);
303 // }
304 // }
305
306 // else if (i%3 == 1) {
307
308 // for (Point p: c.points){
309 // colors[p.index]=color(0,0,0);
310 // float dif = (p.y - c.y);
311 // // colors[p.index] =
312 // // color(bg.getValuef(),
313 // // map(curl.getValuef(), 0, Cube.EDGE_HEIGHT, 20, 100),
314 // // 100 - 10*abs(dif - curl.getValuef()), ADD );
315 // }
316 // }
317 // else if (i%3 == 2){
318 // centerlist[i].sub(cubeorigin(i);
319 for (Point p: c.points) {
320 PVector pv = new PVector(p.x, p.y, p.z);
321 colors[p.index] =color( constrain(4* pv.dist(centerlist.get(i)), 0, 360) , 50, 100 );
322 // colors[p.index] =color(constrain(centerlist[i].x, 0, 360), constrain(centerlist[i].y, 0, 100), );
323
324
325 }
326
327
328 //}
329
330 }
331 }
332 }
333
334 class HueTestHSB extends SCPattern{
335 BasicParameter HueT = new BasicParameter("Hue", .5);
336 BasicParameter SatT = new BasicParameter("Sat", .5);
337 BasicParameter BriT = new BasicParameter("Bright", .5);
338
339 HueTestHSB(GLucose glucose) {
340 super(glucose);
341 addParameter(HueT);
342 addParameter(SatT);
343 addParameter(BriT);
344 }
345 void run(double deltaMs){
346
347 for (Point p : model.points) {
348 color c = 0;
349 c = blendColor(c, lx.hsb(360*HueT.getValuef(), 100*SatT.getValuef(), 100*BriT.getValuef()), ADD);
350 colors[p.index]= c;
351 }
352 int now= millis();
353 if (now % 1000 <= 20)
354 {
355 println("Hue: " + 360*HueT.getValuef() + "Sat: " + 100*SatT.getValuef() + "Bright: " + 100*BriT.getValuef());
356 }
357 }
358
359 }