xna 4.0 在3D 场景中用spritebatch 写2D文字3D显示不正常的问题

     用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();

  恢复原来的参数:

xna 4.0 在3D 场景中用spritebatch 写2D文字3D显示不正常的问题
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);
            }
xna 4.0 在3D 场景中用spritebatch 写2D文字3D显示不正常的问题

比较下 恢复GraphicsDevices之前和之后的状态

xna 4.0 在3D 场景中用spritebatch 写2D文字3D显示不正常的问题xna 4.0 在3D 场景中用spritebatch 写2D文字3D显示不正常的问题

看,这回地板出来了吧!

本人邮箱: 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

xna 4.0 在3D 场景中用spritebatch 写2D文字3D显示不正常的问题

上一篇:联名强烈要求将卑劣小人天天恶意污辱诋毁他人与作品整日到处抹屎的博客园毒瘤garbageMan(薛非)赶出博客园!支持的博友请发评论并点支持!


下一篇:Linux虚拟存储系统