From 2ca78df17f5d916a23017b438a507559c6444520 Mon Sep 17 00:00:00 2001 From: Edwin Jakobs Date: Fri, 20 Mar 2020 10:47:26 +0100 Subject: [PATCH] Add StretchWaves filter --- .../src/main/kotlin/distort/StretchWaves.kt | 41 ++++++++++++++++++ .../extra/fx/gl3/distort/stretch-waves.frag | 42 +++++++++++++++++++ 2 files changed, 83 insertions(+) create mode 100644 orx-fx/src/main/kotlin/distort/StretchWaves.kt create mode 100644 orx-fx/src/main/resources/org/openrndr/extra/fx/gl3/distort/stretch-waves.frag diff --git a/orx-fx/src/main/kotlin/distort/StretchWaves.kt b/orx-fx/src/main/kotlin/distort/StretchWaves.kt new file mode 100644 index 00000000..4445806f --- /dev/null +++ b/orx-fx/src/main/kotlin/distort/StretchWaves.kt @@ -0,0 +1,41 @@ +package org.openrndr.extra.fx.distort + +import org.openrndr.draw.* +import org.openrndr.extra.fx.filterFragmentCode +import org.openrndr.extra.parameters.Description +import org.openrndr.extra.parameters.DoubleParameter + +@Description("Stretch waves") +class StretchWaves : Filter(filterShaderFromCode(filterFragmentCode("distort/stretch-waves.frag"))) { + @DoubleParameter("distortion", -0.0,1.0, 1) + var distortion: Double by parameters + + @DoubleParameter("rotation", -180.0, 180.0) + var rotation: Double by parameters + + @DoubleParameter("phase", -1.0, 1.0) + var phase: Double by parameters + + @DoubleParameter("frequency", 0.0, 100.0) + var frequency: Double by parameters + + @DoubleParameter("feather", 0.0, 100.0, order = 1) + var feather: Double by parameters + + init { + distortion = 0.0 + rotation = 0.0 + phase = 0.0 + frequency = 10.0 + feather = 1.0 + } + + var bicubicFiltering = true + override fun apply(source: Array, target: Array) { + if (bicubicFiltering && source.isNotEmpty()) { + source[0].generateMipmaps() + source[0].filter(MinifyingFilter.LINEAR_MIPMAP_LINEAR, MagnifyingFilter.LINEAR) + } + super.apply(source, target) + } +} \ No newline at end of file diff --git a/orx-fx/src/main/resources/org/openrndr/extra/fx/gl3/distort/stretch-waves.frag b/orx-fx/src/main/resources/org/openrndr/extra/fx/gl3/distort/stretch-waves.frag new file mode 100644 index 00000000..82f49eaa --- /dev/null +++ b/orx-fx/src/main/resources/org/openrndr/extra/fx/gl3/distort/stretch-waves.frag @@ -0,0 +1,42 @@ +#version 330 core + +uniform sampler2D tex0; +in vec2 v_texCoord0; + +uniform float phase; +uniform float rotation; +uniform float distortion; +uniform float frequency; +uniform float feather; +out vec4 o_color; + +void main() { + float phi = radians(rotation); + float cp = cos(phi); + float sp = sin(phi); + mat2 rm = mat2(vec2(cp, sp), vec2(-sp, cp)); + mat2 irm = transpose(rm); + + float tw = 1.0 / frequency; + vec2 uv = rm * (v_texCoord0 - vec2(0.5)) + vec2(0.5) + vec2(phase * tw, 0.0); + + float xd = (uv.x) * frequency; + float xo = (fract(xd) - 0.5) * 2.0; + float xf = fract(xd); + + float offs = (1.0- xo * xo) * 1.0 * xo * distortion * 0.5; + float f = mix(1.0, (1.0 - xo * xo), distortion); + + vec2 fuv = uv; + fuv.x = floor(uv.x * frequency) / frequency; + fuv.x += (xf - offs) * tw; + + fuv = irm * (fuv - vec2(0.5) - vec2(phase * tw, 0.0)) + vec2(0.5); + + vec2 step = fwidth(fuv) * feather; + float fx = smoothstep(0.0, step.x, fuv.x) * smoothstep(1.0, 1.0 - step.x, fuv.x); + float fy = smoothstep(0.0, step.y, fuv.y) * smoothstep(1.0, 1.0 - step.y, fuv.y); + + vec4 c = texture(tex0, fuv) * f * fx * fy; + o_color = c; +} \ No newline at end of file