From 9f2405e1717ea12f6553f96d9a843f7a016941e7 Mon Sep 17 00:00:00 2001 From: Abe Pazos Date: Mon, 1 May 2023 17:21:50 +0200 Subject: [PATCH 1/2] Add Segment and ShapeContour based generators --- .../src/commonMain/kotlin/Segment.kt | 37 +++++++++++++++ .../src/commonMain/kotlin/ShapeContour.kt | 39 ++++++++++++++++ .../src/jvmDemo/kotlin/DemoCurve01.kt | 38 ++++++++++++++++ .../src/jvmDemo/kotlin/DemoCurve02.kt | 45 +++++++++++++++++++ .../src/jvmDemo/kotlin/DemoCurve03.kt | 37 +++++++++++++++ 5 files changed, 196 insertions(+) create mode 100644 orx-mesh-generators/src/commonMain/kotlin/Segment.kt create mode 100644 orx-mesh-generators/src/commonMain/kotlin/ShapeContour.kt create mode 100644 orx-mesh-generators/src/jvmDemo/kotlin/DemoCurve01.kt create mode 100644 orx-mesh-generators/src/jvmDemo/kotlin/DemoCurve02.kt create mode 100644 orx-mesh-generators/src/jvmDemo/kotlin/DemoCurve03.kt diff --git a/orx-mesh-generators/src/commonMain/kotlin/Segment.kt b/orx-mesh-generators/src/commonMain/kotlin/Segment.kt new file mode 100644 index 00000000000..d7970823233 --- /dev/null +++ b/orx-mesh-generators/src/commonMain/kotlin/Segment.kt @@ -0,0 +1,37 @@ +package org.openrndr.extra.meshgenerators + +import org.openrndr.draw.VertexBuffer +import org.openrndr.draw.vertexBuffer +import org.openrndr.draw.vertexFormat +import org.openrndr.math.Vector2 +import org.openrndr.shape.Segment + +/** + * Converts a [Segment] into a [VertexBuffer]. It takes + * [samples] samples of the Segment. The thickness can + * be constant: `segment.toMesh(50) { 10.0 }` + * or variable: `segment.toMesh(50) { t -> 10.0 * t }` + * or even: `segment.toMesh(30) { t -> cos(t * 3.14159) * 10 + 5 }` + */ +fun Segment.toMesh( + samples: Int, + thickness: (Double) -> Double = { 5.0 } +): VertexBuffer { + val vb = vertexBuffer(vertexFormat { + position(3) + textureCoordinate(2) + }, samples * 2) + + vb.put { + repeat(samples) { + val t = it / (samples - 1.0) + val pos = position(t) + val n = normal(t) * 0.5 + write((pos + n * thickness(t)).xy0) + write(Vector2(t, 0.0)) + write((pos - n * thickness(t)).xy0) + write(Vector2(t, 1.0)) + } + } + return vb +} diff --git a/orx-mesh-generators/src/commonMain/kotlin/ShapeContour.kt b/orx-mesh-generators/src/commonMain/kotlin/ShapeContour.kt new file mode 100644 index 00000000000..5c3e744055d --- /dev/null +++ b/orx-mesh-generators/src/commonMain/kotlin/ShapeContour.kt @@ -0,0 +1,39 @@ +package org.openrndr.extra.meshgenerators + +import org.openrndr.draw.VertexBuffer +import org.openrndr.draw.vertexBuffer +import org.openrndr.draw.vertexFormat +import org.openrndr.math.Vector2 +import org.openrndr.shape.ShapeContour + +/** + * Converts a [ShapeContour] into a [VertexBuffer]. It takes + * [samples] samples of the Segment. The thickness can + * be constant: `contour.toMesh(50) { 10.0 }` + * or variable: `contour.toMesh(50) { t -> 10.0 * t }` + * or even: `contour.toMesh(30) { t -> cos(t * 3.14159) * 10 + 5` + */ +fun ShapeContour.toMesh( + samples: Int, + thickness: (Double) -> Double = { 5.0 } +): VertexBuffer { + val actualSamples = samples + if (closed) 1 else 0 + val vb = vertexBuffer(vertexFormat { + position(3) + textureCoordinate(2) + }, actualSamples * 2) + + vb.put { + repeat(actualSamples) { + val t = it / samples.toDouble() + val t1 = t % 1.0 + val pos = position(t1) + val n = normal(t1) * 0.5 + write((pos + n * thickness(t1)).xy0) + write(Vector2(t, 0.0)) + write((pos - n * thickness(t1)).xy0) + write(Vector2(t, 1.0)) + } + } + return vb +} diff --git a/orx-mesh-generators/src/jvmDemo/kotlin/DemoCurve01.kt b/orx-mesh-generators/src/jvmDemo/kotlin/DemoCurve01.kt new file mode 100644 index 00000000000..fe50717b100 --- /dev/null +++ b/orx-mesh-generators/src/jvmDemo/kotlin/DemoCurve01.kt @@ -0,0 +1,38 @@ +import org.openrndr.application +import org.openrndr.color.ColorRGBa +import org.openrndr.draw.DrawPrimitive +import org.openrndr.extra.meshgenerators.toMesh +import org.openrndr.math.Vector2 +import org.openrndr.shape.Segment + +/** + * Demonstrates how to convert a [Segment] into a + * vertex buffer to be drawn as a TRIANGLE_STRIP + * of constant width. + */ +fun main() = application { + program { + // Segments can have 2, 3 or 4 vertices + val seg = Segment( + Vector2(100.0, 100.0), + Vector2(width - 100.0, 100.0), + Vector2(100.0, height - 100.0), + Vector2(width - 100.0, height - 100.0) + ) + val geometry = seg.toMesh(20) { 20.0 } + + extend { + drawer.clear(ColorRGBa.PINK) + + // Visualize the texture coordinates. + // They can be used to map a texture into the mesh. + //drawer.shadeStyle = shadeStyle { + // fragmentTransform = """ + // x_fill.rg = va_texCoord0; + // """.trimIndent() + //} + + drawer.vertexBuffer(geometry, DrawPrimitive.TRIANGLE_STRIP) + } + } +} \ No newline at end of file diff --git a/orx-mesh-generators/src/jvmDemo/kotlin/DemoCurve02.kt b/orx-mesh-generators/src/jvmDemo/kotlin/DemoCurve02.kt new file mode 100644 index 00000000000..737bb1f64e8 --- /dev/null +++ b/orx-mesh-generators/src/jvmDemo/kotlin/DemoCurve02.kt @@ -0,0 +1,45 @@ +import org.openrndr.application +import org.openrndr.color.ColorRGBa +import org.openrndr.draw.DrawPrimitive +import org.openrndr.draw.shadeStyle +import org.openrndr.extra.meshgenerators.toMesh +import org.openrndr.extra.shapes.hobbyCurve +import org.openrndr.math.Polar +import org.openrndr.shape.ShapeContour +import kotlin.math.PI +import kotlin.math.cos + +/** + * Demonstrates how to convert a [ShapeContour] into a + * vertex buffer to be drawn as a TRIANGLE_STRIP. + * Uses a cosine wave to specify the thickness. + */ +fun main() = application { + program { + val contour = ShapeContour.fromPoints( + List(10) { + Polar( + it * 36.0, + 120.0 + (it % 2) * 80.0 + ).cartesian + drawer.bounds.center + }, true + ).hobbyCurve() + + val geometry = contour.toMesh(100) { t -> + 30.0 + 20.0 * cos(t * PI * 6) + } + extend { + drawer.clear(ColorRGBa.PINK) + + // Visualize the texture coordinates + // They can be used to map a texture into the mesh. + //drawer.shadeStyle = shadeStyle { + // fragmentTransform = """ + // x_fill.rg = va_texCoord0; + // """.trimIndent() + //} + + drawer.vertexBuffer(geometry, DrawPrimitive.TRIANGLE_STRIP) + } + } +} \ No newline at end of file diff --git a/orx-mesh-generators/src/jvmDemo/kotlin/DemoCurve03.kt b/orx-mesh-generators/src/jvmDemo/kotlin/DemoCurve03.kt new file mode 100644 index 00000000000..74b6572bff1 --- /dev/null +++ b/orx-mesh-generators/src/jvmDemo/kotlin/DemoCurve03.kt @@ -0,0 +1,37 @@ +import org.openrndr.application +import org.openrndr.color.ColorRGBa +import org.openrndr.draw.DrawPrimitive +import org.openrndr.draw.shadeStyle +import org.openrndr.extra.meshgenerators.toMesh +import org.openrndr.shape.Circle +import org.openrndr.shape.Rectangle +import org.openrndr.shape.ShapeContour + +/** + * Demonstrates how to convert shapes like a [Rectangle] and a [Circle] + * into a vertex buffer to be drawn as a TRIANGLE_STRIP mesh. + * Meshes can be drawn in 3D and deformed with vertex shaders. + */ +fun main() = application { + program { + val rect = Rectangle.fromCenter(drawer.bounds.center, 350.0).contour + val cir = Circle(drawer.bounds.center, 130.0).contour + + val rectMesh = rect.toMesh(100) { 40.0 } + val cirMesh = cir.toMesh(100) { 20.0 } + extend { + drawer.clear(ColorRGBa.PINK) + + // Uses the texture coordinates to create an animated pattern + drawer.shadeStyle = shadeStyle { + fragmentTransform = """ + x_fill.a = step(0.5, fract(va_texCoord0.x * 10.0 + p_seconds)); + """.trimIndent() + parameter("seconds", seconds) + } + + drawer.vertexBuffer(rectMesh, DrawPrimitive.TRIANGLE_STRIP) + drawer.vertexBuffer(cirMesh, DrawPrimitive.TRIANGLE_STRIP) + } + } +} \ No newline at end of file From 800aa2d4f2f84b5bb18feeaeef9d9446131c897b Mon Sep 17 00:00:00 2001 From: Vechro <6316604+Vechro@users.noreply.github.com> Date: Fri, 5 May 2023 21:18:58 +0100 Subject: [PATCH 2/2] Fix publishing dependency version mapping (#309) --- .../kotlin-multiplatform.gradle.kts | 26 +++++++++++++++---- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/buildSrc/src/main/kotlin/org/openrndr/extra/convention/kotlin-multiplatform.gradle.kts b/buildSrc/src/main/kotlin/org/openrndr/extra/convention/kotlin-multiplatform.gradle.kts index dae7a2b1fed..66ff570bd28 100644 --- a/buildSrc/src/main/kotlin/org/openrndr/extra/convention/kotlin-multiplatform.gradle.kts +++ b/buildSrc/src/main/kotlin/org/openrndr/extra/convention/kotlin-multiplatform.gradle.kts @@ -104,17 +104,33 @@ if (shouldPublish) { val fjdj = tasks.create("fakeJavaDocJar", Jar::class) { archiveClassifier.set("javadoc") } - matching { it.name == "jvm" }.forEach { p -> - p as MavenPublication - p.artifact(fjdj) + named("js") { + this as MavenPublication + versionMapping { + allVariants { + fromResolutionOf("jsMainResolvableDependenciesMetadata") + } + } } - all { + named("jvm") { this as MavenPublication + this.artifact(fjdj) versionMapping { allVariants { - fromResolutionOf("commonMainApiDependenciesMetadata") + fromResolutionOf("jvmMainResolvableDependenciesMetadata") } } + } + named("kotlinMultiplatform") { + this as MavenPublication + versionMapping { + allVariants { + fromResolutionOf("commonMainResolvableDependenciesMetadata") + } + } + } + all { + this as MavenPublication pom { name.set(project.name) description.set(project.name)