XNA 4.0中由于混用2D、3D绘图导致的问题解决

由于2D和3D绘图都会改变显卡的一些参数,所以再重新绘制时需要重置显卡的参数,否则会出现各种新鲜的图形错位之类的问题。

曾经发过一个blog,转自Shawn Hargreaves Blog,现在再转一篇,这次变成4.0的了。

 

If you are mixing 3D rendering with 2D objects using SpriteBatch, you may notice that your 3D graphics no longer draw correctly after you have rendered sprites. This is because the SpriteBatch changes several device states to values that may not be appropriate for drawing in 3D.

So exactly which states does SpriteBatch change? Here's the complete list: 这是会改变的参数:

    GraphicsDevice.BlendState = BlendState.AlphaBlend;
    GraphicsDevice.DepthStencilState = DepthStencilState.None;
    GraphicsDevice.RasterizerState = RasterizerState.CullCounterClockwise;
    GraphicsDevice.SamplerStates[0] = SamplerState.LinearClamp;

SpriteBatch also modifies the vertex buffer, index buffer, and applies its own effect onto the GraphicsDevice.

 

 

下面是重置的语句:

Before you draw anything in 3D you will probably want to reset these states:

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

Depending on your 3D content, you may also want to set: 有时候也需要加这个语句:

    GraphicsDevice.SamplerStates[0] = SamplerState.LinearWrap;

 我在实践中遇到过一次这样的问题,弄了半天,最后发现其实是因为模型过于复杂,背面剔除的那个语句实际上也是需要的,

所以明智点还是加上比较好。

  GraphicsDevice.RasterizerState = RasterizerState.CullNone;
当然这个得看模型的情况的。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章