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

Daily Creative Coding

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

無限の流れ

インタラクション
f:id:aa_debdeb:20161012202520j:plain

endless stream - OpenProcessing

/**
* endless stream
*
* @author aa_debdeb 
* @date 2016/1019
*/

float MAX_SPEED = 20;
float MIN_SPEED = 5;

ArrayList<Rectangle> rectangles; 
float speed;
float distance;
float rectWNoise = random(10000);
float rectHNoise = random(10000);
float colRNoise = random(10000);
float colGNoise = random(10000);
float colBNoise = random(10000);
float noiseScale = 0.001;

void setup(){
  size(500, 500);
  noStroke();
  rectMode(CENTER);
  rectangles = new ArrayList<Rectangle>();
  speed = lerp(MIN_SPEED, MAX_SPEED, 0.5);
  distance = 0;
}

void draw(){
  background(0);
  for(Rectangle rectangle: rectangles){
    rectangle.display(); 
  }
  distance += speed;
  ArrayList<Rectangle> nextRects = new ArrayList<Rectangle>();
  for(Rectangle rectangle: rectangles){
    if(rectangle.update()){
      nextRects.add(rectangle);
    }
  }
  rectangles = nextRects;
  int num = int(width * speed * map(noise(distance * noiseScale), 0, 1, 0, 0.002)) ;
  for(int i = 0; i < num; i++){
    float x = random(width);
    float y = random(-speed - 50, 0);
    rectangles.add(new Rectangle(x, y));
  }
  speed += map(mouseY, 0, height, 0.05, -0.05);
  speed = constrain(speed, MIN_SPEED, MAX_SPEED);
}

class Rectangle{
  
  PVector loc, size;
  color c;
  
  Rectangle(float x, float y){
    loc = new PVector(x, y);
    float sizeX = random(map(noise(distance * noiseScale + rectWNoise), 0, 1, 2, 12), map(noise(distance * noiseScale + rectWNoise), 0, 1, 12, 24));
    float sizeY = random(map(noise(distance * noiseScale + rectHNoise), 0, 1, 2, 12), map(noise(distance * noiseScale + rectHNoise), 0, 1, 12, 24));
    size = new PVector(sizeX, sizeY);
    c = color(random(map(noise(distance * noiseScale + colRNoise), 0, 1, 0, 128), map(noise(distance * noiseScale + colRNoise), 0, 1, 128, 255)),
              random(map(noise(distance * noiseScale + colGNoise), 0, 1, 0, 128), map(noise(distance * noiseScale + colGNoise), 0, 1, 128, 255)),
              random(map(noise(distance * noiseScale + colBNoise), 0, 1, 0, 128), map(noise(distance * noiseScale + colBNoise), 0, 1, 128, 255)));
  }
  
  void display(){
    fill(c);
    rect(loc.x, loc.y, size.x, size.y);
  }
  
  boolean update(){
    loc.y += speed;
    if(loc.y > height + size.y){
      return false; 
    } else {
      return true;
    }
  }
  
}