2010 update: if you are using a version of XNA Game Studio >= 4.0, see here instead.
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 renderstates to values that may not be appropriate for drawing in 3D.
An obvious way to avoid this problem is to pass SaveStateMode.SaveState when you call SpriteBatch.Begin, but there is a potential pitfall here. Saving and restoring device state can be slow, so if you do this a lot, your framerate will suffer.
A more efficient approach is to always set whatever renderstates you need before you draw any graphics. SpriteBatch will automatically set what it needs for drawing in 2D, so you just need to set these back to what you want before drawing in 3D.
So exactly which renderstates does SpriteBatch change? Here’s the complete list:
GraphicsDevice.RenderState.CullMode = CullMode.CullCounterClockwiseFace;
GraphicsDevice.RenderState.DepthBufferEnable = false;
GraphicsDevice.RenderState.AlphaBlendEnable = true;
GraphicsDevice.RenderState.AlphaBlendOperation = BlendFunction.Add;
GraphicsDevice.RenderState.SourceBlend = Blend.SourceAlpha;
GraphicsDevice.RenderState.DestinationBlend = Blend.InverseSourceAlpha;
GraphicsDevice.RenderState.SeparateAlphaBlendEnabled = false;
GraphicsDevice.RenderState.AlphaTestEnable = true;
GraphicsDevice.RenderState.AlphaFunction = CompareFunction.Greater;
GraphicsDevice.RenderState.ReferenceAlpha = 0;
GraphicsDevice.SamplerStates.AddressU = TextureAddressMode.Clamp;
GraphicsDevice.SamplerStates.AddressV = TextureAddressMode.Clamp;
GraphicsDevice.SamplerStates.MagFilter = TextureFilter.Linear;
GraphicsDevice.SamplerStates.MinFilter = TextureFilter.Linear;
GraphicsDevice.SamplerStates.MipFilter = TextureFilter.Linear;
GraphicsDevice.SamplerStates.MipMapLevelOfDetailBias = 0.0f;
GraphicsDevice.SamplerStates.MaxMipLevel = 0;
SpriteBatch also modifies the Vertices, Indices, VertexDeclaration, VertexShader, and PixelShader properties on the GraphicsDevice.
Many of these settings are fine for both 2D and 3D rendering, but before you draw anything in 3D you will probably want to reset these states:
GraphicsDevice.RenderState.DepthBufferEnable = true;
GraphicsDevice.RenderState.AlphaBlendEnable = false;
GraphicsDevice.RenderState.AlphaTestEnable = false;
Depending on your 3D content, you may also want to set:
GraphicsDevice.SamplerStates.AddressU = TextureAddressMode.Wrap;
GraphicsDevice.SamplerStates.AddressV = TextureAddressMode.Wrap;