Search Unity

Check if object is visible to Main Camera

Discussion in 'Scripting' started by eco_bach, Mar 24, 2017.

  1. eco_bach

    eco_bach

    Joined:
    Jul 8, 2013
    Posts:
    1,601
    I am trying to find ways to optimize a high poly CAD file imported as FBX. For reasons I won't go into I am unable to reduce the poly count (roughly 4 million tris, verts!!).

    As an alternative I want to explore ways to dynamically hide parts of the imported mesh NOT currently seen by the Main camera. ie I am NOT talking about frustrum culling, which works by default, rather I want to turn an object 'OFF' ie Setactive(false), if that object is hidden or occluded by another object.

    Is this possible? Or would the cost of checking visibility to the Main Camera nullify any performance gains thru a reduced Poly count?

    After some googling I wonder if Raycasting is what I need
    http://answers.unity3d.com/questions/15636/how-can-i-check-the-line-of-sight-to-see-if-the-pl.html

    If the following script was put on the Main camera and I substitute 'player' with the object I am checking>
    Code (csharp):
    1.  
    2. var hit : RaycastHit;
    3. var rayDirection = player.position - transform.position;
    4. if (Physics.Raycast (transform.position, rayDirection, hit)) {
    5. if (hit.transform == player) {
    6. // enemy can see the player!
    7. } else {
    8. // there is something obstructing the view
    9. }
    10. }
    11.  
     
    Last edited: Mar 25, 2017
  2. webox

    webox

    Joined:
    Aug 27, 2015
    Posts:
    72
  3. eco_bach

    eco_bach

    Joined:
    Jul 8, 2013
    Posts:
    1,601
    Thanks will check those out. Also figured a more efficient way to find the 'root' material object of my FBX mesh is to tag them, and then in my Start method populate an Array or list with these objects using FindGameObjectsWithTag
     
  4. makeshiftwings

    makeshiftwings

    Joined:
    May 28, 2011
    Posts:
    3,350
    Dave_Bowman and KevinCodes4Food like this.
  5. eco_bach

    eco_bach

    Joined:
    Jul 8, 2013
    Posts:
    1,601
    Unfortunately my mesh is Dynamic ao AFAIK cannot use Occlusion culling.
    OnBecameInvisible
    https://docs.unity3d.com/ScriptReference/Renderer.OnBecameInvisible.html
    is interesting but confusing.
    After reading this thread https://forum.unity3d.com/threads/are-onbecamevisible-and-onbecameinvisible-named-poorly.34225/
    I assume that OnBecameInvisible ONLY gets called in the editor if your Scene view tab is closed AND the gameObject in question is leaving the Main Camera's frustrum.
    So just frustrum culling which is enabled by default anyways....

    But if I understand correctly, it SHOULD work as anticipated if you are deploying to iOS??
     
  6. vElumi

    vElumi

    Joined:
    Nov 21, 2016
    Posts:
    2
    OnBecameVisible and OnBecameInvisible work in editor too, they just check for the scene view camera as well as your "normal" cameras in hierarchy.

    So in editor, for example, OnBecameInvisible is called when the object is not visible neither in scene view, nor by any of your cameras
     
    BarriaKarl and tonialatalo like this.