# Calculate camera frustum intersection with plane

We have play area composed of hex gameobjects. We only display/load what is visible. Impossible to load large maps at one time. We want to calculate the polygon intersection with the game board plane. We cannot load every possible game object and test if in the frustum or ray cast to the screen.

We need custom code to see which gameobject positions are in the visibility polygon?
How do you get the corner rays of the camera frustum?

As for the :

How do you get the corner rays of the camera frustum?

To get the ray of each corner you can simply use camera.ViewportPointToRay.

``````Ray bottomLeft = camera.ViewportPointToRay(new Vector3(0, 0, 0));
Ray topLeft = camera.ViewportPointToRay(new Vector3(0, 1, 0));
Ray topRight = camera.ViewportPointToRay(new Vector3(1, 1, 0));
Ray bottomRight = camera.ViewportPointToRay(new Vector3(1, 0, 0));
``````

If your plane is on a fixed height you perhaps don’t even have to raycast. As a method like this should return the rays intersection on a given y position. (I used this method because I needed a similar thing, but I didn’t have a collider to raycast against as I used Graphics.DrawMesh).

``````public static Vector3 GetPointAtHeight(Ray ray, float height)
{
return ray.origin + (((ray.origin.y - height) / -ray.direction.y) * ray.direction);
}
``````

Note that this method can thow a division by zero if ray.direction.y is 0.

I think Unity performs its frustum culling by default so you should not worry about it. Also keep in mind that if the objects are very large culling is less efective because when a vertex gets into the frustum the entire object is rendered.
If you still want to get dirt with frustum culling, this couple of functions may interest you:

GeometryUtility.CalculateFrustumPlanes

GeometryUtility.TestPlanesAABB

regards