Unity相机与相机之间GameObject的坐标和旋转转换

通常我们会遇到这样的需求:将游戏场景的3D物体,放在UI坐标的某个位置。当游戏场景和UI使用的不同相机渲染,且需要有动画从游戏场景过渡到UI界面时(比如从游戏场景移动到UI上),此时需要将游戏相机视角的坐标和旋转,转换到UI相机视角下的坐标和旋转。

using UnityEngine;

public class CameraUtils {
    /// <summary>
    /// 将世界坐标tranferWorldPostion从fromCamera转换到toCamera坐标空间
    /// </summary>
    /// <param name="fromCamera">必须为透视相机</param>
    /// <param name="toCamera">必须为透视相机</param>
    /// <param name="tranferWorldPostion"></param>
    /// <returns></returns>
    public static Vector3 TransferPosition(Camera fromCamera, Camera toCamera, Vector3 tranferWorldPostion) {
        Vector3 worldPoint = tranferWorldPostion;
        Matrix4x4 view1 = fromCamera.transform.worldToLocalMatrix;
        Matrix4x4 projection1 = fromCamera.projectionMatrix;
        Matrix4x4 view2 = toCamera.transform.localToWorldMatrix;
        Matrix4x4 projection2 = toCamera.projectionMatrix;
        Vector4 viewPoint1 = view1 * new Vector4(worldPoint.x, worldPoint.y, worldPoint.z, 1.0f);
        Vector4 eyePoint = projection1.inverse * viewPoint1;
        Vector4 viewPoint2 = projection2 * eyePoint;
        Vector3 finalPoint = view2 * viewPoint2;
        return finalPoint;
    }


    /// <summary>
    /// 将世界旋转tranferWorldRotation从fromCamera转换到toCamera坐标空间
    /// </summary>
    /// <param name="fromCamera">必须为透视相机</param>
    /// <param name="toCamera">必须为透视相机</param>
    /// <param name="tranferWorldRotation"></param>
    /// <returns></returns>
    public static Quaternion TransferRotation(Camera fromCamera, Camera toCamera, Quaternion tranferWorldRotation) {
        Quaternion worldRotation = tranferWorldRotation; // 要转换的旋转
        Matrix4x4 viewMatrix1 = fromCamera.worldToCameraMatrix;
        Matrix4x4 viewMatrix2 = toCamera.worldToCameraMatrix;
        Quaternion cameraRotation = Quaternion.LookRotation(viewMatrix1.GetColumn(2), viewMatrix1.GetColumn(1));
        Quaternion localRotation = Quaternion.Inverse(cameraRotation) * worldRotation;
        Vector3 screenPos = fromCamera.projectionMatrix.MultiplyPoint(Vector3.zero);
        screenPos.z = Mathf.Lerp(fromCamera.nearClipPlane, fromCamera.farClipPlane, screenPos.z);
        Quaternion cameraRotation2 = Quaternion.LookRotation(viewMatrix2.GetColumn(2), viewMatrix2.GetColumn(1));
        Quaternion worldRotation2 = cameraRotation2 * localRotation;
        return worldRotation2;
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值