XNA Creators Club Online
Page 1 of 1 (3 items)
Sort Posts: Previous Next

Draw Text in 3D world with depth buffer

Last post 5/16/2009 8:37 PM by Daniel Hanson. 2 replies.
  • 5/16/2009 3:24 PM

    Draw Text in 3D world with depth buffer

    Just in case anybody else is wondering how to draw text at a 3d position in the world which gets occluded by geometry, here is a little function I wrote:

            public void DrawText(Camera camera, string text, Vector3 position, Color color) 
            { 
                Vector3 screenPos = camera.Viewport.Project(position, camera.ProjectionMatrix, camera.ViewMatrix, Matrix.Identity); 
     
                spriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Immediate, SaveStateMode.None); 
                RenderState renderState = Game.Instance.GraphicsDevice.RenderState; 
                renderState.DepthBufferEnable = true
                renderState.DepthBufferWriteEnable = false
                spriteBatch.DrawString(font, text, new Vector2(screenPos.X, screenPos.Y), color, 0, Vector2.Zero, 1, SpriteEffects.None, screenPos.Z); 
                spriteBatch.End(); 
            } 
     

    Hope it's helpful. I used a few classes from my own game (Camera, Game.Instance) but it should be pretty self explanatory where the variables came from.
  • 5/16/2009 3:47 PM In reply to

    Re: Draw Text in 3D world with depth buffer

    I was just trying to figure out how to do this yesterday.  Thanks!
  • 5/16/2009 8:37 PM In reply to

    Re: Draw Text in 3D world with depth buffer

    Be careful. Using SpriteBatch and 3D at the same time affects more in the render state than just the depth buffer. See this article by Shawn Hargreaves.
    Previously known as "Rainault".
    Twitter - me, Jade Vault Games
    Announcing ASCII Quest, a Roguelike under development for Xbox LIVE Indie Games
Page 1 of 1 (3 items) Previous Next