diff --git a/crates/bevy_gizmos/src/gizmos.rs b/crates/bevy_gizmos/src/gizmos.rs index 074a9232e6b79..b16ed88f3ebc0 100644 --- a/crates/bevy_gizmos/src/gizmos.rs +++ b/crates/bevy_gizmos/src/gizmos.rs @@ -507,6 +507,41 @@ where self.strip_colors.push(LinearRgba::NAN); } + /// Draw a line in 3D made of straight segments between the points, with the first and last connected. + /// + /// # Example + /// ``` + /// # use bevy_gizmos::prelude::*; + /// # use bevy_math::prelude::*; + /// # use bevy_color::palettes::basic::GREEN; + /// fn system(mut gizmos: Gizmos) { + /// gizmos.lineloop([Vec3::ZERO, Vec3::X, Vec3::Y], GREEN); + /// } + /// # bevy_ecs::system::assert_is_system(system); + /// ``` + #[inline] + pub fn lineloop(&mut self, positions: impl IntoIterator, color: impl Into) { + if !self.enabled { + return; + } + + // Loop back to the start; second is needed to ensure that + // the joint on the first corner is drawn. + let mut positions = positions.into_iter(); + let first = positions.next(); + let second = positions.next(); + + self.linestrip( + first + .into_iter() + .chain(second) + .chain(positions) + .chain(first) + .chain(second), + color, + ); + } + /// Draw a line in 3D made of straight segments between the points, with a color gradient. /// /// # Example @@ -576,7 +611,7 @@ where } let isometry = isometry.into(); let [tl, tr, br, bl] = rect_inner(size).map(|vec2| isometry * vec2.extend(0.)); - self.linestrip([tl, tr, br, bl, tl], color); + self.lineloop([tl, tr, br, bl], color); } /// Draw a wireframe cube in 3D. @@ -759,23 +794,7 @@ where if !self.enabled { return; } - - // Loop back to the start; second is needed to ensure that - // the joint on the first corner is drawn. - let mut positions = positions.into_iter(); - let first = positions.next(); - let second = positions.next(); - - self.linestrip( - first - .into_iter() - .chain(second) - .chain(positions) - .chain(first) - .chain(second) - .map(|vec2| vec2.extend(0.)), - color, - ); + self.lineloop(positions.into_iter().map(|vec2| vec2.extend(0.)), color); } /// Draw a line in 2D made of straight segments between the points, with a color gradient. diff --git a/crates/bevy_gizmos/src/primitives/dim3.rs b/crates/bevy_gizmos/src/primitives/dim3.rs index ca1172316b2c6..1e38201bc4759 100644 --- a/crates/bevy_gizmos/src/primitives/dim3.rs +++ b/crates/bevy_gizmos/src/primitives/dim3.rs @@ -287,7 +287,7 @@ where let isometry = isometry.into(); let [a, b, c] = primitive.vertices; - self.linestrip([a, b, c, a].map(|vec3| isometry * vec3), color); + self.lineloop([a, b, c].map(|vec3| isometry * vec3), color); } }