【转】UNITY中相机空间,投影空间的正向问题
原文鏈接1:https://www.cnblogs.com/wantnon/p/4570188.html??
原文鏈接2:https://www.cnblogs.com/hefee/p/3820610.html?
?
在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的局部空間 矩陣,兩個矩陣是不一樣的。
?
————————————————————————————————————————————————————————————
?
透視投影
OpenGL透視視錐體與NDC
在透視投影中,截棱錐體(觀察坐標)中的3D點會被映射到立方體(NDC)中。x坐標的范圍從[l,f]到[-1,1],y坐標的范圍從[b,t]到[-1,1],z坐標的范圍從[n,f]到[-1,1]。
注意,觀察坐標為右手坐標系,NDC使用左右坐標系。也就是說,位于原點的照相機在觀察坐標中看向-Z軸,而在NDC中看向+Z軸。因為glFrustum()只接收正的近平面與遠平面距離值,我們需要在構建GL_PROJECTION矩陣時對他們取反。
?
總結
以上是生活随笔為你收集整理的【转】UNITY中相机空间,投影空间的正向问题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 思考:延迟渲染与 Z-PRE PASS的
- 下一篇: 转载 用ShadowVolume画模型的