Texture blending on transparent background

This topic is 3050 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

I want some different way of blending textures. I do a Clear of the background and make it completely transparent. Then let's say I blend a a pure white texture on top with 50% transparency. The texture gets blended with the black background, and if I make a screenshot I'll have a 128,128,128 gray block, I'll have 25% transparency. What I want is for the texture color to stay intact, so basically end up with the original white block and 50% transparency. Any part on top of another texture should blend with what's already there as normal. The background shouldn't be considered as something to blend with, it should act as the white/gray blocked transparent background in Photoshop for example. Thanks for any pointing in the right direction.

Share on other sites
Say you render your white 50% block into the framebuffer the way you want, and then you render a red 50% block on top of it. What result do you want it to produce?

Share on other sites
Maybe try to play a bit with the D3DRS_SEPARATEALPHABLENDENABLE rendering state (together with D3DRS_SRCBLENDALPHA etc.), which allows you set different blending for color and for alpha.

But I'm not sure I understood you correctly. ;)

Share on other sites
After a bit of thinking, I feel I understand what you want. :) BTW, what you ask for makes sense only if you draw everything in a specific order, and don't use a Z buffer (which is a normal assumption when drawing transparent stuff).

I can't think of a simple solution for that, but I can think of a complex one:

Use two passes with stencil. First pass draws without blending, and doesn't draw again on any area that's been previously drawn (this is done by setting the stencil while drawing and not drawing anything with that stencil's value). The second pass draws everything with alpha.

Another way that might work is drawing front to back, and blending using the frame buffer's alpha. You'll have to start with opaque alpha, though, so you'll still need stencil to detect where things weren't draw and turn that transparent at the end. And I'm not sure of the details of this, though googling "front to back blending" did turn out a few links that looked relevant.

Share on other sites
Basically I want it to behave like Photoshop :)

In the end I want to be able to render scenes and use the resulting image to output it via some other output card. Currently I render things back to front, so layer on layer without Z buffer. For example some box and on top of that some text.

All of that works fine, but just not for transparent objects on top of the empty background, because they get blended with the background color. If it's just a little transparent you get away with it, but it's silly that this way I can never have a transparent white texture somewhere if it floats above the background, because then it turns gray.

If I would render a 50% white block and a 50% red block on top of it, I would like to end up with 255,85,85 as color and 75% opacity in that place.

Share on other sites
If I understand what you're trying to do, I believe I had a similar problem.

I was rendering a number of non-z-ordered alpha-blended objects into a texture that was initialised with a transparent background. I then wanted to render this resulting texture into my screen's scene. I had the same problems you describe.

What I worked out was the following formula that renders everything as Photoshop does... (assume the following formula is applied to each pixel...)

// AlphaA  Alpha of the point within the texture being written// AlphaB  Alpha of the point in the background texture// AlphaN  Resultant alpha// ColA    Colour of the point within the texture being written// ColB    Current colour of the point in the background texture// ColN    Resultant colourAlphaN	= AlphaA + (1 - AlphaA) * AlphaBColN    = ColB + (AlphaA * (ColA - ColB)) / AlphaN

I was at the time using the fixed function pipeline and did look for a way of achieving this that way... but no luck. In the end (and I'm pleased I did) I resorted to shaders. It all works now as it should - and it's an exact match with Photoshop.

Hope that helps,
Tim

[Edited by - blairhartley on April 15, 2010 9:02:40 AM]

Share on other sites
Sounds like it's just what I need :)

To calculate this using a shader it will need to know the current color and alpha in the target. I don't think it's possible to know from within a shader what's currently in the backbuffer. Does that mean I'll have to render to a texture if I want to make this work? Or is there some other way to do it?

Share on other sites
Can someone point me in the right direction? How can I know inside my pixelshader what color is currently already on my current pixel position?

Share on other sites
I know what you mean, I had exactly the same problem some time ago. The solution is to use premultiplied alpha instead of straight alpha.

A great article about premultiplied alpha can be found here:
Premultiplied alpha
There's also a follow-up about image composition (which is exactly what you're trying to do):
Premultiplied alpha and image composition

Share on other sites
Thanks for the info. I gave it some tries, but that still doesn't give the right result. If I render a red square with 50% transparency and use premultiplied alpha, I end up with the red channel being half of what it was, because I just multiplied it by 0.5 :) Or am I misunderstanding something?

It basically comes down to being able to make the following decision when blending, at every pixel I blend:
- If no pixel was written before at the current position i want to copy the exact color and alpha from the texture, so no blending.
- If a pixel was written before at the current position, do blending (either 'normal' or premultiplied alpha) with the previously written pixel.

[Edited by - jbravo on May 8, 2010 12:18:00 PM]

1. 1
2. 2
Rutin
21
3. 3
4. 4
frob
17
5. 5

• 9
• 12
• 9
• 33
• 13
• Forum Statistics

• Total Topics
632590
• Total Posts
3007249

×