Viewport.Unproject Method

Converts a screen space point into a corresponding point in world space.

Syntax

'Declaration
Public Function Unproject ( _
         source As Vector3, _
         projection As Matrix, _
         view As Matrix, _
         world As Matrix _
) As Vector3 
public Vector3 Unproject (
         Vector3 source,
         Matrix projection,
         Matrix view,
         Matrix world
)
public:
Vector3 Unproject(
         Vector3 source,
         Matrix projection,
         Matrix view,
         Matrix world
)

Parameters

  • source
    Type: Vector3
    The vector to project.
  • projection
    Type: Matrix
    The projection matrix.
  • view
    Type: Matrix
    The view matrix.
  • world
    Type: Matrix
    The world matrix.

Return Value

Type: Vector3
The vector in object space.

Remarks

A common use for Unproject is determining if the current cursor location intersects with an object in 3D world space. First, calculate two Vector3 values that differ only by their Z value. For instance, assume that the cursor location is currently (100, 100). Therefore, the first vector (located at the near clip plane) becomes (100, 100, 0) and the second (located at the far clip plane) becomes (100, 100, 1).

Call Unproject for each point, and store the result. For example, minPointSource stores the result of "unprojecting" (100,100,0), and maxPointSource stores the result of "unprojecting" (100, 100, 1). Determine the direction vector by subtracting maxPointSource from minPointSource.

Finally, normalize the direction vector, and create a Ray with minPointSource and the now-normalized direction vector. You can now use this ray in a simple intersect test case (for example, Intersects) with the model.

For more usage examples of Unproject, see the following:

  • Picking sample (found at App Hub)
  • Picking with Triangle Accuracy sample (also found at App Hub)

Requirements

Namespace: Microsoft.Xna.Framework.Graphics

Assembly: Microsoft.Xna.Framework.Graphics (in microsoft.xna.framework.graphics.dll)

See Also

Reference

Viewport Structure
Viewport Members
Microsoft.Xna.Framework.Graphics Namespace

Platforms

Windows Phone