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:
[code lang=”csharp”]
// 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);
[/code]
Comments
2 responses to “Disabling Frustum Culling on a Game Object in Unity”
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
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?