camera.CalculateFrustumCorners()

作者:追风剑情 发布于:2023-4-26 17:10 分类:Unity3d

示例:计算近裁剪面四个角的坐标

using UnityEngine;

public class Example : MonoBehaviour
{
    void OnDrawGizmosSelected()
    {
        //摄像机近裁剪面距离
        float nearClipPlane = Camera.main.nearClipPlane;
        //定义视口
        Rect viewport = new Rect(0, 0, 1, 1);
        //存放计算结果
        Vector3[] outCorners = new Vector3[4];
        //计算摄像机近裁剪面四个角的坐标
        Camera.main.CalculateFrustumCorners(viewport, nearClipPlane, Camera.MonoOrStereoscopicEye.Mono, outCorners);
        for (int i = 0; i < outCorners.Length; i++)
        {
            //将坐标从摄像机空间转到世界空间
            var worldSpacePos = Camera.main.transform.TransformPoint(outCorners[i]);
            //在Scene场景中绘制小球
            Gizmos.color = Color.red;
            Gizmos.DrawSphere(worldSpacePos, 0.02F);
        }
    }
}

11111.png

标签: Unity3d

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号