用SpriteBatch 在3D场景中写文字时,会把GraphicsDevice中的参数改掉,因此为了正确显示3D场景,需要在spriteBatch.end() 结束后,把改动过的参数恢复回来。
具体代码如下:
// Draw info spriteBatch.Begin();
string
str = "Avatar position "
+ avatarPosition.ToString();
Vector2 ms = Font.MeasureString(str);
Vector2 position;
position.X = ( this .Window.ClientBounds.Width - ms.X) / 2.0f;
position.Y = 0;
spriteBatch.DrawString(Font, str, position, Color.BlueViolet);
str = "Avatar direction"
+ avatarDirection.ToString();
position.Y = position.Y + 20;
spriteBatch.DrawString(Font, str, position, Color.BlueViolet);
str = "Camera Target"
+ camera.Target.ToString();
position.Y = position.Y + 20;
spriteBatch.DrawString(Font, str, position, Color.BlueViolet);
str = "Camera Position"
+ camera.Position.ToString();
position.Y = position.Y + 20;
spriteBatch.DrawString(Font, str, position, Color.BlueViolet);
str = " Avatar rotation radion"
+ avatarLeftRightRot.ToString();
position.Y = position.Y + 20;
spriteBatch.DrawString(Font, str, position, Color.BlueViolet);
spriteBatch.End();
|
恢复原来的参数:
GraphicsDevice.BlendState = BlendState.Opaque; GraphicsDevice.DepthStencilState = DepthStencilState.Default; GraphicsDevice.SamplerStates[0] = SamplerState.LinearWrap; // TODO: Add your drawing code here basicEffect.VertexColorEnabled = false; basicEffect.TextureEnabled = true; basicEffect.Texture = textureFloor; basicEffect.World = Matrix.Identity; basicEffect.View = camera.ViewMatrix; basicEffect.Projection = camera.ProjectionMatrix; foreach (EffectPass pass in basicEffect.CurrentTechnique.Passes) { pass.Apply(); device.SetVertexBuffer(groundVertexBuf); device.DrawPrimitives(PrimitiveType.TriangleStrip, 0, 2); }
比较下 恢复GraphicsDevices之前和之后的状态
看,这回地板出来了吧!
本人邮箱: pony1976@sina.com ,欢迎转载,请保留邮箱。
参考文章链接:
http://blogs.msdn.com/b/shawnhar/archive/2010/06/18/spritebatch-and-renderstates-in-xna-game-studio-4-0.aspx?CommentPosted=true#commentmessage