Daily Creative Coding

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

変形を繰り返すボックス

/**
* transforming boxes
*
* @author aa_debdeb
* @date 2016/12/15
*/

int LOOP = 120;
float MAX_BLOCK_WIDTH = 35;

ArrayList<Block> blocks;

void setup(){
  size(500, 500, P3D);
  blocks = new ArrayList<Block>();
  float gap = 60;
  int num = 30;
  for(int x = 0; x <= num; x++){
    for(int y = 0; y <= num; y++){
      PVector loc = new PVector(x * gap - num * gap / 2, y * gap - num * gap / 2, 0.0);
      blocks.add(new Block(loc));
    }
  }
}

void draw(){
  background(200, 251, 200);
  fill(122, 251, 152);
  stroke(255);
  translate(width / 2, height / 4, -200);
  rotateX(HALF_PI / 2);
  rotateZ(HALF_PI / 3);
  for(Block b: blocks){
    b.display();
  }
}

class Block{

  PVector loc;
  int loopOffset;
  
  Block(PVector _loc){
    loc = _loc;
    loopOffset = int(random(LOOP));
  }
  
  void display(){
    pushMatrix();
    float time = (frameCount + loopOffset) % LOOP;
    float bx, by, bz;
    float lx, ly, lz;
    if(time < LOOP / 4.0){
      bx = MAX_BLOCK_WIDTH;
      by = MAX_BLOCK_WIDTH;
      bz = map(time, 0, LOOP / 4.0, MAX_BLOCK_WIDTH, MAX_BLOCK_WIDTH / 5.0);
      lx = 0.0;
      ly = 0.0;
      lz = bz / 2;
    } else if(time < LOOP / 4.0 * 2){
      bx = MAX_BLOCK_WIDTH;
      by = map(time, LOOP / 4.0, LOOP / 4.0 * 2, MAX_BLOCK_WIDTH, MAX_BLOCK_WIDTH / 5.0);
      bz = MAX_BLOCK_WIDTH / 5.0;
      lx = 0.0;
      ly = MAX_BLOCK_WIDTH / 2 - by / 2;
      lz = bz / 2;
    } else if(time < LOOP / 4.0 * 3){
      bx = MAX_BLOCK_WIDTH;
      by = MAX_BLOCK_WIDTH / 5.0;
      bz = map(time, LOOP / 4.0 * 2, LOOP / 4.0 * 3, MAX_BLOCK_WIDTH / 5.0, MAX_BLOCK_WIDTH);
      lx = 0.0;
      ly = MAX_BLOCK_WIDTH / 2 - by / 2;
      lz = bz / 2;
    } else {
      bx = MAX_BLOCK_WIDTH;
      by = map(time, LOOP / 4.0 * 3, LOOP / 4.0 * 4, MAX_BLOCK_WIDTH / 5.0, MAX_BLOCK_WIDTH);
      bz = MAX_BLOCK_WIDTH;
      lx = 0.0;
      ly = MAX_BLOCK_WIDTH / 2 - by / 2;
      lz = bz / 2;    
    }
    translate(loc.x + lx, loc.y + ly, loc.z + lz);
    box(bx, by, bz);
    popMatrix();
  }
  
}
f:id:aa_debdeb:20161210221359j:plain