2025年4月10日 星期四

晚安week08

 。3D模型

1.讀入模型loadShape();
//week08_01_loadshape_3Dmodel
size(500,500,P3D);
PShape gundam = loadShape("Gundam.obj");//讀入模型
translate(width/2,height/2);
pushMatrix();
  translate(0,100);
  scale(10, -10, 10); //放大十倍
  //建模軟體座標系統與processing不同,Y軸正負相反導致模型讀入後會上下顛倒
  shape(gundam,0,0);//畫出讀入的shape
popMatrix();
在建模軟體(如Maya,Blender)中,座標系統的原點(0,0,0)在整個空間的正中心
processing原點則位於視窗的左上角,Y軸向下為正,導致讀入時上下相反

執行結果:












2.讓鋼彈轉起來
//week08_02_gundam_rotateY
//轉動
PShape gundam;
void setup(){
  size(500,500,P3D);
  gundam = loadShape("Gundam.obj");
}
void draw(){
  background(125);
translate(width/2,height/2);
pushMatrix();
  translate(-50,100);
  rotateY(radians(frameCount));
  scale(10, -10, 10); 
  shape(gundam,0,0);
popMatrix();
pushMatrix();
  translate(100,100);
  rotateY(radians(frameCount*30));
  scale(10, -10, 10); 
  shape(gundam,0,0);
popMatrix();
}
執行結果:











3.網路上抓來的obj檔案
//week08_03_another_obj
//轉動
PShape model;
void setup(){
  size(500,500,P3D);
  model = loadShape("FinalBaseMesh.obj");
}
void draw(){
  background(255);
  lights();
translate(width/2,height/2);
pushMatrix();
  translate(0,100);
  rotateY(radians(frameCount));
  scale(10, -10, 10); 
  shape(model,0,0);
popMatrix();
}
執行結果:














4.組裝模型,結合上週的T-R-T
//week08_04_gundam_head_rotate
PShape body, head;
void setup() {
  size(500, 500, P3D);
  body = loadShape("body.obj");
  head = loadShape("head.obj");
}
void draw() {
  background(255);
  translate(width/2, height/2+100);
  sphere(10);//基準點
  scale(10, -10, 10);
  pushMatrix();
    translate(0, 22.5);//原來的位置
    //translate(x/10, y/10);
    rotateX(radians(frameCount*10));//轉動
    translate(0, -22.5); //放在x,y的座標
    shape(head, 0, 0);
  popMatrix();
  shape(body, 0, 0);
}
float x=0, y=0;
void mouseDragged() {
  x+= mouseX - pmouseX;
  y+= mouseY - pmouseY;
  print(x/10,y/10);
}

執行結果:


沒有留言:

張貼留言