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

Daily Creative Coding

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

水面を漂うオブジェクト

/**
* floating objects on water
*
* @author aa_debdeb
* @date 2016/07/17
*/

PVector stepSize = new PVector(30, 40);
PVector rectSize = new PVector(20, 30);
float maxZ = 200;
PVector noiseOffset;
float noiseScale = 0.003;
float timeScale = 0.007;

void setup(){
  size(640, 640, P3D);
  noStroke();
  fill(0, 255, 255);
  rectMode(CENTER);
  noiseOffset = new PVector(random(10000), random(10000), random(10000));
  
}

void draw(){
  background(255);
  float t = frameCount * timeScale;
  for(float w = 2 * stepSize.x; w <= width - 2 * stepSize.x; w += stepSize.x){
    for(float h = 2 * stepSize.y; h <= height - 2 * stepSize.y; h += stepSize.y){
      pushMatrix();
      float z = map(noise(w * noiseScale + noiseOffset.x + t, h * noiseScale + noiseOffset.y + t, t + noiseOffset.z), 0, 1, -maxZ, maxZ); 
      translate(w, h, z);
      float pxz = map(noise((w - stepSize.x) * noiseScale + noiseOffset.x + t, h * noiseScale + noiseOffset.y + t, t + noiseOffset.z), 0, 1, -maxZ, maxZ); 
      float nxz = map(noise((w + stepSize.x) * noiseScale + noiseOffset.x + t, h * noiseScale + noiseOffset.y + t, t + noiseOffset.z), 0, 1, -maxZ, maxZ);       
      float radianY = (atan2(pxz - z, stepSize.x) + atan2(z - nxz, stepSize.x)) / 2.0; 
      rotateY(radianY);
      float pyz = map(noise(w * noiseScale + noiseOffset.x + t, (h - stepSize.y) * noiseScale + noiseOffset.y + t, t + noiseOffset.z), 0, 1, -maxZ, maxZ); 
      float nyz = map(noise(w * noiseScale + noiseOffset.x + t, (h + stepSize.y) * noiseScale + noiseOffset.y + t, t + noiseOffset.z), 0, 1, -maxZ, maxZ);       
      float radianX = (atan2(z - pyz, stepSize.y) + atan2(nyz - z, stepSize.y)) / 2.0; 
      rotateX(radianX);
      rect(0, 0, rectSize.x, rectSize.y);
      popMatrix();
    } 
  }  
}