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

Daily Creative Coding

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

グラデーションの球

3D
/**
* gradational sphere
*
* @author aa_debdeb
* @date 2016/09/06
*/

float scale = 100;
float phi = (1 + sqrt(5)) / 2.0;
float radious = sqrt(sq(1) + sq(phi));
ArrayList<Triangle> triangles;
color[][] cols;

void mousePressed(){
  cols = new color[3][2];
  for(int i = 0; i < 3; i++){
    for(int j = 0; j < 2; j++){
      cols[i][j] = color(random(255), random(255), random(255));
    }
  }
}

void setup(){
  size(500, 500, P3D);
  mousePressed();

  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 < 3; i++){
    ArrayList<Triangle> nextTriangles = new ArrayList<Triangle>();
    for(Triangle t: triangles){
      nextTriangles.addAll(t.divide());
    }
    triangles = nextTriangles;
  }  
}

void draw(){
  background(0);
  translate(width / 2, height / 2);
  rotateX(frameCount * 0.01);
  rotateY(frameCount * 0.02);
  rotateZ(frameCount * 0.03);
  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);
//    strokeWeight(0.3);
    
    PVector center = new PVector(0, 0, 0);
    for(int i = 0; i < 3; i++){
      center.add(vertices[i]);
    }
    center.div(3);
    float r = (map(center.x, -radious, radious, red(cols[0][0]), red(cols[0][1])) + map(center.y, -radious, radious, red(cols[1][0]), red(cols[1][1])) + map(center.z, -radious, radious, red(cols[2][0]), red(cols[2][1]))) / 3.0;
    float g = (map(center.x, -radious, radious, green(cols[0][0]), green(cols[0][1])) + map(center.y, -radious, radious, green(cols[1][0]), green(cols[1][1])) + map(center.z, -radious, radious, green(cols[2][0]), green(cols[2][1]))) / 3.0;
    float b = (map(center.x, -radious, radious, blue(cols[0][0]), blue(cols[0][1])) + map(center.y, -radious, radious, blue(cols[1][0]), blue(cols[1][1])) + map(center.z, -radious, radious, blue(cols[2][0]), blue(cols[2][1]))) / 3.0;
    fill(r, g, b);
    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:20160904222524j:plain