zoukankan      html  css  js  c++  java
  • 【转】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
    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矩阵时对他们取反。

  • 相关阅读:
    时间戳(1532249295.179) 转日期格式(2018/07/22 16:48:15 179)
    iscroll.js右侧可滑动的菜单,点击每个菜单都会出现本菜单的详情
    canvas绘制的文字如何换行
    移动端H5页面禁止长按复制和去掉点击时高亮
    一列宽度不缩放,一列宽度弹性缩放,且超出后显示省略号
    js钩子机制(hook)
    mCustomScrollbar.js 漂亮的滚动条插件 适应内容自动更新
    axios.js 实例 -----$.ajax的替代方案
    用 async/await 来处理异步实例
    C#入门经典第18章-WEB编程
  • 原文地址:https://www.cnblogs.com/timeObjserver/p/11901101.html
Copyright © 2011-2022 走看看