Sprites (Images and Text) |
This topic contains the following sections:
A sprite is a 2D image, such as a bitmap or a text. Sprites are positioned in world space, but rendered in screen space. That means, a 16x16 pixel image is usually exactly 16x16 pixel on screen. (Internally the images and texts are rendered using the XNA SpriteBatch, hence the name.)
Here is an example of a 2D sprite in a 3D scene:
There are two types of sprites: ImageSprites and TextSprites.
A sprite is positioned in world space by creating a new SpriteNode and adding it to a 3D scene. Sprite nodes support simple hit testing. I.e. it is easy to check whether the mouse cursor has hit a certain sprite node. See SpriteNode for more information.
The SpriteRenderer is a SceneNodeRenderer that handles SpriteNodes.
Internally, the SpriteRenderer is basically just a wrapper around XNA's SpriteBatch. It converts the positions of the sprite nodes from world space to screen space and draws the images and texts using to the SpriteBatch.
Example:
// Set desired depth-stencil and blend states. graphicsDevice.DepthStencilState = DepthStencilState.None; graphicsDevice.BlendState = BlendState.AlphaBlend; // Render a list of sprite nodes. spriteRenderer.Render(sceneNodes, context, RenderOrder.BackToFront);