From a249d928fd2d746668544bed8315b266458cb953 Mon Sep 17 00:00:00 2001 From: Edwin Jakobs Date: Thu, 27 Feb 2020 19:29:04 +0100 Subject: [PATCH] Add DistanceField filter to orx-jumpflood --- orx-jumpflood/src/main/kotlin/JumpFlood.kt | 103 ++++++++++++------ .../resources/shaders/gl3/pixel-distance.frag | 14 ++- 2 files changed, 79 insertions(+), 38 deletions(-) diff --git a/orx-jumpflood/src/main/kotlin/JumpFlood.kt b/orx-jumpflood/src/main/kotlin/JumpFlood.kt index e1a5d935..f1c9d21e 100644 --- a/orx-jumpflood/src/main/kotlin/JumpFlood.kt +++ b/orx-jumpflood/src/main/kotlin/JumpFlood.kt @@ -3,9 +3,8 @@ package org.openrndr.extra.jumpfill import org.openrndr.color.ColorRGBa import org.openrndr.draw.* import org.openrndr.extra.fx.blend.Passthrough +import org.openrndr.extra.parameters.DoubleParameter - -import org.openrndr.math.Matrix44 import org.openrndr.math.Vector2 import org.openrndr.resourceUrl import kotlin.math.ceil @@ -21,12 +20,21 @@ class JumpFlood : Filter(filterShaderFromUrl(resourceUrl("/shaders/gl3/jumpflood class PixelDirection : Filter(filterShaderFromUrl(resourceUrl("/shaders/gl3/pixel-direction.frag"))) { var originalSize: Vector2 by parameters } + class PixelDistance : Filter(filterShaderFromUrl(resourceUrl("/shaders/gl3/pixel-distance.frag"))) { + var distanceScale: Double by parameters var originalSize: Vector2 by parameters + var signedBit: Boolean by parameters + init { + distanceScale = 1.0 + originalSize = Vector2(512.0, 512.0) + signedBit = true + } } + class ContourPoints : Filter(filterShaderFromUrl(resourceUrl("/shaders/gl3/contour-points.frag"))) class Threshold : Filter(filterShaderFromUrl(resourceUrl("/shaders/gl3/threshold.frag"))) { - var threshold by parameters + var threshold: Double by parameters init { threshold = 0.5 @@ -41,7 +49,7 @@ private val contourPoints by lazy { ContourPoints() } private val threshold by lazy { Threshold() } private val passthrough by lazy { Passthrough() } -class JumpFlooder(val width: Int, val height: Int, format:ColorFormat = ColorFormat.RGB, type:ColorType = ColorType.FLOAT32) { +class JumpFlooder(val width: Int, val height: Int, format: ColorFormat = ColorFormat.RGB, type: ColorType = ColorType.FLOAT32) { private val dimension = max(width, height) private val exp = ceil(Math.log(dimension.toDouble()) / Math.log(2.0)).toInt() @@ -51,53 +59,35 @@ class JumpFlooder(val width: Int, val height: Int, format:ColorFormat = ColorFor listOf(colorBuffer(squareDim, squareDim, format = format, type = type), colorBuffer(squareDim, squareDim, format = format, type = type)) - private val final = renderTarget(width, height) { - colorBuffer(format = format, type = type) + + val final = colorBuffer(squareDim, squareDim, format = format, type = type) + + private val square = colorBuffer(squareDim, squareDim, format = format, type = type).apply { + fill(ColorRGBa.BLACK) } - val encoded: ColorBuffer get() = final.colorBuffer(0) - private val square = renderTarget(squareDim, squareDim) { - colorBuffer(format = format, type = type) - } - - fun jumpFlood(drawer: Drawer, input: ColorBuffer): ColorBuffer { + fun jumpFlood(input: ColorBuffer): ColorBuffer { if (input.width != width || input.height != height) { throw IllegalArgumentException("dimensions mismatch") } - drawer.isolatedWithTarget(square) { - drawer.background(ColorRGBa.BLACK) - drawer.ortho(square) - drawer.view = Matrix44.IDENTITY - drawer.model = Matrix44.IDENTITY - drawer.image(input) - } - encodePoints.apply(square.colorBuffer(0), coordinates[0]) + input.copyTo(square) + encodePoints.apply(square, coordinates[0]) for (i in 0 until exp) { jumpFlood.step = i jumpFlood.apply(coordinates[i % 2], coordinates[(i + 1) % 2]) } - drawer.isolatedWithTarget(final) { - drawer.background(ColorRGBa.BLACK) - drawer.ortho(final) - drawer.view = Matrix44.IDENTITY - drawer.model = Matrix44.IDENTITY - drawer.image(coordinates[exp % 2]) - } - return encoded + coordinates[exp % 2].copyTo(final) + + return final } fun destroy() { coordinates.forEach { it.destroy() } - square.colorBuffer(0).destroy() - square.detachColorBuffers() square.destroy() - - final.colorBuffer(0).destroy() - final.detachColorBuffers() final.destroy() } } @@ -111,7 +101,7 @@ private fun encodeDecodeBitmap(drawer: Drawer, preprocess: Filter, decoder: Filt preprocess.apply(bitmap, _result) - val encoded = _jumpFlooder.jumpFlood(drawer, _result) + val encoded = _jumpFlooder.jumpFlood(_result) decoder.parameters["originalSize"] = Vector2(_jumpFlooder.squareDim.toDouble(), _jumpFlooder.squareDim.toDouble()) decoder.apply(arrayOf(encoded, bitmap), _result) @@ -140,3 +130,48 @@ fun directionFieldFromBitmap(drawer: Drawer, bitmap: ColorBuffer, jumpFlooder: JumpFlooder? = null, result: ColorBuffer? = null ): ColorBuffer = encodeDecodeBitmap(drawer, contourPoints, pixelDirection, bitmap, jumpFlooder, result) + + +class DistanceField : Filter() { + + @DoubleParameter("threshold", 0.0, 1.0) + var threshold = 0.5 + + @DoubleParameter("distance scale", 0.0, 1.0) + var distanceScale = 1.0 + + + + private val thresholdFilter = Threshold() + private var thresholded: ColorBuffer? = null + private val contourFilter = ContourPoints() + private var contoured: ColorBuffer? = null + private var jumpFlooder: JumpFlooder? = null + + private val decodeFilter = PixelDistance() + + override fun apply(source: Array, target: Array) { + + if (thresholded == null) { + thresholded = colorBuffer(target[0].width, target[0].height, format = ColorFormat.R) + } + + if (contoured == null) { + contoured = colorBuffer(target[0].width, target[0].height, format = ColorFormat.R) + } + + if (jumpFlooder == null) { + jumpFlooder = JumpFlooder(target[0].width, target[0].height) + } + + thresholdFilter.threshold = threshold + thresholdFilter.apply(source[0], thresholded!!) + contourFilter.apply(thresholded!!, contoured!!) + val result = jumpFlooder!!.jumpFlood(contoured!!) + decodeFilter.originalSize = Vector2(target[0].width * 1.0, target[0].height * 1.0) + decodeFilter.distanceScale = distanceScale + decodeFilter.signedBit = false + decodeFilter.apply(result, result) + result.copyTo(target[0]) + } +} \ No newline at end of file diff --git a/orx-jumpflood/src/main/resources/shaders/gl3/pixel-distance.frag b/orx-jumpflood/src/main/resources/shaders/gl3/pixel-distance.frag index 37b73d43..0523e12f 100644 --- a/orx-jumpflood/src/main/resources/shaders/gl3/pixel-distance.frag +++ b/orx-jumpflood/src/main/resources/shaders/gl3/pixel-distance.frag @@ -4,6 +4,8 @@ uniform sampler2D tex0; uniform sampler2D tex1; uniform vec2 originalSize; +uniform float distanceScale; +uniform bool signedBit; in vec2 v_texCoord0; @@ -12,12 +14,16 @@ out vec4 o_color; void main() { vec2 size = textureSize(tex0, 0); vec2 fixUp = v_texCoord0; - fixUp.y = 1.0 - fixUp.y; - fixUp *= (size/originalSize); - fixUp.y = 1.0 - fixUp.y; + + + vec2 pixelPosition = fixUp; vec2 centroidPixelPosition = texture(tex0, v_texCoord0).xy; vec2 pixelDistance = (centroidPixelPosition - pixelPosition) * size * vec2(1.0, -1.0); float threshold = texture(tex1, v_texCoord0).r; - o_color = vec4(length(pixelDistance), threshold, 0.0, 1.0); + if (signedBit) { + o_color = vec4(length(pixelDistance)* distanceScale, threshold, 0.0, 1.0); + } else { + o_color = vec4(vec3(length(pixelDistance) * distanceScale), 1.0); + } } \ No newline at end of file