Skip to content

Commit 7f97cdc

Browse files
committed
[orx-mesh-generators] Add Segment and ShapeContour based generators
1 parent 64693b2 commit 7f97cdc

File tree

5 files changed

+195
-0
lines changed

5 files changed

+195
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import org.openrndr.application
2+
import org.openrndr.color.ColorRGBa
3+
import org.openrndr.draw.DrawPrimitive
4+
import org.openrndr.extra.meshgenerators.toMesh
5+
import org.openrndr.math.Vector2
6+
import org.openrndr.shape.Segment
7+
8+
/**
9+
* Demonstrates how to convert a [Segment] into a
10+
* vertex buffer to be drawn as a TRIANGLE_STRIP
11+
* of constant width.
12+
*/
13+
fun main() = application {
14+
program {
15+
// Segments can have 2, 3 or 4 vertices
16+
val seg = Segment(
17+
Vector2(100.0, 100.0),
18+
Vector2(width - 100.0, 100.0),
19+
Vector2(100.0, height - 100.0),
20+
Vector2(width - 100.0, height - 100.0)
21+
)
22+
val geometry = seg.toMesh(20) { 10.0 }
23+
24+
extend {
25+
drawer.clear(ColorRGBa.PINK)
26+
27+
// Visualize the texture coordinates.
28+
// They can be used to map a texture into the mesh.
29+
//drawer.shadeStyle = shadeStyle {
30+
// fragmentTransform = """
31+
// x_fill.rg = va_texCoord0;
32+
// """.trimIndent()
33+
//}
34+
35+
drawer.vertexBuffer(geometry, DrawPrimitive.TRIANGLE_STRIP)
36+
}
37+
}
38+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import org.openrndr.application
2+
import org.openrndr.color.ColorRGBa
3+
import org.openrndr.draw.DrawPrimitive
4+
import org.openrndr.extra.meshgenerators.toMesh
5+
import org.openrndr.extra.shapes.hobbyCurve
6+
import org.openrndr.math.Polar
7+
import org.openrndr.shape.ShapeContour
8+
import kotlin.math.PI
9+
import kotlin.math.cos
10+
11+
/**
12+
* Demonstrates how to convert a [ShapeContour] into a
13+
* vertex buffer to be drawn as a TRIANGLE_STRIP.
14+
* Uses a cosine wave to specify the thickness.
15+
*/
16+
fun main() = application {
17+
program {
18+
val contour = ShapeContour.fromPoints(
19+
List(10) {
20+
Polar(
21+
it * 36.0,
22+
120.0 + (it % 2) * 80.0
23+
).cartesian + drawer.bounds.center
24+
}, true
25+
).hobbyCurve()
26+
27+
val geometry = contour.toMesh(100) { t ->
28+
15.0 + 10.0 * cos(t * PI * 6)
29+
}
30+
extend {
31+
drawer.clear(ColorRGBa.PINK)
32+
33+
// Visualize the texture coordinates
34+
// They can be used to map a texture into the mesh.
35+
//drawer.shadeStyle = shadeStyle {
36+
// fragmentTransform = """
37+
// x_fill.rg = va_texCoord0;
38+
// """.trimIndent()
39+
//}
40+
41+
drawer.vertexBuffer(geometry, DrawPrimitive.TRIANGLE_STRIP)
42+
}
43+
}
44+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import org.openrndr.application
2+
import org.openrndr.color.ColorRGBa
3+
import org.openrndr.draw.DrawPrimitive
4+
import org.openrndr.draw.shadeStyle
5+
import org.openrndr.extra.meshgenerators.toMesh
6+
import org.openrndr.shape.Circle
7+
import org.openrndr.shape.Rectangle
8+
import org.openrndr.shape.ShapeContour
9+
10+
/**
11+
* Demonstrates how to convert shapes like a [Rectangle] and a [Circle]
12+
* into a vertex buffer to be drawn as a TRIANGLE_STRIP mesh.
13+
* Meshes can be drawn in 3D and deformed with vertex shaders.
14+
*/
15+
fun main() = application {
16+
program {
17+
val rect = Rectangle.fromCenter(drawer.bounds.center, 350.0).contour
18+
val cir = Circle(drawer.bounds.center, 130.0).contour
19+
20+
val rectMesh = rect.toMesh(100) { 20.0 }
21+
val cirMesh = cir.toMesh(100) { 10.0 }
22+
extend {
23+
drawer.clear(ColorRGBa.PINK)
24+
25+
// Uses the texture coordinates to create an animated pattern
26+
drawer.shadeStyle = shadeStyle {
27+
fragmentTransform = """
28+
x_fill.a = step(0.5, fract(va_texCoord0.x * 10.0 + p_seconds));
29+
""".trimIndent()
30+
parameter("seconds", seconds)
31+
}
32+
33+
drawer.vertexBuffer(rectMesh, DrawPrimitive.TRIANGLE_STRIP)
34+
drawer.vertexBuffer(cirMesh, DrawPrimitive.TRIANGLE_STRIP)
35+
}
36+
}
37+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package org.openrndr.extra.meshgenerators
2+
3+
import org.openrndr.draw.VertexBuffer
4+
import org.openrndr.draw.vertexBuffer
5+
import org.openrndr.draw.vertexFormat
6+
import org.openrndr.math.Vector2
7+
import org.openrndr.shape.Segment
8+
9+
/**
10+
* Converts a [Segment] into a [VertexBuffer]. It takes
11+
* [samples] samples of the Segment. The thickness can
12+
* be constant: `segment.toMesh(50) { 10.0 }`
13+
* or variable: `segment.toMesh(50) { t -> 10.0 * t }`
14+
* or even: `segment.toMesh(30) { t -> cos(t * 3.14159) * 10 + 5 }`
15+
*/
16+
fun Segment.toMesh(
17+
samples: Int,
18+
thickness: (Double) -> Double = { 5.0 }
19+
): VertexBuffer {
20+
val vb = vertexBuffer(vertexFormat {
21+
position(3)
22+
textureCoordinate(2)
23+
}, samples * 2)
24+
25+
vb.put {
26+
repeat(samples) {
27+
val t = it / (samples - 1.0)
28+
val pos = position(t)
29+
val n = normal(t)
30+
write((pos + n * thickness(t)).xy0)
31+
write(Vector2(t, 0.0))
32+
write((pos - n * thickness(t)).xy0)
33+
write(Vector2(t, 1.0))
34+
}
35+
}
36+
return vb
37+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package org.openrndr.extra.meshgenerators
2+
3+
import org.openrndr.draw.VertexBuffer
4+
import org.openrndr.draw.vertexBuffer
5+
import org.openrndr.draw.vertexFormat
6+
import org.openrndr.math.Vector2
7+
import org.openrndr.shape.ShapeContour
8+
9+
/**
10+
* Converts a [ShapeContour] into a [VertexBuffer]. It takes
11+
* [samples] samples of the Segment. The thickness can
12+
* be constant: `contour.toMesh(50) { 10.0 }`
13+
* or variable: `contour.toMesh(50) { t -> 10.0 * t }`
14+
* or even: `contour.toMesh(30) { t -> cos(t * 3.14159) * 10 + 5`
15+
*/
16+
fun ShapeContour.toMesh(
17+
samples: Int,
18+
thickness: (Double) -> Double = { 5.0 }
19+
): VertexBuffer {
20+
val actualSamples = samples + if (closed) 1 else 0
21+
val vb = vertexBuffer(vertexFormat {
22+
position(3)
23+
textureCoordinate(2)
24+
}, actualSamples * 2)
25+
26+
vb.put {
27+
repeat(actualSamples) {
28+
val t = it / samples.toDouble()
29+
val t1 = t % 1.0
30+
val pos = position(t1)
31+
val n = normal(t1)
32+
write((pos + n * thickness(t1)).xy0)
33+
write(Vector2(t, 0.0))
34+
write((pos - n * thickness(t1)).xy0)
35+
write(Vector2(t, 1.0))
36+
}
37+
}
38+
return vb
39+
}

0 commit comments

Comments
 (0)