compositor for 2d glitch effects
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

effects.pde 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. /*
  2. ASDFPIXELSORT
  3. */
  4. class ASDFPIXELSORT extends Shader {
  5. ASDFPIXELSORT() {
  6. name = "fxASDFPixelSort";
  7. params.add(new Param("black", INTVAL, -17000000, -2000000, new int[]{SINE, SAWTOOTH, RAMPUPDOWN, TAN, TANINVERSE, TRIANG}));
  8. params.add(new Param("target", INTVAL, 0, 2, new int[]{RANDOM}));
  9. }
  10. int previousMode;
  11. void apply() {
  12. if (previousMode != int(params.get(1).value)) {
  13. if (params.get(1).value == 0) changeParam(0, new Param("black", INTVAL, -17000000, -2000000, new int[]{SINE, SAWTOOTH, RAMPUPDOWN, TAN, TANINVERSE, TRIANG}));
  14. if (params.get(1).value == 1) changeParam(0, new Param("brightness", INTVAL, 0, 200, new int[]{SINE, SAWTOOTH, RAMPUPDOWN, TAN, TANINVERSE, TRIANG}));
  15. if (params.get(1).value == 2) changeParam(0, new Param("white", INTVAL, -15000000, -700000, new int[]{SINE, SAWTOOTH, RAMPUPDOWN, TAN, TANINVERSE, TRIANG}));
  16. }
  17. previousMode = int(params.get(1).value);
  18. row = 0;
  19. column = 0;
  20. renderer.beginDraw();
  21. renderer.colorMode(RGB);
  22. colorMode(RGB);
  23. while (column < renderer.width-1) {
  24. renderer.loadPixels();
  25. sortColumn();
  26. column++;
  27. renderer.updatePixels();
  28. }
  29. while (row < renderer.height-1) {
  30. renderer.loadPixels();
  31. sortRow();
  32. row++;
  33. renderer.updatePixels();
  34. }
  35. renderer.endDraw();
  36. }
  37. int row = 0;
  38. int column = 0;
  39. void sortRow() {
  40. int x = 0;
  41. int y = row;
  42. int xend = 0;
  43. while (xend < renderer.width-1) {
  44. switch((int)params.get(1).value) {
  45. case 0:
  46. x = getFirstNotBlackX(x, y);
  47. xend = getNextBlackX(x, y);
  48. break;
  49. case 1:
  50. x = getFirstBrightX(x, y);
  51. xend = getNextDarkX(x, y);
  52. break;
  53. case 2:
  54. x = getFirstNotWhiteX(x, y);
  55. xend = getNextWhiteX(x, y);
  56. break;
  57. default:
  58. break;
  59. }
  60. if (x < 0) break;
  61. int sortLength = xend-x;
  62. color[] unsorted = new color[sortLength];
  63. color[] sorted = new color[sortLength];
  64. for (int i=0; i<sortLength; i++) {
  65. unsorted[i] = renderer.pixels[x + i + y * renderer.width];
  66. }
  67. sorted = sort(unsorted);
  68. for (int i=0; i<sortLength; i++) {
  69. renderer.pixels[x + i + y * renderer.width] = sorted[i];
  70. }
  71. x = xend+1;
  72. }
  73. }
  74. void sortColumn() {
  75. int x = column;
  76. int y = 0;
  77. int yend = 0;
  78. while (yend < renderer.height-1) {
  79. switch((int)params.get(1).value) {
  80. case 0:
  81. y = getFirstNotBlackY(x, y);
  82. yend = getNextBlackY(x, y);
  83. break;
  84. case 1:
  85. y = getFirstBrightY(x, y);
  86. yend = getNextDarkY(x, y);
  87. break;
  88. case 2:
  89. y = getFirstNotWhiteY(x, y);
  90. yend = getNextWhiteY(x, y);
  91. break;
  92. default:
  93. break;
  94. }
  95. if (y < 0) break;
  96. int sortLength = yend-y;
  97. color[] unsorted = new color[sortLength];
  98. color[] sorted = new color[sortLength];
  99. for (int i=0; i<sortLength; i++) {
  100. unsorted[i] = renderer.pixels[x + (y+i) * renderer.width];
  101. }
  102. sorted = sort(unsorted);
  103. for (int i=0; i<sortLength; i++) {
  104. renderer.pixels[x + (y+i) * renderer.width] = sorted[i];
  105. }
  106. y = yend+1;
  107. }
  108. }
  109. //BLACK
  110. int getFirstNotBlackX(int _x, int _y) {
  111. int x = _x;
  112. int y = _y;
  113. color c;
  114. while ( (c = renderer.pixels[x + y * renderer.width]) < params.get(0).value) {
  115. x++;
  116. if (x >= renderer.width) return -1;
  117. }
  118. return x;
  119. }
  120. int getNextBlackX(int _x, int _y) {
  121. int x = _x+1;
  122. int y = _y;
  123. color c;
  124. while ( (c = renderer.pixels[x + y * renderer.width]) > params.get(0).value) {
  125. x++;
  126. if (x >= renderer.width) return renderer.width-1;
  127. }
  128. return x-1;
  129. }
  130. //BRIGHTNESS
  131. int getFirstBrightX(int _x, int _y) {
  132. int x = _x;
  133. int y = _y;
  134. color c;
  135. while (brightness (c = renderer.pixels[x + y * renderer.width]) < params.get(0).value) {
  136. x++;
  137. if (x >= renderer.width) return -1;
  138. }
  139. return x;
  140. }
  141. int getNextDarkX(int _x, int _y) {
  142. int x = _x+1;
  143. int y = _y;
  144. color c;
  145. while (brightness (c = renderer.pixels[x + y * renderer.width]) > params.get(0).value) {
  146. x++;
  147. if (x >= renderer.width) return renderer.width-1;
  148. }
  149. return x-1;
  150. }
  151. //WHITE
  152. int getFirstNotWhiteX(int _x, int _y) {
  153. int x = _x;
  154. int y = _y;
  155. color c;
  156. while ( (c = renderer.pixels[x + y * renderer.width]) > params.get(0).value) {
  157. x++;
  158. if (x >= renderer.width) return -1;
  159. }
  160. return x;
  161. }
  162. int getNextWhiteX(int _x, int _y) {
  163. int x = _x+1;
  164. int y = _y;
  165. color c;
  166. while ( (c = renderer.pixels[x + y * renderer.width]) < params.get(0).value) {
  167. x++;
  168. if (x >= renderer.width) return renderer.width-1;
  169. }
  170. return x-1;
  171. }
  172. //BLACK
  173. int getFirstNotBlackY(int _x, int _y) {
  174. int x = _x;
  175. int y = _y;
  176. color c;
  177. if (y < renderer.height) {
  178. while ( (c = renderer.pixels[x + y * renderer.width]) < params.get(0).value) {
  179. y++;
  180. if (y >= renderer.height) return -1;
  181. }
  182. }
  183. return y;
  184. }
  185. int getNextBlackY(int _x, int _y) {
  186. int x = _x;
  187. int y = _y+1;
  188. color c;
  189. if (y < renderer.height) {
  190. while ( (c = renderer.pixels[x + y * renderer.width]) > params.get(0).value) {
  191. y++;
  192. if (y >= renderer.height) return renderer.height-1;
  193. }
  194. }
  195. return y-1;
  196. }
  197. //BRIGHTNESS
  198. int getFirstBrightY(int _x, int _y) {
  199. int x = _x;
  200. int y = _y;
  201. color c;
  202. if (y < renderer.height) {
  203. while (brightness (c = renderer.pixels[x + y * renderer.width]) < params.get(0).value) {
  204. y++;
  205. if (y >= renderer.height) return -1;
  206. }
  207. }
  208. return y;
  209. }
  210. int getNextDarkY(int _x, int _y) {
  211. int x = _x;
  212. int y = _y+1;
  213. color c;
  214. if (y < renderer.height) {
  215. while (brightness (c = renderer.pixels[x + y * renderer.width]) > params.get(0).value) {
  216. y++;
  217. if (y >= renderer.height) return renderer.height-1;
  218. }
  219. }
  220. return y-1;
  221. }
  222. //WHITE
  223. int getFirstNotWhiteY(int _x, int _y) {
  224. int x = _x;
  225. int y = _y;
  226. color c;
  227. if (y < renderer.height) {
  228. while ( (c = renderer.pixels[x + y * renderer.width]) > params.get(0).value) {
  229. y++;
  230. if (y >= renderer.height) return -1;
  231. }
  232. }
  233. return y;
  234. }
  235. int getNextWhiteY(int _x, int _y) {
  236. int x = _x;
  237. int y = _y+1;
  238. color c;
  239. if (y < renderer.height) {
  240. while ( (c = renderer.pixels[x + y * renderer.width]) < params.get(0).value) {
  241. y++;
  242. if (y >= renderer.height) return renderer.height-1;
  243. }
  244. }
  245. return y-1;
  246. }
  247. }
  248. /*
  249. DISTORTER
  250. */
  251. class DISTORTER extends Shader{
  252. boolean do_blend = false; // blend image after process
  253. int blend_mode = OVERLAY; // blend type
  254. int channel = BRIGHTNESS; // channel used in processing (R,G,B) or (H,S,B)
  255. float scalex = 0.05; // from 0.01 to 1
  256. float scaley = 0.1; // from 0.01 to 1
  257. boolean shift_hue = true;
  258. float shift_amt = 0.1; // from 0 to 1
  259. PImage buffer;
  260. int[][] distort = new int[2][512];
  261. final static float tick = 1.0/512.0;
  262. int mode = 0;
  263. DISTORTER() {
  264. buffer = createImage(renderer.width, renderer.height, ARGB);
  265. name = "fxDIstorter";
  266. params.add(new Param("width", FLOATVAL, 1, buffer.width/4-1, new int[]{SINE, SAWTOOTH, RAMPUPDOWN, TAN, TANINVERSE, TRIANG}));
  267. params.add(new Param("height", FLOATVAL, 1, buffer.height/4-1, new int[]{SINE, SAWTOOTH, RAMPUPDOWN, TAN, TANINVERSE, TRIANG}));
  268. params.add(new Param("do blend", INTVAL, 0, 1, new int[]{RANDOM,SQUAR}));
  269. params.add(new Param("shift hue amount", FLOATVAL, 0, 1, new int[]{SAWTOOTH, SAWTOOTHINVERSE, TAN, TANINVERSE, RAMP, RAMPINVERSE}));
  270. params.add(new Param("scale x", FLOATVAL, 0.01, 1, new int[]{SINE, SAWTOOTH, RAMPUPDOWN, TAN, TANINVERSE, TRIANG}));
  271. params.add(new Param("scale y", FLOATVAL, 0.01, 1, new int[]{SINE, SAWTOOTH, RAMPUPDOWN, TAN, TANINVERSE, TRIANG}));
  272. params.add(new Param("blend mode", INTVAL, 0, 11, new int[]{SINE, SAWTOOTH, RAMPUPDOWN, TAN, TANINVERSE, TRIANG}));
  273. params.add(new Param("channel", INTVAL, 0, 12, new int[]{SINE, SAWTOOTH, RAMPUPDOWN, TAN, TANINVERSE, TRIANG}));
  274. // channel, blend_mode
  275. //params.add(new Param("height", FLOATVAL, 1, buffer.height/4-1, new int[]{SINE, SAWTOOTH, RAMPUPDOWN, TAN, TANINVERSE, TRIANG}));
  276. // prepare distortion pattern
  277. for (int i=0; i<512; i++) {
  278. distort[0][i] = (int)random(-128, 128);
  279. distort[1][i] = (int)random(-128, 128);
  280. }
  281. }
  282. final int[] blends = {
  283. ADD, SUBTRACT, DARKEST, LIGHTEST, DIFFERENCE, EXCLUSION, MULTIPLY, SCREEN, OVERLAY, HARD_LIGHT, SOFT_LIGHT, DODGE, BURN
  284. };
  285. // ALL Channels, Nxxx stand for negative (255-value)
  286. // channels to work with
  287. final static int RED = 0;
  288. final static int GREEN = 1;
  289. final static int BLUE = 2;
  290. final static int HUE = 3;
  291. final static int SATURATION = 4;
  292. final static int BRIGHTNESS = 5;
  293. final static int NRED = 6;
  294. final static int NGREEN = 7;
  295. final static int NBLUE = 8;
  296. final static int NHUE = 9;
  297. final static int NSATURATION = 10;
  298. final static int NBRIGHTNESS = 11;
  299. void apply() {
  300. buffer = renderer.get();
  301. buffer.resize(renderer.width, renderer.height);
  302. float neww = params.get(0).value;
  303. float newh = params.get(1).value;
  304. do_blend = boolean(int(params.get(2).value));
  305. shift_amt = params.get(3).value;
  306. scalex = params.get(4).value;
  307. scaley = params.get(5).value;
  308. blend_mode = blends[(int)params.get(6).value];
  309. channel = (int)params.get(7).value;
  310. float totalnum = neww+newh;
  311. float times = (totalnum/floor(totalnum/neww));
  312. float offx = (totalnum%neww)/times;
  313. float ratiox = neww/buffer.width;
  314. renderer.beginDraw();
  315. renderer.colorMode(RGB);
  316. colorMode(RGB);
  317. renderer.noStroke();
  318. for (int y=0; y<buffer.height; y++) {
  319. float yy = y/(float)buffer.height;
  320. for (int x=0; x<buffer.width; x++) {
  321. float xx = x/(float)buffer.width;
  322. float offy = floor(newh*yy);
  323. float fx = xx*ratiox+offx*offy;
  324. float shift = fx%1.0;
  325. float st = shift/tick;
  326. int no1 = floor(st)%512;
  327. int no2 = ceil(st)%512;
  328. float l = st-(float)no1;
  329. float cx = lerp(distort[0][no1], distort[0][no2], l);
  330. float cy = lerp(distort[1][no1], distort[1][no2], l);
  331. float rx =getChannel(buffer.get(x, y), channel);
  332. int sx = (int)((buffer.width+x+cx*rx*scalex*0.1)%buffer.width);
  333. int sy = (int)((buffer.height+y+cy*scaley)%buffer.height);
  334. color c=buffer.get(sx, sy);
  335. if (shift_hue) {
  336. colorMode(HSB, 255);
  337. c = color((hue(c)+shift_amt*255*noise(newh+y))%255.0, constrain(saturation(c)*1.2, 0, 255), constrain(brightness(c), 0, 255));
  338. colorMode(RGB, 255);
  339. }
  340. // buffer.fill(lerpColor(c,img.get(x,y),0.2));
  341. renderer.fill(c);
  342. renderer.rect(x, y, 1, 1);
  343. }
  344. }
  345. if (do_blend)
  346. renderer.blend(buffer, 0, 0, buffer.width, buffer.height, 0, 0, renderer.width, renderer.height, blend_mode);
  347. renderer.endDraw();
  348. }
  349. float getChannel(color c, int channel) {
  350. int ch = channel>5?channel-6:channel;
  351. float cc;
  352. switch(ch) {
  353. case RED:
  354. cc = red(c);
  355. break;
  356. case GREEN:
  357. cc = green(c);
  358. break;
  359. case BLUE:
  360. cc = blue(c);
  361. break;
  362. case HUE:
  363. cc = hue(c);
  364. break;
  365. case SATURATION:
  366. cc = saturation(c);
  367. break;
  368. default:
  369. cc= brightness(c);
  370. break;
  371. }
  372. return channel>5?255-cc:cc;
  373. }
  374. }