Improve mesh generator (#301)
Co-authored-by: Edwin Jakobs <edwin@rndr.studio>
This commit is contained in:
@@ -1,95 +1,121 @@
|
||||
# orx-mesh-generators
|
||||
|
||||
Generates 3D meshes: sphere, box, cylinder, plane, dodecahedron.
|
||||
Generates various types of 3D meshes.
|
||||
|
||||
##### usage
|
||||
## Simple meshes
|
||||
|
||||
```kotlin
|
||||
// To create simple meshes
|
||||
val sphere = sphereMesh(32, 32, 4.0)
|
||||
val unitSphere = sphereMesh()
|
||||
val cube = boxMesh()
|
||||
val box = boxMesh(2.0, 4.0, 2.0)
|
||||
val cylinder = cylinderMesh(radius = 0.5, length = 1.0, center = true)
|
||||
val dodecahedron = dodecahedronMesh(0.5)
|
||||
val plane = planeMesh(Vector3.ZERO, Vector3.UNIT_X, Vector3.UNIT_Y)
|
||||
val disk = capMesh(sides = 15, radius = 0.5)
|
||||
val tube = revolveMesh(sides = 15, length = 1.0)
|
||||
|
||||
...
|
||||
|
||||
drawer.vertexBuffer(sphere, DrawPrimitive.TRIANGLES)
|
||||
drawer.vertexBuffer(unitSphere, DrawPrimitive.TRIANGLES)
|
||||
drawer.vertexBuffer(cube, DrawPrimitive.TRIANGLES)
|
||||
drawer.vertexBuffer(box, DrawPrimitive.TRIANGLES)
|
||||
|
||||
// To draw the generated meshes
|
||||
drawer.vertexBuffer(dodecahedron, DrawPrimitive.TRIANGLES)
|
||||
```
|
||||
|
||||
## API
|
||||
## Complex triangular mesh generation
|
||||
|
||||
`orx-mesh-generators` comes with `buildTriangleMesh`, which
|
||||
implements a [DSL](https://en.wikipedia.org/wiki/Domain-specific_language)
|
||||
to construct 3D shapes.
|
||||
|
||||
To create shapes we can call methods like `box()`, `sphere()`,
|
||||
`cylinder()`, `dodecahedron()`, `plane()`, `revolve()`,
|
||||
`taperedCylinder()`, `hemisphere()` and `cap()`.
|
||||
|
||||
```kotlin
|
||||
fun sphereMesh(
|
||||
sides: Int = 16,
|
||||
segments: Int = 16,
|
||||
radius: Double = 1.0,
|
||||
invert: Boolean = false): VertexBuffer
|
||||
|
||||
fun groundPlaneMesh(
|
||||
width: Double = 1.0,
|
||||
height: Double = 1.0,
|
||||
widthSegments: Int = 1,
|
||||
heightSegments: Int): VertexBuffer
|
||||
|
||||
fun boxMesh(
|
||||
width: Double = 1.0,
|
||||
height: Double = 1.0,
|
||||
depth: Double = 1.0,
|
||||
widthSegments: Int = 1,
|
||||
heightSegments: Int = 1,
|
||||
depthSegments: Int = 1,
|
||||
invert: Boolean = false): VertexBuffer
|
||||
// Create a rotated box
|
||||
val mesh = buildTriangleMesh {
|
||||
rotate(Vector3.UNIT_Z, 45.0)
|
||||
box()
|
||||
}
|
||||
```
|
||||
<!-- __demos__ >
|
||||
# Demos
|
||||
[DemoBoxKt](src/demo/kotlin/DemoBoxKt.kt
|
||||
` and `rotate()` to create
|
||||
more complex compositions. The `color` property sets the color of
|
||||
the next mesh.
|
||||
|
||||
```kotlin
|
||||
// Create a ring of boxes of various colors
|
||||
val mesh = buildTriangleMesh {
|
||||
repeat(12) {
|
||||
// Take a small step
|
||||
translate(2.0, 0.0, 0.0)
|
||||
// Turn 30 degrees
|
||||
rotate(Vector3.UNIT_Y, 30.0)
|
||||
// Set a color
|
||||
color = rgb(it / 11.0, 1.0, 1.0 - it / 11.0)
|
||||
// Add a colored box
|
||||
box(1.0, 1.0, 1.0)
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
`isolated { ... }` can be used to encapsulate transformations and
|
||||
avoid them accumulating to unpredictable values.
|
||||
|
||||
```kotlin
|
||||
val mesh = buildTriangleMesh {
|
||||
repeat(10) { x ->
|
||||
repeat(10) { y ->
|
||||
isolated {
|
||||
translate(x * 1.0, y * 1.0, 0.0)
|
||||
sphere(8, 8, 0.1)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Other available methods are:
|
||||
|
||||
- `grid()`: creates a tri-dimensional grid of meshes.
|
||||
- `extrudeShape()`: gives depth to 2D `Shape`.
|
||||
- `twist()`: post-processing effect to twist a mesh around an axis.
|
||||
- `extrudeContourSteps()`: uses Parallel Transport Frames to extrude a contour along a 3D path.
|
||||
|
||||
The [demo folder](src/jvmDemo/kotlin) contains examples using these methods.
|
||||
|
||||
Check out the [source code](src/commonMain/kotlin) to learn about function arguments.
|
||||
|
||||
<!-- __demos__ -->
|
||||
## Demos
|
||||
### DemoAll
|
||||
[source code](src/demo/kotlin/DemoAll.kt)
|
||||
[source code](src/jvmDemo/kotlin/DemoAll.kt)
|
||||
|
||||

|
||||
|
||||
### DemoBox
|
||||
[source code](src/demo/kotlin/DemoBox.kt)
|
||||
[source code](src/jvmDemo/kotlin/DemoBox.kt)
|
||||
|
||||

|
||||
|
||||
### DemoComplex01
|
||||
[source code](src/demo/kotlin/DemoComplex01.kt)
|
||||
[source code](src/jvmDemo/kotlin/DemoComplex01.kt)
|
||||
|
||||

|
||||
|
||||
### DemoComplex02
|
||||
[source code](src/demo/kotlin/DemoComplex02.kt)
|
||||
[source code](src/jvmDemo/kotlin/DemoComplex02.kt)
|
||||
|
||||

|
||||
|
||||
### DemoComplex03
|
||||
[source code](src/demo/kotlin/DemoComplex03.kt)
|
||||
[source code](src/jvmDemo/kotlin/DemoComplex03.kt)
|
||||
|
||||

|
||||
|
||||
### DemoComplex04
|
||||
[source code](src/demo/kotlin/DemoComplex04.kt)
|
||||
[source code](src/jvmDemo/kotlin/DemoComplex04.kt)
|
||||
|
||||

|
||||
|
||||
### DemoComplex05
|
||||
[source code](src/demo/kotlin/DemoComplex05.kt)
|
||||
[source code](src/jvmDemo/kotlin/DemoComplex05.kt)
|
||||
|
||||

|
||||
|
||||
Reference in New Issue
Block a user