From 53fbc47636fc92952a12b6da8b81504a8f306700 Mon Sep 17 00:00:00 2001 From: Edwin Date: Fri, 15 Apr 2022 12:58:19 +0200 Subject: [PATCH] [orx-fx] Add super sampling to Contour filter --- orx-fx/src/commonMain/kotlin/edges/Contour.kt | 5 ++++ orx-fx/src/shaders/glsl/edges/contour.frag | 27 ++++++++++++------- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/orx-fx/src/commonMain/kotlin/edges/Contour.kt b/orx-fx/src/commonMain/kotlin/edges/Contour.kt index a0ef1491..c332af1e 100644 --- a/orx-fx/src/commonMain/kotlin/edges/Contour.kt +++ b/orx-fx/src/commonMain/kotlin/edges/Contour.kt @@ -7,6 +7,7 @@ import org.openrndr.extra.fx.mppFilterShader import org.openrndr.extra.parameters.ColorParameter import org.openrndr.extra.parameters.Description import org.openrndr.extra.parameters.DoubleParameter +import org.openrndr.extra.parameters.IntParameter @Description("Contour") class Contour : Filter(mppFilterShader(fx_contour, "contour")) { @@ -25,11 +26,15 @@ class Contour : Filter(mppFilterShader(fx_contour, "contour")) { @ColorParameter("contour color") var contourColor: ColorRGBa by parameters + @IntParameter("window", 0, 10) + var window: Int by parameters + init { levels = 6.0 contourWidth = 0.4 contourColor = ColorRGBa.BLACK backgroundOpacity = 1.0 contourOpacity = 1.0 + window = 1 } } diff --git a/orx-fx/src/shaders/glsl/edges/contour.frag b/orx-fx/src/shaders/glsl/edges/contour.frag index 0d00c4a0..3d82721d 100644 --- a/orx-fx/src/shaders/glsl/edges/contour.frag +++ b/orx-fx/src/shaders/glsl/edges/contour.frag @@ -6,21 +6,28 @@ uniform float contourWidth; uniform float contourOpacity; uniform vec4 contourColor; uniform float backgroundOpacity; +uniform int window; + +float calc_contour(vec2 uv) { + vec4 box = texture(tex0, uv); + float v = sin(3.1415926535 * levels * dot(vec3(1.0/3.0),box.xyz)); + float level = floor(dot(vec3(1.0/3.0),box.xyz) * levels) / levels; + float contour = 1.0 - smoothstep(0., contourWidth, 0.5 * abs(v) / fwidth(v)); + return contour; +} void main() { vec2 step = 1.0 / textureSize(tex0, 0); - vec4 box = vec4(0.0); - for (int j = -1; j <=1; ++j) { - for (int i = -1; i <= 1; ++i) { - box += texture(tex0, v_texCoord0 + step * vec2(i, j)); + float contour = 0.0; + float weight = 0.0; + + for (int i = -window; i <= window; ++i) { + for (int j = -window; j <= window; ++j) { + contour += calc_contour(v_texCoord0 + step/(window+1.0) * vec2(i, j)); + weight += 1.0; } } - box /= 9.0; - float v = sin(3.1415926535 * levels * dot(vec3(1.0/3.0),box.xyz)); - float level = floor(dot(vec3(1.0/3.0),box.xyz) * levels) / levels; - //int plateauIndex = min(levels-1, int(level * levels)); - float contour = 1.0 - smoothstep(0., contourWidth, 0.5 * abs(v) / fwidth(v)); - + contour /= weight; vec4 t = texture(tex0, v_texCoord0); o_output = t * backgroundOpacity * (1.0-contour) + contour * contourColor * contourOpacity * t.a; } \ No newline at end of file