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