[計算機圖形學04]Unity中的仿射變換和透視變換

模型變換

首先咱們能夠看一下在Unity中如何用腳本旋轉一個GameObject,其中一個可行的辦法就是以下。app

void Awake() 
{ 
    cube1 = GameObject.CreatePrimitive(PrimitiveType.Cube); 
    cube1.transform.position = new Vector3(0.75f, 0.0f, 0.0f); 
    cube1.transform.Rotate(90.0f, 0.0f, 0.0f, Space.Self); 
    cube1.GetComponent<Renderer>().material.color = Color.red; 
    cube1.name = "Self"; 
    cube2 = GameObject.CreatePrimitive(PrimitiveType.Cube); 
    cube2.transform.position = new Vector3(-0.75f, 0.0f, 0.0f); 
    cube2.transform.Rotate(90.0f, 0.0f, 0.0f, Space.World); 
    cube2.GetComponent<Renderer>().material.color = Color.green; 
    cube2.name = "World"; 
} 
void Update() 
{ 
    cube1.transform.Rotate(xAngle, yAngle, zAngle, Space.Self); 
    cube2.transform.Rotate(xAngle, yAngle, zAngle, Space.World); 
}

其中用到的函數就是Rotate,它的原型以下:函數

public void Rotate(Vector3 eulers, Space relativeTo = Space.Self);
public void Rotate(float xAngle, float yAngle, float zAngle, Space relativeTo = Space.Self);
The implementation of this method applies a rotation of zAngle degrees around the z axis, xAngle degrees around the x axis, and yAngle degrees around the y axis ( in that order).

根據這個函數的描述,咱們能夠知道,Unity實現這個函數是經過先繞z軸旋轉,而後繞x軸旋轉,最後繞y軸旋轉。也就是:this

$$ P'= M_{rotateY}M_{rotateX}M_{rotateZ}P $$code

從模型空間轉換到世界空間須要考慮Transform的繼承結構。若不考慮父節點,依循先縮放、在旋轉、最後平移的原則,模型空間的某點應當以以下方式換算:也就是:orm

$$ P'= M_{translate}M_{rotateY}M_{rotateX}M_{rotateZ}M_{scale}P $$繼承

若是Transform仍有父親,則繼續按照如上原則計算,直至抵達世界空間。原型

觀察變換

觀察變換須要定義攝像機。一般,攝像機有三個參數,即EyeLookAt和Up。 Eye指攝像機在世界空間中位置的座標;LookAt指攝像機在世界空間觀察的位置的座標;Up則指在世界空間中,近似於攝像機朝上的方向份量,一般定義爲世界座標系的y軸。給定這三個參數便可定義觀察空間,觀察空間的原點位於Eye處。這個空間能夠用如下表示:it

$$ \{U,V,N\} $$io

分別對應x,y,z座標軸。在觀察空間中,攝像機位於原點位置且指向-N,即攝像機的觀察方向,也稱爲朝前方向(forward)爲-Nform

$$ N= \frac{Eye-LookAt}{\mid\mid Eye-LookAt \mid\mid} $$

$$ U = \frac{Up \times N}{\mid\mid Up \times N\mid\mid} $$

$$ V = N \times U $$

觀察矩陣及其推導過程

相關文章
相關標籤/搜索