[orx-fx] Add Lenses Filter
This commit is contained in:
@@ -1,8 +1,12 @@
|
||||
package org.openrndr.extra.fx.distort
|
||||
|
||||
import org.openrndr.draw.ColorBuffer
|
||||
import org.openrndr.draw.Filter
|
||||
import org.openrndr.draw.MagnifyingFilter
|
||||
import org.openrndr.draw.MinifyingFilter
|
||||
import org.openrndr.extra.fx.fx_block_repeat
|
||||
import org.openrndr.extra.fx.mppFilterShader
|
||||
import org.openrndr.extra.parameters.BooleanParameter
|
||||
import org.openrndr.extra.parameters.Description
|
||||
import org.openrndr.extra.parameters.DoubleParameter
|
||||
|
||||
@@ -32,6 +36,17 @@ class BlockRepeat : Filter(mppFilterShader(fx_block_repeat, "block-repeat")) {
|
||||
@DoubleParameter("source y-offset", -.5, .5, order = 6)
|
||||
var sourceOffsetY: Double by parameters
|
||||
|
||||
@BooleanParameter("bicubic filtering")
|
||||
var bicubicFiltering: Boolean by parameters
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
init {
|
||||
blockWidth = 0.25
|
||||
blockHeight = 0.25
|
||||
@@ -40,5 +55,6 @@ class BlockRepeat : Filter(mppFilterShader(fx_block_repeat, "block-repeat")) {
|
||||
sourceOffsetX = 0.0
|
||||
sourceOffsetY = 0.0
|
||||
sourceScale = 0.0
|
||||
bicubicFiltering = true
|
||||
}
|
||||
}
|
||||
|
||||
45
orx-fx/src/commonMain/kotlin/distort/Lenses.kt
Normal file
45
orx-fx/src/commonMain/kotlin/distort/Lenses.kt
Normal file
@@ -0,0 +1,45 @@
|
||||
package org.openrndr.extra.fx.distort
|
||||
|
||||
import org.openrndr.draw.ColorBuffer
|
||||
import org.openrndr.draw.Filter
|
||||
import org.openrndr.draw.MagnifyingFilter
|
||||
import org.openrndr.draw.MinifyingFilter
|
||||
import org.openrndr.extra.fx.fx_lenses
|
||||
import org.openrndr.extra.fx.mppFilterShader
|
||||
import org.openrndr.extra.parameters.BooleanParameter
|
||||
import org.openrndr.extra.parameters.Description
|
||||
import org.openrndr.extra.parameters.DoubleParameter
|
||||
|
||||
@Description("Lenses")
|
||||
class Lenses : Filter(mppFilterShader(fx_lenses, "block-repeat")) {
|
||||
@DoubleParameter("block width", 0.0, 1.0, order = 0)
|
||||
var blockWidth: Double by parameters
|
||||
|
||||
@DoubleParameter("block height", 0.0, 1.0, order = 1)
|
||||
var blockHeight: Double by parameters
|
||||
|
||||
|
||||
@DoubleParameter("scale", 0.5, 1.5, order = 2)
|
||||
var scale: Double by parameters
|
||||
|
||||
@DoubleParameter("rotation", -180.0, 180.0, order = 3)
|
||||
var rotation: Double by parameters
|
||||
|
||||
@BooleanParameter("bicubic filtering")
|
||||
var bicubicFiltering: Boolean by parameters
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
init {
|
||||
blockWidth = 0.25
|
||||
blockHeight = 0.25
|
||||
|
||||
bicubicFiltering = true
|
||||
}
|
||||
}
|
||||
24
orx-fx/src/demo/kotlin/DemoDistortLenses01.kt
Normal file
24
orx-fx/src/demo/kotlin/DemoDistortLenses01.kt
Normal file
@@ -0,0 +1,24 @@
|
||||
import org.openrndr.application
|
||||
import org.openrndr.draw.createEquivalent
|
||||
import org.openrndr.draw.loadImage
|
||||
import org.openrndr.extra.fx.distort.Lenses
|
||||
import kotlin.math.cos
|
||||
|
||||
fun main() = application {
|
||||
configure {
|
||||
width = 640
|
||||
height = 480
|
||||
|
||||
}
|
||||
program {
|
||||
val image = loadImage("demo-data/images/image-001.png")
|
||||
val lenses = Lenses()
|
||||
val edges = image.createEquivalent()
|
||||
extend {
|
||||
lenses.apply(image, edges)
|
||||
lenses.rotation = 0.0
|
||||
lenses.scale = 1.4
|
||||
drawer.image(edges)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -34,6 +34,8 @@ void main() {
|
||||
// float f = smoothstep(0.0, 0.01, blockUV.x) * smoothstep(0.0, 0.01, blockUV.y);
|
||||
|
||||
vec2 sourceOffset = vec2(sourceOffsetX, sourceOffsetY);
|
||||
vec4 c = texture(tex0, mod(tUV + sourceOffset, vec2(1.0)));
|
||||
vec2 gx = dFdx(tUV);
|
||||
vec2 gy = dFdy(tUV);
|
||||
vec4 c = textureGrad(tex0, mod(tUV + sourceOffset, vec2(1.0)), gx, gy );
|
||||
o_color = c;
|
||||
}
|
||||
|
||||
30
orx-fx/src/shaders/glsl/distort/lenses.frag
Normal file
30
orx-fx/src/shaders/glsl/distort/lenses.frag
Normal file
@@ -0,0 +1,30 @@
|
||||
in vec2 v_texCoord0;
|
||||
uniform sampler2D tex0;// input
|
||||
uniform float scale;
|
||||
uniform float rotation;
|
||||
|
||||
out vec4 o_color;
|
||||
void main() {
|
||||
vec2 uv = v_texCoord0;
|
||||
vec2 blockSize = vec2(1.0/8.0, 1.0/6.0);
|
||||
|
||||
vec2 blockIndex = floor(uv / blockSize);
|
||||
vec2 blockCenter = (blockIndex+0.5) * blockSize;
|
||||
|
||||
float ca = cos(radians(rotation));
|
||||
float sa = sin(radians(rotation));
|
||||
|
||||
vec2 ts = textureSize(tex0, 0);
|
||||
|
||||
mat2 rm =mat2(1.0, 0.0, 0.0, ts.x/ts.y) * mat2(vec2(ca, sa), vec2(-sa, ca)) * mat2(1.0, 0.0, 0.0, ts.y/ts.x);
|
||||
vec2 cuv = (rm * (uv - blockCenter)
|
||||
* scale + blockCenter);
|
||||
|
||||
|
||||
|
||||
float sx = step(0.0, cuv.x) * (1.0 - step(1.0, cuv.x));
|
||||
float sy = step(0.0, cuv.y) * (1.0 - step(1.0, cuv.y));
|
||||
vec4 c = texture(tex0, cuv) * sx * sy;
|
||||
|
||||
o_color = c;
|
||||
}
|
||||
Reference in New Issue
Block a user