glsl-blend icon indicating copy to clipboard operation
glsl-blend copied to clipboard

Blend Normal with Alpha

Open omarojo opened this issue 5 years ago • 4 comments

Is it possible to use the Normal Blend.. but if the blend texture has parts that are alpha 0.0 show the basetexture below?

Just like putting a image that has transparent parts ON TOP of a base image. But the opacity of both images is 100%. so you are able to see both at the same time.

omarojo avatar Aug 21 '19 00:08 omarojo

Indeed! I would love for all of the blend modes to take the alpha of the textures into account!

iangilman avatar May 07 '20 23:05 iangilman

This works for transparent images and any blend mode:

float blendOpac = blendLayerOpacity * blend.a;
vec4 outCol;
outCol.rgb = blendScreen(base.rgb, blend.rgb, blendOpac); // works for any blendMode
outCol.a =  blendOpac + base.a;

felixturner avatar Apr 26 '22 18:04 felixturner

@felixturner Awesome, thank you for writing this up! I'll be putting it into use shortly :-)

iangilman avatar Apr 28 '22 18:04 iangilman

Hello! I thought I'd share my approach. I made it comparing to Photopea as a reference. I realized what Photopea does (and I'm assuming Photoshop does too, I don't have access to the software right now) is basically layer the blend texture over the base texture wherever the base texture is transparent. I'll use darken as an example:

vec4 blendDarken(vec4 base, vec4 blend, float opacity){
    blend.a *= opacity;
    vec4 blended = vec4(blendDarken(base.rgb, blend.rgb, opacity), 1.0);
    vec4 over = blend+(base*(1.0-blend.a));
    return mix(blended, over, 1.0-base.a);
}

Edit: This method assumes the function will be receiving values with premultiplied alpha

geikha avatar Feb 02 '23 02:02 geikha