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