From d916e77298bbc2a6d0aed21247dfa412a58c9140 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lynn=20B=C3=BCttgenbach?= <62256001+lynn-lumen@users.noreply.github.com> Date: Wed, 9 Jul 2025 16:51:50 +0200 Subject: [PATCH] Add `ConvexPolygon` gizmos --- crates/bevy_gizmos/src/primitives/dim2.rs | 50 ++++++++++++++++++++++- 1 file changed, 48 insertions(+), 2 deletions(-) diff --git a/crates/bevy_gizmos/src/primitives/dim2.rs b/crates/bevy_gizmos/src/primitives/dim2.rs index 9535c28fbd..4adc3edfc1 100644 --- a/crates/bevy_gizmos/src/primitives/dim2.rs +++ b/crates/bevy_gizmos/src/primitives/dim2.rs @@ -8,8 +8,8 @@ use bevy_color::Color; use bevy_math::{ primitives::{ Annulus, Arc2d, BoxedPolygon, BoxedPolyline2d, Capsule2d, Circle, CircularSector, - CircularSegment, Ellipse, Line2d, Plane2d, Polygon, Polyline2d, Primitive2d, Rectangle, - RegularPolygon, Rhombus, Segment2d, Triangle2d, + CircularSegment, ConvexPolygon, Ellipse, Line2d, Plane2d, Polygon, Polyline2d, Primitive2d, + Rectangle, RegularPolygon, Rhombus, Segment2d, Triangle2d, }, Dir2, Isometry2d, Rot2, Vec2, }; @@ -827,6 +827,52 @@ where } } +// convex polygon 2d + +impl GizmoPrimitive2d> + for GizmoBuffer +where + Config: GizmoConfigGroup, + Clear: 'static + Send + Sync, +{ + type Output<'a> + = () + where + Self: 'a; + + fn primitive_2d( + &mut self, + primitive: &ConvexPolygon, + isometry: impl Into, + color: impl Into, + ) -> Self::Output<'_> { + if !self.enabled { + return; + } + + let isometry = isometry.into(); + + // Check if the polygon needs a closing point + let closing_point = { + let first = primitive.vertices().first(); + (primitive.vertices().last() != first) + .then_some(first) + .flatten() + .cloned() + }; + + self.linestrip_2d( + primitive + .vertices() + .iter() + .copied() + .chain(closing_point) + .map(|vec2| isometry * vec2), + color, + ); + } +} + // boxed polygon 2d impl GizmoPrimitive2d for GizmoBuffer