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

Daily Creative Coding

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

ぐにゃぐにゃする球

/**
* wriggling sphere
* 
* @author aa_debdeb
* @date 2016/07/05
*/

float radious = 200;
int resolution = 40;
PVector noiseOffset;

void setup(){
  size(500, 500, P3D);
  fill(160);
  stroke(200);
  strokeWeight(1);
  mousePressed();
}

void mousePressed(){
  noiseOffset = new PVector(random(10000), random(10000), random(10000));
}

void draw(){
  background(60);
//  lights();
  translate(width / 2, height / 2, 0);
  rotateX(frameCount * 0.01);
  rotateY(frameCount * 0.02);
  rotateZ(frameCount * 0.03);
  float step = TWO_PI / resolution;
  for(int i = 0; i < resolution / 2; i++){
    for(int j = 0; j < resolution; j++){
      float r1 = step * i;
      float nr1 = step * (i + 1);
      float r2 = step * j;
      float nr2 = step * (j + 1);
      PVector p1 = new PVector(radious * sin(r1) * cos(r2), radious * cos(r1), radious * sin(r1) * sin(r2));
      PVector p2 = new PVector(radious * sin(nr1) * cos(r2), radious * cos(nr1), radious * sin(nr1) * sin(r2));
      PVector p3 = new PVector(radious * sin(nr1) * cos(nr2), radious * cos(nr1), radious * sin(nr1) * sin(nr2));
      PVector p4 = new PVector(radious * sin(r1) * cos(nr2), radious * cos(r1), radious * sin(r1) * sin(nr2));
//      float radious1 = radious + map(noise(p1.x + noiseOffset.x, p1.y + noiseOffset.y, p1.z + noiseOffset.z), 0, 1, -40, 40);
//      float radious2 = radious + map(noise(p2.x + noiseOffset.x, p2.y + noiseOffset.y, p2.z + noiseOffset.z), 0, 1, -40, 40);
//      float radious3 = radious + map(noise(p3.x + noiseOffset.x, p3.y + noiseOffset.y, p3.z + noiseOffset.z), 0, 1, -40, 40);
//      float radious4 = radious + map(noise(p4.x + noiseOffset.x, p4.y + noiseOffset.y, p4.z + noiseOffset.z), 0, 1, -40, 40);
      float t = frameCount * 0.03;
      float radious1 = radious + map(noise(p1.x + noiseOffset.x + t, p1.y + noiseOffset.y + t, p1.z + noiseOffset.z + t), 0, 1, -60, 60);
      float radious2 = radious + map(noise(p2.x + noiseOffset.x + t, p2.y + noiseOffset.y + t, p2.z + noiseOffset.z + t), 0, 1, -60, 60);
      float radious3 = radious + map(noise(p3.x + noiseOffset.x + t, p3.y + noiseOffset.y + t, p3.z + noiseOffset.z + t), 0, 1, -60, 60);
      float radious4 = radious + map(noise(p4.x + noiseOffset.x + t, p4.y + noiseOffset.y + t, p4.z + noiseOffset.z + t), 0, 1, -60, 60);
      PVector p1_ = new PVector(radious1 * sin(r1) * cos(r2), radious1 * cos(r1), radious1 * sin(r1) * sin(r2));
      PVector p2_ = new PVector(radious2 * sin(nr1) * cos(r2), radious2 * cos(nr1), radious2 * sin(nr1) * sin(r2));
      PVector p3_ = new PVector(radious3 * sin(nr1) * cos(nr2), radious3 * cos(nr1), radious3 * sin(nr1) * sin(nr2));
      PVector p4_ = new PVector(radious4 * sin(r1) * cos(nr2), radious4 * cos(r1), radious4 * sin(r1) * sin(nr2));      
      beginShape();
      vertex(p1_.x, p1_.y, p1_.z);
      vertex(p2_.x, p2_.y, p2_.z);
      vertex(p3_.x, p3_.y, p3_.z);
      vertex(p4_.x, p4_.y, p4_.z);
      endShape();
    }
  }
}