Commit | Line | Data |
---|---|---|
a41f334c MS |
1 | //---------------------------------------------------------------------------------------------------------------------------------- |
2 | public class Pong extends DPat { | |
e5308763 | 3 | SinLFO x,y,z,dx,dy,dz; |
abef24f4 | 4 | float cRad; BasicParameter pSize; |
a41f334c | 5 | Pick pChoose; |
5096de16 | 6 | PVector v = new PVector(), vMir = new PVector(); |
a41f334c MS |
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 ); | |
5096de16 | 18 | pChoose = addPick ("Animiation" , 2, 2, new String[] {"Pong", "Ball", "Cone"} ); |
a41f334c MS |
19 | } |
20 | ||
abef24f4 | 21 | void StartRun(double deltaMs) { cRad = mMax.x*val(pSize)/6; } |
5096de16 | 22 | color CalcPoint(PVector p) { |
a41f334c MS |
23 | v.set(x.getValuef(), y.getValuef(), z.getValuef()); |
24 | v.z=0;p.z=0;// ignore z dimension | |
25 | switch(pChoose.Cur()) { | |
5096de16 | 26 | case 0: vMir.set(mMax); vMir.sub(p); |
27 | return lx.hsb(lxh(),100,c1c(1 - min(v.dist(p), v.dist(vMir))*.5/cRad)); // balls | |
28 | case 1: return lx.hsb(lxh(),100,c1c(1 - v.dist(p)*.5/cRad)); // ball | |
a41f334c | 29 | case 2: vMir.set(mMax.x/2,0,mMax.z/2); |
5096de16 | 30 | return lx.hsb(lxh(),100,c1c(1 - calcCone(p,v,vMir) * max(.02,.45-val(pSize)))); // spot |
a41f334c | 31 | } |
fb83f322 | 32 | return lx.hsb(0,0,0); |
a41f334c MS |
33 | } |
34 | } | |
35 | //---------------------------------------------------------------------------------------------------------------------------------- | |
36 | public class NDat { | |
e5308763 | 37 | float xz, yz, zz, hue, speed, angle, den; |
a41f334c MS |
38 | float xoff,yoff,zoff; |
39 | float sinAngle, cosAngle; | |
40 | boolean isActive; | |
41 | NDat () { isActive=false; } | |
42 | boolean Active() { return isActive; } | |
e5308763 | 43 | void set (float _hue, float _xz, float _yz, float _zz, float _den, float _speed, float _angle) { |
a41f334c | 44 | isActive = true; |
e5308763 | 45 | hue=_hue; xz=_xz; yz=_yz; zz =_zz; den=_den; speed=_speed; angle=_angle; |
a41f334c MS |
46 | xoff = random(100e3); yoff = random(100e3); zoff = random(100e3); |
47 | } | |
48 | } | |
49 | ||
50 | public class Noise extends DPat | |
51 | { | |
abef24f4 | 52 | int CurAnim, iSymm; |
53 | int XSym=1,YSym=2,RadSym=3; | |
54 | float zTime , zTheta=0, zSin, zCos, rtime, ttime; | |
55 | BasicParameter pSpeed , pDensity, pSharp; | |
56 | Pick pChoose, pSymm; | |
57 | int _ND = 4; | |
58 | NDat N[] = new NDat[_ND]; | |
a41f334c MS |
59 | |
60 | Noise(GLucose glucose) { | |
61 | super(glucose); | |
62 | pSpeed = addParam("Fast" , .55); | |
63 | pDensity = addParam("Dens" , .5); | |
e5308763 | 64 | pSharp = addParam("Shrp" , 0); |
a41f334c MS |
65 | pSymm = addPick("Symmetry" , 0, 3, new String[] {"None", "X", "Y", "Radial"} ); |
66 | pChoose = addPick("Animation", 6, 7, new String[] {"Drip", "Cloud", "Rain", "Fire", "Machine", "Spark","VWave", "Wave"} ); | |
67 | for (int i=0; i<_ND; i++) N[i] = new NDat(); | |
68 | } | |
69 | ||
596f331f | 70 | void onActive() { zTime = random(500); zTheta=0; rtime = 0; ttime = 0; } |
c5391295 | 71 | |
a41f334c | 72 | void StartRun(double deltaMs) { |
abef24f4 | 73 | zTime += deltaMs*(val(pSpeed)-.5)*.002 ; |
79499592 | 74 | zTheta += deltaMs*(spin()-.5)*.01 ; |
a41f334c MS |
75 | rtime += deltaMs; |
76 | iSymm = pSymm.Cur(); | |
a41f334c MS |
77 | zSin = sin(zTheta); |
78 | zCos = cos(zTheta); | |
79 | ||
80 | if (pChoose.Cur() != CurAnim) { | |
81 | CurAnim = pChoose.Cur(); ttime = rtime; | |
82 | pSpin .reset(); zTheta = 0; | |
83 | pDensity .reset(); pSpeed .reset(); | |
84 | for (int i=0; i<_ND; i++) { N[i].isActive = false; } | |
85 | ||
86 | switch(CurAnim) { | |
e5308763 | 87 | // hue xz yz zz den mph angle |
abef24f4 | 88 | case 0: N[0].set(0 ,75 ,75 ,150,45 ,3 ,0 ); pSharp.setValue(1 ); break; // drip |
89 | case 1: N[0].set(0 ,100,100,200,45 ,3 ,180); pSharp.setValue(0 ); break; // clouds | |
90 | case 2: N[0].set(0 ,2 ,400,2 ,20 ,3 ,0 ); pSharp.setValue(.5); break; // rain | |
e5308763 | 91 | case 3: N[0].set(40 ,100,100,200,10 ,1 ,180); |
abef24f4 | 92 | N[1].set(0 ,100,100,200,10 ,5 ,180); pSharp.setValue(0 ); break; // fire 1 |
e5308763 | 93 | case 4: N[0].set(0 ,40 ,40 ,40 ,15 ,2.5,180); |
94 | N[1].set(20 ,40 ,40 ,40 ,15 ,4 ,0 ); | |
95 | N[2].set(40 ,40 ,40 ,40 ,15 ,2 ,90 ); | |
abef24f4 | 96 | N[3].set(60 ,40 ,40 ,40 ,15 ,3 ,-90); pSharp.setValue(.5); break; // machine |
e5308763 | 97 | case 5: N[0].set(0 ,400,100,2 ,15 ,3 ,90 ); |
98 | N[1].set(20 ,400,100,2 ,15 ,2.5,0 ); | |
99 | N[2].set(40 ,100,100,2 ,15 ,2 ,180); | |
abef24f4 | 100 | N[3].set(60 ,100,100,2 ,15 ,1.5,270); pSharp.setValue(.5); break; // spark |
a41f334c | 101 | } |
a41f334c MS |
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 | ||
5096de16 | 110 | color CalcPoint(PVector p) { |
a41f334c | 111 | color c = 0; |
5096de16 | 112 | rotateZ(p, mCtr, zSin, zCos); |
e5308763 | 113 | |
a41f334c | 114 | if (CurAnim == 6 || CurAnim == 7) { |
5096de16 | 115 | setNorm(p); |
e5308763 | 116 | return lx.hsb(lxh(),100, 100 * ( |
5096de16 | 117 | constrain(1-50*(1-val(pDensity))*abs(p.y-sin(zTime*10 + p.x*(300))*.5 - .5),0,1) + |
118 | (CurAnim == 7 ? constrain(1-50*(1-val(pDensity))*abs(p.x-sin(zTime*10 + p.y*(300))*.5 - .5),0,1) : 0)) | |
a41f334c MS |
119 | ); |
120 | } | |
e5308763 | 121 | |
5096de16 | 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; | |
a41f334c MS |
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; | |
e5308763 | 129 | |
5096de16 | 130 | float b = (iSymm==RadSym ? noise(zTime*n.speed+n.xoff-p.dist(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)) | |
a41f334c MS |
132 | *1.8; |
133 | ||
abef24f4 | 134 | b += n.den/100 -.4 + val(pDensity) -1; |
e5308763 | 135 | c = blendColor(c,lx.hsb(lxh()+n.hue,100,c1c(b)),ADD); |
a41f334c MS |
136 | } |
137 | return c; | |
138 | } | |
139 | } | |
140 | //---------------------------------------------------------------------------------------------------------------------------------- | |
141 | public class Play extends DPat | |
142 | { | |
143 | public class rAngle { | |
144 | float prvA, dstA, c; | |
145 | float prvR, dstR, r; | |
146 | float _cos, _sin, x, y; | |
147 | float fixAngle (float a, float b) { return a<b ? | |
148 | (abs(a-b) > abs(a+2*PI-b) ? a : a+2*PI) : | |
149 | (abs(a-b) > abs(a-2*PI-b) ? a : a-2*PI) ; } | |
150 | float getX(float r) { return mCtr.x + _cos*r; } | |
151 | float getY(float r) { return mCtr.y + _sin*r; } | |
152 | void move() { c = interp(t,prvA,dstA); | |
153 | r = interp(t,prvR,dstR); | |
154 | _cos = cos(c); _sin = sin(c); | |
155 | x = getX(r); y = getY(r); } | |
156 | void set() { prvA = dstA; dstA = random(2*PI); prvA = fixAngle(prvA, dstA); | |
157 | prvR = dstR; dstR = random(mCtr.y); } | |
158 | } | |
159 | ||
abef24f4 | 160 | BasicParameter pAmp, pRadius, pBounce; |
d6b5635a | 161 | Pick pTimePattern, pTempoMult, pShape; |
a41f334c | 162 | |
a41f334c MS |
163 | ArrayList<rWave> waves = new ArrayList<rWave>(10); |
164 | ||
d6b5635a | 165 | int nBeats = 0; |
166 | float t,amp,rad,bnc,zTheta=0; | |
167 | ||
5096de16 | 168 | rAngle a1 = new rAngle(), a2 = new rAngle(), |
169 | a3 = new rAngle(), a4 = new rAngle(); | |
170 | PVector cPrev = new PVector(), cRand = new PVector(), | |
171 | cMid = new PVector(), V = new PVector(), | |
172 | theta = new PVector(), tSin = new PVector(), | |
173 | tCos = new PVector(), cMidNorm = new PVector(), | |
174 | Pn = new PVector(); | |
a41f334c | 175 | float LastBeat=3, LastMeasure=3; |
3ce73bc7 | 176 | int curRandTempo = 1, curRandTPat = 1; |
a41f334c MS |
177 | |
178 | Play(GLucose glucose) { | |
179 | super(glucose); | |
180 | pRadius = addParam("Rad" , .1 ); | |
181 | pBounce = addParam("Bnc" , .2 ); | |
182 | pAmp = addParam("Amp" , .2 ); | |
41f26e8f | 183 | pTempoMult = addPick ("TMult" , 5 , 5 , new String[] {"1x", "2x", "4x", "8x", "16x", "Rand" } ); |
184 | pTimePattern= addPick ("TPat" , 7 , 7 , new String[] {"Bounce", "Sin", "Roll", "Quant", "Accel", "Deccel", "Slide", "Rand"} ); | |
185 | pShape = addPick ("Shape" , 7 , 15 , new String[] {"Line", "Tap", "V", "RandV", | |
a41f334c MS |
186 | "Pyramid", "Wings", "W2", "Clock", |
187 | "Triangle", "Quad", "Sphere", "Cone", | |
188 | "Noise", "Wave", "?", "?"} ); | |
189 | } | |
190 | ||
191 | public class rWave { | |
192 | float v0, a0, x0, t,damp,a; | |
193 | boolean bDone=false; | |
194 | final float len=8; | |
195 | rWave(float _x0, float _a0, float _v0, float _damp) { x0=_x0*len; a0=_a0; v0=_v0; t=0; damp = _damp; } | |
196 | void move(double deltaMs) { | |
197 | t += deltaMs*.001; | |
198 | if (t>4) bDone=true; | |
199 | } | |
200 | float val(float _x) { | |
201 | _x*=len; | |
202 | float dist = t*v0 - abs(_x-x0); | |
203 | if (dist<0) { a=1; return 0; } | |
204 | a = a0*exp(-dist*damp) * exp(-abs(_x-x0)/(.2*len)); // * max(0,1-t/dur) | |
205 | return -a*sin(dist); | |
206 | } | |
207 | } | |
208 | ||
d6b5635a | 209 | void onReset() { zTheta=0; super.onReset(); } |
c5391295 | 210 | void onActive() { |
211 | zTheta=0; | |
212 | while (lx.tempo.bpm() > 40) lx.tempo.setBpm(lx.tempo.bpm()/2); | |
213 | } | |
935819e6 | 214 | |
215 | int KeyPressed = -1; | |
216 | boolean noteOn(Note note) { | |
217 | int row = note.getPitch(), col = note.getChannel(); | |
218 | if (row == 57) {KeyPressed = col; return true; } | |
219 | return super.noteOn(note); | |
220 | } | |
221 | ||
a41f334c MS |
222 | void StartRun(double deltaMs) { |
223 | t = lx.tempo.rampf(); | |
abef24f4 | 224 | amp = pAmp .getValuef(); |
225 | rad = pRadius .getValuef(); | |
226 | bnc = pBounce .getValuef(); | |
227 | zTheta += deltaMs*(val(pSpin)-.5)*.01; | |
a41f334c | 228 | |
3ce73bc7 | 229 | theta .set(val(pRotX)*PI*2, val(pRotY)*PI*2, val(pRotZ)*PI*2 + zTheta); |
230 | tSin .set(sin(theta.x), sin(theta.y), sin(theta.z)); | |
231 | tCos .set(cos(theta.x), cos(theta.y), cos(theta.z)); | |
a41f334c MS |
232 | |
233 | if (t<LastMeasure) { | |
3ce73bc7 | 234 | if (random(3) < 1) { curRandTempo = int(random(4)); if (curRandTempo == 3) curRandTempo = int(random(4)); } |
235 | if (random(3) < 1) { curRandTPat = pShape.Cur() > 6 ? 2+int(random(5)) : int(random(7)); } | |
a41f334c MS |
236 | } LastMeasure = t; |
237 | ||
3ce73bc7 | 238 | int nTempo = pTempoMult .Cur(); if (nTempo == 5) nTempo = curRandTempo; |
239 | int nTPat = pTimePattern.Cur(); if (nTPat == 7) nTPat = curRandTPat ; | |
a41f334c MS |
240 | |
241 | switch (nTempo) { | |
242 | case 0: t = t; break; | |
243 | case 1: t = (t*2. )%1.; break; | |
244 | case 2: t = (t*4. )%1.; break; | |
245 | case 3: t = (t*8. )%1.; break; | |
246 | case 4: t = (t*16.)%1.; break; | |
247 | } | |
248 | ||
249 | int i=0; while (i< waves.size()) { | |
250 | rWave w = waves.get(i); | |
251 | w.move(deltaMs); if (w.bDone) waves.remove(i); else i++; | |
252 | } | |
253 | ||
935819e6 | 254 | if ((t<LastBeat && pShape.Cur()!=14) || KeyPressed>-1) { |
a41f334c | 255 | waves.add(new rWave( |
935819e6 | 256 | KeyPressed>-1 ? map(KeyPressed,0,7,0,1) : random(1), // location |
a41f334c MS |
257 | bnc*10, // bounciness |
258 | 7, // velocity | |
259 | 2*(1-amp))); // dampiness | |
935819e6 | 260 | KeyPressed=-1; |
a41f334c MS |
261 | if (waves.size() > 5) waves.remove(0); |
262 | } | |
263 | ||
264 | if (t<LastBeat) { | |
5096de16 | 265 | cPrev.set(cRand); setRand(cRand); |
a41f334c MS |
266 | a1.set(); a2.set(); a3.set(); a4.set(); |
267 | } LastBeat = t; | |
268 | ||
269 | switch (nTPat) { | |
270 | case 0: t = sin(PI*t); break; // bounce | |
271 | case 1: t = norm(sin(2*PI*(t+PI/2)),-1,1); break; // sin | |
272 | case 2: t = t; break; // roll | |
273 | case 3: t = constrain(int(t*8)/7.,0,1); break; // quant | |
274 | case 4: t = t*t*t; break; // accel | |
275 | case 5: t = sin(PI*t*.5); break; // deccel | |
276 | case 6: t = .5*(1-cos(PI*t)); break; // slide | |
277 | } | |
278 | ||
5096de16 | 279 | cMid.set (cPrev); interpolate(t,cMid,cRand); |
280 | cMidNorm.set (cMid); setNorm(cMidNorm); | |
a41f334c MS |
281 | a1.move(); a2.move(); a3.move(); a4.move(); |
282 | } | |
283 | ||
5096de16 | 284 | color CalcPoint(PVector Px) { |
285 | if (theta.x != 0) rotateX(Px, mCtr, tSin.x, tCos.x); | |
286 | if (theta.y != 0) rotateY(Px, mCtr, tSin.y, tCos.y); | |
287 | if (theta.z != 0) rotateZ(Px, mCtr, tSin.z, tCos.z); | |
a41f334c | 288 | |
5096de16 | 289 | Pn.set(Px); setNorm(Pn); |
a41f334c MS |
290 | |
291 | float mp = min(Pn.x, Pn.z); | |
292 | float yt = map(t,0,1,.5-bnc/2,.5+bnc/2); | |
293 | float r,d; | |
294 | ||
295 | switch (pShape.Cur()) { | |
296 | case 0: V.set(Pn.x, yt , Pn.z); break; // bouncing line | |
297 | case 1: V.set(Pn.x, map(cos(PI*t * Pn.x),-1,1,0,1) , Pn.z); break; // top tap | |
298 | 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 | |
299 | case 3: V.set(Pn.x, Pn.x < cMidNorm.x ? map(Pn.x,0,cMidNorm.x, .5,yt) : | |
300 | map(Pn.x,cMidNorm.x,1, yt,.5), Pn.z); break; // Random V shape | |
301 | ||
302 | case 4: V.set(Pn.x, .5*(Pn.x < cMidNorm.x ? map(Pn.x,0,cMidNorm.x, .5,yt) : | |
303 | map(Pn.x,cMidNorm.x,1, yt,.5)) + | |
304 | .5*(Pn.z < cMidNorm.z ? map(Pn.z,0,cMidNorm.z, .5,yt) : | |
305 | map(Pn.z,cMidNorm.z,1, yt,.5)), Pn.z); break; // Random Pyramid shape | |
306 | ||
307 | case 5: V.set(Pn.x, bnc*map((Pn.x-.5)*(Pn.x-.5),0,.25,0,t-.5)+.5, Pn.z); break; // wings | |
308 | case 6: V.set(Pn.x, bnc*map((mp -.5)*(mp -.5),0,.25,0,t-.5)+.5, Pn.z); break; // wings | |
309 | ||
310 | case 7: d = min( | |
311 | distToSeg(Px.x, Px.y, a1.getX(70),a1.getY(70), mCtr.x, mCtr.y), | |
312 | distToSeg(Px.x, Px.y, a2.getX(40),a2.getY(40), mCtr.x, mCtr.y)); | |
313 | d = constrain(30*(rad*40-d),0,100); | |
e5308763 | 314 | return lx.hsb(lxh(),100, d); // clock |
a41f334c MS |
315 | |
316 | case 8: r = amp*200 * map(bnc,0,1,1,sin(PI*t)); | |
317 | d = min( | |
318 | distToSeg(Px.x, Px.y, a1.getX(r),a1.getY(r), a2.getX(r),a2.getY(r)), | |
319 | distToSeg(Px.x, Px.y, a2.getX(r),a2.getY(r), a3.getX(r),a3.getY(r)), | |
320 | distToSeg(Px.x, Px.y, a3.getX(r),a3.getY(r), a1.getX(r),a1.getY(r)) // triangle | |
321 | ); | |
322 | d = constrain(30*(rad*40-d),0,100); | |
e5308763 | 323 | return lx.hsb(lxh(),100, d); // clock |
a41f334c MS |
324 | |
325 | case 9: r = amp*200 * map(bnc,0,1,1,sin(PI*t)); | |
326 | d = min( | |
327 | distToSeg(Px.x, Px.y, a1.getX(r),a1.getY(r), a2.getX(r),a2.getY(r)), | |
328 | distToSeg(Px.x, Px.y, a2.getX(r),a2.getY(r), a3.getX(r),a3.getY(r)), | |
329 | distToSeg(Px.x, Px.y, a3.getX(r),a3.getY(r), a4.getX(r),a4.getY(r)), | |
330 | distToSeg(Px.x, Px.y, a4.getX(r),a4.getY(r), a1.getX(r),a1.getY(r)) // quad | |
331 | ); | |
332 | d = constrain(30*(rad*40-d),0,100); | |
e5308763 | 333 | return lx.hsb(lxh(),100, d); // clock |
a41f334c MS |
334 | |
335 | case 10: | |
336 | r = map(bnc,0,1,a1.r,amp*200*sin(PI*t)); | |
e5308763 | 337 | return lx.hsb(lxh(),100,c1c(.9+2*rad - dist(Px.x,Px.y,a1.getX(r),a1.getY(r))*.03) ); // sphere |
a41f334c MS |
338 | |
339 | case 11: | |
340 | Px.z=mCtr.z; cMid.z=mCtr.z; | |
5096de16 | 341 | return lx.hsb(lxh(),100,c1c(1 - calcCone(Px,cMid,mCtr) * 0.02 > .5?1:0)); // cone |
a41f334c | 342 | |
e5308763 | 343 | case 12: return lx.hsb(lxh() + noise(Pn.x,Pn.y,Pn.z + (NoiseMove+50000)/1000.)*200, |
a41f334c MS |
344 | 85,c1c(Pn.y < noise(Pn.x + NoiseMove/2000.,Pn.z)*(1+amp)-amp/2.-.1 ? 1 : 0)); // noise |
345 | ||
935819e6 | 346 | case 13: |
347 | case 14: float y=0; for (rWave w : waves) y += .5*w.val(Pn.x); // wave | |
a41f334c MS |
348 | V.set(Pn.x, .7+y, Pn.z); |
349 | break; | |
350 | ||
fb83f322 | 351 | default: return lx.hsb(0,0,0); |
a41f334c MS |
352 | } |
353 | ||
5096de16 | 354 | return lx.hsb(lxh(), 100, c1c(1 - V.dist(Pn)/rad)); |
a41f334c MS |
355 | } |
356 | } | |
357 | //---------------------------------------------------------------------------------------------------------------------------------- | |
d6b5635a | 358 | boolean dDebug = false; |
a41f334c | 359 | class dCursor { |
41f26e8f | 360 | dVertex vCur, vNext, vDest; |
361 | float destSpeed; | |
362 | int posStop, pos,posNext; // 0 - 65535 | |
a41f334c | 363 | color clr; |
0fbf977b | 364 | |
41f26e8f | 365 | dCursor() {} |
366 | ||
367 | boolean isDone () { return pos==posStop; } | |
368 | boolean atDest () { return vCur.s==vDest.s || | |
abef24f4 | 369 | xyDist(vCur.getPoint(0), vDest.getPoint(0)) < 12 || |
370 | xyDist(vCur.getPoint(0), vDest.getPoint(15))< 12;} | |
935819e6 | 371 | void setCur (dVertex _v, int _p) { p2=null; vCur=_v; pos=_p; pickNext(); } |
41f26e8f | 372 | void setCur (dPixel _p) { setCur(_p.v, _p.pos); } |
373 | void setNext (dVertex _v, int _p, int _s) { vNext = _v; posNext = _p<<12; posStop = _s<<12; } | |
374 | void setDest (dVertex _v, float _speed) { vDest = _v; destSpeed = _speed; } | |
935819e6 | 375 | void onDone () { setCur(vNext, posNext); pickNext(); } |
41f26e8f | 376 | |
377 | float minDist; | |
378 | int nTurns; | |
379 | boolean bRandEval; | |
380 | ||
935819e6 | 381 | void evaluate(dVertex v, int p, int s) { |
41f26e8f | 382 | if (v == null) return; ++nTurns; |
383 | if (bRandEval) { | |
384 | if (random(nTurns) < 1) setNext(v,p,s); return; } | |
385 | else { | |
abef24f4 | 386 | float d = xyDist(v.getPoint(15), vDest.getPoint(0)); |
41f26e8f | 387 | if (d < minDist) { minDist=d; setNext(v,p,s); } |
388 | if (d == minDist && random(2)<1) { minDist=d; setNext(v,p,s); } | |
389 | } | |
390 | } | |
a41f334c | 391 | |
935819e6 | 392 | void evalTurn(dTurn t) { |
41f26e8f | 393 | if (t == null || t.pos0<<12 <= pos) return; |
935819e6 | 394 | evaluate(t.v , t.pos1, t.pos0); |
395 | evaluate(t.v.opp, 16-t.pos1, t.pos0); | |
a41f334c MS |
396 | } |
397 | ||
935819e6 | 398 | void pickNext() { |
41f26e8f | 399 | bRandEval = random(.05+destSpeed) < .05; minDist=500; nTurns=0; |
935819e6 | 400 | evaluate(vCur.c0, 0, 16); evaluate(vCur.c1, 0, 16); |
abef24f4 | 401 | evaluate(vCur.c2, 0, 16); evaluate(vCur.c3, 0, 16); |
935819e6 | 402 | evalTurn(vCur.t0); evalTurn(vCur.t1); |
abef24f4 | 403 | evalTurn(vCur.t2); evalTurn(vCur.t3); |
a41f334c | 404 | } |
41f26e8f | 405 | |
2bb56822 | 406 | LXPoint p1, p2; int i2; |
41f26e8f | 407 | |
408 | int draw(int nAmount, SCPattern pat) { | |
409 | int nFrom = (pos ) >> 12; | |
410 | int nMv = min(nAmount, posStop-pos); | |
411 | int nTo = min(15,(pos+nMv) >> 12); | |
412 | dVertex v = vCur; | |
413 | ||
935819e6 | 414 | if (dDebug) { p1 = v.getPoint(nFrom); float d = (p2 == null ? 0 : pointDist(p1,p2)); if (d>5) { println("too wide! quitting: " + d); exit(); }} |
a5e5b14c | 415 | for (int i = nFrom; i <= nTo; i++) { pat.getColors()[v.ci + v.dir*i ] = clr; } |
41f26e8f | 416 | if (v.same != null) for (int i = nFrom; i <= nTo; i++) { pat.getColors()[v.same.ci + v.same.dir*i] = clr; } |
a5e5b14c | 417 | |
41f26e8f | 418 | if (dDebug) { p2 = v.getPoint(nTo); i2 = nTo; } |
419 | ||
420 | pos += nMv; return nAmount - nMv; | |
935819e6 | 421 | } |
a41f334c MS |
422 | } |
423 | ||
a41f334c MS |
424 | //---------------------------------------------------------------------------------------------------------------------------------- |
425 | class Worms extends SCPattern { | |
0fbf977b | 426 | float StripsPerSec = 10; |
41f26e8f | 427 | float TrailTime = 3000; |
428 | int numCursors = 50; | |
429 | ArrayList<dCursor> cur = new ArrayList<dCursor>(30); | |
430 | ||
431 | private GraphicEQ eq = null; | |
432 | ||
433 | private BasicParameter pBeat = new BasicParameter("BEAT", 0); | |
434 | private BasicParameter pSpeed = new BasicParameter("FAST", .2); | |
435 | private BasicParameter pBlur = new BasicParameter("BLUR", .3); | |
436 | private BasicParameter pWorms = new BasicParameter("WRMS", .3); | |
437 | private BasicParameter pConfusion = new BasicParameter("CONF", .1); | |
438 | private BasicParameter pEQ = new BasicParameter("EQ" , 0); | |
439 | private BasicParameter pSpawn = new BasicParameter("DIR" , 0); | |
e5308763 | 440 | private BasicParameter pColor = new BasicParameter("CLR" , .1); |
41f26e8f | 441 | |
5096de16 | 442 | float zMidLat = 82.; |
41f26e8f | 443 | float nConfusion; |
444 | private final Click moveChase = new Click(1000); | |
445 | ||
5096de16 | 446 | PVector middle; |
e5308763 | 447 | int AnimNum() { return floor(pSpawn.getValuef()*(4-.01)); } |
41f26e8f | 448 | float randX() { return random(model.xMax-model.xMin)+model.xMin; } |
449 | float randY() { return random(model.yMax-model.yMin)+model.yMin; } | |
5096de16 | 450 | PVector randEdge() { |
451 | return random(2) < 1 ? new PVector(random(2)<1 ? model.xMin:model.xMax, randY(), zMidLat) : | |
452 | new PVector(randX(), random(2)<1 ? model.yMin:model.yMax, zMidLat) ; | |
41f26e8f | 453 | } |
454 | ||
455 | Worms(GLucose glucose) { | |
456 | super(glucose); | |
457 | addModulator(moveChase).start(); | |
458 | addParameter(pBeat); addParameter(pSpeed); | |
459 | addParameter(pBlur); addParameter(pWorms); | |
460 | addParameter(pEQ); addParameter(pConfusion); | |
e5308763 | 461 | addParameter(pSpawn); addParameter(pColor); |
462 | ||
22cb1606 | 463 | middle = new PVector(1.5*model.cx, 1.5*model.cy, 71); |
41f26e8f | 464 | if (lattice == null) lattice = new dLattice(); |
465 | for (int i=0; i<numCursors; i++) { dCursor c = new dCursor(); reset(c); cur.add(c); } | |
466 | onParameterChanged(pEQ); setNewDest(); | |
467 | } | |
468 | ||
a5e5b14c | 469 | void onParameterChanged(LXParameter parameter) { |
79499592 | 470 | super.onParameterChanged(parameter); |
41f26e8f | 471 | nConfusion = 1-pConfusion.getValuef(); |
472 | for (int i=0; i<numCursors; i++) { | |
473 | if (parameter==pSpawn) reset(cur.get(i)); | |
474 | cur.get(i).destSpeed = nConfusion; | |
475 | } | |
476 | } | |
0fbf977b | 477 | |
e5308763 | 478 | float getClr() { return lx.getBaseHuef() + random(pColor.getValuef()*300); } |
41f26e8f | 479 | void reset(dCursor c) { |
480 | switch(AnimNum()) { | |
e5308763 | 481 | case 0: c.clr = lx.hsb(getClr(),100,100); // middle to edges |
41f26e8f | 482 | c.setDest(lattice.getClosest(randEdge()).v, nConfusion); |
483 | c.setCur (lattice.getClosest(middle)); | |
484 | break; | |
0fbf977b | 485 | |
e5308763 | 486 | case 1: c.clr = lx.hsb(getClr(),100,100); // top to bottom |
41f26e8f | 487 | float xLin = randX(); |
5096de16 | 488 | c.setDest(lattice.getClosest(new PVector(xLin, 0 , zMidLat)).v, nConfusion); |
489 | c.setCur (lattice.getClosest(new PVector(xLin, model.yMax, zMidLat))); | |
41f26e8f | 490 | break; |
0fbf977b | 491 | |
e5308763 | 492 | case 2: c.clr = lx.hsb(getClr(),100,100); break; // chase a point around |
a5e5b14c | 493 | |
494 | case 3: boolean bLeft = random(2)<1; | |
e5308763 | 495 | c.clr = lx.hsb(getClr()+random(120),100,100); // sideways |
a5e5b14c | 496 | float yLin = randX(); |
5096de16 | 497 | c.setDest(lattice.getClosest(new PVector(bLeft ? 0 : model.xMax,yLin,zMidLat)).v, nConfusion); |
498 | c.setCur (lattice.getClosest(new PVector(bLeft ? model.xMax : 0,yLin,zMidLat))); | |
a5e5b14c | 499 | break; |
41f26e8f | 500 | } |
a5e5b14c | 501 | if (pBlur.getValuef() == 1 && random(2)<1) c.clr = lx.hsb(0,0,0); |
a41f334c MS |
502 | } |
503 | ||
41f26e8f | 504 | void setNewDest() { |
505 | if (AnimNum() != 2) return; | |
5096de16 | 506 | PVector dest = new PVector(randX(), randY(), zMidLat); |
41f26e8f | 507 | for (int i=0; i<numCursors; i++) { |
508 | cur.get(i).setDest(lattice.getClosest(dest).v, nConfusion); | |
e5308763 | 509 | cur.get(i).clr = lx.hsb(getClr()+75,100,100); // chase a point around |
41f26e8f | 510 | } |
a41f334c MS |
511 | } |
512 | ||
41f26e8f | 513 | void run(double deltaMs) { |
514 | if (deltaMs > 100) return; | |
515 | if (moveChase.click()) setNewDest(); | |
516 | ||
517 | float fBass=0, fTreble=0; | |
a5e5b14c | 518 | if (pEQ.getValuef()>0) { // EQ |
41f26e8f | 519 | eq.run(deltaMs); |
520 | fBass = eq.getAverageLevel(0, 4); | |
521 | fTreble = eq.getAverageLevel(eq.numBands-7, 7); | |
522 | } | |
523 | ||
a5e5b14c | 524 | if (pBlur.getValuef() < 1) { // trails |
525 | for (int i=0,s=model.points.size(); i<s; i++) { | |
e5308763 | 526 | color c = colors[i]; float b = lx.b(c); |
527 | if (b>0) colors[i] = lx.hsb(lx.h(c), lx.s(c), constrain((float)(b-100*deltaMs/(pBlur.getValuef()*TrailTime)),0,100)); | |
a5e5b14c | 528 | } |
0fbf977b | 529 | } |
a41f334c | 530 | |
41f26e8f | 531 | int nWorms = floor(pWorms.getValuef() * numCursors * |
532 | map(pEQ.getValuef(),0,1,1,constrain(2*fTreble,0,1))); | |
533 | ||
534 | for (int i=0; i<nWorms; i++) { | |
535 | dCursor c = cur.get(i); | |
536 | int nLeft = floor((float)deltaMs*.001*StripsPerSec * 65536 * (5*pSpeed.getValuef())); | |
537 | nLeft *= (1 - lx.tempo.rampf()*pBeat.getValuef()); | |
538 | while(nLeft > 0) { | |
539 | nLeft = c.draw(nLeft,this); if (!c.isDone()) continue; | |
540 | c.onDone(); if (c.atDest()) reset(c); | |
541 | } | |
a41f334c MS |
542 | } |
543 | } | |
41f26e8f | 544 | |
545 | ||
546 | public void onActive() { if (eq == null) { | |
547 | eq = new GraphicEQ(lx, 16); eq.slope.setValue(0.6); | |
548 | eq.level.setValue(0.65); eq.range.setValue(0.35); | |
549 | eq.release.setValue(0.4); | |
550 | }} | |
a41f334c MS |
551 | } |
552 | //---------------------------------------------------------------------------------------------------------------------------------- |