Disabling Frustum Culling on a Game Object in Unity

You should never disable frustum culling in a release build.

But sometimes it can be useful to do so for debugging or when dealing with a really wacky vertex shader where mesh bounds don’t make sense anymore. Here’s an easy way to disable frustum culling on a game object by moving its bounds into the center of the camera’s frustum:

// boundsTarget is the center of the camera's frustum, in world coordinates:
Vector3 camPosition = camera.transform.position;
Vector3 normCamForward = Vector3.Normalize(camera.transform.forward);
float boundsDistance = (camera.farClipPlane - camera.nearClipPlane) / 2 + camera.nearClipPlane;
Vector3 boundsTarget = camPosition + (normCamForward * boundsDistance);

// The game object's transform will be applied to the mesh's bounds for frustum culling checking.
// We need to "undo" this transform by making the boundsTarget relative to the game object's transform:
Vector3 realtiveBoundsTarget = this.transform.InverseTransformPoint(boundsTarget);

// Set the bounds of the mesh to be a 1x1x1 cube (actually doesn't matter what the size is)
Mesh mesh = GetComponent().mesh;
mesh.bounds = new Bounds(realtiveBoundsTarget, Vector3.one);

[Download C# Unity Script Component]

Tags: , , , , ,

4 Responses to “Disabling Frustum Culling on a Game Object in Unity”

  • Martin says:

    Hi,

    I didn’t tried it, but
    wouldn’t be “better” do (only once) something like:

    mesh.bounds.min = Vector3( -inf, -inf, -inf )
    mesh.bounds.max = Vector3( +inf, +inf, +inf )

    where -/+ inf will be “reasonably” small/big value?
    Or (maybe even better ?!?) do it on corresponding renderer.

    BR,
    Martin

  • Yeah, thanks Martin — That probably would work in all cases as well, and would be a tad more efficient.

  • Dk says:

    If you have a large piece of buffer geometry where at least part of it will be in the viewing frustum every frame, doesn’t it make sense to disable frustum culling?

  • It would, but only if Unity had a built-in on/off switch for per-object frustum culling. Since there is none, it’s better to just let Unity do its normal bounds checking steps (since there is no way of disabling these).

Leave a Reply