Search Unity

Check if Object is visible by camera

Discussion in 'Scripting' started by Cellenseres, Apr 22, 2020.

  1. Cellenseres

    Cellenseres

    Joined:
    Mar 30, 2015
    Posts:
    68
    Hey Guys, currently I'm trying to get a system to work that checks if an object is visible by the camera or not.
    I've tried to different approaches, one that used renderer.isVisible, but there was the problem that it ignored the fact that there might be an object between the camera and wall (according to the debugger, the object was still markes as "isVisible", even if the object was completely hidden behind a wall.

    My second approach was to try it with raycast.
    This also didn't work as I wanted it to. It worked better than my first approach, but when I hide myself (the camera) behind the wall so that only a small part of the object was visible, my script marked the object as "not visible", even if it was still visible a bit.

    I want to do something when the Object is visible.
    It should be marked as visible when:
    - the Object is fully or partly visible by the camera (even if it's only a small part).

    It should be marked as not visible when:
    - the object is completely out of view.
    - when the Object is completely hidden behind a wall


    Results of my current approach:
    - This one is marked as visible:


    this one isn't (even if the object is still visible)



    Used my PainsSkills to show what I wan't to achieve:



    Would be awesome if someone could help me out with that with a code snippet.

    Thank you in advance
     
  2. Cellenseres

    Cellenseres

    Joined:
    Mar 30, 2015
    Posts:
    68
    Any Idea?
     
  3. arenspace

    arenspace

    Joined:
    Jul 23, 2019
    Posts:
    1
    did you ever figure this one out? im working on something similar. Its close to what i want, but it still has some flaws lol
     
  4. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,433
    Renderer.isVisible is only false when the object is culled, their entire bounds being outside the camera's frustum. It has no idea about zbuffers or pixels or raycasts or any of that.

    It would be nice if there was an inverse of "SphereCast." That function returns true if the entire tube of space between source and target is clear of any obstructions. What you want to know is whether the entire tube of space has been completely blocked.

    There are two general ways to do this.

    One practical approach is to do multiple raycasts. Do a series of raycasts from all six or eight extents of the object, plus one or more centroids, back to the camera position. Of course, shortcut your loop and return "visible" if ANY of the raycasts are able to get back to the camera. The more rays from different parts of the sneaky object, the higher the fidelity of being able to spot them through narrow gaps.

    Another theoretical approach is to use a shader to count the number of pixels that ended up written. Draw all possible occluding objects to a render texture, then after those are drawn, draw the sneaky thing that is possibly hidden and count the number of pixels that were not culled by zbuffer values. Alternatively, draw the scene in any order, but with black as the occluding color and white as the sneaky object's color, and count the final white pixels.
     
  5. Max-om

    Max-om

    Joined:
    Aug 9, 2017
    Posts:
    499
    Pretty sure isvisible should take oclussion into account if you have baked occlusion offcourse.

    Edit: I might mixing it up with WillRenderObject. Either case look into CullingGroup
     
    Last edited: Dec 27, 2022