読者です 読者をやめる 読者になる 読者になる

Daily Creative Coding

元「30 min. Processing」。毎日、Creative Codingします。

カメラから取り込んだ映像を三次元のメッシュに加工する

f:id:aa_debdeb:20151203215410j:plain
/**
* Camera by Square Mesh
*
* @author aa_debdeb
* @date 2015/12/14
*/

import processing.video.*;

Capture camera;
float gridWidth = 12;
float maxZ = -100;

void setup(){
  size(640, 480, OPENGL);
  smooth();
  camera = new Capture(this, width, height);
  camera.start();
}

void draw(){
  lights();
  background(105);
  camera.loadPixels();
  int gridX = int(camera.width / gridWidth);
  int gridY = int(camera.height / gridWidth);
  float[][] brightness = new float[gridX][gridY];
  for(int gx = 0; gx < gridX; gx++){
    for(int gy = 0; gy < gridY; gy++){
      int x = int(gx * gridWidth);
      int y = int(gy * gridWidth);
      color rgb = camera.pixels[y * camera.width + x];
      brightness[gx][gy] = map(max(red(rgb), green(rgb), blue(rgb)), 0, 255, 0, 1);      
    }
  }
  
  translate(0, 0, -50);
  stroke(128);
  strokeWeight(1);
  fill(0, 191, 255);
  for(int gx = 0; gx < gridX - 1; gx++){
    for(int gy = 0; gy < gridY - 1; gy++){
      int x = gx * int(gridWidth);
      int y = gy * int(gridWidth);
      beginShape();
      vertex(x, y, brightness[gx][gy] * maxZ);
      vertex(x + gridWidth, y, brightness[gx + 1][gy] * maxZ);
      vertex(x + gridWidth, y + gridWidth, brightness[gx + 1][gy + 1] * maxZ);
      vertex(x, y + gridWidth, brightness[gx][gy + 1] * maxZ);
      endShape(CLOSE);
    }
  }
}