Export (0) Print
Expand All

Viewport.Unproject Method

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

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

public Vector3 Unproject (
         Vector3 source,
         Matrix projection,
         Matrix view,
         Matrix world


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

Return Value

The vector in object space.

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)

Xbox 360, Windows 7, Windows Vista, Windows XP

Community Additions

© 2014 Microsoft