dpat updates
[SugarCubes.git] / DanHorwitz.pde
1 //----------------------------------------------------------------------------------------------------------------------------------
2 public class Pong extends DPat {
3 SinLFO x,y,z,dx,dy,dz;
4 float cRad; DParam pSize;
5 Pick pChoose;
6 xyz v = new xyz(), vMir = new xyz();
7
8 Pong(GLucose glucose) {
9 super(glucose);
10 cRad = mMax.x/10;
11 addModulator(dx = new SinLFO(6000, 500, 30000 )).trigger();
12 addModulator(dy = new SinLFO(3000, 500, 22472 )).trigger();
13 addModulator(dz = new SinLFO(1000, 500, 18420 )).trigger();
14 addModulator(x = new SinLFO(cRad, mMax.x - cRad, 0)).trigger(); x.modulateDurationBy(dx);
15 addModulator(y = new SinLFO(cRad, mMax.y - cRad, 0)).trigger(); y.modulateDurationBy(dy);
16 addModulator(z = new SinLFO(cRad, mMax.z - cRad, 0)).trigger(); z.modulateDurationBy(dz);
17 pSize = addParam ("Size" , 0.4 );
18 pChoose = addPick ("Animiation" , 0, 2, new String[] {"Pong", "Ball", "Cone"} );
19 }
20
21 void StartRun(double deltaMs) { cRad = mMax.x*pSize.Val()/6; }
22 color CalcPoint(xyz p) {
23 v.set(x.getValuef(), y.getValuef(), z.getValuef());
24 switch(pChoose.Cur()) {
25 case 0: vMir.set(mMax); vMir.subtract(p);
26 return color(0,0,c1c(1 - min(v.distance(p), v.distance(vMir))*.5/cRad)); // balls
27 case 1: return color(0,0,c1c(1 - v.distance(p)*.5/cRad)); // ball
28 case 2: vMir.set(mMax.x/2,0,mMax.z/2);
29 return color(0,0,c1c(1 - CalcCone(p,v,vMir) * max(.02,.45-pSize.Val()))); // spot
30 }
31 return color(0,0,0);
32 }
33 }
34 //----------------------------------------------------------------------------------------------------------------------------------
35 public class NDat {
36 float xz, yz, zz, hue, sat, speed, angle, den;
37 float xoff,yoff,zoff;
38 float sinAngle, cosAngle;
39 boolean isActive;
40 NDat () { isActive=false; }
41 boolean Active() { return isActive; }
42 void set (float _hue, float _sat, float _xz, float _yz, float _zz, float _den, float _speed, float _angle) {
43 isActive = true;
44 hue=_hue; sat=_sat; xz=_xz; yz=_yz; zz =_zz; den=_den; speed=_speed; angle=_angle;
45 xoff = random(100e3); yoff = random(100e3); zoff = random(100e3);
46 }
47 }
48
49 public class Noise extends DPat
50 {
51 int CurAnim, iSymm;
52 int XSym=1,YSym=2,RadSym=3;
53 float zTime , zTheta=0, zSin, zCos, rtime, ttime, transAdd;
54 DParam pSpeed , pDensity, pRotZ;
55 Pick pChoose, pSymm;
56 int _ND = 4;
57 NDat N[] = new NDat[_ND];
58
59 Noise(GLucose glucose) {
60 super(glucose);
61 pRotZ = addParam("RotZ" , .5 ); pSpeed = addParam("Fast", .55);
62 pDensity= addParam("Dens" , .5);
63 pSymm = addPick("Symmetry" , 0, 3, new String[] {"None", "X", "Y", "Radial"} );
64 pChoose = addPick("Animation", 6, 7, new String[] {"Drip", "Cloud", "Rain", "Fire", "Machine", "Spark","VWave", "Wave"} );
65 for (int i=0; i<_ND; i++) N[i] = new NDat();
66 }
67
68 void StartPattern() { zTime = random(500); zTheta=0; rtime = 0; ttime = 0; transAdd=0; }
69 void StartRun(double deltaMs) {
70 zTime += deltaMs*(pSpeed.Val()-.5)*.002 ;
71 zTheta += deltaMs*(pRotZ .Val()-.5)*.01 ;
72 rtime += deltaMs;
73 iSymm = pSymm.Cur();
74 transAdd = 1*(1 - constrain(rtime - ttime,0,1000)/1000);
75 zSin = sin(zTheta);
76 zCos = cos(zTheta);
77
78 if (pChoose.Cur() != CurAnim) {
79 CurAnim = pChoose.Cur(); ttime = rtime;
80 pRotZ .reset(); zTheta = 0;
81 pDensity .reset(); pSpeed .reset();
82 for (int i=0; i<_ND; i++) { N[i].isActive = false; }
83
84 switch(CurAnim) {
85 // hue sat xz yz zz den mph angle
86 case 0: N[0].set(0 ,0 ,75 ,75 ,150,45 ,3 ,0 ); pSharp.set(1 ); break; // drip
87 case 1: N[0].set(0 ,0 ,100,100,200,45 ,3 ,180); pSharp.set(0 ); break; // clouds
88 case 2: N[0].set(0 ,0 ,2 ,400,2 ,20 ,3 ,0 ); pSharp.set(.5); break; // rain
89 case 3: N[0].set(40 ,1 ,100,100,200,10 ,1 ,180);
90 N[1].set(0 ,1 ,100,100,200,10 ,5 ,180); pSharp.set(0 ); break; // fire 1
91 case 4: N[0].set(0 ,1 ,40 ,40 ,40 ,15 ,2.5,180);
92 N[1].set(20 ,1 ,40 ,40 ,40 ,15 ,4 ,0 );
93 N[2].set(40 ,1 ,40 ,40 ,40 ,15 ,2 ,90 );
94 N[3].set(60 ,1 ,40 ,40 ,40 ,15 ,3 ,-90); pSharp.set(.5); break; // machine
95 case 5: N[0].set(0 ,1 ,400,100,2 ,15 ,3 ,90 );
96 N[1].set(20 ,1 ,400,100,2 ,15 ,2.5,0 );
97 N[2].set(40 ,1 ,100,100,2 ,15 ,2 ,180);
98 N[3].set(60 ,1 ,100,100,2 ,15 ,1.5,270); pSharp.set(.5); break; // spark
99 }
100
101 DG.UpdateLights();
102 }
103
104 for (int i=0; i<_ND; i++) if (N[i].Active()) {
105 N[i].sinAngle = sin(radians(N[i].angle));
106 N[i].cosAngle = cos(radians(N[i].angle));
107 }
108 }
109
110 color CalcPoint(xyz P) {
111 color c = 0;
112 P.RotateZ(mCtr, zSin, zCos);
113
114 if (CurAnim == 6 || CurAnim == 7) {
115 P.setNorm();
116 return color(0,0, 100 * (
117 constrain(1-50*(1-pDensity.Val())*abs(P.y-sin(zTime*10 + P.x*(300))*.5 - .5),0,1) +
118 (CurAnim == 7 ? constrain(1-50*(1-pDensity.Val())*abs(P.x-sin(zTime*10 + P.y*(300))*.5 - .5),0,1) : 0))
119 );
120 }
121
122 if (iSymm == XSym && P.x > mMax.x/2) P.x = mMax.x-P.x;
123 if (iSymm == YSym && P.y > mMax.y/2) P.y = mMax.y-P.y;
124
125 for (int i=0;i<_ND; i++) if (N[i].Active()) {
126 NDat n = N[i];
127 float zx = zTime * n.speed * n.sinAngle,
128 zy = zTime * n.speed * n.cosAngle;
129
130 float b = (iSymm==RadSym ? noise(zTime*n.speed+n.xoff-Dist(P,mCtr)/n.xz)
131 : noise(P.x/n.xz+zx+n.xoff,P.y/n.yz+zy+n.yoff,P.z/n.zz+n.zoff))
132 *1.8;
133
134 b += n.den/100 -.4 + pDensity.Val() -1;
135 b += transAdd;
136 c = blendColor(c,color(n.hue,100*n.sat,c1c(b)),ADD);
137 }
138 return c;
139 }
140 }
141 //----------------------------------------------------------------------------------------------------------------------------------
142 public class Play extends DPat
143 {
144 public class rAngle {
145 float prvA, dstA, c;
146 float prvR, dstR, r;
147 float _cos, _sin, x, y;
148 float fixAngle (float a, float b) { return a<b ?
149 (abs(a-b) > abs(a+2*PI-b) ? a : a+2*PI) :
150 (abs(a-b) > abs(a-2*PI-b) ? a : a-2*PI) ; }
151 float getX(float r) { return mCtr.x + _cos*r; }
152 float getY(float r) { return mCtr.y + _sin*r; }
153 void move() { c = interp(t,prvA,dstA);
154 r = interp(t,prvR,dstR);
155 _cos = cos(c); _sin = sin(c);
156 x = getX(r); y = getY(r); }
157 void set() { prvA = dstA; dstA = random(2*PI); prvA = fixAngle(prvA, dstA);
158 prvR = dstR; dstR = random(mCtr.y); }
159 }
160
161 int nBeats = 0;
162 DParam pAmp, pRadius, pBounce;
163 DParam pRotX, pRotY, pRotZ;
164
165 float t,amp,rad,bnc;
166
167 ArrayList<rWave> waves = new ArrayList<rWave>(10);
168
169 rAngle a1 = new rAngle(), a2 = new rAngle(),
170 a3 = new rAngle(), a4 = new rAngle();
171 xyz cPrev = new xyz(), cRand = new xyz(),
172 cMid = new xyz(), V = new xyz(),
173 Theta = new xyz(), TSin = new xyz(),
174 TCos = new xyz(), cMidNorm = new xyz(),
175 Pn = new xyz();
176 float LastBeat=3, LastMeasure=3;
177 int CurRandTempo = 1, CurRandTPat = 1;
178
179
180 Pick pTimePattern, pTempoMult, pShape, pForm;
181 int RandCube;
182
183 Play(GLucose glucose) {
184 super(glucose);
185 pRotX = addParam("RotX", .5);
186 pRotY = addParam("RotY", .5);
187 pRotZ = addParam("RotZ", .5);
188 pRadius = addParam("Rad" , .1 );
189 pBounce = addParam("Bnc" , .2 );
190 pAmp = addParam("Amp" , .2 );
191 pTempoMult = addPick ("TMult" , 0 , 5 , new String[] {"1x", "2x", "4x", "8x", "16x", "Rand" } );
192 pTimePattern= addPick ("TPat" , 6 , 7 , new String[] {"Bounce", "Sin", "Roll", "Quant", "Accel", "Deccel", "Slide", "Rand"} ); pShape = addPick ("Shape" , 2 , 15 , new String[] {"Line", "Tap", "V", "RandV",
193 "Pyramid", "Wings", "W2", "Clock",
194 "Triangle", "Quad", "Sphere", "Cone",
195 "Noise", "Wave", "?", "?"} );
196 pForm = addPick ("Form" , 2 , 2 , new String[] {"Bar", "Volume", "Fade"} );
197 }
198
199 public class rWave {
200 float v0, a0, x0, t,damp,a;
201 boolean bDone=false;
202 final float len=8;
203 rWave(float _x0, float _a0, float _v0, float _damp) { x0=_x0*len; a0=_a0; v0=_v0; t=0; damp = _damp; }
204 void move(double deltaMs) {
205 t += deltaMs*.001;
206 if (t>4) bDone=true;
207 }
208 float val(float _x) {
209 _x*=len;
210 float dist = t*v0 - abs(_x-x0);
211 if (dist<0) { a=1; return 0; }
212 a = a0*exp(-dist*damp) * exp(-abs(_x-x0)/(.2*len)); // * max(0,1-t/dur)
213 return -a*sin(dist);
214 }
215 }
216
217 void StartRun(double deltaMs) {
218 t = lx.tempo.rampf();
219 amp = pAmp.Val();
220 rad = pRadius.getValuef();
221 bnc = pBounce.getValuef();
222
223 Theta .set(pRotX.Val()*PI*2, pRotY.Val()*PI*2, pRotZ.Val()*PI*2);
224 TSin .set(sin(Theta.x), sin(Theta.y), sin(Theta.z));
225 TCos .set(cos(Theta.x), cos(Theta.y), cos(Theta.z));
226
227 if (t<LastMeasure) {
228 if (random(3) < 1) { CurRandTempo = int(random(4)); if (CurRandTempo == 3) CurRandTempo = int(random(4)); }
229 if (random(3) < 1) { CurRandTPat = pShape.Cur() > 6 ? 2+int(random(5)) : int(random(7)); }
230 } LastMeasure = t;
231
232 int nTempo = pTempoMult .Cur(); if (nTempo == 5) nTempo = CurRandTempo;
233 int nTPat = pTimePattern.Cur(); if (nTPat == 7) nTPat = CurRandTPat ;
234
235 switch (nTempo) {
236 case 0: t = t; break;
237 case 1: t = (t*2. )%1.; break;
238 case 2: t = (t*4. )%1.; break;
239 case 3: t = (t*8. )%1.; break;
240 case 4: t = (t*16.)%1.; break;
241 }
242
243 int i=0; while (i< waves.size()) {
244 rWave w = waves.get(i);
245 w.move(deltaMs); if (w.bDone) waves.remove(i); else i++;
246 }
247
248 if (t<LastBeat) {
249 cPrev.set(cRand); cRand.setRand();
250 a1.set(); a2.set(); a3.set(); a4.set();
251 waves.add(new rWave(
252 random(1), // location
253 bnc*10, // bounciness
254 7, // velocity
255 2*(1-amp))); // dampiness
256 if (waves.size() > 5) waves.remove(0);
257 } LastBeat = t;
258
259 switch (nTPat) {
260 case 0: t = sin(PI*t); break; // bounce
261 case 1: t = norm(sin(2*PI*(t+PI/2)),-1,1); break; // sin
262 case 2: t = t; break; // roll
263 case 3: t = constrain(int(t*8)/7.,0,1); break; // quant
264 case 4: t = t*t*t; break; // accel
265 case 5: t = sin(PI*t*.5); break; // deccel
266 case 6: t = .5*(1-cos(PI*t)); break; // slide
267 }
268
269 cMid.set (cPrev); cMid.interpolate (t,cRand);
270 cMidNorm.set (cMid); cMidNorm.setNorm();
271 a1.move(); a2.move(); a3.move(); a4.move();
272 }
273
274 color CalcPoint(xyz Px) {
275 if (Theta.x != 0) Px.RotateX(mCtr, TSin.x, TCos.x);
276 if (Theta.y != 0) Px.RotateY(mCtr, TSin.y, TCos.y);
277 if (Theta.z != 0) Px.RotateZ(mCtr, TSin.z, TCos.z);
278
279 Pn.set(Px); Pn.setNorm();
280
281 float mp = min(Pn.x, Pn.z);
282 float yt = map(t,0,1,.5-bnc/2,.5+bnc/2);
283 float r;
284
285 switch (pShape.Cur()) {
286 case 0: V.set(Pn.x, yt , Pn.z); break; // bouncing line
287 case 1: V.set(Pn.x, map(cos(PI*t * Pn.x),-1,1,0,1) , Pn.z); break; // top tap
288 case 2: V.set(Pn.x, bnc*map(Pn.x<.5?Pn.x:1-Pn.x,0,.5 ,0,t-.5)+.5, Pn.z); break; // V shape
289 case 3: V.set(Pn.x, Pn.x < cMidNorm.x ? map(Pn.x,0,cMidNorm.x, .5,yt) :
290 map(Pn.x,cMidNorm.x,1, yt,.5), Pn.z); break; // Random V shape
291
292 case 4: V.set(Pn.x, .5*(Pn.x < cMidNorm.x ? map(Pn.x,0,cMidNorm.x, .5,yt) :
293 map(Pn.x,cMidNorm.x,1, yt,.5)) +
294 .5*(Pn.z < cMidNorm.z ? map(Pn.z,0,cMidNorm.z, .5,yt) :
295 map(Pn.z,cMidNorm.z,1, yt,.5)), Pn.z); break; // Random Pyramid shape
296
297 case 5: V.set(Pn.x, bnc*map((Pn.x-.5)*(Pn.x-.5),0,.25,0,t-.5)+.5, Pn.z); break; // wings
298 case 6: V.set(Pn.x, bnc*map((mp -.5)*(mp -.5),0,.25,0,t-.5)+.5, Pn.z); break; // wings
299
300
301
302 case 7: return color(0,0, min(
303 distToSeg(Px.x, Px.y, a1.getX(70),a1.getY(70), mCtr.x, mCtr.y),
304 distToSeg(Px.x, Px.y, a2.getX(40),a2.getY(40), mCtr.x, mCtr.y)) <rad*40?100:0); // clock
305
306 case 8: r = amp*200 * map(bnc,0,1,1,sin(PI*t)); return color(0,0, min(
307 distToSeg(Px.x, Px.y, a1.getX(r),a1.getY(r), a2.getX(r),a2.getY(r)),
308 distToSeg(Px.x, Px.y, a2.getX(r),a2.getY(r), a3.getX(r),a3.getY(r)),
309 distToSeg(Px.x, Px.y, a3.getX(r),a3.getY(r), a1.getX(r),a1.getY(r)) // triangle
310 ) <rad*40?100:0);
311
312 case 9: r = amp*200 * map(bnc,0,1,1,sin(PI*t)); return color(0,0, min(
313 distToSeg(Px.x, Px.y, a1.getX(r),a1.getY(r), a2.getX(r),a2.getY(r)),
314 distToSeg(Px.x, Px.y, a2.getX(r),a2.getY(r), a3.getX(r),a3.getY(r)),
315 distToSeg(Px.x, Px.y, a3.getX(r),a3.getY(r), a4.getX(r),a4.getY(r)),
316 distToSeg(Px.x, Px.y, a4.getX(r),a4.getY(r), a1.getX(r),a1.getY(r)) // quad
317 ) <rad*40?100:0); // shape
318
319
320 case 10: r = map(bnc,0,1,a1.r,amp*200*sin(PI*t));
321 return color(0,0,c1c(.9+2*rad - dist(Px.x,Px.y,a1.getX(r),a1.getY(r))*.03) ); // sphere
322
323 case 11: return color(0,0,c1c(1 - CalcCone(Px,cMid,mCtr) * 0.02 > .5?1:0)); // cone
324
325 case 12: return color(100 + noise(Pn.x,Pn.y,Pn.z + (NoiseMove+50000)/1000.)*200,
326 85,c1c(Pn.y < noise(Pn.x + NoiseMove/2000.,Pn.z)*(1+amp)-amp/2.-.1 ? 1 : 0)); // noise
327
328 case 13: float y=0; for (rWave w : waves) y += .5*w.val(Pn.x);
329 V.set(Pn.x, .7+y, Pn.z);
330 break;
331
332 default: return color(0,0,0);
333 }
334
335 switch (pForm.Cur()) {
336 case 0: return color(0,0,c1c(1 - V.distance(Pn)/rad > .5?1:0));
337 case 1: return color(0,0,c1c(Pn.y < V.y ?1:0));
338 case 2: return color(0,0,c1c(1 - V.distance(Pn)/rad));
339
340 default: return color(0,0,c1c(Pn.y < V.y ?1:0));
341 }
342 }
343 }
344 //----------------------------------------------------------------------------------------------------------------------------------