From 41e3f9505d63178b40cbc934c03474b1701e7bf9 Mon Sep 17 00:00:00 2001 From: Rein van der Woerd Date: Sun, 16 Feb 2020 11:00:40 +0100 Subject: [PATCH 1/2] Added Matt DesLauriers' crosshatch shader as a Filter... It's also using Hugh Kennedy's glsl-luma, which is a dependency of glsl-crosshatch-filter. --- orx-fx/src/main/kotlin/dither/Crosshatch.kt | 30 ++++++++++ .../extra/fx/gl3/dither/crosshatch.frag | 55 +++++++++++++++++++ 2 files changed, 85 insertions(+) create mode 100644 orx-fx/src/main/kotlin/dither/Crosshatch.kt create mode 100644 orx-fx/src/main/resources/org/openrndr/extra/fx/gl3/dither/crosshatch.frag diff --git a/orx-fx/src/main/kotlin/dither/Crosshatch.kt b/orx-fx/src/main/kotlin/dither/Crosshatch.kt new file mode 100644 index 00000000..b4006e2d --- /dev/null +++ b/orx-fx/src/main/kotlin/dither/Crosshatch.kt @@ -0,0 +1,30 @@ +package org.openrndr.extra.fx.dither + +import org.openrndr.draw.Filter +import org.openrndr.draw.Shader +import org.openrndr.extra.fx.filterFragmentCode +import org.openrndr.extra.parameters.Description +import org.openrndr.extra.parameters.DoubleParameter + + +@Description("Crosshatch") +class Crosshatch: Filter(Shader.createFromCode(filterVertexCode, filterFragmentCode("dither/crosshatch.frag"))) { + @DoubleParameter("threshold 1", 0.0, 1.0) + var t1: Double by parameters + + @DoubleParameter("threshold 2", 0.0, 1.0) + var t2: Double by parameters + + @DoubleParameter("threshold 3", 0.0, 1.0) + var t3: Double by parameters + + @DoubleParameter("threshold 4", 0.0, 1.0) + var t4: Double by parameters + + init { + t1 = 1.0 + t2 = 0.75 + t3 = 0.5 + t4 = 0.3 + } +} \ No newline at end of file diff --git a/orx-fx/src/main/resources/org/openrndr/extra/fx/gl3/dither/crosshatch.frag b/orx-fx/src/main/resources/org/openrndr/extra/fx/gl3/dither/crosshatch.frag new file mode 100644 index 00000000..8397a225 --- /dev/null +++ b/orx-fx/src/main/resources/org/openrndr/extra/fx/gl3/dither/crosshatch.frag @@ -0,0 +1,55 @@ +#version 330 + +in vec2 v_texCoord0; +uniform sampler2D tex0; +out vec4 o_color; + +// thresholds +uniform float t1; +uniform float t2; +uniform float t3; +uniform float t4; + + +// glsl-luma +float luma(vec3 color) { + return dot(color, vec3(0.299, 0.587, 0.114)); +} + +float luma(vec4 color) { + return dot(color.rgb, vec3(0.299, 0.587, 0.114)); +} + +// glsl-crosshatch +vec3 crosshatch(vec3 texColor, float t1, float t2, float t3, float t4) { + float lum = luma(texColor); + vec3 color = vec3(1.0); + if (lum < t1) { + if (mod(gl_FragCoord.x + gl_FragCoord.y, 10.0) == 0.0) { + color = vec3(0.0); + } + } + if (lum < t2) { + if (mod(gl_FragCoord.x - gl_FragCoord.y, 10.0) == 0.0) { + color = vec3(0.0); + } + } + if (lum < t3) { + if (mod(gl_FragCoord.x + gl_FragCoord.y - 5.0, 10.0) == 0.0) { + color = vec3(0.0); + } + } + if (lum < t4) { + if (mod(gl_FragCoord.x - gl_FragCoord.y - 5.0, 10.0) == 0.0) { + color = vec3(0.0); + } + } + return color; +} + + +void main() { + vec4 color = texture(tex0, v_texCoord0); + o_color.rgb = crosshatch(color.rgb, t1, t2, t3, t4); + o_color.a = color.a; +} \ No newline at end of file From 361431d903e9aa8ceb6a9bff0e313d577071b88f Mon Sep 17 00:00:00 2001 From: Edwin Jakobs Date: Sun, 16 Feb 2020 17:46:39 +0100 Subject: [PATCH 2/2] Fix premultiplied alpha in-output of crosshatch shader --- .../org/openrndr/extra/fx/gl3/dither/crosshatch.frag | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/orx-fx/src/main/resources/org/openrndr/extra/fx/gl3/dither/crosshatch.frag b/orx-fx/src/main/resources/org/openrndr/extra/fx/gl3/dither/crosshatch.frag index 8397a225..6b5fc0e5 100644 --- a/orx-fx/src/main/resources/org/openrndr/extra/fx/gl3/dither/crosshatch.frag +++ b/orx-fx/src/main/resources/org/openrndr/extra/fx/gl3/dither/crosshatch.frag @@ -50,6 +50,6 @@ vec3 crosshatch(vec3 texColor, float t1, float t2, float t3, float t4) { void main() { vec4 color = texture(tex0, v_texCoord0); - o_color.rgb = crosshatch(color.rgb, t1, t2, t3, t4); - o_color.a = color.a; + vec3 demultiplied = color.a == 0.0 ? vec3(0.0) : color.rgb/color.a; + o_color = vec4(crosshatch(demultiplied, t1, t2, t3, t4), 1.0) * color.a; } \ No newline at end of file