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