Processing入门教程(二) Kinect 粒子系统

接着上一篇的入门教程一,我们这次将Kinect 也结合进来

环境: processing 3.4 和 kinect v2

找到深度图的中心点标记出来,将粒子系统跟随这个中心点

入口:

import KinectPV2.*;
KinectPV2 kinect2;
float minThresh = 518;
float maxThresh = 1184;
PImage img;
int offset;
int d;
ParticleSystem ps;
void setup() {
  size(512, 414, P3D);
  kinect2 = new KinectPV2(this);
  kinect2.enableDepthImg(true);
  ps = new ParticleSystem();
  img = createImage(kinect2.getDepthImage().width, kinect2.getDepthImage().height, RGB);
  println(img.width, img.height);
  kinect2.init();
}
void draw() {
  background(255);

  img.loadPixels();

  //minThresh = map(mouseX,0,width,0,4500);  
  //maxThresh = map(mouseY,0,height,0,4500);
  //println(minThresh, maxThresh);
  int[] depth = kinect2.getRawDepthData();

  float sumX = 0;
  float sumY = 0;
  float totalPixels = 0;
  // image(img,0,0);
  for (int x = 0; x < img.width; x++) {
    for (int y = 0; y < img.height; y ++) {
      offset = x + y*img.width;
      d = depth[offset];
      if (d> minThresh && d< maxThresh) {
        img.pixels[offset] = color(255, 0, 150);
        
          //ps.addParticle(new PVector(x, y));
          //ps.run();
        
        sumX += x;
        sumY += y;
        totalPixels ++;
      } else {
        img.pixels[offset] = color(0);
      }
    }
  }

  img.updatePixels();
  image(img, 0, 0);
  float avgX = sumX /totalPixels;
  float avgY = sumY / totalPixels;
  fill(150, 0, 255);
  ellipse(avgX, avgY, 64, 64);
  // 3==> the speed
  for (int i = 0; i<5; i++) {
    ps.addParticle(new PVector(avgX,avgY));
    ps.run();
  }
  // ps.addParticle(mouseX,mouseY);
  //ps.run();
  fill(255);
  textSize(32);
  stroke(255);
  text("offset: " + offset+ "  d: " + d, 50, 50);
}

2.Particle类

class Particle {
  PVector location;
  PVector velocity;
  PVector acceleration;

  float lifespan;
  Particle(PVector l) {
    // The acceleration
    acceleration = new PVector(0, 0.01);
    // circel's x and y ==> range
    velocity = new PVector(random(-1, 1), random(-2, 0));
    // apawn's position
    location = l.copy();
    // the circle life time
    lifespan = 255.0;
  }
  void run() {
    update();
    display();
  }
  void update() {
    velocity.add(acceleration);
    location.add(velocity);
    lifespan-=4.0;
  }

  boolean isDead() {
    if (lifespan <= 0) {
      return true;
    } else {
      return false;
    }
  }
  void display() {
    // border
    stroke(0, lifespan);
    // border's weight
    strokeWeight(1);
    float r = random(0,255);
    float g = random(0,255);
    float b = random(0,255);
    // random the circle's color
    fill(r,g,b, lifespan);
    // draw circle
    ellipse(location.x, location.y, 3, 3);
  }
}

3.ParticleSystem管理类:

// A class to describe a group of Particles
// An ArrayList is used to manage the list of Particles 

class ParticleSystem {
  ArrayList particles;
  PVector origin;

  ParticleSystem() {
    particles = new ArrayList();
  }

  void addParticle(PVector position) {
     origin = position.copy();
    particles.add(new Particle(origin));
  }

  void run() {
    for (int i = particles.size()-1; i >= 0; i--) {
      Particle p = particles.get(i);
      p.run();
      if (p.isDead()) {
        particles.remove(i);
      }
    }
  }
}

 4.效果图如下:

Processing入门教程(二) Kinect 粒子系统_第1张图片

你可能感兴趣的:(Processing)