A Z-buffered Sprite uses the Z buffer (also known as a depth buffer) to perform proper overlapping. Since the Z buffer does not consider partial transparency, creating Z buffered Sprites with transparency can create graphical artifacts.
Z-buffered sprites will only write to and read from the z buffer if the Camera's ClearsDepthBuffer is true.
The following code creates two Z buffered Sprites.
The following code creates two Sprites with the same properties as above, but the Sprites are created as regular (ordered) Sprites instead of Z buffered Sprites. Notice that one Sprite completely overlaps the other.
Z Buffered Sprites with transparency may not draw correctly. The reason for this is because the depth buffer does not necessarily know how to treat transparent pixels. FlatRedBall XNA attempts to resolve this issue by setting an alpha threshold for what gets drawn to the depth buffer (see below). If you are experiencing issues with transparency, consider the following:
If some of your Sprites have transparency while others are solid, try to make only the solid Sprites Z Buffered. Sprites that do not use the Z Buffer will still sort properly with the Z Buffer. Therefore, you may be able to mix Z Buffered with non-Z Buffered Sprites and still achieve the desired overlapping result.
Eliminate transparency - this may not be preferred, but if you can eliminate transparency from your source image, you may be able to avoid this artifact.
Reposition objects or your camera to avoid the undesired graphical issues.
The Alpha Threshold (also known as ReferenceAlpha in XNA terms) is a value that defines the minimum alpha that will be rendered. FlatRedBall uses this value to prevent fully-transparent pixels from rendering to the depth buffer. The end result is that if you have an image which has either fully-opaque or fully transparent pixels, then you can use this image as a Z-Buffered Sprite without any problems. More specifically, the RenderState's ReferenceAlpha property is set to 1 (in a range of 0-255). This means that anything with an alpha value of 1 or greater will be rendered while pixels with an alpha of 0 will not be rendered, and more importantly will not modify the depth buffer.
File used:WhiteGradient.png Since Z buffered Sprites write to the depth buffer, partial transparency can block things behind it. For example, consider the following code: