在特定 Z 位置将屏幕空间坐标转换为世界空间坐标的 C# 函数?

问题描述:

我正在制作一个 2D 游戏,渲染为 3D.

I am making a 2D game, rendered as 3D.

当点击我的游戏时,我希望它将这些屏幕空间像素坐标转换为世界空间坐标,知道该点应该击中的 Z 轴点 (0).

When clicking on my game, I want it to convert those screen space pixel coordinates into worldspace coordinates, knowing the Z axis point (0) that the point should hit.

我怎样才能用一个函数来做到这一点?

How can I do this with a function?

我在 C# 中使用 XNA.

I am using XNA with C#.

从 2d 到 3d 的转换是非投影的.为此,xna 中有一个内置方法:

Converting from 2d to 3d is unprojecting. there is a built in method within xna for this:

 Vector3 3dWorldPosition = GraphicsDevice.Viewport.Unproject(2DscreenVector & desired Z comp, viewMatrix, projectionMatrix);