Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How to know if an object is actually visible by the camera?

Discussion in 'General Graphics' started by georgeq, Oct 21, 2016.

  1. georgeq

    georgeq

    Joined:
    Mar 5, 2014
    Posts:
    660
    I'm building a huge scene, with lots of stuff in it, in order to make it efficient I'm dividing the scene into containers, each container can contain one or more enemies. Containers use OnBecameVisible and OnBecameInvisible to enable / disable updates on the GameObjects inside it. If the container is inside the camera's frustum OnBecameVisible is called, if the object is outside the frustum camera OnBecameInvisible is called, so far so good. But even when the container is inside the camera's frustum that doesn't mean it is actually visible, since it may be occluded by a closer container. By enabling Occlusion Culling, I expected this behavior would be modified as follows: OnBecameVisible would be called when the container was inside the frustum AND was not occluded, likewise OnBecameInvisible would be called when the container was outside the frustum OR it was occluded by a closer object, however this doesn't happen.

    Occlusion culling workings correctly, containers and their children become invisible on the Scene View when they are occluded, and visible when they are not, but since OnBecameInvisible is not called when the containers are occluded I'm not getting a steady performance boost, because there are times when lots of containers are inside the frustum, but most of them are occluded.

    So the question is: How do I know if an object is actually visible by the camera or not?
     
  2. fffMalzbier

    fffMalzbier

    Joined:
    Jun 14, 2011
    Posts:
    3,276
    The visibility does include the scene camera and shadow calculation.
    If you watching your scene with the scene view while testing, the object is culled for the main camera but still viable for the scene view camera, so OnBecameInvisible will not get called unless no camera is seeing the object.
     
  3. raphael-ernaelsten-heaj

    raphael-ernaelsten-heaj

    Joined:
    Mar 9, 2016
    Posts:
    78
    You can use the CullingGroup class. It takes the referent camera as an argument and you manually feed the boundingSpheres to be tested.
     
    fffMalzbier likes this.
  4. georgeq

    georgeq

    Joined:
    Mar 5, 2014
    Posts:
    660
    Thank you!

    After some testing I could see how the Scene View camera affects the way OnBecameVisible and OnBecameInvisible are called, but that is not obvious and is not mentioned in the documentation.
     
  5. fffMalzbier

    fffMalzbier

    Joined:
    Jun 14, 2011
    Posts:
    3,276
    I poked around in the documentation and found this text in the documentation page for Renderer.isVisible
    and on MonoBehaviour.OnBecameVisible()
    not directly where and how i would have expected it so it is easy to miss.
     
  6. georgeq

    georgeq

    Joined:
    Mar 5, 2014
    Posts:
    660
    You are right, I could swear it wasn't there yesterday... seems like I didn't read as careful as I thought.