Cleanup of various patterns, expanded colors
[SugarCubes.git] / TobySegaran.pde
CommitLineData
a673599f 1class GlitchPlasma extends SCPattern {
4ad0649d
TS
2 private int pos = 0;
3 private float satu = 100;
4 private float speed = 1;
64d16e07 5 private float glitch = 0;
4ad0649d
TS
6 BasicParameter saturationParameter = new BasicParameter("SATU", 1.0);
7 BasicParameter speedParameter = new BasicParameter("SPEED", 0.1);
64d16e07 8 BasicParameter glitchParameter = new BasicParameter("GLITCH", 0.0);
4ad0649d 9
a673599f 10 public GlitchPlasma(GLucose glucose) {
4ad0649d
TS
11 super(glucose);
12 addParameter(saturationParameter);
13 addParameter(speedParameter);
64d16e07 14 addParameter(glitchParameter);
4ad0649d
TS
15 }
16 public void onParameterChanged(LXParameter parameter) {
17 if (parameter == saturationParameter) {
18 satu = 100*parameter.getValuef();
19 } else if (parameter == speedParameter) {
a673599f 20 speed = 8*parameter.getValuef();
64d16e07
TS
21 } else if (parameter == glitchParameter) {
22 glitch = parameter.getValuef();
4ad0649d
TS
23 }
24 }
25
26 public void run(int deltaMs) {
27 for (Point p : model.points) {
28 float hv = sin(dist(p.fx + pos, p.fy, 128.0, 128.0) / 8.0)
29 + sin(dist(p.fx, p.fy, 64.0, 64.0) / 8.0)
30 + sin(dist(p.fx, p.fy + pos / 7, 192.0, 64.0) / 7.0)
31 + sin(dist(p.fx, p.fz + pos, 192.0, 100.0) / 8.0);
32 float bv = 100;
821ceae9 33 colors[p.index] = color((hv+2)*50, satu, bv);
4ad0649d 34 }
a673599f
TS
35 if (random(1.0)<glitch/20) {
36 pos=pos-int(random(10,30));
64d16e07 37 }
4ad0649d 38 pos+=speed;
64d16e07 39 if (pos >= MAX_INT-1) pos=0;
4ad0649d
TS
40 }
41}
42
99c99d34 43// This is very much a work in progress. Trying to get a flame effect.
64d16e07 44class FireEffect extends SCPattern {
4ad0649d
TS
45 private float[][] intensity;
46 private float hotspot;
47 private float decay = 0.3;
48 private int xm;
49 private int ym;
50 BasicParameter decayParameter = new BasicParameter("DECAY", 0.3);
51
64d16e07 52 public FireEffect(GLucose glucose) {
4ad0649d
TS
53 super(glucose);
54 xm = int(model.xMax);
55 ym = int(model.yMax);
56
57 intensity = new float[xm][ym];
58 addParameter(decayParameter);
59 }
60 public void onParameterChanged(LXParameter parameter) {
61 if (parameter == decayParameter) {
62 decay = parameter.getValuef();
63 }
64 }
65 private color flameColor(float level) {
66 if (level<=0) return color(0,0,0);
67 float br=min(100,sqrt(level)*15);
68 return color(level/1.7,100,br);
69 }
70 public void run(int deltaMs) {
71 for (int x=10;x<xm-10;x++) {
72 if (x%50>45 || x%50<5) {
73 intensity[x][ym-1] = random(30,100);
74 } else {
75 intensity[x][ym-1] = random(0,50);
76 }
77 }
78 for (int x=1;x<xm-1;x++) {
79 for (int y=0;y<ym-1;y++) {
80 intensity[x][y] = (intensity[x-1][y+1]+intensity[x][y+1]+intensity[x+1][y+1])/3-decay;
81 }
82 }
83
84 for (Point p : model.points) {
64d16e07
TS
85 int x = max(0,(int(p.fx)+int(p.fz))%xm);
86 int y = constrain(ym-int(p.fy),0,ym-1);
4ad0649d
TS
87 colors[p.index] = flameColor(intensity[x][y]);
88 }
89 }
90}
64d16e07 91
b4aaf4e4
TS
92class StripBounce extends SCPattern {
93 private final int numOsc = 30;
94 SinLFO[] fX = new SinLFO[numOsc]; //new SinLFO(0, model.xMax, 5000);
95 SinLFO[] fY = new SinLFO[numOsc]; //new SinLFO(0, model.yMax, 4000);
96 SinLFO[] fZ = new SinLFO[numOsc]; //new SinLFO(0, model.yMax, 3000);
97 SinLFO[] sat = new SinLFO[numOsc];
98 float[] colorOffset = new float[numOsc];
99
100 public StripBounce(GLucose glucose) {
101 super(glucose);
102 for (int i=0;i<numOsc;i++) {
103 fX[i] = new SinLFO(0, model.xMax, random(2000,20000));
104 fY[i] = new SinLFO(0, model.yMax, random(2000,20000));
105 fZ[i] = new SinLFO(0, model.zMax, random(2000,20000));
106 sat[i] = new SinLFO(60, 100, random(2000,50000));
107 addModulator(fX[i]).trigger();
108 addModulator(fY[i]).trigger();
109 addModulator(fZ[i]).trigger();
821ceae9 110 colorOffset[i]=random(0,256);
b4aaf4e4
TS
111 }
112 }
113
114 public void run(int deltaMs) {
115 float[] bright = new float[model.points.size()];
116 for (Strip strip : model.strips) {
117 for (int i=0;i<numOsc;i++) {
118 float avgdist=0.0;
a673599f 119 avgdist = dist(strip.points.get(8).fx,strip.points.get(8).fy,strip.points.get(8).fz,fX[i].getValuef(),fY[i].getValuef(),fZ[i].getValuef());
b4aaf4e4 120 boolean on = avgdist<30;
821ceae9 121 float hv = (lx.getBaseHuef()+colorOffset[i])%360;
b4aaf4e4
TS
122 float br = max(0,100-avgdist*4);
123 for (Point p : strip.points) {
124 if (on && br>bright[p.index]) {
125 colors[p.index] = color(hv,sat[i].getValuef(),br);
126 bright[p.index] = br;
127 }
128 }
129 }
130 }
10692893
TS
131 }
132}
133
821ceae9 134class SoundRain extends SCPattern {
10692893
TS
135
136 private FFT fft = null;
137 private LinearEnvelope[] bandVals = null;
1b8ec15f 138 private float[] lightVals = null;
10692893 139 private int avgSize;
1b8ec15f
TS
140 SawLFO pos = new SawLFO(0, 9, 8000);
141
821ceae9 142 public SoundRain(GLucose glucose) {
10692893 143 super(glucose);
1b8ec15f 144 addModulator(pos).trigger();
10692893
TS
145 }
146
147 protected void onActive() {
148 if (this.fft == null) {
149 this.fft = new FFT(lx.audioInput().bufferSize(), lx.audioInput().sampleRate());
150 this.fft.window(FFT.HAMMING);
151 this.fft.logAverages(40, 1);
152 this.avgSize = this.fft.avgSize();
153 this.bandVals = new LinearEnvelope[this.avgSize];
154 for (int i = 0; i < this.bandVals.length; ++i) {
155 this.addModulator(this.bandVals[i] = (new LinearEnvelope(0, 0, 700+i*4))).trigger();
b4aaf4e4 156 }
1b8ec15f 157 lightVals = new float[avgSize];
b4aaf4e4
TS
158 }
159 }
10692893
TS
160
161 public void run(int deltaMs) {
162 this.fft.forward(this.lx.audioInput().mix);
10692893
TS
163 for (int i = 0; i < avgSize; ++i) {
164 float value = this.fft.getAvg(i);
165 this.bandVals[i].setEndVal(value,40).trigger();
1b8ec15f 166 float lv = min(value*25,100);
821ceae9
TS
167 if (lv>lightVals[i]) {
168 lightVals[i]=min(lightVals[i]+10,lv,100);
1b8ec15f 169 } else {
821ceae9 170 lightVals[i]=max(lv,lightVals[i]-5,0);
1b8ec15f 171 }
10692893 172 }
1b8ec15f
TS
173 for (int i=0; i<model.strips.size(); i++) {
174 //Cube c = model.cubes.get(i);
175 Strip c = model.strips.get(i);
821ceae9 176 //int seq=(i+int(pos.getValuef()))%avgSize;
1b8ec15f
TS
177 float mult = 100.0/avgSize;
178 for (Point p : c.points) {
821ceae9
TS
179 int seq = int(p.fy*avgSize/model.yMax+pos.getValuef())%avgSize;
180 seq=abs(seq-(avgSize/2));
1b8ec15f 181 //colors[p.index] = color((avgSize-seq)*mult+bandVals[seq].getValuef(),bandVals[seq].getValuef()*25,bandVals[seq].getValuef()*20+10 );
821ceae9 182 colors[p.index] = color(200,lightVals[seq],lightVals[seq]);
1b8ec15f 183 }
10692893
TS
184 }
185 }
b4aaf4e4 186}
821ceae9
TS
187
188class FaceSync extends SCPattern {
189 SinLFO xosc = new SinLFO(-10, 10, 3000);
190 SinLFO zosc = new SinLFO(-10, 10, 3000);
191
192 public FaceSync(GLucose glucose) {
193 super(glucose);
194 addModulator(xosc).trigger();
195 addModulator(zosc).trigger();
196 zosc.setValue(0);
197 }
198
199 public void run(int deltaMs) {
200 int i=0;
201 for (Cube c : model.cubes) {
202 i++;
203 for (Point p : c.points) {
204 float dx, dz;
205 if (i%2==0) {
206 dx = p.fx - (c.cx+xosc.getValuef());
207 dz = p.fz - (c.cz+zosc.getValuef());
208 } else {
209 dx = p.fx - (c.cx+zosc.getValuef());
210 dz = p.fz - (c.cz+xosc.getValuef());
211 }
212 //println(dx);
213 colors[p.index] = color(100,0,100-abs(dx*5)-abs(dz*5));
214 }
215 }
216 }
217}