Question

I have problem with trees in XNA. When I have tree, I need to use alpha blending to make non-leaf parts transparent, but some parts of model are not correctly displayed and thus alpha blending fails. I want to know if there is possibility to read mesh position to sort them or is there any other way to have transparent non-leaf parts. Sorting in model probably won't work because I want to look at this tree from all angles.

Was it helpful?

Solution

Alpha blending in a 3D environment can be a tricky issue. Shawn Hargreaves wrote an article several years ago that touches on most of the major issues (the section titled Painter's Algorithm deals with your question specifically). Long story short: there exists no technique to do what you want perfectly, so the question becomes: what trade-offs are you willing to make?

Doing alpha testing rather than alpha blending may be the simplest solution. It's a binary test, where an opaque pixel either appears or doesn't, so the order in which those pixels are drawn is largely irrelevant. This will give you hard edges, but it can still look pretty good if your textures have enough resolution; I know World of Warcraft uses this technique, and I'm pretty sure I remember seeing it in Diablo III.

You can use the built-in AlphaTestEffect to do this, or implement it yourself in a pixel shader.

OTHER TIPS

What you're looking for is called order indepdendent transparency and this can be achieved using a method called "Depth peeling". If you are NOT using the Reach profile of XNA and can write custom shaders - you can implement this technique to achieve correct blending without worrying about mesh level information or having to re-sort them for the current frame/view.

Here is one more DX9 (but easily adapted to XNA) implementation.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top