compositor for 2d glitch effects
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

shader.pde 4.4KB

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