compositor for 2d shader
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

DRAWSTROKES.pde 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. class DRAWSTROKES extends Shader {
  2. int stat_type = ABSDIST2; // type of diff calculation: fast: ABSDIST, DIST, slow: HUE, SATURATION, BRIGHTNESS
  3. int stroke_len = 3; // length of the stroke, values: 1 and above
  4. int angles_no = 30; // number of directions stroke can be drew, 2 and above
  5. int segments = 500; // number of segments of single thread
  6. float stroke_width = 1; // width of the stroke, 0.5 - 3
  7. int stroke_alpha = 100; // alpha channel of the stroke: 30 - 200
  8. color background_color = color(255, 255, 255); // RGB
  9. boolean interactive = false;
  10. int max_display_size = 800; // viewing window size (regardless image size)
  11. int len;
  12. // working buffer
  13. PGraphics buffer;
  14. int currx, curry;
  15. int[] sintab, costab;
  16. int sqwidth;
  17. int calcDiff(PImage img1, PImage img2) {
  18. int err = 0;
  19. for (int i=0; i<img1.pixels.length; i++)
  20. err += getStat(img1.pixels[i], img2.pixels[i]);
  21. return err;
  22. }
  23. DRAWSTROKES() {
  24. name = "fxDrawStrokes";
  25. /*
  26. params.add(new Param ("stat_type", 6)); //0 bis 5, 4 und 5 sind mit abstand am schnellsten
  27. params.add(new Param ("stroke_len", 1, 15));
  28. params.add(new Param ("angles_no", 2, 50));
  29. params.add(new Param ("segments", 50, 1500));
  30. params.add(new Param ("stroke_width", 0.5, 3));
  31. params.add(new Param ("stroke_alpha", 50, 200));
  32. */
  33. params.get(0).randomize();
  34. params.get(1).randomize();
  35. params.get(2).randomize();
  36. params.get(3).randomize();
  37. params.get(4).randomize();
  38. params.get(5).randomize();
  39. len = (renderer.width<renderer.height?renderer.width:renderer.height)/3;
  40. buffer = createGraphics(renderer.width, renderer.height);
  41. //buffer.smooth(8);
  42. buffer.beginDraw();
  43. buffer.background(255);
  44. buffer.noFill();
  45. buffer.endDraw();
  46. rw = renderer.width;
  47. reinit();
  48. }
  49. void reinit() {
  50. buffer.beginDraw();
  51. buffer.strokeWeight(stroke_width);
  52. //buffer.background(background_color); //ENABLE THIS TO DRAW FROM BLANK
  53. buffer.endDraw();
  54. currx = (int)random(renderer.width);
  55. curry = (int)random(renderer.height);
  56. sintab = new int[angles_no];
  57. costab = new int[angles_no];
  58. for (int i=0; i<angles_no; i++) {
  59. sintab[i] = (int)(stroke_len * sin(TWO_PI*i/(float)angles_no));
  60. costab[i] = (int)(stroke_len * cos(TWO_PI*i/(float)angles_no));
  61. }
  62. sqwidth = stroke_len * 2 + 4;
  63. }
  64. void animate() {
  65. if (random(1) > .99) {
  66. for (int i = 0; i < params.size(); i++)
  67. params.get(i).randomize();
  68. stat_type = (int)params.get(0).value;
  69. stroke_len = (int)params.get(1).value;
  70. angles_no = (int)params.get(2).value;
  71. segments = (int)params.get(3).value;
  72. stroke_width = params.get(4).value;
  73. stroke_alpha = (int)params.get(5).value;
  74. reinit();
  75. }
  76. }
  77. int rw;
  78. void apply() {
  79. if (rw != renderer.width) {
  80. rw = renderer.width;
  81. PGraphics save = createGraphics(renderer.width, renderer.height);
  82. save.beginDraw();
  83. save.image(buffer, 0, 0, save.width, save.height);
  84. save.endDraw();
  85. buffer.setSize(renderer.width, renderer.height);
  86. buffer.beginDraw();
  87. buffer.image(save, 0, 0, buffer.width, buffer.height);
  88. buffer.endDraw();
  89. }
  90. currx = (int)random(renderer.width);
  91. curry = (int)random(renderer.height);
  92. buffer.beginDraw();
  93. //draw whole segment using current color
  94. buffer.stroke(renderer.get(currx, curry), stroke_alpha);
  95. for (int iter=0; iter<segments; iter++) {
  96. // corners of square containing new strokes
  97. int corx = currx-stroke_len-2;
  98. int cory = curry-stroke_len-2;
  99. // take square from image and current screen
  100. PImage imgpart = renderer.get(corx, cory, sqwidth, sqwidth);
  101. PImage mypart = buffer.get(corx, cory, sqwidth, sqwidth);
  102. imgpart.loadPixels();
  103. mypart.loadPixels();
  104. // calc current diff
  105. float localerr = calcDiff(imgpart, mypart);
  106. // chosen stroke will be here
  107. PImage destpart = null;
  108. int _nx=currx, _ny=curry;
  109. // start with random angle
  110. int i = (int)random(angles_no);
  111. int iterangles = angles_no;
  112. while (iterangles-- > 0) {
  113. // take end points
  114. int nx = currx + costab[i];
  115. int ny = curry + sintab[i];
  116. // if not out of the screen
  117. if (nx>=0 && nx<renderer.width-1 && ny>=0 && ny<renderer.height-1) {
  118. // clean region and draw line
  119. buffer.image(mypart, corx, cory);
  120. buffer.line(currx, curry, nx, ny);
  121. // take region with line and calc diff
  122. PImage curr = buffer.get(corx, cory, sqwidth, sqwidth);
  123. curr.loadPixels();
  124. int currerr = calcDiff(imgpart, curr);
  125. // if better, remember this region and line endpoint
  126. if (currerr < localerr) {
  127. destpart = curr;
  128. _nx = nx;
  129. _ny = ny;
  130. localerr = currerr;
  131. }
  132. }
  133. // next angle
  134. i = (i+1)%angles_no;
  135. }
  136. // if we have new stroke, draw it
  137. if (destpart != null) {
  138. buffer.image(destpart, corx, cory);
  139. currx = _nx;
  140. curry = _ny;
  141. } else {
  142. break; // skip
  143. }
  144. }
  145. buffer.endDraw();
  146. renderer.beginDraw();
  147. renderer.image(buffer, 0, 0, renderer.width, renderer.height);
  148. renderer.endDraw();
  149. }
  150. final static int DIST = 0;
  151. final static int HUE = 1;
  152. final static int BRIGHTNESS = 2;
  153. final static int SATURATION = 3;
  154. final static int ABSDIST = 4;
  155. final static int ABSDIST2 = 5;
  156. final float getStat(color c1, color c2) {
  157. switch(stat_type) {
  158. case HUE:
  159. abs(hue(c1)-hue(c2));
  160. case BRIGHTNESS:
  161. abs(brightness(c1)-brightness(c2));
  162. case SATURATION:
  163. abs(saturation(c1)-saturation(c2));
  164. case ABSDIST:
  165. return abs(red(c1)-red(c2))+abs(green(c1)-green(c2))+abs(blue(c1)-blue(c2));
  166. case ABSDIST2:
  167. return abs( (red(c1)+blue(c1)+green(c1)) - (red(c2)+blue(c2)+green(c2)) );
  168. default:
  169. return sq(red(c1)-red(c2)) + sq(green(c1)-green(c2)) + sq(blue(c1)-blue(c2));
  170. }
  171. }
  172. }