鸟语天空
camera.CalculateFrustumCorners()
post by:追风剑情 2023-4-26 17:10

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

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

评论:
发表评论:
昵称

邮件地址 (选填)

个人主页 (选填)

内容