From 1060f751d7c65233b6c7f7a83ca36050cd0a69e9 Mon Sep 17 00:00:00 2001 From: Abe Pazos Date: Tue, 2 Apr 2024 13:17:06 +0200 Subject: [PATCH] Fix demos (#331) --- .../src/demo/kotlin/DemoBillboardCircles01.kt | 13 ++- .../demo/kotlin/DemoCompositionDrawer01.kt | 37 -------- .../demo/kotlin/DemoShaderStorageBuffer01.kt | 90 +++++++++++-------- orx-color/build.gradle.kts | 1 + orx-composition/build.gradle.kts | 4 +- .../jvmDemo/kotlin/DemoCompositionDrawer01.kt | 42 +++++++++ .../kotlin/DemoCompositionDrawer02.kt | 5 +- .../kotlin/DemoCompositionDrawer03.kt | 5 +- orx-fx/src/jvmDemo/kotlin/DemoBlur01.kt | 1 + 9 files changed, 113 insertions(+), 85 deletions(-) delete mode 100644 openrndr-demos/src/demo/kotlin/DemoCompositionDrawer01.kt create mode 100644 orx-composition/src/jvmDemo/kotlin/DemoCompositionDrawer01.kt rename {openrndr-demos/src/demo => orx-composition/src/jvmDemo}/kotlin/DemoCompositionDrawer02.kt (78%) rename {openrndr-demos/src/demo => orx-composition/src/jvmDemo}/kotlin/DemoCompositionDrawer03.kt (80%) diff --git a/openrndr-demos/src/demo/kotlin/DemoBillboardCircles01.kt b/openrndr-demos/src/demo/kotlin/DemoBillboardCircles01.kt index 07ace9be..ce19e55c 100644 --- a/openrndr-demos/src/demo/kotlin/DemoBillboardCircles01.kt +++ b/openrndr-demos/src/demo/kotlin/DemoBillboardCircles01.kt @@ -26,7 +26,7 @@ fun main() = application { circlePositions.put { for (i in 0 until circlePositions.vertexCount) { - write(Vector3.uniformRing(0.0, 3.0)) + write(Vector3.uniformRing(2.0, 3.0)) write(Math.random().toFloat()*0.1f) } } @@ -36,8 +36,7 @@ fun main() = application { drawer.perspective(90.0, width*1.0/height*1.0, 0.1, 100.0) drawer.fill = ColorRGBa.PINK - drawer.stroke = ColorRGBa.GREEN - drawer.strokeWeight = 0.05 + drawer.stroke = null drawer.drawStyle.alphaToCoverage = true drawer.depthWrite = true @@ -52,6 +51,14 @@ fun main() = application { x_position = viewOffset + vec3(a_position.xy * i_scale, 0.0); vi_radius = vec2(i_scale); """.trimIndent() + + // The circle bounds can be used to calculate a color or to sample a texture + fragmentTransform = """ + float r = length(c_boundsPosition.xy - 0.5) * 2.0; + x_fill.rg = c_boundsPosition.xy; + x_fill.a = 1.0 - step(1.0, r); + """.trimIndent() + attributes(circlePositions) } diff --git a/openrndr-demos/src/demo/kotlin/DemoCompositionDrawer01.kt b/openrndr-demos/src/demo/kotlin/DemoCompositionDrawer01.kt deleted file mode 100644 index 50e67af7..00000000 --- a/openrndr-demos/src/demo/kotlin/DemoCompositionDrawer01.kt +++ /dev/null @@ -1,37 +0,0 @@ -import org.openrndr.application -import org.openrndr.color.ColorRGBa -import org.openrndr.math.Vector2 -import org.openrndr.shape.drawComposition -//import org.openrndr.svg.svgNamespaceInkscape -import org.openrndr.svg.toSVG -import org.openrndr.svg.writeSVG - -fun main() { - application { - program { - extend { - drawer.clear(ColorRGBa.WHITE) - - val composition = drawComposition { - val layer = group { - fill = ColorRGBa.PINK - stroke = ColorRGBa.BLACK - strokeWeight = 10.0 - circle(Vector2(width / 2.0, height / 2.0), 100.0) - circle(Vector2(200.0, 200.0), 50.0) - } - // demonstrating how to set custom attributes on the CompositionNode - // these are stored in SVG -// layer.id = "Layer_2" -// layer.attributes["inkscape:label"] = "Layer 1" -// layer.attributes["inkscape:groupmode"] = "layer" - } - // draw the composition to the screen - drawer.composition(composition) - - // print the svg to the console -// println(composition.toSVG(namespaces = listOf(svgNamespaceInkscape))) - } - } - } -} \ No newline at end of file diff --git a/openrndr-demos/src/demo/kotlin/DemoShaderStorageBuffer01.kt b/openrndr-demos/src/demo/kotlin/DemoShaderStorageBuffer01.kt index 8f316e69..f880592f 100644 --- a/openrndr-demos/src/demo/kotlin/DemoShaderStorageBuffer01.kt +++ b/openrndr-demos/src/demo/kotlin/DemoShaderStorageBuffer01.kt @@ -1,39 +1,51 @@ -//import org.openrndr.application -//import org.openrndr.draw.VertexElementType -//import org.openrndr.draw.shadeStyle -//import org.openrndr.draw.shaderStorageBuffer -//import org.openrndr.draw.shaderStorageFormat -//import java.nio.ByteBuffer -//import java.nio.ByteOrder -// -//fun main() = application { -// program { -// -// val ssb = shaderStorageBuffer(shaderStorageFormat { -// //member("foo", VertexElementType.FLOAT32, 1000) -// -// }) -// val ss = shadeStyle { -// buffer("someBuffer", ssb) -// fragmentTransform = "float a = b_someBuffer.foo[0]; b_someBuffer.foo[1] += 2.0;" -// } -// -// val bb = ByteBuffer.allocateDirect(ssb.format.size) -// bb.order(ByteOrder.nativeOrder()) -// -// extend { -// ssb.clear() -// -// drawer.shadeStyle = ss -// drawer.circle(100.0, 100.0, 200.0) -// bb.rewind() -// ssb.read(bb) -// bb.rewind() -// val f0 = bb.float -// val f1 = bb.float -// println(f1) -// -// } -// -// } -//} \ No newline at end of file +import org.openrndr.application +import org.openrndr.draw.* +import java.nio.ByteBuffer +import java.nio.ByteOrder + +// A demo of shaderStorageBuffer doing no useful work + +fun main() = application { + program { + + // Construct a SSB + val ssb = shaderStorageBuffer(shaderStorageFormat { + primitive("foo", BufferPrimitiveType.FLOAT32, 1000) + }) + + // A ShadeStyle that reads from and writes into an SSB + val ss = shadeStyle { + buffer("someBuffer", ssb) + fragmentTransform = """ + float a = b_someBuffer.foo[0]; + b_someBuffer.foo[1] += 2.0; + """.trimIndent() + } + + // A ByteBuffer in RAM to download the GPU data into + val bb = ByteBuffer.allocateDirect(ssb.format.size) + bb.order(ByteOrder.nativeOrder()) + + extend { + // Clear the SSB + ssb.clear() + + drawer.shadeStyle = ss + drawer.circle(100.0, 100.0, 200.0) + + // Download the SSB into RAM + bb.rewind() + ssb.read(bb) + + bb.rewind() + val f0 = bb.float + val f1 = bb.float + println(f1) + // The shade style runs for every pix el in the circle. + // The order in which the pixels are processed is not known + // Therefore the value of `f1` can vary from frame to frame, + // because we don't know how many times `+= 2.0` was executed. + } + + } +} \ No newline at end of file diff --git a/orx-color/build.gradle.kts b/orx-color/build.gradle.kts index b0719cf0..6660b7b2 100644 --- a/orx-color/build.gradle.kts +++ b/orx-color/build.gradle.kts @@ -44,6 +44,7 @@ kotlin { implementation(project(":orx-mesh-generators")) implementation(project(":orx-color")) implementation(project(":orx-jvm:orx-gui")) + implementation(project(":orx-shade-styles")) } } } diff --git a/orx-composition/build.gradle.kts b/orx-composition/build.gradle.kts index 9612c244..08e84e5a 100644 --- a/orx-composition/build.gradle.kts +++ b/orx-composition/build.gradle.kts @@ -22,8 +22,8 @@ kotlin { @Suppress("UNUSED_VARIABLE") val jvmDemo by getting { dependencies { - implementation(project(":orx-shapes")) + implementation(project(":orx-svg")) } } } -} \ No newline at end of file +} diff --git a/orx-composition/src/jvmDemo/kotlin/DemoCompositionDrawer01.kt b/orx-composition/src/jvmDemo/kotlin/DemoCompositionDrawer01.kt new file mode 100644 index 00000000..0e34c773 --- /dev/null +++ b/orx-composition/src/jvmDemo/kotlin/DemoCompositionDrawer01.kt @@ -0,0 +1,42 @@ +import org.openrndr.application +import org.openrndr.color.ColorRGBa +import org.openrndr.extra.composition.composition +import org.openrndr.extra.composition.drawComposition +import org.openrndr.extra.svg.saveToFile +import org.openrndr.extra.svg.toSVG +import org.openrndr.math.Vector2 +import java.io.File + +fun main() { + application { + program { + val composition = drawComposition { + val layer = group { + fill = ColorRGBa.PINK + stroke = ColorRGBa.BLACK + strokeWeight = 10.0 + circle(Vector2(width / 2.0, height / 2.0), 100.0) + circle(Vector2(200.0, 200.0), 50.0) + } + // demonstrating how to set custom attributes on the CompositionNode + // these are stored in SVG + //layer.id = "Layer_2" + //layer.attributes["inkscape:label"] = "Layer 1" + //layer.attributes["inkscape:groupmode"] = "layer" + } + + // print the svg to the console + println(composition.toSVG()) + + // save svg to a File + //composition.saveToFile(File("/path/to/design.svg")) + + extend { + drawer.clear(ColorRGBa.WHITE) + + // draw the composition to the screen + drawer.composition(composition) + } + } + } +} \ No newline at end of file diff --git a/openrndr-demos/src/demo/kotlin/DemoCompositionDrawer02.kt b/orx-composition/src/jvmDemo/kotlin/DemoCompositionDrawer02.kt similarity index 78% rename from openrndr-demos/src/demo/kotlin/DemoCompositionDrawer02.kt rename to orx-composition/src/jvmDemo/kotlin/DemoCompositionDrawer02.kt index 7f0f4544..8bb0e211 100644 --- a/openrndr-demos/src/demo/kotlin/DemoCompositionDrawer02.kt +++ b/orx-composition/src/jvmDemo/kotlin/DemoCompositionDrawer02.kt @@ -1,7 +1,8 @@ import org.openrndr.application import org.openrndr.color.ColorRGBa -import org.openrndr.shape.ClipMode -import org.openrndr.shape.drawComposition +import org.openrndr.extra.composition.ClipMode +import org.openrndr.extra.composition.composition +import org.openrndr.extra.composition.drawComposition fun main() { application { diff --git a/openrndr-demos/src/demo/kotlin/DemoCompositionDrawer03.kt b/orx-composition/src/jvmDemo/kotlin/DemoCompositionDrawer03.kt similarity index 80% rename from openrndr-demos/src/demo/kotlin/DemoCompositionDrawer03.kt rename to orx-composition/src/jvmDemo/kotlin/DemoCompositionDrawer03.kt index dc482c4d..8f7bcf57 100644 --- a/openrndr-demos/src/demo/kotlin/DemoCompositionDrawer03.kt +++ b/orx-composition/src/jvmDemo/kotlin/DemoCompositionDrawer03.kt @@ -1,7 +1,8 @@ import org.openrndr.application import org.openrndr.color.ColorRGBa -import org.openrndr.shape.ClipMode -import org.openrndr.shape.drawComposition +import org.openrndr.extra.composition.ClipMode +import org.openrndr.extra.composition.composition +import org.openrndr.extra.composition.drawComposition fun main() { application { diff --git a/orx-fx/src/jvmDemo/kotlin/DemoBlur01.kt b/orx-fx/src/jvmDemo/kotlin/DemoBlur01.kt index 12967a92..90d401db 100644 --- a/orx-fx/src/jvmDemo/kotlin/DemoBlur01.kt +++ b/orx-fx/src/jvmDemo/kotlin/DemoBlur01.kt @@ -63,6 +63,7 @@ fun main() { is HashBlur -> { blur.samples = 50 blur.radius = 5.0 + blur.time = seconds } is GaussianBlur -> { blur.window = 25