123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209 |
- class DRAWSTROKES extends Shader {
-
- int stat_type = ABSDIST2; // type of diff calculation: fast: ABSDIST, DIST, slow: HUE, SATURATION, BRIGHTNESS
- int stroke_len = 3; // length of the stroke, values: 1 and above
- int angles_no = 30; // number of directions stroke can be drew, 2 and above
- int segments = 500; // number of segments of single thread
- float stroke_width = 1; // width of the stroke, 0.5 - 3
- int stroke_alpha = 100; // alpha channel of the stroke: 30 - 200
-
- color background_color = color(255, 255, 255); // RGB
-
- boolean interactive = false;
- int max_display_size = 800; // viewing window size (regardless image size)
-
- int len;
- // working buffer
- PGraphics buffer;
-
- int currx, curry;
- int[] sintab, costab;
- int sqwidth;
-
- int calcDiff(PImage img1, PImage img2) {
- int err = 0;
- for (int i=0; i<img1.pixels.length; i++)
- err += getStat(img1.pixels[i], img2.pixels[i]);
- return err;
- }
-
- DRAWSTROKES() {
- name = "fxDrawStrokes";
- /*
- params.add(new Param ("stat_type", 6)); //0 bis 5, 4 und 5 sind mit abstand am schnellsten
- params.add(new Param ("stroke_len", 1, 15));
- params.add(new Param ("angles_no", 2, 50));
- params.add(new Param ("segments", 50, 1500));
- params.add(new Param ("stroke_width", 0.5, 3));
- params.add(new Param ("stroke_alpha", 50, 200));
- */
- params.get(0).randomize();
- params.get(1).randomize();
- params.get(2).randomize();
- params.get(3).randomize();
- params.get(4).randomize();
- params.get(5).randomize();
-
-
-
-
- len = (renderer.width<renderer.height?renderer.width:renderer.height)/3;
-
- buffer = createGraphics(renderer.width, renderer.height);
- //buffer.smooth(8);
- buffer.beginDraw();
- buffer.background(255);
- buffer.noFill();
- buffer.endDraw();
-
- rw = renderer.width;
-
-
- reinit();
- }
-
- void reinit() {
- buffer.beginDraw();
- buffer.strokeWeight(stroke_width);
- //buffer.background(background_color); //ENABLE THIS TO DRAW FROM BLANK
- buffer.endDraw();
-
- currx = (int)random(renderer.width);
- curry = (int)random(renderer.height);
-
- sintab = new int[angles_no];
- costab = new int[angles_no];
-
- for (int i=0; i<angles_no; i++) {
- sintab[i] = (int)(stroke_len * sin(TWO_PI*i/(float)angles_no));
- costab[i] = (int)(stroke_len * cos(TWO_PI*i/(float)angles_no));
- }
-
- sqwidth = stroke_len * 2 + 4;
- }
-
- void animate() {
- if (random(1) > .99) {
- for (int i = 0; i < params.size(); i++)
- params.get(i).randomize();
- stat_type = (int)params.get(0).value;
- stroke_len = (int)params.get(1).value;
- angles_no = (int)params.get(2).value;
- segments = (int)params.get(3).value;
- stroke_width = params.get(4).value;
- stroke_alpha = (int)params.get(5).value;
- reinit();
- }
- }
-
- int rw;
- void apply() {
- if (rw != renderer.width) {
- rw = renderer.width;
- PGraphics save = createGraphics(renderer.width, renderer.height);
- save.beginDraw();
- save.image(buffer, 0, 0, save.width, save.height);
- save.endDraw();
- buffer.setSize(renderer.width, renderer.height);
- buffer.beginDraw();
- buffer.image(save, 0, 0, buffer.width, buffer.height);
- buffer.endDraw();
- }
- currx = (int)random(renderer.width);
- curry = (int)random(renderer.height);
-
- buffer.beginDraw();
- //draw whole segment using current color
- buffer.stroke(renderer.get(currx, curry), stroke_alpha);
-
- for (int iter=0; iter<segments; iter++) {
- // corners of square containing new strokes
- int corx = currx-stroke_len-2;
- int cory = curry-stroke_len-2;
-
- // take square from image and current screen
- PImage imgpart = renderer.get(corx, cory, sqwidth, sqwidth);
- PImage mypart = buffer.get(corx, cory, sqwidth, sqwidth);
- imgpart.loadPixels();
- mypart.loadPixels();
-
- // calc current diff
- float localerr = calcDiff(imgpart, mypart);
-
- // chosen stroke will be here
- PImage destpart = null;
- int _nx=currx, _ny=curry;
-
- // start with random angle
- int i = (int)random(angles_no);
- int iterangles = angles_no;
-
- while (iterangles-- > 0) {
- // take end points
- int nx = currx + costab[i];
- int ny = curry + sintab[i];
-
- // if not out of the screen
- if (nx>=0 && nx<renderer.width-1 && ny>=0 && ny<renderer.height-1) {
- // clean region and draw line
- buffer.image(mypart, corx, cory);
- buffer.line(currx, curry, nx, ny);
-
- // take region with line and calc diff
- PImage curr = buffer.get(corx, cory, sqwidth, sqwidth);
- curr.loadPixels();
- int currerr = calcDiff(imgpart, curr);
-
- // if better, remember this region and line endpoint
- if (currerr < localerr) {
- destpart = curr;
- _nx = nx;
- _ny = ny;
- localerr = currerr;
- }
- }
-
- // next angle
- i = (i+1)%angles_no;
- }
-
- // if we have new stroke, draw it
- if (destpart != null) {
- buffer.image(destpart, corx, cory);
- currx = _nx;
- curry = _ny;
- } else {
- break; // skip
- }
- }
-
- buffer.endDraw();
- renderer.beginDraw();
- renderer.image(buffer, 0, 0, renderer.width, renderer.height);
- renderer.endDraw();
- }
-
- final static int DIST = 0;
- final static int HUE = 1;
- final static int BRIGHTNESS = 2;
- final static int SATURATION = 3;
- final static int ABSDIST = 4;
- final static int ABSDIST2 = 5;
-
- final float getStat(color c1, color c2) {
- switch(stat_type) {
- case HUE:
- abs(hue(c1)-hue(c2));
- case BRIGHTNESS:
- abs(brightness(c1)-brightness(c2));
- case SATURATION:
- abs(saturation(c1)-saturation(c2));
- case ABSDIST:
- return abs(red(c1)-red(c2))+abs(green(c1)-green(c2))+abs(blue(c1)-blue(c2));
- case ABSDIST2:
- return abs( (red(c1)+blue(c1)+green(c1)) - (red(c2)+blue(c2)+green(c2)) );
- default:
- return sq(red(c1)-red(c2)) + sq(green(c1)-green(c2)) + sq(blue(c1)-blue(c2));
- }
- }
- }
|