From f1d85fb0c6f9bbb54b51af9c1623ff063a09b0cc Mon Sep 17 00:00:00 2001 From: Abe Pazos Date: Thu, 20 Oct 2022 19:15:49 +0000 Subject: [PATCH] [orx-jumpflood] Add DemoDirectionField01.kt and DemoDistanceField01.kt (#281) --- .../src/demo/kotlin/DemoDirectionField01.kt | 80 +++++++++++++++++ .../src/demo/kotlin/DemoDistanceField01.kt | 85 +++++++++++++++++++ 2 files changed, 165 insertions(+) create mode 100644 orx-jumpflood/src/demo/kotlin/DemoDirectionField01.kt create mode 100644 orx-jumpflood/src/demo/kotlin/DemoDistanceField01.kt diff --git a/orx-jumpflood/src/demo/kotlin/DemoDirectionField01.kt b/orx-jumpflood/src/demo/kotlin/DemoDirectionField01.kt new file mode 100644 index 00000000..e9845a6e --- /dev/null +++ b/orx-jumpflood/src/demo/kotlin/DemoDirectionField01.kt @@ -0,0 +1,80 @@ +import org.openrndr.application +import org.openrndr.color.ColorRGBa +import org.openrndr.draw.* +import org.openrndr.extra.jumpfill.DirectionalField +import org.openrndr.extra.noise.simplex +import org.openrndr.math.Vector2 +import org.openrndr.math.Vector3 +import org.openrndr.shape.Rectangle + +/** + * Shows how to use the [DirectionalField] filter. + * Draws moving white shapes on black background, + * then applies the DirectionalField filter which returns a [ColorBuffer] in which + * the red and green components encode the direction to the closest black/white edge. + * + * Hold down a mouse button to see the raw animation. + */ +fun main() = application { + configure { + width = 1024 + height = 1024 + } + + program { + val rt = renderTarget(width, height) { colorBuffer() } + val directionalField = DirectionalField().also { + it.distanceScale = 0.004 + } + + // Needs to be FLOAT32 so we can have negative values + val result = colorBuffer(width, height, type = ColorType.FLOAT32) + val shader = shadeStyle { + fragmentTransform = """ + x_fill.rgb = vec3(x_fill.rg + 0.5, x_fill.b); + + // interesting when distanceScale = 1.0 + //x_fill.rgb = vec3(1.0 / (x_fill.r + x_fill.g)); + """ + } + + extend { + // Draw moving white shapes on a black background + drawer.isolatedWithTarget(rt) { + clear(ColorRGBa.BLACK) + stroke = null + fill = ColorRGBa.WHITE + repeat(10) { + val pos = Vector2.simplex(it, seconds * 0.2) * + bounds.center + bounds.center + val size = (it * it + 5.0) * 2.0 + + isolated { + translate(pos) + if (it % 2 == 0) { + circle(Vector2.ZERO, size) + } else { + rotate(Vector3.UNIT_Z, pos.x) + rectangle( + Rectangle.fromCenter( + Vector2.ZERO, size, size * 2 + ) + ) + } + } + } + } + + directionalField.apply(rt.colorBuffer(0), result) + + drawer.isolated { + if (mouse.pressedButtons.isEmpty()) { + shadeStyle = shader + image(result) + } else { + image(rt.colorBuffer(0)) + } + } + } + } +} \ No newline at end of file diff --git a/orx-jumpflood/src/demo/kotlin/DemoDistanceField01.kt b/orx-jumpflood/src/demo/kotlin/DemoDistanceField01.kt new file mode 100644 index 00000000..ff77c7ae --- /dev/null +++ b/orx-jumpflood/src/demo/kotlin/DemoDistanceField01.kt @@ -0,0 +1,85 @@ +import org.openrndr.application +import org.openrndr.color.ColorRGBa +import org.openrndr.draw.* +import org.openrndr.extra.jumpfill.DistanceField +import org.openrndr.extra.noise.simplex +import org.openrndr.math.Vector2 +import org.openrndr.math.Vector3 +import org.openrndr.shape.Rectangle + +/** + * Shows how to use the [DistanceField] filter. + * Draws moving white shapes on black background, + * then applies the DistanceField filter which returns a [ColorBuffer] in which + * the red component encodes the distance to the closest black/white edge. + * The value is positive when on the black background and negative + * when inside white shapes. The sign is used in the [shadeStyle] to choose + * between two colors. The inverse of the distance is used to obtain a + * non-linear brightness. + * Hold down a mouse button to see the raw animation. + */ +fun main() = application { + configure { + width = 1024 + height = 1024 + } + + program { + val rt = renderTarget(width, height) { colorBuffer() } + val distanceField = DistanceField() + + // Needs to be FLOAT32 so we can have negative values + val result = colorBuffer(width, height, type = ColorType.FLOAT32) + val shader = shadeStyle { + fragmentTransform = """ + float distance = abs(x_fill.r); + float bri = 1.0 / (1.0 + 0.03 * distance); + + // wavy effect + // bri *= (1.0 + 0.2 * sin(distance * 0.2)); + + x_fill.rgb = bri * (x_fill.r > 0 ? + vec3(1.0, 0.0, 0.0) : vec3(0.0, 1.0, 1.0)); + """ + } + + extend { + // Draw moving white shapes on a black background + drawer.isolatedWithTarget(rt) { + clear(ColorRGBa.BLACK) + stroke = null + fill = ColorRGBa.WHITE + repeat(10) { + val pos = Vector2.simplex(it, seconds * 0.2) * + bounds.center + bounds.center + val size = (it * it + 5.0) * 2.0 + + isolated { + translate(pos) + if (it % 2 == 0) { + circle(Vector2.ZERO, size) + } else { + rotate(Vector3.UNIT_Z, pos.x) + rectangle( + Rectangle.fromCenter( + Vector2.ZERO, size, size * 2 + ) + ) + } + } + } + } + + distanceField.apply(rt.colorBuffer(0), result) + + drawer.isolated { + if (mouse.pressedButtons.isEmpty()) { + shadeStyle = shader + image(result) + } else { + image(rt.colorBuffer(0)) + } + } + } + } +} \ No newline at end of file