Stop Mesh rendering if it intersect

465 Views Asked by At

I am generating a camera frustum mesh through code and it is working fine.

Now I am searching for a solution (shader based or else) to restrict the camera frustum mesh if it intersects with any object. As you can see in the image, my frustum is passing through the plane which is not correct.

How can I control it? I searched and tried to apply different kinds of shaders but nothing seems to work.

enter image description here

1

There are 1 best solutions below

4
On

Get the frustum planes of the camera.

Plane[] frustumPlanes = GeometryUtility.CalculateFrustumPlanes(Camera.main); //Or whatever camera you are using

For each GameObject in the scene, get its Renderer component's bounds if any.

Renderer renderer = GetComponent<Renderer>();
if (renderer != null) 
{
  //get renderer.bounds
}

Test if the renderers bounds intersect with any of the frustum planes.

bool canSee = GeometryUtility.TestPlanesAABB(frustumPlanes, renderer.bounds);

if (canSee) 
{
  //do something
}

This should be a pretty good approximation of whether your camera can "see" the mesh (i.e if the frustum intersects with any object)