( SOLVED ) View Frustum 8 Corners in world space ?

DexterZDexterZ Posts: 10Member
edited April 16 in 3D

Hi,

Is there a way to get the 8 corners position of the camera view Frustum, I know that GetFrustum() returns the 6 planes of the view Frustum, but is there a way to get the 8 corners of the Bounding view Frustum near to far view in global space or how can I construct the 8 corners base on the 6 planes ?

Something like this:

Many thanks in advanced and advice are appreciated,

Dex

Comments

  • DexterZDexterZ Posts: 10Member

    It seems that I don't need to roll my own frustum view **Plane **has a built in intersection to **AABB **

  • DexterZDexterZ Posts: 10Member

    Finally I got it, if any one is interested see the code below C# tho : )

      /// <summary>
        /// Draw from Near rect is from Screen Projection and far is computed from setting.
        /// </summary>
        private void DrawViewFrustum()
        {
    
            float m_FarOffset =  _Camera.Far;
            float m_NearOffset   = _Camera.Near;
            float m_ScreenWidth  = _Camera.GetViewport().Size.x;
            float m_ScreenHeight = _Camera.GetViewport().Size.y;
            //
            float m_AspectRatio  = (m_ScreenWidth > m_ScreenHeight)? (m_ScreenWidth / m_ScreenHeight) : (m_ScreenHeight / m_ScreenWidth);        
            //
            float m_FarHeight    = 2.0f * ((float)Math.Tan(_Camera.Fov / 2.0f)) * m_FarOffset;
            float m_FarWidth     = m_FarOffset * m_AspectRatio;
            // 
            m_FarHeight = m_FarHeight + (( m_FarHeight/ 7) * 3);
            m_FarWidth  = m_FarWidth  + (( m_FarWidth / 7) * 3);
            //
            Vector3 m_Forward = _Camera.GlobalTransform.origin + Vector3.Forward * m_FarOffset;
    
    
            //--> Near World
            //
            Vector3 m_NearTopLeft     = _Camera.ProjectPosition(new Vector2(0, 0), _Camera.Near);
            Vector3 m_NearTopRight    = _Camera.ProjectPosition(new Vector2(m_ScreenWidth, 0), _Camera.Near);
            Vector3 m_NearBottomLeft  = _Camera.ProjectPosition(new Vector2(0, m_ScreenHeight), _Camera.Near);
            Vector3 m_NearBottomRight = _Camera.ProjectPosition(new Vector2(m_ScreenWidth, m_ScreenHeight), _Camera.Near);
    
    
            //--> Far LOCAL
            //
            Vector3 m_FarTopLeft     = m_Forward + (Vector3.Up * m_FarHeight / 2.0f) - (Vector3.Right * m_FarWidth / 2.0f);
            Vector3 m_FarTopRight    = m_Forward + (Vector3.Up * m_FarHeight / 2.0f) + (Vector3.Right * m_FarWidth / 2.0f);
            Vector3 m_FarBottomLeft  = m_Forward - (Vector3.Up * m_FarHeight / 2.0f) - (Vector3.Right * m_FarWidth / 2.0f);
            Vector3 m_FarBottomRight = m_Forward - (Vector3.Up * m_FarHeight / 2.0f) + (Vector3.Right * m_FarWidth / 2.0f);
            //
            //--> Far WORLD
            //
            m_FarTopLeft     = _Camera.Transform.Xform(m_FarTopLeft);
            m_FarTopRight    = _Camera.Transform.Xform(m_FarTopRight);
            m_FarBottomRight = _Camera.Transform.Xform(m_FarBottomRight);
            m_FarBottomLeft  = _Camera.Transform.Xform(m_FarBottomLeft);
    
    
            //--> Draw lines
            //
            ZC.FromToPoints[] m_Lines = new ZC.FromToPoints[12];
            //
            // Far rect lines
            m_Lines[0].FromPoint = m_FarTopLeft;
            m_Lines[0].ToPoint   = m_FarTopRight;
            //
            m_Lines[1].FromPoint = m_FarTopRight;
            m_Lines[1].ToPoint   = m_FarBottomRight;
            //
            m_Lines[2].FromPoint = m_FarBottomRight;
            m_Lines[2].ToPoint   = m_FarBottomLeft;
            //
            m_Lines[3].FromPoint = m_FarBottomLeft;
            m_Lines[3].ToPoint   = m_FarTopLeft;
            //
            // Near rect lines
            m_Lines[4].FromPoint = m_NearTopLeft;
            m_Lines[4].ToPoint   = m_NearTopRight;
            //
            m_Lines[5].FromPoint = m_NearTopRight;
            m_Lines[5].ToPoint   = m_NearBottomRight;
            //
            m_Lines[6].FromPoint = m_NearBottomRight;
            m_Lines[6].ToPoint   = m_NearBottomLeft;
            //
            m_Lines[7].FromPoint = m_NearBottomLeft;
            m_Lines[7].ToPoint   = m_NearTopLeft;
            //
            // Left lines        
            m_Lines[8].FromPoint = m_NearTopLeft;
            m_Lines[8].ToPoint   = m_FarTopLeft;
            //
            m_Lines[9].FromPoint = m_NearBottomLeft;
            m_Lines[9].ToPoint   =  m_FarBottomLeft;
            //
            // Right lines        
            m_Lines[10].FromPoint = m_NearTopRight;
            m_Lines[10].ToPoint   = m_FarTopRight;
            //
            m_Lines[11].FromPoint = m_NearBottomRight;
            m_Lines[11].ToPoint   = m_FarBottomRight;
    
    
            //--> Create 3d Line
            //
            _3DLineMngr.Create(this, m_Lines, Color.ColorN("green", 1));
    
    
        }
    

Leave a Comment

BoldItalicStrikethroughOrdered listUnordered list
Emoji
Image
Align leftAlign centerAlign rightToggle HTML viewToggle full pageToggle lights
Drop image/file