- Home /
How do I check if an object is completely visible?
How do I check if an object is completely within my camera frustum? ie Is the whole object is being rendered.
I am currently using a script which detects only if any single part of the object is within the camera frustum which isn't what I want..
Current code:
function IsRenderedFrom(renderer : Renderer, camera : Camera) : boolean
{
planes= GeometryUtility.CalculateFrustumPlanes(camera);
return GeometryUtility.TestPlanesAABB(planes, renderer.bounds);
}
Looks this was never answered. Did you haver find an answer, I was looking for the same thing.
Answer by senad · Mar 15, 2012 at 12:25 PM
This is the standard frustum test, because normally you are interested if any of the geometry is inside the view frustum. (is visible)
What you would need would be to test if any of the bounding geometry is outside of the frustum. Because if not, then everything is completely inside.
I am a little new to Unity3D so I do not know if they offer this functionality, but I would suspect not. If you want to implement this yourself you have to test each BB vertex against six frustum planes, but the test succeeds if the vertex is outside.
You can google frustum culling to read up on this.
Answer by Meltdown · Mar 15, 2012 at 01:27 PM
You can use renderer.isVisible to determine if the object is within the frustum.
He was asking to know if the object is completely visible, not only (partially) visible. :)
Answer by Shinugami · Oct 16, 2012 at 02:03 PM
You can use renderer.isVisible to determine if the object is within the frustum.
Yes this is correct but if the object casts shadows even though it is technically off camera then it is still considered as visible: /Documentation/ScriptReference/Renderer-isVisible.html
"Note that object is considered visible when it needs to be rendered in the scene. It might not be actually visible by any camera, but still need to be rendered for shadows for example. Also, when running in the editor, the scene view cameras will also cause this value to be true."
Answer by Bunny83 · Jul 08, 2020 at 04:33 AM
Since the question was already bumped...
You can actually use TestPlanesAABB with the inverted planes and check that the renderer is not in front of the planes which will ensure it's within the frustum. Keep in mind that this only accounts for the AABB of the object. So even it might be completely visible it might not be considered visible because the AABB is larger than the visible object.
public static bool IsRenderedFrom(Renderer aRenderer, Camera aCam)
{
var planes = GeometryUtility.CalculateFrustumPlanes(aCam);
for(int i = 0; i < planes.Length; i++)
planes[i] = planes[i].flipped;
return !GeometryUtility.TestPlanesAABB(planes, aRenderer.bounds);
}
By flipping all 6 planes we essentially form a negative space. So the planes will cover everything except the inner part of the frustum. So whenever an object is outside the frustum or partially overlapped with one plane, TestPlanesAABB will return true. This is our indication that it's not fully visible. Only when TestPlanesAABB returns false we know it has to be completely inside the frustum.
Flipping a plane is a trivial operation. We just have to flip the sign of all 4 float values that define our plane. The normal vector is inverted ( so it points in the opposite direction) and the distance from the origin is also inverted in order to keep the plane at the same position in space.