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

Started by
5 comments, last by ayrton2388 12 years, 11 months ago
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.

[media]
[/media]
Advertisement
The easiest solution is to use alpha-testing instead of alpha blending.
As far as i know , you cant use alpha-testing in XNA 4.0. Am i wrong?
Hi there. You will run into performance problem if you make your trees this way. You should split the tree into a trunk mesh and use quads for leaves. Then you will want to also use instancing. Look up the dx sdks island instance sample.
The wonderful Shawn Hargreaves has blogged about this exact issue:

http://blogs.msdn.com/b/shawnhar/archive/2009/11/06/premultiplied-alpha.aspx

and here:

http://blogs.msdn.com/b/shawnhar/archive/2011/05/06/antialiasing-alpha-cutouts.aspx

He had another more related post, but I can't seem to find it at the moment.

As far as i know , you cant use alpha-testing in XNA 4.0. Am i wrong?


You certainly can, there's an AlphaTest effect built in for it. Or do the alpha testing in your own shader (it's shader-based in 4.0, like clipping planes).

As for using it, I think the biggest thing you have to worry about is the aliasing. You get a freebie in terms of not having to order it (since the pixels that are rejected are simply rejected, rather than the pixel being kept and blended with whats underneath it), but you may have ugly cut outs. But if you use pre-multiplied alpha you can reduce those artifacts (as described by the links Moe posted).

It seems Shawn is posting a lot about anti-aliasing these days, heh.
Thank you for your replies. I really appreciate it.

I was thinking about using a two pass rendering technique for now.

* 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?
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. I will sort that out somehow, but i wanna know if im going in a right direction.

This topic is closed to new replies.

Advertisement