- Home /
Detect if bounds are "partially" inside camera frustum
So I'm using the useful convenience methods within Unity's API to find if my object is inside the camera view frustum, but I'm wanting to detect if it is partially inside. I'm creating my own quadtree culling system.
public void Update(){
planes = GeometryUtility.CalculateFrustumPlanes(Camera.main);
if(GeometryUtility.TestPlanesAABB(planes, renderer.bounds)){
Debug.Log("is inside");
} else {
Debug.Log("isn't inside");
}
}
Anyone got any ideas or will I have to create my own TestPlanesAABB method?
Answer by CodeMasterMike · Dec 18, 2012 at 09:44 AM
I don't have a code for it, but use the Bounds structure.
In the Bounds structure you have the min, max and size. And with those you can easily calculate all corners of your boundingbox.
When you have those corners you can check each individual position of the corners to see if they are inside the boundaries you have (in your case inside the frustum). And if all corners are inside, then the whole object is inside. If only 1,2 or 3 are inside, then its only partial inside.
Documentation for Bounds structure.
Good luck!
Thanks that a great solution, thanks. I'm wondering, once I have the corner positions, how would I best go about checking them against the frustum plane array?
If you know which plane is which, then you know that a corner should be to the right of the rightside plane, in front of the nearplane etc.
I've never used the frustum calculation so I don't know how it creates the frustum planes.
$$anonymous$$aybe the Plane.GetSide() function can help you there. http://docs.unity3d.com/Documentation/ScriptReference/Plane.GetSide.html
Perfect, got it working great. Thanks @Code$$anonymous$$aster$$anonymous$$ike
@Caiuse - any chance you can post working code here? I am dealing with similar problem right now.
However note that, contrary to intuition, if all corners are outside the object might still intersect with the frustum.
Answer by KevinCodes4Food · May 05, 2014 at 07:55 PM
For some of our apps, we create a plane game object at each frustum plane position. A bounding box or other collider can then be added to these planes easily:
I posted code for this on another answer here: