Version: 2017.1

Camera.CalculateFrustumCorners

切换到手册
public void CalculateFrustumCorners (Rect viewport, float z, Camera.MonoOrStereoscopicEye eye, Vector3[] outCorners);

参数

viewport 用于视锥体计算的标准化视口坐标。
z 从摄像机原点开始的 Z 深度(将在该位置计算四角)。
eye 要使用的摄像机眼投影矩阵。
outCorners 包含视锥体四角矢量的输出数组。不能为 null,且长度必须 >= 4。

描述

给定视口坐标,计算指向指定摄像机深度处视锥体四角的视图空间矢量。

用于有效地计算图像特效着色器中的像素在世界空间中的位置。请参阅全局雾效的标准资源实现。

using UnityEngine;

public class ExampleClass : MonoBehaviour { void Update() { // this example shows the different camera frustums when using asymmetric projection matrices (like those used by OpenVR).

var camera = GetComponent<Camera>(); Vector3[] frustumCorners = new Vector3[4]; camera.CalculateFrustumCorners(new Rect(0, 0, 1, 1), camera.farClipPlane, Camera.MonoOrStereoscopicEye.Mono, frustumCorners);

for (int i = 0; i < 4; i++) { var worldSpaceCorner = camera.transform.TransformVector(frustumCorners[i]); Debug.DrawRay(camera.transform.position, worldSpaceCorner, Color.blue); }

camera.CalculateFrustumCorners(new Rect(0, 0, 1, 1), camera.farClipPlane, Camera.MonoOrStereoscopicEye.Left, frustumCorners);

for (int i = 0; i < 4; i++) { var worldSpaceCorner = camera.transform.TransformVector(frustumCorners[i]); Debug.DrawRay(camera.transform.position, worldSpaceCorner, Color.green); }

camera.CalculateFrustumCorners(new Rect(0, 0, 1, 1), camera.farClipPlane, Camera.MonoOrStereoscopicEye.Right, frustumCorners);

for (int i = 0; i < 4; i++) { var worldSpaceCorner = camera.transform.TransformVector(frustumCorners[i]); Debug.DrawRay(camera.transform.position, worldSpaceCorner, Color.red); } } }

这些视锥体如下图所示: