Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Mirror
- * by Daniel Shiffman.
- *
- * Each pixel from the video source is drawn as a rectangle with rotation based on brightness.
- */
- import processing.video.*;
- // Size of each cell in the grid
- int cellSize = 20;
- // Number of columns and rows in our system
- int cols, rows;
- // Variable for capture device
- Capture video;
- PImage lastFrame;
- ArrayList<Particula> particulas;
- void setup() {
- particulas = new ArrayList<Particula>();
- size(1280, 720);
- frameRate(60);
- cols = width / cellSize;
- rows = height / cellSize;
- colorMode(RGB, 255, 255, 255, 100);
- // This the default video input, see the GettingStartedCapture
- // example if it creates an error
- video = new Capture(this, width, height);
- // Start capturing the images from the camera
- video.start();
- lastFrame = createImage(width, height, RGB);
- background(255);
- }
- // New frame available from camera
- void captureEvent(Capture video) {
- // Save previous frame for motion detection!!
- lastFrame.copy(video, 0, 0, video.width, video.height, 0, 0, width, height);
- lastFrame.updatePixels();
- video.read();
- }
- void draw() {
- clear();
- background(255);
- if (video.available()) {
- loadPixels();
- video.loadPixels();
- lastFrame.loadPixels();
- // Begin loop for columns
- for (int i = 0; i < cols; i++) {
- // Begin loop for rows
- for (int j = 0; j < rows; j++) {
- // Where are we, pixel-wise?
- //Color New Frame
- int x = i*cellSize;
- int y = j*cellSize;
- int loc = (video.width - x - 1) + y*video.width; // Reversing x to mirror the image
- float rNewFrame = red(video.pixels[loc]);
- float gNewFrame = green(video.pixels[loc]);
- float bNewFrame = blue(video.pixels[loc]);
- // Make a new color with an alpha component
- color cNewFrame = color(rNewFrame, gNewFrame, bNewFrame, 255);
- //color last Frame
- float rLastFrame = red(lastFrame.pixels[loc]);
- float gLastFrame = green(lastFrame.pixels[loc]);
- float bLastFrame = blue(lastFrame.pixels[loc]);
- // Make a new color with an alpha component
- color cLastFrame = color(rLastFrame, gLastFrame, bLastFrame, 255);
- float diff = dist(red(cNewFrame), green(cNewFrame), blue(cNewFrame), red(cLastFrame), green(cLastFrame), blue(cLastFrame));
- //float diff = dist(0, 0, blue(cNewFrame), 0, 0, blue(cLastFrame));
- if(diff > 50){
- // Code for drawing a single rect
- // Using translate in order for rotation to work properly
- //Particula(float x, float y, float size, color c, int frames)
- int frames = (int)brightness(cNewFrame)/2;
- float size = 15;
- particulas.add(new Particula(x+cellSize/2,y+cellSize/2, size,cNewFrame,frames));
- }
- }
- }
- }
- if(particulas.size()!=0){
- int i = 0;
- while(i<particulas.size()){
- particulas.get(i).update();
- if(particulas.get(i).getFrames() == 0){
- particulas.remove(i);
- }
- else{
- i++;
- }
- }
- }
- }
- class Particula{
- float x, y, size;
- float[] speed;
- float[] acc;
- color c;
- float alpha,r,g,b;
- int frames;
- float maxSpeed,maxRadius,minRadius,minColor,maxColor,minAlpha,maxAlpha,maxRandomAcc;
- Particula(float x, float y, float size, color c, int frames){
- //controladores
- maxSpeed = 30;
- maxRadius = 75;
- minRadius = 10;
- minColor= 155;
- maxColor = 255;
- minAlpha = 10;
- maxAlpha = 120;
- maxRandomAcc = 50;
- this.x = x;
- this.y = y;
- this.size = size;
- speed = new float[]{random(-maxSpeed, maxSpeed),random(-maxSpeed, maxSpeed)};
- acc = new float[]{random(-maxRandomAcc, maxRandomAcc),random(-maxRandomAcc, maxRandomAcc)};
- this.c = c;
- alpha = alpha(c);
- r = red(c);
- g = green(c);
- b = blue(c);
- this.frames = frames;
- }
- void update(){
- frames--;
- x += (speed[0]*(0.144));
- if(x>width+maxRadius){
- x = x-width-maxRadius;
- }
- else if(x<0-maxRadius){
- x = width+x+maxRadius;
- }
- y += (speed[1]*(0.144));
- if(y>height+maxRadius){
- y = y-height-maxRadius;
- }
- else if(y<0-maxRadius){
- y = height+y+maxRadius;
- }
- speed[0] += (acc[0]*(0.144));
- if(speed[0]>maxSpeed || speed[0]<-maxSpeed){
- speed[0] -= (acc[0]*(0.144));
- }
- speed[1] += (acc[1]*(0.144));
- if(speed[1]>maxSpeed || speed[1]<-maxSpeed){
- speed[1] -= (acc[1]*(0.144));
- }
- acc[0] = random(-maxRandomAcc,maxRandomAcc);
- acc[1] = random(-maxRandomAcc,maxRandomAcc);
- /*
- //cor
- //10-255 50--1 x- 60-radius*255/50
- float addR = random(-10,10);
- r += addR;
- if(r>maxColor || r<minColor){
- r -= addR;
- }
- addR = random(-10,10);
- g += addR;
- if(g>maxColor || g<minColor){
- g -= addR;
- }
- addR = random(-10,10);
- b += addR;
- if(b>maxColor || b<minColor){
- b -= addR;
- }
- */
- //alpha e size
- if((frames/2)>255){
- alpha = 255;
- }
- else{
- alpha = (frames/2);
- }
- if((frames/2)>15){
- size = 15;
- }
- else{
- size = (frames/2);
- }
- fill(r, g, b, alpha);
- noStroke();
- ellipse(x, y, size, size);
- }
- int getFrames(){
- return frames;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment