Game Development Reference
In-Depth Information
From lines 10-17, the GeometryUtility class is used to generate an array of
plane objects that describe the camera frustum. Planes are to 3D space what
lines are to 2D space; they mark out a flat, imaginary surface in 3D. The frustum
planes are a collection of six planes that are rotated and aligned in 3D space to
represent the complete trapezoidal camera frustum. This array is then used by
the TestPlanesAABB function, Axially Aligned Bounding Box ( AABB ), which
determines whether the collision boundary of a mesh renderer exists inside the
frustum as defined by the planes.
Frustum testing - points
Of course, you don't always want to test renderers for visibility. Instead, you might
simply want to test for a point. This might be for two main reasons. First, you
might want to know whether an object, such as a particle or a gun target location,
is actually visible. Second, you might not only want to know whether a point is
visible but also where in the screen space; this will be rendered by the camera.
The following code sample 5-5 will do this. It will test whether a point is within
the camera frustum, and if so, it would further return where the point would be
rendered on screen in a normalized viewport space (between 1-0).
//---------------------------------------------------------
//Determines if point is within frustum of camera
//Returns true if point is within frustum, else false
//The out param ViewPortLoc defines the location
public static bool IsPointInFrustum(Vector3 Point, Camera Cam, out
Vector3 ViewPortLoc)
{
//Create new bounds with no size
Bounds B = new Bounds(Point, Vector3.zero);
//Construct frustum planes from camera
//Each plane represents one wall of frustrum
Plane[] planes =
GeometryUtility.CalculateFrustumPlanes(Cam);
//Test whether point is within frustum planes
bool IsVisible = GeometryUtility.TestPlanesAABB(planes,
B);
 
Search WWH ::




Custom Search