作者热门文章
- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我想渲染一个模型,然后,如果特定的顶点(我将如何标记它们?)是可见的,我想在它们所在的位置渲染一些二维的东西。
我该怎么做呢?
最佳答案
首先,您需要将顶点位置作为 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/
我正在使用 OpenGLES 的包装器编写 2D 游戏。有一个相机瞄准一堆纹理,这些纹理是游戏的 Sprite 。用户应该能够通过在屏幕上四处移动手指来移动 View 。问题是,相机距离纹理大约 10
我在 CSS 中定义了一个圆,或者通常是一个椭圆,例如像这样: 圆: border-radius:50%;宽度:50px;高度:50px; 椭圆: border-radius: 50%;宽度:30px
我是一名优秀的程序员,十分优秀!