0

In alpha compositing wiki, I see the blending equation is like this (no pre-multiplying):

equation0

But in this slides, the equation is like this:

equation1

In the first equation, outRGB is divided by outAlpha. What does it mean? Which one is the correct equation?

K.Miao
  • 811
  • 7
  • 21

1 Answers1

3

The first formula with division is correct. The second one is either wrong or is designed to take non-premultiplied colors and produce premultiplied ones.

Anyway, it's pretty easy to derive the non-premultiplied formula from the premultiplied one, which happens to be very simple and intuitive:

blended = front + back * (1.0 - front.a);

The intuition is: we take the front color and add a bit of the back color to it, namely as much as we can see through the front color.

The non-premultiplied version will then be:

blended.a = front.a + back.a * (1.0 - front.a);
blended.rgb = (front.rgb * front.a + back.rgb * back.a * (1.0 - front.a)) / blended.a;

Note that this formula, as well as the ones you presented assumes alpha to be in the range of [0, 1] rather than [0, 255].

Joseph Artsimovich
  • 1,499
  • 10
  • 13
  • But in OpenGL/WebGL, the non-premultiplied function is like this:`gl.blendEquationSeparate( gl.FUNC_ADD, gl.FUNC_ADD ); gl.blendFuncSeparate( gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA, gl.ONE, gl.ONE_MINUS_SRC_ALPHA );` So it's `blended.rgb = front.rgb * front.a + back.rgb * (1 - front.a)`. It's different from the above equation you mentioned. Why? – K.Miao Jul 16 '17 at 17:52
  • 1
    That formula only works with a fully opaque background, that is back.a == 1. OpenGL's blending machinery simply can't handle the general "translucent A over translucent B" case for non-premultiplied colors. – Joseph Artsimovich Jul 17 '17 at 07:13