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

Daily Creative Coding

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

擬似3Dパーティクル

/**
* Like 3D
*
* @author aa_debdeb
* @date 2016/02/04
*/

ArrayList<Particle> particles;

void setup(){
  size(500, 500);
  smooth();
  frameRate(30);

  particles = new ArrayList<Particle>();
  for(int i = 0; i < 300; i++){
    particles.add(new Particle());
  }
}

void draw(){
  background(255);
  translate(width / 2, height / 2);
  for(Particle particle: particles){
    particle.display();
    particle.update();
  }
}

class Particle{
  
  float radious;
  float radian1;
  float radian2;
  float radian1Velocity;
  float radian2Velocity;
  
  Particle(){
    radious = 200; 
    radian1 = random(TWO_PI);
    radian2 = random(TWO_PI);
    radian1Velocity = map(random(1), 0, 1, -PI / 64, PI / 64);
    radian2Velocity = map(random(1), 0, 1, -PI / 64, PI / 64);
  }
  
  void display(){
    float x = radious * sin(radian1) * cos(radian2);
    float y = radious * sin(radian1) * sin(radian2);
    float z = radious * cos(radian1);
    float size = map(z, -radious, radious, 6, 12); 
    float alpha = map(z, -radious, radious, 32, 128);
    fill(64, alpha);
    noStroke();
    ellipse(x, y, size, size);
  }
  
  void update(){
    radian1 += radian1Velocity;
    if(radian1 >= TWO_PI){
      radian1 -= TWO_PI;
    }
    if(radian1 < 0){
      radian1 += TWO_PI;
    }
    radian2 += radian2Velocity;  
    if(radian2 >= TWO_PI){
      radian2 -= TWO_PI;
    }
    if(radian2 < 0){
      radian2 += TWO_PI;
    }
  }
}