gpt4 book ai didi

c# - 获取 3D 模型特定顶点可见时的屏幕空间坐标

转载 作者:太空狗 更新时间:2023-10-29 23:10:37 37 4
gpt4 key购买 nike

我想渲染一个模型,然后,如果特定的顶点(我将如何标记它们?)是可见的,我想在它们所在的位置渲染一些二维的东西。

我该怎么做呢?

最佳答案

首先,您需要将顶点位置作为 Vector4(透视投影需要使用齐次坐标;设置 W = 1)。我假设你知道你想要哪个顶点以及如何获得它的位置。它的位置将在模型空间中。

现在只需将该点转换为投影空间。也就是说 - 将它乘以您的 World-View-Projection 矩阵。即:

Vector4 position = new Vector4(/* your position as a Vector3 */, 1);
IEffectMatrices e = /* a BasicEffect or whatever you are using to render with */
Matrix worldViewProjection = e.World * e.View * e.Projection;
Vector4 result = Vector4.Transform(position, worldViewProjection);
result /= result.W;

现在您的结果将在投影空间中,即屏幕左下角的 (-1,-1) 和右上角的 (1,1)。如果你想获得你在客户空间中的位置(这是 SpriteBatch 使用的),那么只需使用与 SpriteBatch< 使用的隐式 View-Projection 矩阵相匹配的矩阵的逆矩阵来转换它.

Viewport vp = GraphicsDevice.Viewport;
Matrix invClient = Matrix.Invert(Matrix.CreateOrthographicOffCenter(0, vp.Width, vp.Height, 0, -1, 1));
Vector2 clientResult = Vector2.Transform(new Vector2(result.X, result.Y), invClient);

免责声明:我没有测试过任何这段代码。

(显然,要检查特定顶点是否可见,只需检查它是否在投影空间中的 (-1,-1) 到 (1,1) 范围内。)

关于c# - 获取 3D 模型特定顶点可见时的屏幕空间坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6137426/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com