Particle Blend Issue with premultiplied alpha

Started by
5 comments, last by aprilspirit89 11 years, 9 months ago
I was trying to save some texture from 3D rendering scene,and then get them to reuse.
The big problem is that rgb value can not match its alpha value.I need some picture without black edge,so I must use rgb colors to divide its alpha value(Image manipulation software such as Photoshop can just deal with picture which alpha channel is nonpremultiplied.)
Unfortunally,the color is too light that some result value are cut off to 1.
So I turned to a technique called premultiplied alpha.(See more). Instead of using shaders,I just use separate alpha calculation.
For example:
RenderState.SourceBlend = Blend.SourceAlpha;
RenderState.DestinationBlend = Blend.InverseSourceAlpha;

Now I add some renderstate.
RenderState.SourceBlendAlpha = Blend.One;
RenderState.DestinationBlendAlpha = Blend.InverseSourceAlpha;

It works well.But when I try to handle following things:
RenderState.SourceBlend = Blend.SourceAlpha;
RenderState.DestinationBlend = Blend.One;
RenderState.SourceBlendAlpha = Blend.One;
RenderState.DestinationBlendAlpha = Blend.One;

The result is totally wrong.
Because when I use nonpremultiplied blend state,which is SourceAlpha and inverseSourceAlpha.The rgba value is definitely controlled within 0~1.But when I switch to additive state,which is SourceAlpha and 1,the rgb values might be over one,thus cause the incorrect value.
Now my problem is how to control the alpha value,to make sure it keep all details and do not overflow at the same time?

//Modified
PS:If you don't understand what I am trying to do,there is a commercial software called "Particle Illusion".You can create various particles and then save the scene to texture,where you can choose to remove background of particles.
[attachment=9970:particleIllusionDemostrate.png]
Advertisement
The states for premultiplied alpha blending are:
[font=courier new,courier,monospace] RenderState.SourceBlend = Blend.One; [/font]
[font=courier new,courier,monospace] RenderState.DestinationBlend = Blend.InverseSourceAlpha; [/font]

The states for premultiplied alpha blending are:
[font=courier new,courier,monospace] RenderState.SourceBlend = Blend.One; [/font]
[font=courier new,courier,monospace] RenderState.DestinationBlend = Blend.InverseSourceAlpha; [/font]

Thank you for your reply.I had tried the original pre-multiplied alpha technique.But I must set alpha to 0 when I use additive mode,that destroy the final alpha.
It is not correct to use SourceAlpha, One when using pre-multiplied alpha in order to do an additive blend. The thing with pre-multiplied alpha is that an additive blend is controlled purely by the alpha channel of the source.

Here is the formula for the typical SrcAlpha/OneMinusSrcAlpha blend:


OutColor=Src.rgb*Src.alpha + Dest.rgb*(1-Src.alpha)


Premultiplication merely performs the Src.rgb*Src.alpha step beforehand, so that component of the function can be replaced as so:


OutColor=Src.rgb+Dest.rgb*(1-Src.alpha)


Say you have a typical alpha-blended sprite with alpha of 1 where the colors are non-zero (ie not black, or not supposed to be transparent) and alpha of 0 with rgb=0 otherwise. The alpha is premultiplied. Wherever the color is black and the alpha is 0, the result of the operation is this:

OutColor=0+Dest.rgb*(1-0)
OutColor=Dest.rgb


Meaning that the final pixel is solely what is already in the destination, no blend. Conversely, wherever there is color and an alpha of 1:


OutColor=Src.rgb + Dest.rgba*(1-1)
OutColor=Src.rgb


Meaning that the final pixel is solely what is in the source, none of the destination affects it. Values in between 0 and 1 properly affect it.

Now this assumes that the Src.rgb*src.alpha operation was done as a pre-process pass, so that places where the alpha=0 are set to rgb=0,0,0. What if this is not the case, though? What if you have a pixel in the source with an alpha of 0 and a non-zero rgb?


