Search Unity

How to make a flat world-aligned plane that fits the camera frustum?

Discussion in 'Scripting' started by duke, Apr 6, 2011.

  1. duke

    duke

    Joined:
    Jan 10, 2007
    Posts:
    763
    What a mouthful!

    I'm trying to follow the Projected Grid paper, and i'm having trouble getting my head around the right matrix math to get this to happen:

    I'd appreciate any tips :)
     

    Attached Files:

  2. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    You could do a ray from the screen 0,0 and screen.width, screen.height and get 4 Plane.Raycast's from that to get your info. The plane would be whatever your world is.

    Are you wanting it like your image?

    http://unity3d.com/support/documentation/ScriptReference/Plane.Raycast.html
    http://unity3d.com/support/documentation/ScriptReference/Camera.ScreenPointToRay.html

    If I am guessing correctly, you want to then create some geometry to show your plane. Create it at world center and make the 4 vertices those ray hits.
     
  3. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    You might find camera.ViewportToWorldPoint useful here. The viewport point <0.5, 0.5, distance> is converted to the centre of the camera at the specified distance, so you can use this to position your plane. It is easiest if you can use a plane that extends in the local X and Y directions because you can then simply copy the camera's transform.rotation to the plane object to get it correctly oriented. As for the size of the frustum, the height at a specified distance is given by:-
    Code (csharp):
    1. var height = 2.0 * Mathf.Tan(camera.fieldOfView * Mathf.Deg2Rad * 0.5) * distance;
    You can then multiply the height by the camera's aspect ratio to get the width of the frustum at the same distance.
     
  4. duke

    duke

    Joined:
    Jan 10, 2007
    Posts:
    763
    Thanks for the responses. I shall try them out tonight!