123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- class DISTORTER extends Shader {
-
- PImage buffer;
-
- int[][] distort = new int[2][512];
- final static float tick = 1.0/512.0;
- final int[] blends = {
- ADD, SUBTRACT, DARKEST, LIGHTEST, DIFFERENCE, EXCLUSION, MULTIPLY, SCREEN, OVERLAY, HARD_LIGHT, SOFT_LIGHT, DODGE, BURN
- };
- // ALL Channels, Nxxx stand for negative (255-value)
- // channels to work with
- final static int RED = 0;
- final static int GREEN = 1;
- final static int BLUE = 2;
- final static int HUE = 3;
- final static int SATURATION = 4;
- final static int BRIGHTNESS = 5;
- final static int NRED = 6;
- final static int NGREEN = 7;
- final static int NBLUE = 8;
- final static int NHUE = 9;
- final static int NSATURATION = 10;
- final static int NBRIGHTNESS = 11;
-
- DISTORTER() {
- name = "fxDistorter";
- /*
- //params.add(new Param ("shifthue", 1)); //boolean shift_hue
- params.add(new Param("hueshift", 0, 1)); //float shift_amt
- params.add(new Param("scalex", 0.01, 1)); //float scalex
- params.add(new Param("scaley", 0.01, 1)); //float scaley
- //params.add(new Param ("blend", 1)); //boolean do_blend
- params.add(new Param ("blendmode", blends.length)); //int blend_mode
- params.add(new Param("channel", 12));*/
-
- buffer = createImage(renderer.width, renderer.height, ARGB);
-
- // prepare distortion pattern
- for (int i=0; i<512; i++) {
- distort[0][i] = (int)random(-128, 128);
- distort[1][i] = (int)random(-128, 128);
- }
- }
-
- float inc1, inc2;
- void animate() {
- inc1+=.03;
- inc2+=.04;
- params.get(1).setValue((sin(inc1)+1)/2);
- params.get(2).setValue((sin(inc2)+1)/2);
- if (random(1) > .99) {
- for (int i = 0; i < params.size(); i++)
- params.get(i).randomize();
- }
- }
-
- void apply() {
- buffer = renderer.get();
- buffer.resize(renderer.width, renderer.height);
-
- float neww = map(params.get(1).value, 0, 1, 1, buffer.width/4);
- float newh = map(params.get(2).value, 0, 1, 1, buffer.height/4);
-
- float totalnum = neww+newh;
- float times = (totalnum/floor(totalnum/neww));
- float offx = (totalnum%neww)/times;
- float ratiox = neww/buffer.width;
-
- renderer.beginDraw();
- renderer.noStroke();
-
- for (int y=0; y<buffer.height; y++) {
- float yy = y/(float)buffer.height;
- for (int x=0; x<buffer.width; x++) {
- float xx = x/(float)buffer.width;
-
- float offy = floor(newh*yy);
- float fx = xx*ratiox+offx*offy;
-
- float shift = fx%1.0;
- float st = shift/tick;
- int no1 = floor(st)%512;
- int no2 = ceil(st)%512;
- float l = st-(float)no1;
-
- float cx = lerp(distort[0][no1], distort[0][no2], l);
- float cy = lerp(distort[1][no1], distort[1][no2], l);
-
- float rx =getChannel(buffer.get(x, y), (int)params.get(4).value);
- int sx = (int)((buffer.width+x+cx*rx*params.get(1).value*0.1)%buffer.width);
- int sy = (int)((buffer.height+y+cy*params.get(0).value)%buffer.height);
-
- color c=buffer.get(sx, sy);
-
- //if (boolean((int)(params.get(0).value))) {
- colorMode(HSB, 255);
- c = color((hue(c)+params.get(0).value*255*noise(newh+y))%255.0, constrain(saturation(c)*1.2, 0, 255), constrain(brightness(c), 0, 255));
- colorMode(RGB, 255);
- //}
- //renderer.fill(lerpColor(c,buffer.get(x,y),0.2));
- renderer.fill(c);
- renderer.rect(x, y, 1, 1);
- }
- }
-
- //if (boolean((int)(params.get(4).value)))
- renderer.blend(buffer, 0, 0, buffer.width, buffer.height, 0, 0, renderer.width, renderer.height, (int)params.get(3).value);
-
- colorMode(RGB);
- renderer.endDraw();
- }
-
-
-
- float getChannel(color c, int channel) {
- int ch = channel>5?channel-6:channel;
- float cc;
-
- switch(ch) {
- case RED:
- cc = red(c);
- break;
- case GREEN:
- cc = green(c);
- break;
- case BLUE:
- cc = blue(c);
- break;
- case HUE:
- cc = hue(c);
- break;
- case SATURATION:
- cc = saturation(c);
- break;
- default:
- cc= brightness(c);
- break;
- }
-
- return channel>5?255-cc:cc;
- }
- }
|