Guest User

Untitled

a guest
Jan 1st, 2020
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.46 KB | None | 0 0
  1. /**
  2.  * Mirror
  3.  * by Daniel Shiffman.  
  4.  *
  5.  * Each pixel from the video source is drawn as a rectangle with rotation based on brightness.  
  6.  */
  7.  
  8. import processing.video.*;
  9.  
  10. // Size of each cell in the grid
  11. int cellSize = 20;
  12. // Number of columns and rows in our system
  13. int cols, rows;
  14. // Variable for capture device
  15. Capture video;
  16. PImage lastFrame;
  17. ArrayList<Particula> particulas;
  18.  
  19. void setup() {
  20.   particulas = new ArrayList<Particula>();
  21.   size(1280, 720);
  22.   frameRate(60);
  23.  
  24.   cols = width / cellSize;
  25.   rows = height / cellSize;
  26.   colorMode(RGB, 255, 255, 255, 100);
  27.  
  28.   // This the default video input, see the GettingStartedCapture
  29.   // example if it creates an error
  30.   video = new Capture(this, width, height);
  31.  
  32.   // Start capturing the images from the camera
  33.   video.start();  
  34.   lastFrame = createImage(width, height, RGB);
  35.    
  36.   background(255);
  37. }
  38.  
  39. // New frame available from camera
  40. void captureEvent(Capture video) {
  41.   // Save previous frame for motion detection!!
  42.   lastFrame.copy(video, 0, 0, video.width, video.height, 0, 0, width, height);
  43.   lastFrame.updatePixels();  
  44.   video.read();
  45. }
  46.  
  47.  
  48. void draw() {
  49.   clear();
  50.   background(255);
  51.   if (video.available()) {
  52.     loadPixels();
  53.     video.loadPixels();
  54.     lastFrame.loadPixels();
  55.  
  56.    
  57.     // Begin loop for columns
  58.     for (int i = 0; i < cols; i++) {
  59.       // Begin loop for rows
  60.       for (int j = 0; j < rows; j++) {
  61.      
  62.         // Where are we, pixel-wise?
  63.         //Color New Frame
  64.         int x = i*cellSize;
  65.         int y = j*cellSize;
  66.         int loc = (video.width - x - 1) + y*video.width; // Reversing x to mirror the image
  67.      
  68.         float rNewFrame = red(video.pixels[loc]);
  69.         float gNewFrame = green(video.pixels[loc]);
  70.         float bNewFrame = blue(video.pixels[loc]);
  71.         // Make a new color with an alpha component
  72.         color cNewFrame = color(rNewFrame, gNewFrame, bNewFrame, 255);
  73.        
  74.        
  75.         //color last Frame
  76.         float rLastFrame = red(lastFrame.pixels[loc]);
  77.         float gLastFrame = green(lastFrame.pixels[loc]);
  78.         float bLastFrame = blue(lastFrame.pixels[loc]);
  79.         // Make a new color with an alpha component
  80.         color cLastFrame = color(rLastFrame, gLastFrame, bLastFrame, 255);
  81.         float diff = dist(red(cNewFrame), green(cNewFrame), blue(cNewFrame), red(cLastFrame), green(cLastFrame), blue(cLastFrame));
  82.         //float diff = dist(0, 0, blue(cNewFrame), 0, 0, blue(cLastFrame));
  83.  
  84.         if(diff > 50){
  85.           // Code for drawing a single rect
  86.           // Using translate in order for rotation to work properly
  87.            //Particula(float x, float y, float size, color c, int frames)
  88.           int frames = (int)brightness(cNewFrame)/2;
  89.           float size = 15;
  90.           particulas.add(new Particula(x+cellSize/2,y+cellSize/2, size,cNewFrame,frames));
  91.         }
  92.       }
  93.     }
  94.    
  95.   }
  96.  
  97.   if(particulas.size()!=0){
  98.     int i = 0;
  99.    
  100.     while(i<particulas.size()){
  101.        particulas.get(i).update();
  102.        if(particulas.get(i).getFrames() == 0){
  103.          particulas.remove(i);
  104.        }
  105.        else{
  106.          i++;
  107.        }
  108.     }
  109.   }
  110.  
  111.  
  112. }
  113.  
  114. class Particula{
  115.   float x, y, size;
  116.   float[] speed;
  117.   float[] acc;
  118.  
  119.   color c;
  120.   float alpha,r,g,b;
  121.  
  122.   int frames;
  123.  
  124.   float maxSpeed,maxRadius,minRadius,minColor,maxColor,minAlpha,maxAlpha,maxRandomAcc;
  125.  
  126.   Particula(float x, float y, float size, color c, int frames){
  127.      //controladores
  128.     maxSpeed = 30;
  129.     maxRadius = 75;
  130.     minRadius = 10;
  131.     minColor= 155;
  132.     maxColor = 255;
  133.     minAlpha = 10;
  134.     maxAlpha = 120;
  135.     maxRandomAcc = 50;
  136.    
  137.     this.x = x;
  138.     this.y = y;
  139.     this.size = size;
  140.     speed = new float[]{random(-maxSpeed, maxSpeed),random(-maxSpeed, maxSpeed)};
  141.     acc = new float[]{random(-maxRandomAcc, maxRandomAcc),random(-maxRandomAcc, maxRandomAcc)};
  142.     this.c = c;
  143.    
  144.     alpha = alpha(c);
  145.     r = red(c);
  146.     g = green(c);
  147.     b = blue(c);
  148.    
  149.     this.frames = frames;
  150.   }
  151.  
  152.   void update(){
  153.     frames--;
  154.    
  155.     x += (speed[0]*(0.144));
  156.     if(x>width+maxRadius){
  157.       x = x-width-maxRadius;
  158.     }
  159.     else if(x<0-maxRadius){
  160.       x = width+x+maxRadius;
  161.     }
  162.    
  163.     y += (speed[1]*(0.144));
  164.     if(y>height+maxRadius){
  165.       y = y-height-maxRadius;
  166.     }
  167.     else if(y<0-maxRadius){
  168.       y = height+y+maxRadius;
  169.     }
  170.  
  171.     speed[0] += (acc[0]*(0.144));
  172.     if(speed[0]>maxSpeed || speed[0]<-maxSpeed){
  173.        speed[0] -= (acc[0]*(0.144));
  174.     }
  175.     speed[1] += (acc[1]*(0.144));
  176.     if(speed[1]>maxSpeed || speed[1]<-maxSpeed){
  177.        speed[1] -= (acc[1]*(0.144));
  178.     }
  179.    
  180.     acc[0] = random(-maxRandomAcc,maxRandomAcc);
  181.     acc[1] = random(-maxRandomAcc,maxRandomAcc);
  182.    
  183.     /*
  184.     //cor
  185.     //10-255  50--1 x- 60-radius*255/50
  186.     float addR = random(-10,10);
  187.     r += addR;
  188.     if(r>maxColor || r<minColor){
  189.       r -= addR;
  190.     }
  191.     addR = random(-10,10);
  192.     g += addR;
  193.     if(g>maxColor || g<minColor){
  194.       g -= addR;
  195.     }
  196.     addR = random(-10,10);
  197.     b += addR;
  198.     if(b>maxColor || b<minColor){
  199.       b -= addR;
  200.     }
  201.     */
  202.     //alpha e size
  203.     if((frames/2)>255){
  204.       alpha = 255;
  205.     }
  206.     else{
  207.       alpha = (frames/2);
  208.     }
  209.    
  210.     if((frames/2)>15){
  211.       size = 15;
  212.     }
  213.     else{
  214.       size = (frames/2);
  215.     }
  216.    
  217.    
  218.     fill(r, g, b, alpha);
  219.     noStroke();
  220.     ellipse(x, y, size, size);
  221.   }
  222.  
  223.   int getFrames(){
  224.     return frames;
  225.   }
  226.  
  227. }
Advertisement
Add Comment
Please, Sign In to add comment