OutColor=Src.rgb+Dest.rgb*(1-0)
OutColor=Src.rgb+Dest.rgb


The result is an additive blend of Src.rgb and Dest.rgb. No change of blend state is required. By varying the alpha value, you can reduce the power of the additive effect until at 1 it becomes just the standard replacement blend. So by keeping your alpha values close to 0, you can have varying strengths of additive blend, without having to overload the source alpha to 0 and losing your details.


Now this assumes that the Src.rgb*src.alpha operation was done as a pre-process pass, so that places where the alpha=0 are set to rgb=0,0,0. What if this is not the case, though? What if you have a pixel in the source with an alpha of 0 and a non-zero rgb?


OutColor=Src.rgb+Dest.rgb*(1-0)
OutColor=Src.rgb+Dest.rgb


The result is an additive blend of Src.rgb and Dest.rgb. No change of blend state is required. By varying the alpha value, you can reduce the power of the additive effect until at 1 it becomes just the standard replacement blend. So by keeping your alpha values close to 0, you can have varying strengths of additive blend, without having to overload the source alpha to 0 and losing your details.


Thank you very much.Now I'm a little confused.You said I can keep alpha value to 0,to vary the strength of additive mode.But I can't see other way except to change the source alpha.I do the premultiplication in PS shader,then set alpha to 0.And the pipeline will do the alpha blend thing like this

OutColor=Src.rgb+Dest.rgb*(1-0)
OutColor=Src.rgb+Dest.rgb
//srcColor 1
//destColor 1-srcAlpha

Then I get correct color and wrong alpha.Did I do it in a wrong way?
I said close to 0. If you give your source alpha values some low range to vary within, you can get the additive blend to greater or lesser degree. It's all just how you play with alpha. But yes, if you just naively set your alpha to 0, you're going to lose whatever variance exists within the alpha channel of the texture.

Also, why are you doing the premultiplication in the shader? That's not premultiplication. That's just multiplication. The typical use-case of pre-multiplication is that you perform the Source.rgb*Source.a during asset creation, when the texture is created (via Photoshop, Gimp, or whatever); or, conversely, as some sort of asset-crunching process pass done, say, when you do a rebuild of your asset tree in preparation for a release build of your game. Or maybe at game load, if you don't have to do a whole bunch of them. However you construct your asset pipeline. The thing is, if you do the multiply in the shader, then you don't take advantage of the additive use-case of premultiplied alpha, which doesn't actually multiply Source.rgb*Source.a. You're just defaulting everything to a regular Source.rgb*Source.a blend.

However, before you go with a solution such as premultiplied alpha, why don't you figure out exactly what it is you are trying to achieve, and figure out exactly what configuration of blend modes and alpha setups you need to achieve the effect?

However, before you go with a solution such as premultiplied alpha, why don't you figure out exactly what it is you are trying to achieve, and figure out exactly what configuration of blend modes and alpha setups you need to achieve the effect?

You're very helpful.I thought doing multiply alpha it in shader is equal to doing it in making assert stage.Yes,my problem maybe not the proper customer for pre-multiplied alpha technique.But so far,it's the closet way,because I have good result about alpha blend mode(srcAlpha VS 1-srcAlpha).
As said above,I am rendering 3D particle sytems in 3D world,then I'm trying to save them to 2D texture arrays,so that they can be reused to 3D or 2D game world.I got a texture seems alright but shows black edges in Photoshop.They are inflenced by the default back ground color.I described my problem here.
What the PS accept is straight alpha,which is opposite to pre-multiplied alpha.RGB value should be independent from alpha.But rgb is definitely influnced by alpha during rendering process.So I tried to get rid of it.Because I got both premultilied alpha texture(particle resource) and unpremultiplied ones,I use mix alpha mode,additive and blend.Just devide rgb by alpha won't work.I studied premultiplied alpha and it gives me an inspiration,so I separate alpha calculation.It works well with alpha blend mode,while additive mode still remains unsolved.

This topic is closed to new replies.

Advertisement