2
\$\begingroup\$

I've followed example from various place regarding setting OutputMerger's BlendState to enable alpha/transparent texture on mesh. The setup is as follows:

            var transParentOp = new BlendStateDescription {
            SourceBlend = BlendOption.SourceAlpha,
            DestinationBlend = BlendOption.InverseDestinationAlpha,
            BlendOperation = BlendOperation.Add,
            SourceAlphaBlend = BlendOption.Zero,
            DestinationAlphaBlend = BlendOption.Zero,
            AlphaBlendOperation = BlendOperation.Add,
        };

I've made up a sample that display 3 mesh A, B and C, where each overlaps one another. They are drawn sequentially, A to C. Distance from camera where A is nearest and C is furthest. So, the expected output is that A see through and saw part of B and C. B will see through and saw part of C.

But what I get was none of them see through in that order, but if I move C closer to the camera, then it will be semi transparent and see through A and B. B if move closer to camera will see A but not C. Sort of reverse.

So it seems that I need to draw them in reverse order where furthest from camera is drawn first then nearest to camera is drawn last.

Is it suppose to be done this way, or I can actually configure the blendstate so it works no matter in which order i draw them?

Thanks

\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

Yes, that's the basics of blending: you must manually sort your objects from farther to closer, so when object B is drawn and occludes A, the alpha function merges the two colors correctly. Also, disable depth writing when drawing transparent object.

\$\endgroup\$
2
  • \$\begingroup\$ Thanks.Why do we need to disable depth writing when drawing transparent object? And we need to disable per draw call of each mesh? Will that take away depth info as I was having that problem with depth earlier (stackoverflow.com/questions/5119641/…) \$\endgroup\$
    – faulty
    Commented Feb 27, 2011 at 7:06
  • \$\begingroup\$ No, you just disable the depth state once and then draw all the transparent objects after that call. This is done because since you can see through alpha blended polygons, if you output depth information and some object is then drawn farther, it will appear with some parts "cut" by the transparent outline. \$\endgroup\$
    – r2d2rigo
    Commented Feb 27, 2011 at 13:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .