From 39055254c651405d0ff8ed3425f4e5b9cc2262b0 Mon Sep 17 00:00:00 2001 From: Edwin Jakobs Date: Fri, 15 Dec 2023 01:03:34 +0100 Subject: [PATCH] [orx-fx] Correct BlendSpectral --- .../commonMain/kotlin/blend/BlendSpectral.kt | 32 ++++++++++++------- .../src/jvmDemo/kotlin/DemoSpectralBlend01.kt | 6 ++-- 2 files changed, 24 insertions(+), 14 deletions(-) diff --git a/orx-fx/src/commonMain/kotlin/blend/BlendSpectral.kt b/orx-fx/src/commonMain/kotlin/blend/BlendSpectral.kt index 4089d8a7..b66fb0aa 100644 --- a/orx-fx/src/commonMain/kotlin/blend/BlendSpectral.kt +++ b/orx-fx/src/commonMain/kotlin/blend/BlendSpectral.kt @@ -42,32 +42,40 @@ vec3 linear_to_srgb(vec3 c) { void main() { vec4 a = texture(tex0, v_texCoord0); vec4 b = texture(tex1, v_texCoord0); + + // depremultiply alpha + vec4 na = a.a == 0.0 ? vec4(0.0): vec4(a.rgb / a.a,a.a); + vec4 nb = b.a == 0.0 ? vec4(0.0): vec4(b.rgb / b.a,b.a); + + if (linearizeInputA) { - a.rgb = srgb_to_linear(a.rgb); + na.rgb = srgb_to_linear(na.rgb); } if (linearizeInputB) { - b.rgb = srgb_to_linear(b.rgb); + nb.rgb = srgb_to_linear(nb.rgb); } - // depremultiply alpha - vec3 na = a.a == 0.0 ? vec3(0.0): a.rgb / a.a; - vec3 nb = b.a == 0.0 ? vec3(0.0): b.rgb / b.a; + vec4 mixed = vec4(spectral_mix(na.rgb, nb.rgb, min(1.0, fill)), 1.0); - vec4 mixed = vec4(spectral_mix(na, nb, min(1.0, b.a * fill) ), min(a.a, b.a)); - - // premultiply alpha - mixed.rgb *= mixed.a; - if (!clip) { - vec4 b_over_a = a * (1.0 - b.a) + b; - mixed = b_over_a * (1.0-mixed.a) + mixed; + na.rgb *= a.a; + nb.rgb *= b.a; + mixed = na * (1.0 - nb.a) + nb * (1.0 - na.a) + mixed * na.a * nb.a; + } else { + mixed = mixed * na.a * nb.a; } + mixed.rgb = mixed.a == 0.0 ? vec3(0.0): mixed.rgb / mixed.a; + if (delinearizeOutput) { mixed.rgb = linear_to_srgb(mixed.rgb); } +// premultiply alpha + mixed.rgb *= mixed.a; + + o_color = mixed; } diff --git a/orx-fx/src/jvmDemo/kotlin/DemoSpectralBlend01.kt b/orx-fx/src/jvmDemo/kotlin/DemoSpectralBlend01.kt index c92b8dde..b4f4e92a 100644 --- a/orx-fx/src/jvmDemo/kotlin/DemoSpectralBlend01.kt +++ b/orx-fx/src/jvmDemo/kotlin/DemoSpectralBlend01.kt @@ -3,6 +3,7 @@ import org.openrndr.color.ColorRGBa import org.openrndr.draw.createEquivalent import org.openrndr.drawImage import org.openrndr.extra.fx.blend.BlendSpectral +import org.openrndr.extra.fx.blur.BoxBlur import org.openrndr.extra.fx.patterns.Checkers import org.openrndr.math.Vector2 import kotlin.math.sin @@ -17,7 +18,7 @@ fun main() { val a = drawImage(width, height) { drawer.stroke = null drawer.fill = ColorRGBa.BLUE - drawer.circle(drawer.bounds.center - Vector2(100.0, 0.0) , drawer.width * 0.25) + drawer.circle(drawer.bounds.center - Vector2(100.0, 0.0), drawer.width * 0.25) } val b = drawImage(width, height) { drawer.clear(ColorRGBa.TRANSPARENT) @@ -26,7 +27,7 @@ fun main() { drawer.fill = ColorRGBa.YELLOW.opacify(1.0) drawer.circle(drawer.bounds.center + Vector2(100.0, 0.0), drawer.width * 0.25) } - + BoxBlur().apply { window = 10 }.apply(b, b) val checked = a.createEquivalent() Checkers().apply(emptyArray(), checked) @@ -35,6 +36,7 @@ fun main() { extend { drawer.image(checked) blendSpectral.fill = sin(seconds) * 0.5 + 0.5 + blendSpectral.clip = seconds.mod(4.0) > 2.0 blendSpectral.apply(a, b, mixed) drawer.image(mixed) }