Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

any luck with GeometryUtility.CalculateFrustumPlanes ?

Discussion in 'Editor & General Support' started by creat327, Mar 10, 2011.

  1. creat327

    creat327

    Joined:
    Mar 19, 2009
    Posts:
    1,756
    Hi

    I want to display the frustum planes of any given camera during the game. Not only the current camera, but actually see the 'frustum' moving from other cameras.

    I did a simple copy and paste of the code from the script manual and the planes I see seem completely off from the real frustum.


    My code, simple:

    Code (csharp):
    1.  
    2. Plane[] planes=GeometryUtility.CalculateFrustumPlanes(mycamera);
    3.  
    4.         if (targetGO == null)
    5.  
    6.                 {
    7.  
    8.            
    9.  
    10.                     int i = 0;
    11.  
    12. while (i < planes.Length) {
    13.  
    14. GameObject p = GameObject.CreatePrimitive(PrimitiveType.Plane);
    15.  
    16. p.name = "Plane " + i.ToString();
    17.  
    18. p.transform.position = -planes[i].normal * planes[i].distance;
    19.  
    20. p.transform.rotation = Quaternion.FromToRotation(Vector3.up, planes[i].normal);
    21.  
    22.                 p.transform.localScale=new Vector3(100f,100f,100f);
    23.  
    24. i++;
    25.  
    26. }
    27.  
    28.         }
    29.  
    I added the localScale to make sure it wasn't my imagination, but all frustum seem to be positioned at the wrong place. When I display the planes using the code, I see them like 1000 units away from the origin.
    My camera is a children of other objects, so I don't know if that's what's messing it up, I tried 'unchilding' it, but same behaviour.
     
    Last edited: Mar 10, 2011
  2. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    I've checked your code and it is correct but the mathematical definition of the planes may be a bit confusing here. The normal and distance values are defined relative to the world origin and extend infinitely. They are great for testing whether an object is on one side of the plane or the other but they are not very convenient for positioning plane objects close to the camera to visualise the planes.
     
  3. D22888

    D22888

    Joined:
    Apr 18, 2009
    Posts:
    95
    Is there a good way to do this?
    It would nice to draw some lines down the frustum of any given camera, or get the planes to be aligned with it....
     
  4. PolyMad

    PolyMad

    Joined:
    Mar 19, 2009
    Posts:
    2,350
    Build a 3d model of pyramid with its top angle = 90 degrees and play with it?
     
  5. Jesse Anders

    Jesse Anders

    Joined:
    Apr 5, 2008
    Posts:
    2,857
    You can intersect the frustum planes in groups of 3 to yield the corners of the frustum, and then draw line segments connecting the corners (or use whatever visualization method you prefer).