X-Git-Url: https://git.sesse.net/?p=movit;a=blobdiff_plain;f=overlay_effect.frag;fp=overlay_effect.frag;h=38970eab7e85469943353b85e9690d87ba59e5a8;hp=0000000000000000000000000000000000000000;hb=5e05605a85a15944b95d7000745446756cdcbfa8;hpb=49ef2523611e215b327cd26f7bf698d1516edd46 diff --git a/overlay_effect.frag b/overlay_effect.frag new file mode 100644 index 0000000..38970ea --- /dev/null +++ b/overlay_effect.frag @@ -0,0 +1,26 @@ +// If we didn't have to worry about alpha in the bottom layer, +// this would be a simple mix(). However, since people might +// compose multiple layers together and we don't really have +// any control over the order, it's better to do it right. +// +// These formulas come from Wikipedia: +// +// http://en.wikipedia.org/wiki/Alpha_compositing + +vec4 FUNCNAME(vec2 tc) { + vec4 bottom = INPUT1(tc); + vec4 top = INPUT2(tc); + float new_alpha = mix(bottom.a, 1.0, top.a); + if (new_alpha < 1e-6) { + // new_alpha = 0 only if top.a = bottom.a = 0, at least as long as + // both alphas are within range. (If they're not, the result is not + // meaningful anyway.) And if new_alpha = 0, we don't really have + // any meaningful output anyway, so just set it to zero instead of + // getting division-by-zero below. + return vec4(0.0); + } else { + vec3 premultiplied_color = mix(bottom.rgb * bottom.aaa, top.rgb, top.a); + vec3 color = premultiplied_color / new_alpha; + return vec4(color.r, color.g, color.b, new_alpha); + } +}