XNA Render Multiple Alpha Issue
I'm having an issue with trying to render multiple alpha objects over the top of each other. At the moment, if i render one alpha object on top of another alpha object, the top alpha object is displayed but the bottom object isn't until you go past/around the first object. I'm trying to get an additive blending effect, so the top object shows that there is another transparent object behind it. How do you do this in XNA?? My current render state settings are:
RenderState rs = Globals.g_Device.RenderState;
rs.AlphaBlendEnable = true;
rs.BlendFunction = BlendFunction.Add;
rs.DepthBufferFunction = CompareFunction.LessEqual;
rs.DepthBufferEnable = true;
rs.D开发者_如何学运维epthBufferWriteEnable = true;
Globals.g_Device.PresentationParameters.EnableAutoDepthStencil = true;
Globals.g_Device.PresentationParameters.AutoDepthStencilFormat = DepthFormat.Depth24Stencil8;
The depth buffer is so that it doesn't matter what order you render objects in. If you draw a pixel "under" what is already drawn, it is just skipped. If you draw a pixel "above" what is on screen, it gets drawn and overwrites the contents of the screen (and the depth buffer, which provides the correct depth to compare with the next triangle drawn).
Unfortunately this will not work for transparent objects. If you draw a transparent object on top of your scene, it will blend with the visible scene. But there is no way to blend depth, so it just overwrites it. So when you go to draw another object "under" that transparent one, it gets skipped as normal.
Here is the general process for rendering a scene containing transparent objects:
Draw the opaque objects in your scene with depth reads and writes turned on.
Sort your transparent objects from back to front.
Draw your sorted transparent objects with depth reads turned on.
When you do this, your transparent objects will be correctly obscured by opaque objects. But, because they are correctly sorted, transparent objects won't have an opportunity to obscure other transparent objects yet to be drawn.
精彩评论