From 387a7e5020092237b47a948a668a4efeb8ea29a9 Mon Sep 17 00:00:00 2001 From: Abe Pazos Date: Sat, 18 Dec 2021 21:39:59 +0100 Subject: [PATCH] [orx-mesh-generators] Add Rectangle.toMesh() (#216) --- orx-mesh-generators/src/main/kotlin/Plane.kt | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/orx-mesh-generators/src/main/kotlin/Plane.kt b/orx-mesh-generators/src/main/kotlin/Plane.kt index d56fd019..8ad555c6 100644 --- a/orx-mesh-generators/src/main/kotlin/Plane.kt +++ b/orx-mesh-generators/src/main/kotlin/Plane.kt @@ -3,6 +3,7 @@ package org.openrndr.extras.meshgenerators import org.openrndr.draw.VertexBuffer import org.openrndr.math.Vector2 import org.openrndr.math.Vector3 +import org.openrndr.shape.Rectangle fun planeMesh(center: Vector3, right: Vector3, @@ -20,6 +21,18 @@ fun planeMesh(center: Vector3, return vb } +/** + * Converts a [Rectangle] to a [VertexBuffer] 2D mesh matching its location and + * dimensions. [resolution] specifies the size in pixels of the triangles in + * the mesh. + */ +fun Rectangle.toMesh(resolution: Double = 2.0) = planeMesh( + center.xy0, Vector3.UNIT_X, Vector3.UNIT_Y, Vector3.UNIT_Z, + width, height, + (width / resolution).toInt(), + (height / resolution).toInt() +) + /** * generates a finite plane with its center at (0,0,0) and spanning the xz-plane */