Ejemplo n.º 1
0
    /// <summary>
    /// 设置相机的参数看向目标包围盒
    /// </summary>
    public void SetCameraLookToTarget()
    {
        //相机旋转朝向目标对象
        Bounds  bounds       = targetRenderer.bounds;
        Vector3 boundsCenter = bounds.center;

        targetCamera.transform.LookAt(boundsCenter);
        //包围盒角点
        Vector3[] points = FuncUtil.GetBoundsCorners(boundsCenter, bounds.extents);
        //所有角点投射到平面
        Vector3 planeNormal = boundsCenter - targetCamera.transform.position;

        FuncUtil.WorldPointsToPlane(points, points.Length, planeNormal);
        //平面中心
        Vector3 planeCenter = Vector3.ProjectOnPlane(boundsCenter, planeNormal);
        //取平面上各个点与平面中心的最大距离作为相机的视野矩形框大小
        float halfHeight = GetMaxDistanceToPlaneCenter(points, points.Length, planeCenter);
        //相机与包围盒中心的距离(世界坐标为单位)
        float distance = Vector3.Distance(boundsCenter, targetCamera.transform.position);

        //得到视野大小
        targetCamera.fieldOfView = Mathf.Atan2(halfHeight, distance) * Mathf.Rad2Deg * 2;
    }