绘制 2D HUD 会弄乱我的 3D 模型的渲染? [英] Drawing a 2D HUD messes up rendering of my 3D models?

查看:30
本文介绍了绘制 2D HUD 会弄乱我的 3D 模型的渲染?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 XNA 3.1

I'm using XNA 3.1

我最近通过在我的游戏中使用 Components.Add(myComponent) 创建了一个 2D 平视显示器 (HUD).HUD 看起来不错,显示了 2D 地图、十字准线和帧率计数器.问题是,只要 HUD 出现在屏幕上,游戏中的 3D 对象就不再正确绘制.

I have recently created a 2D Heads Up Display (HUD) by using Components.Add(myComponent) to my game. The HUD looks fine, showing a 2D map, crosshairs and framerate counter. The thing is, whenever the HUD is on screen the 3D objects in the game no longer draw correctly.

在靠近我的玩家之后,可能会吸引到离我的玩家更远的东西,当我走过它们时,模型有时会失去清晰度.当我移除 HUD 时,一切都正常绘制.

Something further from my player might get drawn after something closer, the models sometimes lose definition when I walk past them. When I remove the HUD all is drawn normally.

他们是否有任何我应该注意的已知问题?我应该如何在我的 3D 游戏区域上绘制 2D HUD?

Are their any known issues regarding this that I should be aware of? How should I draw a 2D HUD over my 3D game area?

这是没有 GameComponent 的样子:

这是使用 GameComponent 的样子(在这种情况下,它只是屏幕左上角的一些显示帧率的文本),注意后面的树是如何出现在靠近相机的树:

And here's how it looks with a GameComponent (in this case it's just some text offscreen in the upper left corner that shows framerate), notice how the tree in the back is appearing in front of the tree closer to the camera:

推荐答案

您必须启用深度缓冲区:

You have to enable the depth buffer:

// XNA 3.1
GraphicsDevice.RenderState.DepthBufferEnable = true;
GraphicsDevice.RenderState.DepthBufferWriteEnable = true;

// XNA 4.0
GraphicsDevice.DepthStencilState = DepthStencilState.Default;

SpriteBatch.Begin 改变图形管道的状态:

SpriteBatch.Begin alters the state of the graphics pipeline:

在两个版本中都禁用了深度缓冲,这就是导致问题的原因.

In both versions depth buffering is disabled, that's what causes the issue.

再次强调这一点:

在绘制任何几何图形之前,始终确保正确设置所有渲染状态.

  • 混合状态
  • DepthStencilState
  • 光栅化状态
  • 视口
  • 渲染目标
  • 着色器
  • 采样状态
  • 纹理
  • 常量

每个状态的目的 和渲染管道中的每个阶段.如果有疑问,请尝试将所有内容重置为默认值.

Educate yourself on the purpose of each state and each stage in the rendering pipeline. If in doubt, try resetting everything to default.

这篇关于绘制 2D HUD 会弄乱我的 3D 模型的渲染?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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