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.
2 Answers
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.

- 4,846
- 1
- 17
- 20
-
No - there IS a technique to do what he wants. – Ani Dec 18 '12 at 19:31
-
I didn't say that there isn't any technique to do what he wants. Individually sorting every triangle is a very simple technique that does what he wants, for example. I said that there is no _perfect_ technique, and that's also true of depth peeling. It requires you to use the HiDef profile, it requires you to write complex shaders, it requires additional geometry passes that may or may not be acceptable... only Thun can answer the question of what technique suits his needs. I suggested one of many. – Cole Campbell Dec 18 '12 at 20:04
-
This sorting is done at the fragment level, so it actually produces pixel perfect results given enough passes. It is also easy to fine-tune this for performance vs quality. Although you are right in saying that it is complex and requires multiple passes. – Ani Dec 18 '12 at 20:38
-
I used Alpha-testing but at edges there's margin of fail. I mean that on edges alpha fails and pixels don't have proper value. But I guess it's better than writing shaders. I'm learning XNA and I only wrote some basic effects in HLSL(like basic lightning) so depth peeling might be hard for me now. Thanks for help – Thun Dec 18 '12 at 20:58
-
You should be able to tweak the way `AlphaTestEffect` works by modifying its parameters. In particular, the `ReferenceAlpha` property is used to determine the fail/pass threshold. Setting it to 1 should give you a clean edge, but it still probably won't look quite right. You'll need to rework your leaf texture to take into account that you don't have any partial transparency. – Cole Campbell Dec 18 '12 at 21:02
-
Actually, now that I've looked at the documentation, you'll probably also want to take a look at the `AlphaFunction` property, which determines the operation used to compare pixels to the `ReferenceAlpha` value. The defaults are `AlphaFunction` = `Greater` and `ReferenceAlpha` = `0`, which means, "only allow pixels with an alpha component greater than zero." – Cole Campbell Dec 18 '12 at 21:04
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.

- 10,826
- 3
- 27
- 46