I've started looking into adding some deep sky objects such as star clusters and galaxies, and have been thinking about the following:
- Loading the data.
- Sprite markers for the position, size and type of deep sky object.
- Rendering a text label at the appropriate position and scale.
Once again there is plenty of data available in the public domain. I found some basic lists of objects to get me started, converted the data to XML and used the automatic content serialization in XNA Game Studio 3.1 to compress the XML to an XNB file, as per my Background Stars.
Adding sprites in the appropriate positions was relatively straightforward. In order to specify the size of the object, I created a custom VertexElement structure, VertexPositionColorSize, which let me set a VertexBuffer with all the data the shader needed to position, color and scale the sprite.
The labels were more interesting in that there were several options. I had the following requirements:
- I wanted the labels to render efficiently, as there may be many instances visible at once.
- I was considering scaling the labels as a size/proximity dimension.
- I didn't need to use the depth buffer since these labels would always be at the "back".
With these requirements in mind, the following approaches were apparent:
- Use SpriteBatch.DrawString() with SpriteFonts.
- Using line primitives to render vector-based fonts.
The SpriteBatch approach is shown below in Figure 1. While not ideal for scaling labels to large sizes (the fonts are drawn as scaled bitmaps), the approach proved surprisingly performant.
It is possible to extract vector definitions from the "Modern", "Roman", and "Script" Windows Fonts. I took the approach of pre-generating the line primitives for a given label at load time and passing them to the shader with a given scale and offset per frame. This proved slightly more performant than the SpriteBatch approach, and provided a good option for scaling the text to large sizes without occluding any objects in the background. Examples are shown in Figures 2 - 4.
Note that the Deep Sky Objects are shown at an exaggerated scale for clarity, and without anti-aliasing.