Add StretchWaves filter

This commit is contained in:
Edwin Jakobs
2020-03-20 10:47:26 +01:00
parent 210d75a7b1
commit 2ca78df17f
2 changed files with 83 additions and 0 deletions

View File

@@ -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<ColorBuffer>, target: Array<ColorBuffer>) {
if (bicubicFiltering && source.isNotEmpty()) {
source[0].generateMipmaps()
source[0].filter(MinifyingFilter.LINEAR_MIPMAP_LINEAR, MagnifyingFilter.LINEAR)
}
super.apply(source, target)
}
}

View File

@@ -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;
}