From 6fc76daa138c37c9c01e10beafa8c0756bfa9828 Mon Sep 17 00:00:00 2001 From: Edwin Jakobs Date: Thu, 13 Aug 2020 10:51:39 +0200 Subject: [PATCH] [orx-mesh-generators] Add arguments to make sides in shape extrude optional --- .../src/main/kotlin/GeneratorBuffer.kt | 30 +++++++- .../src/main/kotlin/MeshGenerators.kt | 77 +++++++++---------- 2 files changed, 62 insertions(+), 45 deletions(-) diff --git a/orx-mesh-generators/src/main/kotlin/GeneratorBuffer.kt b/orx-mesh-generators/src/main/kotlin/GeneratorBuffer.kt index d853e3c8..5cd94376 100644 --- a/orx-mesh-generators/src/main/kotlin/GeneratorBuffer.kt +++ b/orx-mesh-generators/src/main/kotlin/GeneratorBuffer.kt @@ -143,16 +143,40 @@ fun GeneratorBuffer.extrudeShape( scale: Double = 1.0, frontCap: Boolean = true, backCap: Boolean = true, + sides: Boolean = true, distanceTolerance: Double = 0.5 ) { - extrudeShape(shape, -length / 2.0, length / 2.0, scale, scale, frontCap, backCap, distanceTolerance, false, this::write) + extrudeShape( + shape = shape, + front = -length / 2.0, + back = length / 2.0, + frontScale = scale, + backScale = scale, + frontCap = frontCap, + backCap = backCap, + sides = sides, + distanceTolerance = distanceTolerance, + flipNormals = false, + writer = this::write + ) } fun GeneratorBuffer.extrudeShapes(shapes: List, length: Double, scale: Double = 1.0, distanceTolerance: Double = 0.5) { - extrudeShapes(shapes, -length / 2.0, length / 2.0, scale, scale, true, true, distanceTolerance, false, this::write) + extrudeShapes( + shapes = shapes, + front = -length / 2.0, + back = length / 2.0, + frontScale = scale, + backScale = scale, + frontCap = true, + backCap = true, + sides = true, + distanceTolerance = distanceTolerance, + flipNormals = false, + writer = this::write + ) } - fun meshGenerator(vertexBuffer: VertexBuffer? = null, builder: GeneratorBuffer.() -> Unit): VertexBuffer { val gb = GeneratorBuffer() gb.builder() diff --git a/orx-mesh-generators/src/main/kotlin/MeshGenerators.kt b/orx-mesh-generators/src/main/kotlin/MeshGenerators.kt index 87ea1e4a..ac0c8237 100644 --- a/orx-mesh-generators/src/main/kotlin/MeshGenerators.kt +++ b/orx-mesh-generators/src/main/kotlin/MeshGenerators.kt @@ -53,6 +53,7 @@ fun extrudeShape(baseTriangles: List, contours: List>, fr backScale: Double = 1.0, frontCap: Boolean = true, backCap: Boolean = true, + sides: Boolean = true, distanceTolerance: Double = 0.5, flipNormals: Boolean = false, writer: VertexWriter) { @@ -75,49 +76,51 @@ fun extrudeShape(baseTriangles: List, contours: List>, fr } } - contours.forEach { - val points = it + if (sides) { + contours.forEach { + val points = it - val normals = (points.indices).map { index -> - (points[mod(index + 1, points.size)] - points[mod(index - 1, points.size)]).safeNormalized * -flip - } - val forward = Vector3(0.0, 0.0, depth) - val base = Vector3(0.0, 0.0, front) + val normals = (points.indices).map { index -> + (points[mod(index + 1, points.size)] - points[mod(index - 1, points.size)]).safeNormalized * -flip + } + val forward = Vector3(0.0, 0.0, depth) + val base = Vector3(0.0, 0.0, front) - var offset = 0.0 - (points zip normals).zipWithNext().forEach { (left, right) -> + var offset = 0.0 + (points zip normals).zipWithNext().forEach { (left, right) -> - val width = right.first.distanceTo(left.first) + val width = right.first.distanceTo(left.first) - val frontRight = (right.first * frontScale).xy0 + base - val frontLeft = (left.first * frontScale).xy0 + base + val frontRight = (right.first * frontScale).xy0 + base + val frontLeft = (left.first * frontScale).xy0 + base - val backRight = (right.first * backScale).xy0 + base + forward - val backLeft = (left.first * backScale).xy0 + base + forward + val backRight = (right.first * backScale).xy0 + base + forward + val backLeft = (left.first * backScale).xy0 + base + forward - val height = frontRight.distanceTo(backRight) + val height = frontRight.distanceTo(backRight) - val backRightUV = Vector2(offset + width, 0.0) - val backLeftUV = Vector2(offset, 0.0) + val backRightUV = Vector2(offset + width, 0.0) + val backLeftUV = Vector2(offset, 0.0) - val frontLeftUV = Vector2(offset, height) - val frontRightUV = Vector2(offset + width, height) + val frontLeftUV = Vector2(offset, height) + val frontRightUV = Vector2(offset + width, height) - val lnormal = (frontLeft - backLeft).normalized.cross(left.second.xy0) - val rnormal = (frontRight - backRight).normalized.cross(right.second.xy0) + val lnormal = (frontLeft - backLeft).normalized.cross(left.second.xy0) + val rnormal = (frontRight - backRight).normalized.cross(right.second.xy0) - writer(frontLeft, lnormal, frontLeftUV) - writer(frontRight, rnormal, frontRightUV) - writer(backRight, rnormal, backRightUV) + writer(frontLeft, lnormal, frontLeftUV) + writer(frontRight, rnormal, frontRightUV) + writer(backRight, rnormal, backRightUV) - writer(backRight, rnormal, backRightUV) - writer(backLeft, lnormal, backLeftUV) - writer(frontLeft, lnormal, frontLeftUV) + writer(backRight, rnormal, backRightUV) + writer(backLeft, lnormal, backLeftUV) + writer(frontLeft, lnormal, frontLeftUV) - offset += width + offset += width + } } } } @@ -133,12 +136,13 @@ fun extrudeShape(shape: Shape, backScale: Double = 1.0, frontCap: Boolean = true, backCap: Boolean = true, + sides: Boolean = true, distanceTolerance: Double = 0.5, flipNormals: Boolean = false, writer: VertexWriter) { val baseTriangles = triangulate(shape, distanceTolerance) val points = shape.contours.map { it.adaptivePositions(distanceTolerance) } - extrudeShape(baseTriangles, points, front, back, frontScale, backScale, frontCap, backCap, distanceTolerance, + extrudeShape(baseTriangles, points, front, back, frontScale, backScale, sides, frontCap, backCap, distanceTolerance, flipNormals, writer) } @@ -149,10 +153,11 @@ fun extrudeShapes(shapes: List, backScale: Double = 1.0, frontCap: Boolean = true, backCap: Boolean = true, + sides: Boolean = true, distanceTolerance: Double = 0.5, flipNormals: Boolean = false, writer: VertexWriter) { shapes.forEach { - extrudeShape(it, front, back, frontScale, backScale, frontCap, backCap, distanceTolerance, flipNormals, writer) + extrudeShape(it, front, back, frontScale, backScale, frontCap, backCap, sides, distanceTolerance, flipNormals, writer) } } @@ -164,15 +169,3 @@ private val Vector2.safeNormalized: Vector2 Vector2.ZERO } } - -/** - * @suppress - */ -private fun sample() { - val shape = Circle(100.0, 100.0, 200.0).shape - val vbo = meshVertexBuffer(400) - - val vertexCount = vbo.put { - extrudeShape(shape, 0.0, 10.0, 0.05, bufferWriter(this)) - } -} \ No newline at end of file