Bump version to 0.0.15

Add thresholded image to result of jump flood
This commit is contained in:
Edwin Jakobs
2018-12-02 19:09:35 +01:00
parent 4876e87924
commit 414a0fcc95
3 changed files with 145 additions and 144 deletions

View File

@@ -4,7 +4,7 @@ plugins {
allprojects { allprojects {
group 'org.openrndr.extra' group 'org.openrndr.extra'
version '0.0.14' version '0.0.15'
} }
repositories { repositories {
@@ -13,7 +13,7 @@ repositories {
} }
ext { ext {
openrndrVersion = "0.3.30-rc2" openrndrVersion = "0.3.30"
} }
subprojects { subprojects {

View File

@@ -1,129 +1,129 @@
package org.openrndr.extra.jumpfill 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.filter.filterShaderFromUrl import org.openrndr.filter.filterShaderFromUrl
import org.openrndr.math.Matrix44 import org.openrndr.math.Matrix44
import org.openrndr.math.Vector2 import org.openrndr.math.Vector2
import org.openrndr.resourceUrl import org.openrndr.resourceUrl
class EncodePoints : Filter(filterShaderFromUrl(resourceUrl("/shaders/gl3/encode-points.frag"))) class EncodePoints : Filter(filterShaderFromUrl(resourceUrl("/shaders/gl3/encode-points.frag")))
class JumpFlood : Filter(filterShaderFromUrl(resourceUrl("/shaders/gl3/jumpflood.frag"))) { class JumpFlood : Filter(filterShaderFromUrl(resourceUrl("/shaders/gl3/jumpflood.frag"))) {
var maxSteps: Int by parameters var maxSteps: Int by parameters
var step: Int by parameters var step: Int by parameters
} }
class PixelDistance : Filter(filterShaderFromUrl(resourceUrl("/shaders/gl3/pixel-distance.frag"))) class PixelDistance : Filter(filterShaderFromUrl(resourceUrl("/shaders/gl3/pixel-distance.frag")))
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 by parameters
init { init {
threshold = 0.5 threshold = 0.5
} }
} }
val encodePoints by lazy { EncodePoints() } val encodePoints by lazy { EncodePoints() }
val jumpFlood by lazy { JumpFlood() } val jumpFlood by lazy { JumpFlood() }
val pixelDistance by lazy { PixelDistance() } val pixelDistance by lazy { PixelDistance() }
val contourPoints by lazy { ContourPoints() } val contourPoints by lazy { ContourPoints() }
val threshold by lazy { Threshold() } val threshold by lazy { Threshold() }
class JumpFlooder(val width: Int, val height: Int) { class JumpFlooder(val width: Int, val height: Int) {
private val dimension = Math.max(width, height) private val dimension = Math.max(width, height)
private val exp = Math.ceil(Math.log(dimension.toDouble()) / Math.log(2.0)).toInt() private val exp = Math.ceil(Math.log(dimension.toDouble()) / Math.log(2.0)).toInt()
private val squareDim = Math.pow(2.0, exp.toDouble()).toInt() private val squareDim = Math.pow(2.0, exp.toDouble()).toInt()
private val coordinates = private val coordinates =
listOf(colorBuffer(squareDim, squareDim, format = ColorFormat.RG, type = ColorType.FLOAT32), listOf(colorBuffer(squareDim, squareDim, format = ColorFormat.RGB, type = ColorType.FLOAT32),
colorBuffer(squareDim, squareDim, format = ColorFormat.RG, type = ColorType.FLOAT32)) colorBuffer(squareDim, squareDim, format = ColorFormat.RGB, type = ColorType.FLOAT32))
private val final = renderTarget(width, height) { private val final = renderTarget(width, height) {
colorBuffer(type = ColorType.FLOAT32) colorBuffer(type = ColorType.FLOAT32)
} }
val result: ColorBuffer get() = final.colorBuffer(0) val result: ColorBuffer get() = final.colorBuffer(0)
private val square = renderTarget(squareDim, squareDim) { private val square = renderTarget(squareDim, squareDim) {
colorBuffer() colorBuffer()
} }
private var contourUsed = false private var contourUsed = false
private val thresholded by lazy { colorBuffer(width, height) } private val thresholded by lazy { colorBuffer(width, height) }
private val edges by lazy { colorBuffer(width, height) } private val edges by lazy { colorBuffer(width, height) }
fun distanceToContour(drawer: Drawer, input: ColorBuffer, thresholdValue: Double = 0.5): ColorBuffer { fun distanceToContour(drawer: Drawer, input: ColorBuffer, thresholdValue: Double = 0.5): ColorBuffer {
threshold.threshold = thresholdValue threshold.threshold = thresholdValue
threshold.apply(input, thresholded) threshold.apply(input, thresholded)
contourPoints.apply(thresholded, edges) contourPoints.apply(thresholded, edges)
contourUsed = true contourUsed = true
return jumpFlood(drawer, edges) return jumpFlood(drawer, edges)
} }
fun directions(xRange: IntProgression = 0 until width, yRange: IntProgression = 0 until height): Array<List<Vector2>> { fun directions(xRange: IntProgression = 0 until width, yRange: IntProgression = 0 until height): Array<List<Vector2>> {
result.shadow.download() result.shadow.download()
return result.shadow.mapIndexed(xRange, yRange) { _, _, r, g, _, _ -> Vector2(r, g) } return result.shadow.mapIndexed(xRange, yRange) { _, _, r, g, _, _ -> Vector2(r, g) }
} }
fun jumpFlood(drawer: Drawer, input: ColorBuffer): ColorBuffer { 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) { drawer.isolatedWithTarget(square) {
drawer.background(ColorRGBa.BLACK) drawer.background(ColorRGBa.BLACK)
drawer.ortho(square) drawer.ortho(square)
drawer.view = Matrix44.IDENTITY drawer.view = Matrix44.IDENTITY
drawer.model = Matrix44.IDENTITY drawer.model = Matrix44.IDENTITY
drawer.image(input) drawer.image(input)
} }
encodePoints.apply(square.colorBuffer(0), coordinates[0]) 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])
} }
pixelDistance.apply(coordinates[exp % 2], coordinates[exp % 2]) pixelDistance.apply( arrayOf(coordinates[exp % 2], thresholded), coordinates[exp % 2])
drawer.isolatedWithTarget(final) { drawer.isolatedWithTarget(final) {
drawer.background(ColorRGBa.BLACK) drawer.background(ColorRGBa.BLACK)
drawer.ortho(final) drawer.ortho(final)
drawer.view = Matrix44.IDENTITY drawer.view = Matrix44.IDENTITY
drawer.model = Matrix44.IDENTITY drawer.model = Matrix44.IDENTITY
drawer.image(coordinates[exp % 2]) drawer.image(coordinates[exp % 2])
} }
return result return result
} }
fun destroy(destroyFinal: Boolean = true) { fun destroy(destroyFinal: Boolean = true) {
coordinates.forEach { it.destroy() } coordinates.forEach { it.destroy() }
square.colorBuffer(0).destroy() square.colorBuffer(0).destroy()
square.detachColorBuffers() square.detachColorBuffers()
square.destroy() square.destroy()
if (destroyFinal) { if (destroyFinal) {
final.colorBuffer(0).destroy() final.colorBuffer(0).destroy()
} }
final.detachColorBuffers() final.detachColorBuffers()
final.destroy() final.destroy()
if (contourUsed) { if (contourUsed) {
edges.destroy() edges.destroy()
thresholded.destroy() thresholded.destroy()
} }
} }
} }
fun jumpFlood(drawer: Drawer, points: ColorBuffer): ColorBuffer { fun jumpFlood(drawer: Drawer, points: ColorBuffer): ColorBuffer {
val jumpFlooder = JumpFlooder(points.width, points.height) val jumpFlooder = JumpFlooder(points.width, points.height)
jumpFlooder.jumpFlood(drawer, points) jumpFlooder.jumpFlood(drawer, points)
val result = jumpFlooder.result val result = jumpFlooder.result
jumpFlooder.destroy(false) jumpFlooder.destroy(false)
return result return result
} }

View File

@@ -1,15 +1,16 @@
#version 330 core #version 330 core
uniform sampler2D tex0; uniform sampler2D tex0;
in vec2 v_texCoord0; uniform sampler2D tex1;
in vec2 v_texCoord0;
out vec4 o_color;
out vec4 o_color;
void main() {
vec2 size = textureSize(tex0, 0); void main() {
vec2 pixelPosition = v_texCoord0; vec2 size = textureSize(tex0, 0);
vec2 centroidPixelPosition = texture(tex0, v_texCoord0).xy; vec2 pixelPosition = v_texCoord0;
vec2 pixelDistance = (centroidPixelPosition - pixelPosition) * size * vec2(1.0, -1.0); vec2 centroidPixelPosition = texture(tex0, v_texCoord0).xy;
vec2 pixelDistance = (centroidPixelPosition - pixelPosition) * size * vec2(1.0, -1.0);
o_color = vec4(pixelDistance, 0.0, 1.0); float threshold = texture(tex1, v_texCoord0).r;
o_color = vec4(pixelDistance, threshold, 1.0);
} }