Share via


ViewportExtensions.Unproject Method

Microsoft Silverlight will reach end of support after October 2021. Learn more.

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

Namespace:  Microsoft.Xna.Framework.Graphics
Assembly:  Microsoft.Xna.Framework.Graphics.Extensions (in Microsoft.Xna.Framework.Graphics.Extensions.dll)

Syntax

'Declaration
Public Shared Function Unproject ( _
    viewport As Viewport, _
    source As Vector3, _
    projection As Matrix, _
    view As Matrix, _
    world As Matrix _
) As Vector3
public static Vector3 Unproject(
    Viewport viewport,
    Vector3 source,
    Matrix projection,
    Matrix view,
    Matrix world
)

Parameters

Return Value

Type: Microsoft.Xna.Framework.Vector3
The vector in the 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.

Version Information

Silverlight

Supported in: 5

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.