Add DirectionalField to orx-jumpflood

This commit is contained in:
Edwin Jakobs
2020-02-29 14:01:32 +01:00
parent 050d6b3035
commit 6eade9e632
4 changed files with 104 additions and 48 deletions

View File

@@ -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<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.apply(result, result)
result.copyTo(target[0])
}
}

View File

@@ -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<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])
}
}

View File

@@ -3,6 +3,7 @@ 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.Description
import org.openrndr.extra.parameters.DoubleParameter import org.openrndr.extra.parameters.DoubleParameter
import org.openrndr.math.Vector2 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"))) { class PixelDirection : Filter(filterShaderFromUrl(resourceUrl("/shaders/gl3/pixel-direction.frag"))) {
var distanceScale: Double by parameters
var originalSize: Vector2 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"))) { 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) ): 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])
}
}

View File

@@ -3,7 +3,8 @@
uniform sampler2D tex0; uniform sampler2D tex0;
uniform sampler2D tex1; uniform sampler2D tex1;
uniform vec2 originalSize; uniform vec2 originalSize;
uniform vec2 directionalField;
uniform float distanceScale;
in vec2 v_texCoord0; in vec2 v_texCoord0;
out vec4 o_color; out vec4 o_color;
@@ -11,12 +12,9 @@ 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(pixelDistance, threshold, 1.0); o_color = vec4(pixelDistance * distanceScale, threshold, 1.0);
} }