日常の進捗

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

#Codevember day16: Star

最近どうも思ったように時間が取れず,こういう自分の楽しみを納得いくところまで作業が進められない.それはそれで仕方がない部分もありつつ,違う楽しさを見つけるような感じにもなってきている.3Dの立体視はカメラつまり視点からの距離で赤/緑などの色をずらす必要がある.

let stars = [];

function setup() {
  createCanvas(windowWidth, windowHeight, WEBGL);

  for (let i = 0; i < 300; i++) {
    let x = randomNormal(-width / 2, width / 2);
    let y = randomNormal(-height / 2, height / 2);
    let z = randomNormal(-2000, 2000);
    stars[i] = new Star(x, y, z);
  }
}

function randomNormal(_min, _max) {
  let n = random(_min, _max) + random(_min, _max) + random(_min, _max) + random(_min, _max) + random(_min, _max);
    return n / 5;
}

function draw() {
  background(0,0,0);
    push();
    rotateZ(frameCount*0.004);      
  for (let i = 0; i < stars.length; i++) {
    stars[i].update();
    stars[i].draw();
  }
    pop();
}

class Star {
  constructor(_x, _y, _z) {
    this.x = _x;
    this.y = _y;
    this.z = _z;
    let sd = 5;
    this.w = random(sd/2,sd*2);
    this.h = random(sd/2,sd*2);
    this.d = random(sd/2,sd*2);
  }
  update() {
    this.z += 5;
    if (this.z > 1000) {
      this.z -= 2000;
    }
  }

  draw() {
    let offset = 1;
    
    push();
    translate(this.x-offset, this.y-offset, this.z);
    rotateX((this.x + frameCount)/90);
    rotateY((this.y + frameCount)/80);
    rotateZ((this.z + frameCount)/1000);
    noFill();
    stroke(0,255,0);
    strokeWeight(0.5);
    box(this.w,this.h,this.d);    
    pop();

    push();
    translate(this.x+offset, this.y+offset, this.z);
    rotateX((this.x + frameCount)/90);
    rotateY((this.y + frameCount)/80);
    rotateZ((this.z + frameCount)/1000);
    noFill();
    stroke(255,0,0);
    strokeWeight(0.5);
    box(this.w,this.h,this.d); 
    pop();
    
  }
}