unity相机锥形视角(剪裁空间)平移

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ControlCamera : MonoBehaviour
{
    private Rect viewRect = new Rect(0, 0, 1, 1);
    private Vector3[] frustumCorners = new Vector3[4];
    //相机锥形视角(剪裁空间)平移
    private Vector3 OffsetPosByUV(float offsetZ, Vector2 uv)
    {
        //offsetZ物体距离相机的Z轴距离
        if (Mathf.Approximately(uv.x, 0.5f) && Mathf.Approximately(uv.y, 0.5f))
            return Vector3.zero;

        Camera.main.CalculateFrustumCorners(viewRect, offsetZ,
            Camera.MonoOrStereoscopicEye.Mono, frustumCorners);
        float x = Mathf.Lerp(frustumCorners[2].x, frustumCorners[0].x, uv.x);
        float y = Mathf.Lerp(frustumCorners[1].y, frustumCorners[0].y, uv.y);
        return this.transform.TransformVector(new Vector3(x, y, 0));
    }

    // Start is called before the first frame update
    void Start()
    {
   
    }

    public Vector3 lookPosAt;
    public Vector2 offsetUV = Vector2.one / 2f;
    public float offizeZ;
    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            transform.position = lookPosAt + OffsetPosByUV(offizeZ, offsetUV);
        }

     

    }
    public Transform UIWorldPositionTra;
}

你可能感兴趣的:(unity,数码相机,游戏引擎)