简体   繁体   English

在Unity3D中将点从世界转换为相机空间

[英]Convert a point from world to camera space in Unity3D

I want to convert a point in world coordinates ( P_w ) to camera coordinates ( P_c ). 我想将世界坐标( P_w )中的点转换为相机坐标( P_c )。

To do so I first have to translate P_w by the negative camera position (in world coordinates C_pos ) and after that rotate P_w - C_pos x degrees around the x-axis, y degrees around the y-axis and z degrees around the z-axis. 这样做,我第一次来翻译P_w由负摄像机位置(在世界坐标C_pos ),之后转动P_w - C_pos X绕X轴度,Y度绕Y轴和Z度绕Z轴。

I wrote this function in Unity to do this: 我在Unity中编写了此功能来做到这一点:

static public Vector4 convertWorldToCameraCoordinatesByDegrees (Vector3 point, PhotoData photoData)
{
    // translate the point by the negative camera-offset 
    //and convert to Vector4
    Vector4 translatedPoint = point - photoData.cameraPosition;
    // by default translatedPoint.w is 0
    translatedPoint.w = 1.0f;       

    // create rotation matrices
    Matrix4x4 rotationMatrixX = Matrix4x4.identity;
    Matrix4x4 rotationMatrixY = Matrix4x4.identity;
    Matrix4x4 rotationMatrixZ = Matrix4x4.identity;

    // setup rotationMatrixX
    rotationMatrixX.SetRow (1, 
        new Vector4 (0,
            Mathf.Cos (photoData.rotation.x),
            - Mathf.Sin (photoData.rotation.x),
            0));
    rotationMatrixX.SetRow (2,
        new Vector4 (0,
            Mathf.Sin (photoData.rotation.x),
            Mathf.Cos (photoData.rotation.x),
            0));

    // setup rotationMatrixY
    rotationMatrixY.SetRow (0, 
        new Vector4 (Mathf.Cos (photoData.rotation.y),
             0,
             Mathf.Sin (photoData.rotation.y),
             0));
    rotationMatrixY.SetRow (2,
        new Vector4 (- Mathf.Sin (photoData.rotation.y),
            0,
            Mathf.Cos (photoData.rotation.y),
            0));                            

    // setup rotationMatrixZ
    rotationMatrixZ.SetRow (0, 
        new Vector4 (Mathf.Cos (photoData.rotation.z),
            - Mathf.Sin (photoData.rotation.z),
            0,
            0));
    rotationMatrixZ.SetRow (1,
        new Vector4 (Mathf.Sin (photoData.rotation.z),
            Mathf.Cos (photoData.rotation.z),
            0,
            0));

    // unity doc says z-x-y in this order
    Matrix4x4 rotationMatrix = rotationMatrixY * rotationMatrixX * rotationMatrixZ;

    Vector4 transformedPoint = rotationMatrix * translatedPoint;

    return transformedPoint;
}

But the results are different from another function where build my roations matrix using the up, forward and right vector of my camera. 但是结果与另一个函数不同,在另一个函数中,我使用摄像机的上,前,右矢量来构建我的旋转矩阵。

PhotoData is a class that has some information about the camera including the position and the rotation. PhotoData是一类,具有有关相机的一些信息,包括位置和旋转。 PhotoData.rotation is a Vector3 which has the camera rotation around the x, the y, and the z axis as x-, y-, z-values. PhotoData.rotation是一个Vector3 ,它使相机绕x,y和z轴旋转为x,y,z值。

Can someone tell me why this is not working? 有人可以告诉我为什么这不起作用吗?

// edit: Some people have mentioned build-in functions, but I have to compute this with out them. //编辑:有些人提到了内置函数,但是我必须在不使用它们的情况下对其进行计算。 I forgot to write it in the original post. 我忘了在原始帖子中写它。 I wrote this algorithm first in c++ and wanted to test it in Unity since it's easier to verify it. 我首先用c ++编写了该算法,并希望在Unity中对其进行测试,因为它更易于验证。

Looking at your code and think you are massively overcomplicating simple task, or I do not understand the question. 查看您的代码,以为您使简单的任务复杂化了很多,否则我不明白这个问题。 You can definitely use Camera.WorldToScreenPoint 您绝对可以使用Camera.WorldToScreenPoint

http://docs.unity3d.com/ScriptReference/Camera.WorldToScreenPoint.html http://docs.unity3d.com/ScriptReference/Camera.WorldToScreenPoint.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM