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;
當然這個得看模型的情況的。

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