Skip to content

[orx-mesh-generators] Add Segment and ShapeContour based generators #284

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
37 changes: 37 additions & 0 deletions orx-mesh-generators/src/commonMain/kotlin/Segment.kt
Original file line number Diff line number Diff line change
@@ -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
}
39 changes: 39 additions & 0 deletions orx-mesh-generators/src/commonMain/kotlin/ShapeContour.kt
Original file line number Diff line number Diff line change
@@ -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
}
38 changes: 38 additions & 0 deletions orx-mesh-generators/src/jvmDemo/kotlin/DemoCurve01.kt
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
45 changes: 45 additions & 0 deletions orx-mesh-generators/src/jvmDemo/kotlin/DemoCurve02.kt
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
37 changes: 37 additions & 0 deletions orx-mesh-generators/src/jvmDemo/kotlin/DemoCurve03.kt
Original file line number Diff line number Diff line change
@@ -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)
}
}
}