Search Unity

Determine if object is visible to camera(including obstruction?)

Discussion in 'Scripting' started by poohbear2, Aug 5, 2020.

  1. poohbear2

    poohbear2

    Joined:
    Jul 25, 2020
    Posts:
    1
    Hi all,

    I am working on a system to determine whether any part of an object is directly visible by the player camera. My first try used OnBecameVisible() and OnBecameInvisible(), but it doesn't work because those methods only take into account the camera direction, not any occlusion by other objects. I've done a lot of googling and have seen raycasting solutions suggested, but this doesn't account for cases when only some part of the center of the object is visible like through a window. I've also seen some ideas of doing a render with certain colors for certain objects and checking that way, but that seems like it might run into performance issues.

    Is there any way to do this accurately? I shouldn't need a lot of objects to use this system at once.

    Thanks!
     
  2. Cyber-Dog

    Cyber-Dog

    Joined:
    Sep 12, 2018
    Posts:
    352
    What you ask is not so simple, so any 100% accurate method will require a bit of your CPU.

    If you want to meet half way, I would just work with your colliders. If its characters, add a rag doll. Static build covex..

    From memory, Im pretty sure you can cycle through the vertex points of a collider.

    So on your object get a list of all collider components.
    Then for each vertex in each collider, check if that vector3 pos is visible in screen space.
    If so, do a ray-cast. If ray-cast has nothing in between then your visible, if not then keep checking other verts.