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

| 我想渲染一个模型,然后,如果可见特定的顶点(我将如何标记它们?),我想在它们所在的位置渲染2D图像。 我将如何去做?     
已邀请:
首先,您需要将顶点位置设为
Vector4
(透视投影需要使用齐次坐标;将set1ѭ设置)。我假设您知道所需的顶点以及如何获得其位置。它的位置将在模型空间中。 现在,只需将该点转换为投影空间即可。也就是说,将其乘以您的“世界视图投影”矩阵。那是:
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)范围内即可。)     
看一下引擎的遮挡剔除功能。对于XNA,您可以在此处查阅框架指南(带有示例)。 http://roecode.wordpress.com/2008/02/18/xna-framework-gameengine-development-part-13-occlusion-culling-and-frustum-culling/     
最好的方法是使用
BoundingFrustrum
。它基本上就像一个向特定方向扩展的矩形,类似于玩家的相机的工作方式。然后,您可以检查所需的给定点是否包含在BoundingFrustrum中,如果包含,则渲染对象。 基本上,其形状如下所示: 例:
// A view frustum almost always is initialized with the ViewMatrix * ProjectionMatrix
BoundingFrustum viewFrustum = new BoundingFrustum(ActivePlayer.ViewMatrix * ProjectionMatrix);

// Check every entity in the game to see if it collides with the frustum.
foreach (Entity sourceEntity in entityList)
{
    // Create a collision sphere at the entities location. Collision spheres have a
    // relative location to their entity and this translates them to a world location.
    BoundingSphere sourceSphere = new BoundingSphere(sourceEntity.Position,
                                  sourceEntity.Model.Meshes[0].BoundingSphere.Radius);

    // Checks if entity is in viewing range, if not it is not drawn
    if (viewFrustum.Intersects(sourceSphere))
        sourceEntity.Draw(gameTime);
}
该示例实际上是用于剔除游戏中的所有对象,但是可以很容易地对其进行修改以处理您要执行的操作。 来源示例:http://nfostergames.com/Lessons/SimpleViewCulling.htm 要将世界坐标放到屏幕空间中,请查看Viewport.Project。     

要回复问题请先登录注册