compositor for 2d glitch effects
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

shader.pde 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. class Param {
  2. String name;
  3. int type;
  4. float minValue, maxValue;
  5. float value;
  6. int[] osciModes;
  7. Param(String name_, int type_, float minValue_, float maxValue_, int[] osciModes_) {
  8. name = name_;
  9. type = type_; //FLOATVAL or INTVAL
  10. minValue = minValue_;
  11. maxValue = maxValue_;
  12. osciModes = osciModes_;
  13. }
  14. }
  15. class Shader {
  16. int id;
  17. int pos, previousPos;
  18. boolean previousActive;
  19. String name;
  20. ArrayList<Param> params = new ArrayList<Param>();
  21. ArrayList<Float> paramValues = new ArrayList<Float>();
  22. boolean paramChanged = true;
  23. PGraphics canvas = createGraphics(renderer.width, renderer.height);
  24. PGraphics result;
  25. int direction = 0;
  26. int directionParamIndex = -1;
  27. Shader() {
  28. canvas.beginDraw();
  29. canvas.image(renderer, 0, 0, canvas.width, canvas.height);
  30. canvas.endDraw();
  31. }
  32. void getValuesFromGUI() {
  33. try { //problematic
  34. params = gui.bricks.get(pos).params;
  35. }
  36. catch(Exception e) {
  37. println(e);
  38. }
  39. //this is only for checking if a param has changed. paramValues could also be used for a save/load function
  40. if (paramValues.size() != params.size()) {
  41. for (int i = 0; i < params.size(); i++) {
  42. paramValues.add(0.0);
  43. }
  44. }
  45. paramChanged = false;
  46. for (int i = 0; i < params.size(); i++) {
  47. if (paramValues.get(i) != params.get(i).value) {
  48. paramValues.set(i, params.get(i).value);
  49. paramChanged = true;
  50. }
  51. }
  52. if (pos != previousPos) {
  53. paramChanged = true;
  54. }
  55. previousPos = pos;
  56. try{
  57. if (previousActive != gui.bricks.get(pos).active) { //fails if brick has not yet been loaded but shader has
  58. paramChanged = true;
  59. }
  60. previousActive = gui.bricks.get(pos).active;
  61. } catch (Exception e){
  62. println("Brick not yet loaded, couldn't catch active state");
  63. }
  64. if (!paramChanged) {
  65. if (imageChanged) paramChanged = true;
  66. for (int i = this.pos; i >= 0; i--) {
  67. if (gui.shaderList.get(i).paramChanged) paramChanged = true;
  68. }
  69. }
  70. if (frameRate < 1) println("Rendering " + this + " on position " + pos + "...");
  71. }
  72. void changeParam(int paramNo, Param newParam) {
  73. gui.bricks.get(pos).params.remove(paramNo);
  74. gui.bricks.get(pos).params.add(paramNo, newParam);
  75. gui.bricks.get(pos).exchangeSlider(paramNo, newParam);
  76. paramChanged = true;
  77. }
  78. void shade() {
  79. if (renderSizeChanged || paramChanged) {
  80. this.setupCanvas();
  81. this.apply();
  82. this.render();
  83. } else {
  84. // println("nothing changed");
  85. renderer.beginDraw();
  86. renderer.image(result, 0, 0);
  87. renderer.endDraw();
  88. }/*
  89. this.setupCanvas();
  90. this.apply();
  91. this.render();
  92. */
  93. }
  94. void setupCanvas() {
  95. if (directionParamIndex >= 0) direction = (int)this.params.get(directionParamIndex).value;
  96. int cw = (direction == 0 || direction == 2) ? renderer.width : renderer.height;
  97. int ch = (direction == 0 || direction == 2) ? renderer.height : renderer.width;
  98. canvas = createGraphics(cw, ch);
  99. canvas.beginDraw();
  100. canvas.imageMode(CENTER);
  101. canvas.clear();
  102. canvas.pushMatrix();
  103. canvas.translate(cw/2, ch/2);
  104. canvas.rotate((PI/2)*direction);
  105. canvas.image(renderer, 0, 0);
  106. canvas.popMatrix();
  107. canvas.endDraw();
  108. }
  109. void apply() {
  110. }; //override me with an actual fragment shader
  111. void render() {
  112. renderer.beginDraw();
  113. renderer.imageMode(CENTER);
  114. renderer.pushMatrix();
  115. renderer.translate(renderer.width/2, renderer.height/2);
  116. renderer.rotate(-(PI/2)*direction);
  117. renderer.image(canvas, 0, 0);
  118. renderer.popMatrix();
  119. renderer.imageMode(CORNER);
  120. renderer.endDraw();
  121. result = createGraphics(renderer.width, renderer.height);
  122. result.beginDraw();
  123. result.image(renderer, 0, 0);
  124. result.endDraw();
  125. if (pos == gui.bricks.size()-1) {
  126. //println("Finished rendering");
  127. if (imageChanged) { //kind of hacky but it does synchronize the threads. sometimes.
  128. stepOne++;
  129. if (stepOne == 2) {
  130. stepOne = 0;
  131. imageChanged = false;
  132. }
  133. }
  134. }
  135. }
  136. @ Override String toString() {
  137. return getClass().getName();
  138. }
  139. }
  140. int stepOne = 0;