Question

Essentially, what I want to do (in DirectX) is to take two partially-transparent images and blend them together. This works fine with default blending, insofar as they both show up as overlapping, etc. However, the problem is that the opacity goes up markedly where the two intersect. This causes increasing problems as more sprites overlap. What I'd like to do is keep the blending the same, except keep a global opacity for all these sprites being blended, regardless of how they overlap.

Seems like there would be a render setting for this (all of these sprites are alone in their sprite batch, which keeps that part easy), but if so I don't know it. Right now I'm kind of shooting in the dark, and I've tried a lot of different things and none of them have looked right at all. I know I probably need some sort of variant of D3DBLENDOP, but I just don't know what sort of settings there I really need (I have tried many things, but it is all guessing at this stage).

Here is a screenshot of what is actually happening with standard blending (the best I can get it): http://arcengames.com/share/FFActual.png Here is a screenshot with a mockup of how I would want the blending to turn out (the forcefields were added to the same layer in Photoshop, then given a shared alpha value): http://arcengames.com/share/FFMockup.png

This is how I did it in Photoshop: 1. Take the two images, and remove all transparency (completely transparent pixels excepted). 2. Combine them into one layer, which blends the color but which has no partial alpha at all. 3. Now set the global transparency for that layer to (say) 40%.

The result is something that looks kind of blended together color-wise, but which has no increase in opaqueness on the overlapped sections.

UPDATE: Okay, thanks very much to Goz below, who suggested using the Z-Buffer. That works! The blending, by and large, is perfect and just what I would want. The only remaining problem? Using that new method, there is a huge artifact around the edge of the force field image that is rendered last. See this: http://www.arcengames.com/share/FFZBuffer.png

UPDATE: Below is the final solution in C# (SlimDX)

  1. Clearing the ZBuffer to black, transparent, or white once per frame all has the same effect (this is right before BeginScene is called)

Direct3DWrapper.ClearDevice( SlimDX.Direct3D9.ClearFlags.ZBuffer, Color.Transparent, 0 );

  1. All other sprites are drawn at Z=1, with the ZBuffer disabled for them:

device.SetRenderState( RenderState.ZEnable, ZBufferType.DontUseZBuffer );

  1. The force field sprites are drawn at Z=2, with the ZBuffer enabled and ZWrite enabled and ZFunc as Less:

device.SetRenderState( RenderState.ZEnable, ZBufferType.UseZBuffer ); device.SetRenderState( RenderState.ZWriteEnable, true ); device.SetRenderState( RenderState.ZFunc, Compare.Less );

  1. The following flags are also set at this time, to prevent the black border artifact I encountered:

device.SetRenderState( RenderState.AlphaTestEnable, true ); device.SetRenderState( RenderState.AlphaFunc, Compare.GreaterEqual ); device.SetRenderState( RenderState.AlphaRef, 55 );

Note that AlphaRef is at 55 because of the alpha levels set in the specific source image I was using. If my source image had a higher alpha value, then the AlphaRef would also need to be higher.

Was it helpful?

Solution

Best I can tell is that the forcefields are a whole object. Why not render them last, in front to back order, and with Z-buffering enabled. That will give you the effect you are after.

ie its not blending settings thats your problem at all.

Edit: Can you use render-to-texture then? IF so you could easily do what you did under photoshop. Render them all together into the texture and then blend the texture back over the screen.

Edit2: How about

ALPHATESTENABLE  = TRUE;
ALPHAFUNC = LESS

ALPHABLENDENABLE = TRUE;
SRCBLEND = SRCALPHA;
DESTBLEND = INVSRCALPHA;

SEPERATEALPHABLENDENABLE = TRUE;
SRCBLENDALPHA = ONE;
DESTBLENDALPHA = ZERO;

You need to make sure the alpha is cleared to 0xff in the frame buffer each frame. You then do the standard alpha blend. while passing the alpha value straight through to the backbuffer. This is, though, where the alpha test comes in. You test the final alpha value against the one in the back buffer. If it is less than whats in the backbuffer then that pixel has not been blended yet and will be put into the frame buffer. If it is equal (or greater) then it HAS been blended already and the alpha value will be discarded.

That said ... using a Z-Buffer would cost you a load of RAM but would be faster overall as it would be able to throw away the pixels far earlier in the pipeline. Seeing as all the shields would just need to be written to a given Z-plane you wouldn't even need to go through the hell I suggested earlier. If the Z value it receives is less than whats there already then it will render it if it is greater or equal then it will discard it, fortunately before the blend calculation is ever performed.

That said ... you could also do it by using the stencil buffer which would require a Z-buffer anyway.

Anyway ... hope one of those methods is of some help.

Edit3: DO you render the forcefield with some form of feathering around the edge? Most likely that edge is caused by the fact that the alpha fades off slightly and then the "slightly alpha" pixels are getting written to the z-buffer and hence any subsequent draw doesn't overwrite them.

Try the following settings

ALPHATESTENABLE = TRUE
ALPHAFUNC = GREATEREQUAL // if this doesn't work try less .. i may be being a retard
ALPHAREF = 255

To fine tune the feathering around the edge adjust the alpharef but i'd suspect you need to keep it as above.

OTHER TIPS

You can specify the D3DBLENDOP used when blending the two images together for the alpha channel. It sounds like your using D3DBLENDOP_ADD currently - try switching this to D3DBLENDOP_MAX, as that will just use the opacity of the "most opaque" image.

It is hard to tell exactly what you are trying to accomplish from your mock up since both forcefields are the same color; do you want to blend the colors and cap the alpha? Just take one of the colors?

Based off the above discussion it isnt' clear if you are setting all the relevant render states:

D3DRS_ALPHABLENDENABLE = TRUE (default: FALSE)

D3DRS_BLENDOP = D3DBLENDOP_MAX (default: D3DBLENDOP_ADD)

D3DRS_SRCBLEND = D3DBLEND_ONE (default: D3DBLEND_ONE)

D3DRS_DESTBLEND = D3DBLEND_ONE (default: D3DBLEND_ZERO)

It sounds like you are setting the first two, but what about the last two?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top