Search Unity

gameobject in view fustrum

Discussion in 'Scripting' started by BigB, May 19, 2011.

  1. BigB

    BigB

    Joined:
    Oct 16, 2008
    Posts:
    672
    Hey guys,

    Is there a way to know if a gameObject is in the view fustrum ?
    I cannot use renderer.isVisible, etc, because my gameObject doens't always have a renderer associated with it.
    Any clues ?

    Thanks,
    Bruno
     
  2. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    I would create a function that cycles through the children of the game object, finds all the renderers in it and tests each one.

    Code (csharp):
    1.  
    2. function isInViewFrustrum(testObject : gameObject){
    3.     var visible=false;
    4.     var objs : Component=testObject.GetComponentsInChildren(Renderer);
    5.     for(var obj : GameObject in objs){
    6.         if(obj.renderer.isVisible)visible=true;
    7.     }
    8.     if(testObject.renderer)if(testObject.renderer.isVisible) then visible=true;
    9.     return visible;
    10. }
    11.  
     
  3. Jesse Anders

    Jesse Anders

    Joined:
    Apr 5, 2008
    Posts:
    2,857
    If the game object has no renderer(s) associated with it, you can perform the test manually. If you just need something approximate, a simple 'field of view' test might be sufficient, but if you need a more exact result you can perform the frustum test yourself. (Not looking at the docs right now, but I believe the Unity API even includes a convenience function for computing the planes of the frustum.)
     
  4. L-Tyrosine

    L-Tyrosine

    Joined:
    Apr 27, 2011
    Posts:
    305
  5. Alesk

    Alesk

    Joined:
    Jul 15, 2010
    Posts:
    340
    Hi,

    I'm working on a custom particle emitter, and I would like to know wich particles are in the camera Frustum or not.
    I've tried the sample code in the manual, but I get wrong or weird results.
    What should I do exactly to know if a point is in view or not ?

    EDIT : ok, I've found the problem : my particle emitter wasn't positionned at 0,0,0... that was my weirdness ;)
     
    Last edited: Jun 4, 2011