XNA SpriteBatch 导致 BasicEffect 出现问题 [英] XNA SpriteBatch causing problems with BasicEffect

查看:29
本文介绍了XNA SpriteBatch 导致 BasicEffect 出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 XNA 来可视化一些数据,我正在尝试使用广告牌来展示数据,并尝试使用 spritebatch 来绘制 HUD 文本.

对于广告牌,我使用了以下示例,效果很好:http://create.msdn.com/en-US/education/catalog/sample/3d_audio

在那个例子中,有一个猫和狗的精灵,猫在狗的前面或后面经过,这取决于它们的位置和你期望的相机位置.调用 Cat.Draw 和 Dog.Draw 的顺序无关紧要.这些家伙是从 BasicEffect 中绘制的.

但是,如果我添加一个继承 DrawableGameComponent 并使用 SpriteBatch 的类,则其他组件中的 BasicEffect 将失去其深度排序,并且 Quad 将按照调用的顺序绘制.注意这个组件是通过 this.Components.Add(...) 添加到 Game 类中的.

BasicEffect 和 SpriteBatch 之间是否存在不兼容?每当调用 SpriteBatch.Begin()/End() 时就会出现问题.如果我不称之为猫/狗渲染就好了.

有什么想法吗?

解决方案

您需要重置 SpriteBatch 更改的一些渲染状态.

尝试在您的基本效果调用之前设置这些:

GraphicsDevice.BlendState = BlendState.Opaque;GraphicsDevice.DepthStencilState = DepthStencilState.Default;

有关 SpriteBatch 更改哪些渲染状态的更多信息:http://blogs.msdn.com/b/shawnhar/archive/2010/06/18/spritebatch-and-renderstates-in-xna-game-studio-4-0.aspx>

I'm using XNA to visualize some data, and I'm trying to use billboards for data, and spritebatch for HUD text drawing.

For the billboards, I'm using the following example, which works great: http://create.msdn.com/en-US/education/catalog/sample/3d_audio

In that example, there is a cat and dog sprite, where the cat passes in front of or behind the dog, depending on their positions and the camera position as you'd expect. It doesn't matter what order Cat.Draw and Dog.Draw are called in. These guys are drawn from a BasicEffect.

However if I add a class that inherits DrawableGameComponent and uses a SpriteBatch, the BasicEffect in other components loses it's depth sorting and the Quads are drawn in the order called. Note that this component is added to the Game class via this.Components.Add(...).

Is there an incompatibility between BasicEffect and SpriteBatch? The problem occurs whenever SpriteBatch.Begin()/End() is called. If I don't call this the cat/dog render fine.

Any ideas?

解决方案

You need to reset some renderstates that SpriteBatch changes.

Try setting these before your basiceffect calls:

GraphicsDevice.BlendState = BlendState.Opaque;
GraphicsDevice.DepthStencilState = DepthStencilState.Default;

More information about which render states SpriteBatch alters: http://blogs.msdn.com/b/shawnhar/archive/2010/06/18/spritebatch-and-renderstates-in-xna-game-studio-4-0.aspx

这篇关于XNA SpriteBatch 导致 BasicEffect 出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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