AddZBufferedSprite
Last updated
Was this helpful?
Last updated
Was this helpful?
A Z-buffered uses the (also known as a ) to perform proper overlapping. Since the does not consider partial transparency, creating with transparency can create graphical artifacts.
Z-buffered sprites will only write to and read from the z buffer if the Camera's is true.
The following code creates two .
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:
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.
The following code creates two Sprites with the same properties as above, but the are created as regular (ordered) instead of . Notice that one completely overlaps the other.
File used: Since Sprites write to the , partial transparency can block things behind it. For example, consider the following code:
If some of your have transparency while others are solid, try to make only the solid 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.