gpt4 book ai didi

c# - 如何找到相机视野所包含的区域?

转载 作者:行者123 更新时间:2023-11-30 16:50:14 25 4
gpt4 key购买 nike

鉴于相机的视角是一个金字塔形状,如何找到这个金字塔包含的区域?

最佳答案

这是一个使用相机视口(viewport)而不是屏幕的简单脚本。这是一种更通用的方法:

using UnityEngine;
using System.Collections.Generic;

public class MyCameraUtilityScript : MonoBehaviour
{
public List<Vector3> GetCameraFrustrumCorners()
{
List<Vector3> corners = new List<Vector3>(8); // 8 corners

Camera c = GetComponent<Camera>();

// corners counterclockwise at near clip plane
corners.Add(c.ViewportToWorldPoint(new Vector3(0.0f, 0.0f, c.nearClipPlane))); // bottom-left
corners.Add(c.ViewportToWorldPoint(new Vector3(1.0f, 0.0f, c.nearClipPlane))); // bottom-right
corners.Add(c.ViewportToWorldPoint(new Vector3(1.0f, 1.0f, c.nearClipPlane))); // top-right
corners.Add(c.ViewportToWorldPoint(new Vector3(0.0f, 1.0f, c.nearClipPlane))); // top-left

// corners counterclockwise at far clip plane
corners.Add(c.ViewportToWorldPoint(new Vector3(0.0f, 0.0f, c.farClipPlane))); // bottom-left
corners.Add(c.ViewportToWorldPoint(new Vector3(1.0f, 0.0f, c.farClipPlane))); // bottom-right
corners.Add(c.ViewportToWorldPoint(new Vector3(1.0f, 1.0f, c.farClipPlane))); // top-right
corners.Add(c.ViewportToWorldPoint(new Vector3(0.0f, 1.0f, c.farClipPlane))); // top-left

return corners;
}
}

关于c# - 如何找到相机视野所包含的区域?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35346015/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com