Game Development Reference
In-Depth Information
Frustum testing - renderers
There are many times when the Unity native camera events, as we saw earlier,
are not sufficient for your visibility and frustum-testing requirements. Specifically,
you might simply want to test whether just one specific camera can see a renderer,
whether an invisible object would be seen if it were visible, whether a specified point
in space is seen by the camera, or whether a camera would see a specific object if it
were moved to a new location. All of these cases can be important visibility tests in
different situations, and all of them require some degree of manual testing. To meet
these camera visibility needs, we'll need to code more intensively. The functions in
the following sections will be compiled together as static functions in a dedicated
CamUtility class. Let's start by creating a function to test whether a specific renderer
component is within the frustum of a specific Camera object, as shown in the
following code sample 5-4:
01 using UnityEngine;
02 using System.Collections;
03 //---------------------------------------------------------
04 public class CamUtility
05 {
06 //---------------------------------------------------------
07 //Function to determine whether a renderer is within frustum of
a specified camera
08 //Returns true if renderer is within frustum, else false
09 public static bool IsRendererInFrustum(Renderer Renderable,
Camera Cam)
10 {
11 //Construct frustum planes from camera
12 //Each plane represents one wall of frustrum
13 Plane[] planes =
GeometryUtility.CalculateFrustumPlanes(Cam);
14
15 //Test whether renderable is within frustum planes
16 return GeometryUtility.TestPlanesAABB(planes,
Renderable.bounds);
17 }
18 //---------------------------------------------------------
19 }
 
Search WWH ::




Custom Search