日常の進捗

主に自分のための,行為とその習慣化の記録

Mod:Coding Challenge #26: 3D Supershapes

f:id:takawo:20170927003139g:plain

スーパーシェイプの3D版。前回の続きを自分なりに書き換えていったが、展開がうまく言ってない気がするというか、登録する頂点と頂点の接続がうまくつながってない。

Supershapes / SuperFormula

コード

float a = 1;
float b = 1;
float m = 0.1;
float freq = 0;

float scale = 400;
float incAngleA = 10;
float incAngleB =1;  
float offset = 0;

// setup関数 : 初回1度だけ実行される
void setup() {
  size(960, 540, P3D); // ウィンドウサイズを960px,540pxに
  colorMode(HSB, 360, 100, 100); // HSBでの色指定にする
  smooth(); // 描画を滑らかに
}

// draw関数 : setup関数実行後繰り返し実行される
void draw() {
  background(0, 0, 0);
  translate(width/2, height/2, -500);
  rotateX(frameCount*0.01);
  rotateY(frameCount*0.0035);

  m = map(sin(freq), -1, 1, 0, 7);
  freq = freq + 0.02; 
  beginShape(TRIANGLE_STRIP);

  for (float angleB = -180; angleB < 180; angleB += incAngleB) {
    float thetaB = radians(angleB);
    float r2 = superShape(thetaB, m, 0.2, 1.7, 1.7);
    for (float angleA = 0; angleA <= 360; angleA += incAngleA) {
      float thetaA = radians(angleA);
      fill(((angleB+angleA+frameCount))%360, 80, 100);
      noStroke();
      float r1 = superShape(thetaA, m, 0.2, 1.7, 1.7);
      float x = cos(thetaA) * r1 * cos(thetaB) * r2;
      float y = sin(thetaA) * r1 * cos(thetaB) * r2;
      float z = sin(thetaB) * r2;
      vertex(x*scale, y*scale, z*scale);
      if (angleB < 180-incAngleB) {
        float px = cos(thetaA) * r1 * cos(thetaB+radians(incAngleB))* r2;        
        float py = sin(thetaA) * r1 * cos(thetaB+radians(incAngleB))* r2;
        float pz = sin(thetaB+radians(incAngleB)) * r2;
        vertex(px*scale, py*scale, pz*scale);
      }
    }
  }
  endShape(CLOSE);
  offset += 5;
}

float superShape(float theta, float m, float n1, float n2, float n3) {
  float t1 = abs((1/a)*cos(m * theta / 4));
  t1 = pow(t1, n2);
  float t2 = abs((1/b)*sin(m * theta/4));
  t2 = pow(t2, n3);
  float t3 = t1 + t2;
  float r = pow(t3, - 1 / n1);
  return r;
}

リファレンス