diff --git a/orx-jumpflood/src/main/kotlin/DirectionalField.kt b/orx-jumpflood/src/main/kotlin/DirectionalField.kt new file mode 100644 index 00000000..f106842f --- /dev/null +++ b/orx-jumpflood/src/main/kotlin/DirectionalField.kt @@ -0,0 +1,46 @@ +package org.openrndr.extra.jumpfill + +import org.openrndr.draw.ColorBuffer +import org.openrndr.draw.ColorFormat +import org.openrndr.draw.Filter +import org.openrndr.draw.colorBuffer +import org.openrndr.extra.parameters.Description +import org.openrndr.extra.parameters.DoubleParameter +import org.openrndr.math.Vector2 + +@Description("Directional field") +class DirectionalField : 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 = PixelDirection() + + 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.apply(result, result) + result.copyTo(target[0]) + } +} \ No newline at end of file diff --git a/orx-jumpflood/src/main/kotlin/DistanceField.kt b/orx-jumpflood/src/main/kotlin/DistanceField.kt new file mode 100644 index 00000000..741216bf --- /dev/null +++ b/orx-jumpflood/src/main/kotlin/DistanceField.kt @@ -0,0 +1,47 @@ +package org.openrndr.extra.jumpfill + +import org.openrndr.draw.ColorBuffer +import org.openrndr.draw.ColorFormat +import org.openrndr.draw.Filter +import org.openrndr.draw.colorBuffer +import org.openrndr.extra.parameters.Description +import org.openrndr.extra.parameters.DoubleParameter +import org.openrndr.math.Vector2 + +@Description("Distance field") +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/kotlin/JumpFlood.kt b/orx-jumpflood/src/main/kotlin/JumpFlood.kt index f1c9d21e..0e96b0f7 100644 --- a/orx-jumpflood/src/main/kotlin/JumpFlood.kt +++ b/orx-jumpflood/src/main/kotlin/JumpFlood.kt @@ -3,6 +3,7 @@ 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.Description import org.openrndr.extra.parameters.DoubleParameter import org.openrndr.math.Vector2 @@ -18,7 +19,14 @@ class JumpFlood : Filter(filterShaderFromUrl(resourceUrl("/shaders/gl3/jumpflood } class PixelDirection : Filter(filterShaderFromUrl(resourceUrl("/shaders/gl3/pixel-direction.frag"))) { + var distanceScale: Double by parameters var originalSize: Vector2 by parameters + + init { + distanceScale = 1.0 + originalSize = Vector2(512.0, 512.0) + + } } class PixelDistance : Filter(filterShaderFromUrl(resourceUrl("/shaders/gl3/pixel-distance.frag"))) { @@ -132,46 +140,3 @@ fun directionFieldFromBitmap(drawer: Drawer, bitmap: ColorBuffer, ): 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-direction.frag b/orx-jumpflood/src/main/resources/shaders/gl3/pixel-direction.frag index 307cfebf..852654fd 100644 --- a/orx-jumpflood/src/main/resources/shaders/gl3/pixel-direction.frag +++ b/orx-jumpflood/src/main/resources/shaders/gl3/pixel-direction.frag @@ -3,7 +3,8 @@ uniform sampler2D tex0; uniform sampler2D tex1; uniform vec2 originalSize; - +uniform vec2 directionalField; +uniform float distanceScale; in vec2 v_texCoord0; out vec4 o_color; @@ -11,12 +12,9 @@ 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(pixelDistance, threshold, 1.0); + o_color = vec4(pixelDistance * distanceScale, threshold, 1.0); } \ No newline at end of file