Search code examples
3dtransparencyxna-4.0

XNA 3D Transparency depending on drawing order?


I am drawing two 3D objects, both of them can fade from opaque to transparent independently, and they can intersect between them (so you cannot say when one of them is before the other one). Look at the image for a better understanding (one of the object is a terrain and the other one an area):

Correct draw

Now, if I apply transparency to both of them, and draw the terrain before the area, the terrain is not transparent respecting to the area, but the area is:

Incorrect transparency

And finally, if I draw the area before the terrain, then the area is not transparent respecting of the terrain:

Incorrect transparency 2

QUESTION: How can I make all the objects transparent to the rest of objects without depending on the drawing order?


Solution

  • You can't. At least, not trivially - the operation you are doing is inherently order dependent. Even your terrain on its own will exhibit issues when rendered from different view points and translucency.

    So really you either have to change what you are doing such that you use a non order-dependent operation (such as additive or subtractive blending, though this probably won't get a useful result in your case) or explore other options for controlling draw order beyond just sorting at an object level.

    For example, you could investigate depth-peeling, which involves rendering your scene in multiple passes, compositing pixels in layers. That can handle intersecting polygons, but the more layers you need to render, the more expensive it's going to get.