unity, 相机空间 与 相机gameObject的局部空间
在unity里 相機空間 與 相機gameObject的局部空間 不重合。
Camera.worldToCameraMatrix的文檔中有這樣一句話:
Note that camera space matches OpenGL convention: camera's forward is the negative Z axis. This is different from Unity's convention, where forward is the positive Z axis.
?
意思是說unity中相機gameObject的藍軸是相機空間的-Z。
為了確認,做了如下實驗:
如圖,立方體的坐標是(0,0,0),相機的坐標是(0,0,-3),我們要計算并輸出立方體在相機空間的坐標。
做法是為立方體添加如下腳本:
using?UnityEngine;
using?System.Collections;
public?class?printPosInCameraSpace?:?MonoBehaviour?{
????public?GameObject?m_cameraRef;
????//?Use?this?for?initialization
????void?Start?()?{
????????Matrix4x4?worldToCameraMat?= m_cameraRef.GetComponent<Camera>().worldToCameraMatrix;
????????Vector3?thisPosInWorld?=?transform.position;
????????Vector3?thisPosInCamera?=?worldToCameraMat.MultiplyPoint?(thisPosInWorld);
????????Debug.Log?(thisPosInCamera);
????}
}
并在編輯器中將camera賦給m_cameraRef.
然后運行腳本,得到輸出結果為:(0,0,-3)。
這說明相機gameObject的藍軸確實是相機空間-Z軸。
進一步實驗:
將腳本改為:
using?UnityEngine;
using?System.Collections;
public?class?printPosInCameraGameObjectLocalSpace?:?MonoBehaviour?{
????public?GameObject?m_cameraRef;
????//?Use?this?for?initialization
????void?Start?()?{
????????Matrix4x4?worldToCameraMat?= m_cameraRef.transform.worldToLocalMatrix;
????????Vector3?thisPosInWorld?=?transform.position;
????????Vector3?thisPosInCamera?=?worldToCameraMat.MultiplyPoint?(thisPosInWorld);
????????Debug.Log?(thisPosInCamera);
????}
}
輸出結果為:(0,0,3)。
?
--結論:
相機空間 和 相機gameObject的局部空間 是不重合的。圖中這三個坐標軸表示的是 相機gameObject的局部空間。 而 相機空間 則Z軸方向與之相反。
Camera.worldToCameraMatrix是 世界空間to相機空間 矩陣。Camera.transform.worldToLocalMatrix是 世界空間to相機gameObject的局部空間 矩陣,兩個矩陣是不一樣的。
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的unity, 相机空间 与 相机gameObject的局部空间的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CheckBox as Image us
- 下一篇: AngularJS入门心得2——何为双向