[XNA ]Drawing the alpha-blended leafs of a 3d tree. Video inside

577f9d13db6c0246b4a0bbdf959e7099
0
ayrton2388 101 May 19, 2011 at 12:48

I need some help rendering this model right. It`s a palm tree, made of a single mesh.

I am using BlendState.AlphaBlend before drawing it, but it looks wrong. I know that transparent models should be ordered and all, but since all the leaves are on a single mesh, how can i render it correctly? Here is a short video, so you can see for yourself.

http://www.youtube.com/watch?v=phDOAQ27Ocs

Any ideas?

5 Replies

Please log in or register to post a reply.

350d397466e93a8ca8dac72b068fc5fb
0
Brac 101 May 19, 2011 at 14:27

For alphablending to work you would have to sort the triangles, so that is not an option.

Use AlphaTest with a reasonable AlphaRef limit (aka only draw pixels above X alpha)

A8433b04cb41dd57113740b779f61acb
0
Reedbeta 168 May 19, 2011 at 17:06

You could use a modeling tool to split up the leaves into separate pieces that you can then sort (this may not fix the problem 100%). You could use alpha-test as Brac suggested (indeed this is how many, many games draw vegetation). If you’re comfortable with shaders, rendertargets and so forth, you could also explore more advanced techniques such as depth peeling or the newer stochastic transparency.

577f9d13db6c0246b4a0bbdf959e7099
0
ayrton2388 101 May 19, 2011 at 18:37

Thank you for the replies.

Isn`t order independent transparency a bit expensive? Anyway, might be a bit out of my league. I did write some basic shaders, but still pretty new to them.

Someone suggested that i should use a two pass rendering technique:

* Pass 1: draw the solid part: alpha blending disabled, alpha test set to only accept the 100% opaque areas, and depth buffer enabled
* Pass 2: draw the fringes: alpha blending enabled, alpha test set to only accept pixels with alpha < 1, depth buffer enabled, depth writes disabled

Would this work in my case?
EDIT: just tried the default AlphaTestEffect. It`s way better, of course, but i still get some pixels wrong. Here`s a screen:

http://imageshack.us/f/695/alphacv.jpg/

Again, would that two pass render help here?

A8433b04cb41dd57113740b779f61acb
0
Reedbeta 168 May 19, 2011 at 19:01

That will help somewhat. The sorting will definitely be correct in the opaque areas, but it will still be wrong in the translucent areas. But if the translucent areas are only a thin fringe around the edges of the leaves, it might not be very noticable.

It will also help performance (probably). The two-pass method is likely to be faster than rendering everything transparently because on the second pass, you’ll be getting early-Z-culling from the depth laid down in the first pass. YMMV though.

As for the order-independent techniques, yes they will be more expensive on the GPU. They can still be useful for cases where correctness is a must and per-object or per-poly sorting isn’t going to cut it, or is prohibitively expensive. But it’s just one more tool in the toolbox.

577f9d13db6c0246b4a0bbdf959e7099
0
ayrton2388 101 May 19, 2011 at 19:12

Thank you. I really appreciate your help, guys.
Thanks a lot!

It`s not hard to write that shader, i can do it, but i cant texture these tree models right in my custom shader yet, since they have 2-3 textures, and there`s just 1 mesh. But at least i know im going in a right direction.