diff --git a/rsc/shaders/filters/chromakey.glsl b/rsc/shaders/filters/chromakey.glsl index d7b248d..dc61fe6 100644 --- a/rsc/shaders/filters/chromakey.glsl +++ b/rsc/shaders/filters/chromakey.glsl @@ -42,16 +42,24 @@ void mainImage( out vec4 fragColor, in vec2 fragCoord ) { vec4 texColor0 = texture(iChannel0, fragCoord.xy / iResolution.xy); + // RGB is alpha-premultiplied : revert + vec4 col = vec4( texColor0.rgb / max(texColor0.a, 0.001), 1.0); + //convert from RGB to YCvCr/YUV vec4 keyYUV = RGBtoYUV * chromaKey; - vec4 yuv = RGBtoYUV * texColor0; + vec4 yuv = RGBtoYUV * col; + // correction of color float t = (1. - Tolerance) * 0.2; float mask = 1.0 - colordistance(yuv.rgb, keyYUV.rgb, vec2(t, t + 0.2)); - vec4 col = max(texColor0 - mask * chromaKey, 0.0); + col = max(col - mask * chromaKey, 0.0); + // correction of alpha t = Threshold * Threshold * 0.2; col.a -= 1.0 - colordistance(yuv.rgb, keyYUV.rgb, vec2(t, t + 0.25)); + // restore texture alpha + col.a *= texColor0.a; + fragColor = col; } diff --git a/rsc/shaders/filters/lumakey.glsl b/rsc/shaders/filters/lumakey.glsl index f8ab624..3ce86ad 100644 --- a/rsc/shaders/filters/lumakey.glsl +++ b/rsc/shaders/filters/lumakey.glsl @@ -9,40 +9,42 @@ uniform float Threshold; uniform float Tolerance; -//float sRGBtoLin( in float v ) { -// // Send this function a decimal sRGB gamma encoded color value -// // between 0.0 and 1.0, and it returns a linearized value. -// if ( v <= 0.04045 ) { -// return v / 12.92; -// } else { -// return pow((( v + 0.055)/1.055),2.4); -// } -//} +float sRGBtoLin( in float v ) { + // Send this function a decimal sRGB gamma encoded color value + // between 0.0 and 1.0, and it returns a linearized value. + if ( v <= 0.04045 ) { + return v / 12.92; + } else { + return pow((( v + 0.055)/1.055),2.4); + } +} -//// returns L* which is "perceptual lightness" -//float lightness ( in vec3 color ) -//{ -// float Y = 0.2126 * sRGBtoLin(color.r) -// + 0.7152 * sRGBtoLin(color.g) -// + 0.0722 * sRGBtoLin(color.b); +// returns L* which is "perceptual lightness" +float lightness ( in vec3 color ) +{ + float Y = 0.2126 * sRGBtoLin(color.r) + + 0.7152 * sRGBtoLin(color.g) + + 0.0722 * sRGBtoLin(color.b); -// if ( Y <= (216./24389.) ) { // The CIE standard states 0.008856 but 216/24389 is the intent for 0.008856451679036 -// Y = Y * (24389./27.); // The CIE standard states 903.3, but 24389/27 is the intent, making 903.296296296296296 -// } else { -// Y = pow(Y,(1./3.)) * 116. - 16.; -// } + if ( Y <= (216./24389.) ) { // The CIE standard states 0.008856 but 216/24389 is the intent for 0.008856451679036 + Y = Y * (24389./27.); // The CIE standard states 903.3, but 24389/27 is the intent, making 903.296296296296296 + } else { + Y = pow(Y,(1./3.)) * 116. - 16.; + } -// return 0.01 * Y; -//} + return 0.01 * Y; +} void mainImage( out vec4 fragColor, in vec2 fragCoord ) { - vec2 fragPos = fragCoord.xy / iResolution.xy; - vec3 RGB = texture(iChannel0, fragPos).rgb; + vec4 texColor0 = texture(iChannel0, fragCoord.xy / iResolution.xy); + // RGB is alpha-premultiplied : revert + texColor0 = vec4( texColor0.rgb / max(texColor0.a, 0.001), texColor0.a); - float L = dot(RGB, vec3(0.299, 0.587, 0.114)); -// float L = lightness( RGB ); + // modify alpha by the lightness of the image + float L = lightness( texColor0.rgb ); + texColor0.a *= smoothstep( Threshold, Threshold + Tolerance * Tolerance, abs(Luminance - L) ); - fragColor = vec4( RGB, smoothstep( Threshold, Threshold + Tolerance * Tolerance, abs(Luminance - L) ) ); + fragColor = texColor0; }