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

Daily Creative Coding

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

表面が流れる球

/**
* unstable sphere
*
* @author aa_debdeb
* @date 2016/09/05
*/

float scale = 100;
float phi = (1 + sqrt(5)) / 2.0;
float radious = sqrt(sq(1) + sq(phi));
ArrayList<Triangle> triangles;
float nScale = 0.2;
float tScale = 0.005;
PVector nOffset;

void setup(){
  size(500, 500, P3D);
  nOffset = new PVector(random(10000), random(10000), random(10000));

  PVector[] vertices = {new PVector(0, 1, phi),
                        new PVector(0, -1, phi),
                        new PVector(0, -1, -phi),
                        new PVector(0, 1, -phi),
                        new PVector(phi, 0, 1),
                        new PVector(-phi, 0, 1),
                        new PVector(-phi, 0, -1),
                        new PVector(phi, 0, -1),
                        new PVector(1, phi, 0),
                        new PVector(-1, phi, 0),
                        new PVector(-1, -phi, 0),
                        new PVector(1, -phi, 0)};
  triangles = new ArrayList<Triangle>();
  triangles.add(new Triangle(vertices[0], vertices[8], vertices[9]));
  triangles.add(new Triangle(vertices[0], vertices[9], vertices[5]));
  triangles.add(new Triangle(vertices[0], vertices[5], vertices[1]));
  triangles.add(new Triangle(vertices[0], vertices[1], vertices[4]));
  triangles.add(new Triangle(vertices[0], vertices[4], vertices[8]));
  triangles.add(new Triangle(vertices[1], vertices[5], vertices[10]));
  triangles.add(new Triangle(vertices[1], vertices[10], vertices[11]));
  triangles.add(new Triangle(vertices[1], vertices[11], vertices[4]));
  triangles.add(new Triangle(vertices[2], vertices[3], vertices[7]));
  triangles.add(new Triangle(vertices[2], vertices[7], vertices[11]));
  triangles.add(new Triangle(vertices[2], vertices[11], vertices[10]));
  triangles.add(new Triangle(vertices[2], vertices[10], vertices[6]));
  triangles.add(new Triangle(vertices[2], vertices[6], vertices[3]));
  triangles.add(new Triangle(vertices[3], vertices[6], vertices[9]));
  triangles.add(new Triangle(vertices[3], vertices[9], vertices[8]));
  triangles.add(new Triangle(vertices[3], vertices[8], vertices[7]));
  triangles.add(new Triangle(vertices[4], vertices[11], vertices[7]));
  triangles.add(new Triangle(vertices[4], vertices[7], vertices[8]));
  triangles.add(new Triangle(vertices[5], vertices[9], vertices[6]));
  triangles.add(new Triangle(vertices[5], vertices[6], vertices[10]));
 
  for(int i = 0; i < 4; i++){
    ArrayList<Triangle> nextTriangles = new ArrayList<Triangle>();
    for(Triangle t: triangles){
      nextTriangles.addAll(t.divide());
    }
    triangles = nextTriangles;
  }  
}

void draw(){
  background(255);
  translate(width / 2, height / 2);
  rotateY(frameCount * 0.001);
  for(Triangle t: triangles){
    t.display();
  }
}

class Triangle{
  
  PVector[] vertices;
  
  Triangle(PVector v0, PVector v1, PVector v2){
    vertices = new PVector[3];
    vertices[0] = v0;
    vertices[1] = v1;
    vertices[2] = v2;
  }
  
  void display(){
    
    stroke(255, 255, 255);
    fill(0, 206, 209);
    
    PVector center = new PVector(0, 0, 0);
    for(int i = 0; i < 3; i++){
      center.add(vertices[i]);
    }
    center.div(3);
    float time = frameCount * tScale;
    if(noise(center.x * nScale + time + nOffset.x, center.y * nScale + time + nOffset.y, center.z * nScale + time + nOffset.z) < 0.5){
    beginShape();
    for(int i = 0; i < 3; i++){
      vertex(vertices[i].x * scale, vertices[i].y * scale, vertices[i].z * scale);
    }
    endShape(CLOSE);
    }
  }
  
  ArrayList<Triangle> divide(){
    PVector[] midpoints = new PVector[3];
    for(int i = 0; i < 3; i++){
      int j = i != 2 ? i + 1: 0;
      PVector m = PVector.lerp(vertices[i], vertices[j], 0.5);
      m.normalize();
      m.mult(radious);
      midpoints[i] = m;
    }
    ArrayList<Triangle> triangles = new ArrayList<Triangle>();
    
    triangles.add(new Triangle(vertices[0], midpoints[0], midpoints[2]));
    triangles.add(new Triangle(vertices[1], midpoints[1], midpoints[0]));
    triangles.add(new Triangle(vertices[2], midpoints[2], midpoints[1]));
    triangles.add(new Triangle(midpoints[0], midpoints[1], midpoints[2]));   
    return triangles;
  }
}
f:id:aa_debdeb:20160904180025j:plain