Add DistanceField filter to orx-jumpflood
This commit is contained in:
@@ -3,9 +3,8 @@ package org.openrndr.extra.jumpfill
|
|||||||
import org.openrndr.color.ColorRGBa
|
import org.openrndr.color.ColorRGBa
|
||||||
import org.openrndr.draw.*
|
import org.openrndr.draw.*
|
||||||
import org.openrndr.extra.fx.blend.Passthrough
|
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.math.Vector2
|
||||||
import org.openrndr.resourceUrl
|
import org.openrndr.resourceUrl
|
||||||
import kotlin.math.ceil
|
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"))) {
|
class PixelDirection : Filter(filterShaderFromUrl(resourceUrl("/shaders/gl3/pixel-direction.frag"))) {
|
||||||
var originalSize: Vector2 by parameters
|
var originalSize: Vector2 by parameters
|
||||||
}
|
}
|
||||||
|
|
||||||
class PixelDistance : Filter(filterShaderFromUrl(resourceUrl("/shaders/gl3/pixel-distance.frag"))) {
|
class PixelDistance : Filter(filterShaderFromUrl(resourceUrl("/shaders/gl3/pixel-distance.frag"))) {
|
||||||
|
var distanceScale: Double by parameters
|
||||||
var originalSize: Vector2 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 ContourPoints : Filter(filterShaderFromUrl(resourceUrl("/shaders/gl3/contour-points.frag")))
|
||||||
class Threshold : Filter(filterShaderFromUrl(resourceUrl("/shaders/gl3/threshold.frag"))) {
|
class Threshold : Filter(filterShaderFromUrl(resourceUrl("/shaders/gl3/threshold.frag"))) {
|
||||||
var threshold by parameters
|
var threshold: Double by parameters
|
||||||
|
|
||||||
init {
|
init {
|
||||||
threshold = 0.5
|
threshold = 0.5
|
||||||
@@ -41,7 +49,7 @@ private val contourPoints by lazy { ContourPoints() }
|
|||||||
private val threshold by lazy { Threshold() }
|
private val threshold by lazy { Threshold() }
|
||||||
private val passthrough by lazy { Passthrough() }
|
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 dimension = max(width, height)
|
||||||
private val exp = ceil(Math.log(dimension.toDouble()) / Math.log(2.0)).toInt()
|
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),
|
listOf(colorBuffer(squareDim, squareDim, format = format, type = type),
|
||||||
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) {
|
fun jumpFlood(input: ColorBuffer): ColorBuffer {
|
||||||
colorBuffer(format = format, type = type)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun jumpFlood(drawer: Drawer, input: ColorBuffer): ColorBuffer {
|
|
||||||
if (input.width != width || input.height != height) {
|
if (input.width != width || input.height != height) {
|
||||||
throw IllegalArgumentException("dimensions mismatch")
|
throw IllegalArgumentException("dimensions mismatch")
|
||||||
}
|
}
|
||||||
|
|
||||||
drawer.isolatedWithTarget(square) {
|
input.copyTo(square)
|
||||||
drawer.background(ColorRGBa.BLACK)
|
encodePoints.apply(square, coordinates[0])
|
||||||
drawer.ortho(square)
|
|
||||||
drawer.view = Matrix44.IDENTITY
|
|
||||||
drawer.model = Matrix44.IDENTITY
|
|
||||||
drawer.image(input)
|
|
||||||
}
|
|
||||||
encodePoints.apply(square.colorBuffer(0), coordinates[0])
|
|
||||||
|
|
||||||
for (i in 0 until exp) {
|
for (i in 0 until exp) {
|
||||||
jumpFlood.step = i
|
jumpFlood.step = i
|
||||||
jumpFlood.apply(coordinates[i % 2], coordinates[(i + 1) % 2])
|
jumpFlood.apply(coordinates[i % 2], coordinates[(i + 1) % 2])
|
||||||
}
|
}
|
||||||
|
|
||||||
drawer.isolatedWithTarget(final) {
|
coordinates[exp % 2].copyTo(final)
|
||||||
drawer.background(ColorRGBa.BLACK)
|
|
||||||
drawer.ortho(final)
|
return final
|
||||||
drawer.view = Matrix44.IDENTITY
|
|
||||||
drawer.model = Matrix44.IDENTITY
|
|
||||||
drawer.image(coordinates[exp % 2])
|
|
||||||
}
|
|
||||||
return encoded
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun destroy() {
|
fun destroy() {
|
||||||
coordinates.forEach { it.destroy() }
|
coordinates.forEach { it.destroy() }
|
||||||
square.colorBuffer(0).destroy()
|
|
||||||
square.detachColorBuffers()
|
|
||||||
square.destroy()
|
square.destroy()
|
||||||
|
|
||||||
final.colorBuffer(0).destroy()
|
|
||||||
final.detachColorBuffers()
|
|
||||||
final.destroy()
|
final.destroy()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -111,7 +101,7 @@ private fun encodeDecodeBitmap(drawer: Drawer, preprocess: Filter, decoder: Filt
|
|||||||
|
|
||||||
preprocess.apply(bitmap, _result)
|
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.parameters["originalSize"] = Vector2(_jumpFlooder.squareDim.toDouble(), _jumpFlooder.squareDim.toDouble())
|
||||||
decoder.apply(arrayOf(encoded, bitmap), _result)
|
decoder.apply(arrayOf(encoded, bitmap), _result)
|
||||||
@@ -140,3 +130,48 @@ fun directionFieldFromBitmap(drawer: Drawer, bitmap: ColorBuffer,
|
|||||||
jumpFlooder: JumpFlooder? = null,
|
jumpFlooder: JumpFlooder? = null,
|
||||||
result: ColorBuffer? = null
|
result: ColorBuffer? = null
|
||||||
): ColorBuffer = encodeDecodeBitmap(drawer, contourPoints, pixelDirection, bitmap, jumpFlooder, result)
|
): 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<ColorBuffer>, target: Array<ColorBuffer>) {
|
||||||
|
|
||||||
|
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])
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -4,6 +4,8 @@ uniform sampler2D tex0;
|
|||||||
uniform sampler2D tex1;
|
uniform sampler2D tex1;
|
||||||
|
|
||||||
uniform vec2 originalSize;
|
uniform vec2 originalSize;
|
||||||
|
uniform float distanceScale;
|
||||||
|
uniform bool signedBit;
|
||||||
|
|
||||||
in vec2 v_texCoord0;
|
in vec2 v_texCoord0;
|
||||||
|
|
||||||
@@ -12,12 +14,16 @@ out vec4 o_color;
|
|||||||
void main() {
|
void main() {
|
||||||
vec2 size = textureSize(tex0, 0);
|
vec2 size = textureSize(tex0, 0);
|
||||||
vec2 fixUp = v_texCoord0;
|
vec2 fixUp = v_texCoord0;
|
||||||
fixUp.y = 1.0 - fixUp.y;
|
|
||||||
fixUp *= (size/originalSize);
|
|
||||||
fixUp.y = 1.0 - fixUp.y;
|
|
||||||
vec2 pixelPosition = fixUp;
|
vec2 pixelPosition = fixUp;
|
||||||
vec2 centroidPixelPosition = texture(tex0, v_texCoord0).xy;
|
vec2 centroidPixelPosition = texture(tex0, v_texCoord0).xy;
|
||||||
vec2 pixelDistance = (centroidPixelPosition - pixelPosition) * size * vec2(1.0, -1.0);
|
vec2 pixelDistance = (centroidPixelPosition - pixelPosition) * size * vec2(1.0, -1.0);
|
||||||
float threshold = texture(tex1, v_texCoord0).r;
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user