WorldToScreen 函数 C# [英] WorldToScreen function C#

查看:44
本文介绍了WorldToScreen 函数 C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能向我解释世界屏幕功能(c#)?我想了解它的作用,如何使用它以及我需要使用它.到目前为止,我知道它将 3d 向量转换为 2d 向量,然后可以在监视器上显示.现在我想知道在屏幕上将 3d 点显示为绘图所需的步骤.提前谢谢:)

Can anyone please explain the world to screen function (c#) to me? I want to understand what it does, how to use it and what I need to use it. So far I know, that it converts 3d vectors to 2d vectors, that then can be shown on the monitor. Now I want to know the steps needed to show a 3d point as a drawing on the screen. Thx in advance :)

推荐答案

我将假设您想要的屏幕空间从上到下排列,因此 {0, 0} 位于左上角,{screenWidth, screenHeight} 位于底部对.我还将假设标准化设备坐标在 [{-1, -1, 0}, {1, 1, 1}] 范围内.

I am going to assume that your desired screen space runs from top to bottom so that {0, 0} is top left and {screenWidth, screenHeight} is bottom right. I am also going to assume that normalized device coordinates are in the range of [{-1, -1, 0}, {1, 1, 1}].

您要做的第一件事是将您的 3D 世界坐标转换为所谓的标准化设备坐标(简称 NDC).这是一个中间步骤,可简化到 ScreenSpace 的映射.

The first thing you wanna do, is convert your 3D world coordinates into what are known as Normalized Device Coordinates (NDC for short). This is an intermediary step which simplifies the mapping to ScreenSpace.

标准化设备坐标用于测量显示器上的相对位置.这些不是像素坐标.屏幕中心始终为 0,顶部始终为 1,底部始终为 -1.左边总是-1,右边总是1.

Normalized Device Coordinates are used to measure relative positions on the monitor. These are not pixel coordinates. The center of the screen is always 0, the top is always 1 and the bottom is always -1. The left side is always -1 and the right side is always 1.

知道视图和投影矩阵,变换如下:

Knowing view and projection matrices, the transformation is following:

Vector3 pointInNdc = Vector3.Transform(pointInWorld, view *projection);

要了解此转换的工作原理,您可以查看 Paradox 游戏引擎的源代码 例如.

To understand how this transformation works, you could take a look at the source code of Paradox Game Engine for example.

一旦坐标标准化,你想把它映射到屏幕空间:

Once the coordinate is normalized, you want to map it into Screen Space:

  1. 要找到 X 坐标,请将其从 [-1,1] -> [0,2] 范围内移动,方法是向其添加 1 并将其除以 2 以将其从 [0,2] -> [0] 范围内移动,1].将其乘以屏幕宽度,您就得到了屏幕空间中的 X 坐标.

float screenX = (pointInNdc.X + 1)/2f * screenWidth;

  1. 求Y坐标类似,但不是加1,而是用1减去pointInNdc.Y来倒置坐标(从Y运行底部到顶部移动到Y运行从上到下)

float screenY = (1 - pointInNdc.Y)/2f * screenHeight;

这里有屏幕空间的 X 和 Y 坐标.有很多很棒的文章(比如这个一个)描述了这个完全相同的过程,以及如何从 ScreenToWorld 返回.

There you have both X and Y coordinates of Screen Space. There are many great articles out there (such as this one) which describe this exact same process and also how to go back from ScreenToWorld.

以下是 WorldToScreen 的完整列表:

Here is the full listing of WorldToScreen:

Vector2 WorldToScreen(Vector3 pointInWorld, Matrix view, Matrix projection, int screenWidth, int screenHeight)
{
    Vector3 pointInNdc = Vector3.Transform(pointInWorld, view * projection);
    float screenX = (pointInNdc.X + 1) / 2f * screenWidth;
    float screenY = (1 - pointInNdc.Y) / 2f * screenHeight;
    return new Vector2(screenX, screenY);
}

这篇关于WorldToScreen 函数 C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