compositor for 2d shader
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. Capture cam;
  2. Movie movie;
  3. boolean moviePlaying;
  4. PImage fallbackImage;
  5. void movieEvent(Movie m) {
  6. if (moviePlaying) {
  7. m.read();
  8. if (mode == 1) {
  9. sourceManager.setImage(movie.get());
  10. movie.jump(random(movie.duration()));
  11. }
  12. }
  13. }
  14. void newMovie(String path) {
  15. movie = new Movie(this, path);
  16. }
  17. boolean isVideo(String path) {
  18. String fpl = path.toLowerCase();
  19. if (fpl.endsWith(".mp4") || fpl.endsWith(".avi") || fpl.endsWith(".webm") || fpl.endsWith(".mkv") || fpl.endsWith(".mov")) return true;
  20. else return false;
  21. }
  22. boolean isImage(String path) {
  23. String fpl = path.toLowerCase();
  24. if (fpl.endsWith(".bmp") || fpl.endsWith(".png") || fpl.endsWith(".jpg") || fpl.endsWith(".jpeg") || fpl.endsWith(".gif")) return true; //gif = special case!
  25. else return false;
  26. }
  27. class SourceManager { //no constructor
  28. // This function returns all the files in a directory as an array of Strings
  29. String[] listFileNames(String dir) {
  30. File file = new File(dir);
  31. if (file.isDirectory()) {
  32. String names[] = file.list();
  33. return names;
  34. } else {
  35. return null;
  36. }
  37. }
  38. String dir;
  39. String[] selection;
  40. int selId;
  41. void importURI(String path) {
  42. File file = new File(path);
  43. if (file.isDirectory()) {
  44. dir = path;
  45. String names[] = file.list();
  46. ArrayList<String> usableFiles = new ArrayList<String>();
  47. for (int i = 0; i < names.length; i++) {
  48. if (isImage(names[i]) || isVideo(names[i])) {
  49. usableFiles.add(names[i]);
  50. }
  51. }
  52. selection = new String[usableFiles.size()];
  53. for (int i = 0; i < usableFiles.size(); i++) selection[i] = usableFiles.get(i);
  54. println("Got new selection from folder. " + selection.length + " usable files.");
  55. path = dir + "/" + selection[(int)random(selection.length)];
  56. }
  57. if (isImage(path) || isVideo(path)) {
  58. println("Importing file " + path);
  59. if (moviePlaying) movie.stop();
  60. if (isImage(path)) {
  61. moviePlaying = false;
  62. setImage(loadImage(path));
  63. } else if (isVideo(path)) {
  64. moviePlaying = true;
  65. newMovie(path);
  66. movie.loop();
  67. }
  68. }
  69. }
  70. void fallback() {
  71. println("Fallback");
  72. mode = 1;
  73. setImage(fallbackImage); //fallbackImage should always be a copy of the last image that worked
  74. }
  75. void update() {
  76. //check if source image is present
  77. if (mode == 0) {
  78. try {
  79. if (cam.available() == true) {
  80. cam.read();
  81. if (cam != null) {
  82. source = cam.get();
  83. } else {
  84. fallback();
  85. }
  86. }
  87. }
  88. catch (Exception e) {
  89. println(e);
  90. println("No cam available, falling back to image..");
  91. fallback();
  92. }
  93. }
  94. if (source == null) {
  95. fallback();
  96. }
  97. int toW, toH;
  98. if (source.width > source.height) {
  99. toW = renderSize;
  100. toH = int(map(source.height, 0, source.width, 0, renderSize));
  101. } else {
  102. toW = int(map(source.width, 0, source.height, 0, renderSize));
  103. toH = renderSize;
  104. }
  105. toW = (toW % 2 == 0) ? toW : toW+1;
  106. toH = (toH % 2 == 0) ? toH : toH+1;
  107. renderer.setSize(toW, toH);
  108. }
  109. void setImage(PImage img) { //set image to source
  110. source = img.get();
  111. fallbackImage = source;
  112. }
  113. void setSource() { //set source to renderer
  114. renderer.beginDraw();
  115. if (mode == 0) {
  116. try { //final check for source image being present & valid
  117. renderer.beginDraw(); //pre-fx
  118. renderer.tint(255, 220, 200); //beautify me (color correct webcam image)
  119. renderer.image(source, 0, 0, renderer.width, renderer.height);
  120. renderer.noTint();
  121. }
  122. catch(Exception e) {
  123. println("error here as well");
  124. }
  125. }
  126. if (mode == 1) {
  127. //else if (mode == 1){
  128. renderer.image(source, 0, 0, renderer.width, renderer.height);
  129. }
  130. renderer.endDraw();
  131. }
  132. }