GeometryUtility.TestPlanesAABB 测试平面

static function TestPlanesAABB (planes : Plane[], bounds : Bounds) : bool

Description描述

Returns true if bounds are inside the plane array.

如果边界框在平面数组内部返回true。

Will return true if the bounding box is inside the planes or intersects any of the planes.

如果边界框在平面数组内部或和任意平面相交,返回true。

The TestPlanesAABB function uses the Plane array to test whether a bounding box is in the frustum or not. You can use this function with CalculateFrustrumPlanes to test whether a camera's view contains an object regardless of whether it is rendered or not.

TestPlanesAABB函数使用Plane数组来测试边界框是否在视景体。你可以使用这个函数用CalculateFrustrumPlanes去测试相机视图是否包含一个物体,无论它是否被渲染。

参考:GeometryUtility.CalculateFrustumPlanes.

using UnityEngine;
using System.Collections;

public class example : MonoBehaviour {
	public GameObject anObject;
	private Camera cam;
	private Plane[] planes;
	void Start() {
		cam = Camera.main;
		planes = GeometryUtility.CalculateFrustumPlanes(cam);
	}
	void Update() {
		if (GeometryUtility.TestPlanesAABB(planes, anObject.collider.bounds))
			Debug.Log(anObject.name + " has been detected!");
		else
			Debug.Log("Nothing has been detected");
	}
}
// Detects manually if anObject is being seen by the main camera
//手工检测,如果一个物体由主摄像机看到
var anObject : GameObject;
private var cam : Camera;
private var planes : Plane[];

function Start() {
	cam = Camera.main;
	planes = GeometryUtility.CalculateFrustumPlanes(cam);
}

function Update() {
	if(GeometryUtility.TestPlanesAABB(planes,anObject.collider.bounds))
		Debug.Log(anObject.name + " has been detected!");
	else
		Debug.Log("Nothing has been detected");
}
最后修改:2011年1月5日 Wednesday 16:45

本脚本参考基于Unity 3.4.1f5

英文部分版权属©Unity公司所有,中文部分© Unity圣典 版权所有,未经许可,严禁转载 。