From a5b226f2d4a35b66dffab730a6af184d2fb80b64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lynn=20B=C3=BCttgenbach?= <62256001+solis-lumine-vorago@users.noreply.github.com> Date: Sun, 18 Feb 2024 04:56:34 +0100 Subject: [PATCH 01/13] Add rounded rect and cuboid gizmos --- crates/bevy_gizmos/src/lib.rs | 1 + crates/bevy_gizmos/src/rounded_box.rs | 359 ++++++++++++++++++++++++++ 2 files changed, 360 insertions(+) create mode 100644 crates/bevy_gizmos/src/rounded_box.rs diff --git a/crates/bevy_gizmos/src/lib.rs b/crates/bevy_gizmos/src/lib.rs index fe6e16553aa0d..d3b2c14a67be2 100644 --- a/crates/bevy_gizmos/src/lib.rs +++ b/crates/bevy_gizmos/src/lib.rs @@ -31,6 +31,7 @@ pub mod circles; pub mod config; pub mod gizmos; pub mod primitives; +pub mod rounded_box; #[cfg(feature = "bevy_sprite")] mod pipeline_2d; diff --git a/crates/bevy_gizmos/src/rounded_box.rs b/crates/bevy_gizmos/src/rounded_box.rs new file mode 100644 index 0000000000000..aae332c27a4b6 --- /dev/null +++ b/crates/bevy_gizmos/src/rounded_box.rs @@ -0,0 +1,359 @@ +//! Additional [`Gizmos`] Functions -- Rounded cuboids and rectangles +//! +//! Includes the implementation of [`Gizmos::rounded_cuboid`] and [`Gizmos::rounded_c`], +//! and assorted support items. + +use std::f32::consts::FRAC_PI_2; + +use crate::prelude::{GizmoConfigGroup, Gizmos}; +use bevy_math::{Quat, Vec2, Vec3}; +use bevy_render::color::Color; +use bevy_transform::components::Transform; + +/// A builder returned by [`Gizmos::rounded_rect`] and [`Gizmos::rounded_rect_2d`] +pub struct RoundedRectBuilder<'a, 'w, 's, T: GizmoConfigGroup> { + size: Vec2, + gizmos: &'a mut Gizmos<'w, 's, T>, + config: RoundedBoxConfig, +} +/// A builder returned by [`Gizmos::rounded_cuboid`] +pub struct RoundedCuboidBuilder<'a, 'w, 's, T: GizmoConfigGroup> { + size: Vec3, + gizmos: &'a mut Gizmos<'w, 's, T>, + config: RoundedBoxConfig, +} +struct RoundedBoxConfig { + position: Vec3, + rotation: Quat, + color: Color, + corner_radius: f32, + arc_segments: usize, +} + +impl RoundedRectBuilder<'_, '_, '_, T> { + /// Change the radius of the corners to be `corner_radius`. + /// The default corner radius is [min axis of size] / 10.0 + pub fn corner_radius(mut self, corner_radius: f32) -> Self { + self.config.corner_radius = corner_radius; + self + } + + /// Change the segments of the arcs at the corners of the rectangle. + /// The default value is 8 + pub fn arc_segments(mut self, arc_segments: usize) -> Self { + self.config.arc_segments = arc_segments; + self + } +} +impl RoundedCuboidBuilder<'_, '_, '_, T> { + /// Change the radius of the edges to be `edge_radius`. + /// The default edge radius is [min axis of size] / 10.0 + pub fn edge_radius(mut self, edge_radius: f32) -> Self { + self.config.corner_radius = edge_radius; + self + } + + /// Change the segments of the arcs at the edges of the cuboid. + /// The default value is 8 + pub fn arc_segments(mut self, arc_segments: usize) -> Self { + self.config.arc_segments = arc_segments; + self + } +} + +impl Drop for RoundedRectBuilder<'_, '_, '_, T> { + fn drop(&mut self) { + if !self.gizmos.enabled { + return; + } + let config = &self.config; + + // Calculate inner and outer half size and ensure that the ede_radius is <= any half_length + let outer_half_size = self.size.abs() / 2.0; + let inner_half_size = (outer_half_size - Vec2::splat(config.corner_radius)).max(Vec2::ZERO); + let corner_radius = (outer_half_size - inner_half_size).min_element(); + let inner_half_size = outer_half_size - Vec2::splat(corner_radius); + + // Handle cases where the rectangle collapses into simpler shapes + if outer_half_size.x * outer_half_size.y == 0. { + self.gizmos.line( + config.position + config.rotation * -outer_half_size.extend(0.), + config.position + config.rotation * outer_half_size.extend(0.), + config.color, + ); + return; + } + if corner_radius == 0. { + self.gizmos + .rect(config.position, config.rotation, self.size, config.color); + return; + } + + let vertices = [ + // top right + Vec3::new(inner_half_size.x, outer_half_size.y, 0.), + Vec3::new(inner_half_size.x, inner_half_size.y, 0.), + Vec3::new(outer_half_size.x, inner_half_size.y, 0.), + // bottom right + Vec3::new(outer_half_size.x, -inner_half_size.y, 0.), + Vec3::new(inner_half_size.x, -inner_half_size.y, 0.), + Vec3::new(inner_half_size.x, -outer_half_size.y, 0.), + // bottom left + Vec3::new(-inner_half_size.x, -outer_half_size.y, 0.), + Vec3::new(-inner_half_size.x, -inner_half_size.y, 0.), + Vec3::new(-outer_half_size.x, -inner_half_size.y, 0.), + // top left + Vec3::new(-outer_half_size.x, inner_half_size.y, 0.), + Vec3::new(-inner_half_size.x, inner_half_size.y, 0.), + Vec3::new(-inner_half_size.x, outer_half_size.y, 0.), + ] + .map(|v| config.position + config.rotation * v); + + for chunk in vertices.chunks_exact(3) { + self.gizmos + .short_arc_3d_between(chunk[1], chunk[0], chunk[2], config.color) + .segments(config.arc_segments); + } + + let edges = [ + (vertices[2], vertices[3]), + (vertices[5], vertices[6]), + (vertices[8], vertices[9]), + (vertices[11], vertices[0]), + ]; + for (start, end) in edges { + self.gizmos.line(start, end, config.color); + } + } +} + +impl Drop for RoundedCuboidBuilder<'_, '_, '_, T> { + fn drop(&mut self) { + if !self.gizmos.enabled { + return; + } + let config = &self.config; + + // Calculate inner and outer half size and ensure that the ede_radius is <= any half_length + let outer_half_size = self.size.abs() / 2.0; + let inner_half_size = (outer_half_size - Vec3::splat(config.corner_radius)).max(Vec3::ZERO); + let edge_radius = (outer_half_size - inner_half_size).min_element(); + let inner_half_size = outer_half_size - Vec3::splat(edge_radius); + + // Handle cases where the rounded cuboid collapses into simpler shapes + if edge_radius == 0. { + let transform = Transform::from_translation(config.position) + .with_rotation(config.rotation) + .with_scale(self.size); + self.gizmos.cuboid(transform, config.color); + return; + } + + let rects = [ + ( + Vec3::X, + Vec2::new(self.size.z, self.size.y), + Quat::from_rotation_y(FRAC_PI_2), + ), + ( + Vec3::Y, + Vec2::new(self.size.x, self.size.z), + Quat::from_rotation_x(FRAC_PI_2), + ), + (Vec3::Z, Vec2::new(self.size.x, self.size.y), Quat::IDENTITY), + ]; + + for (position, size, rotation) in rects { + let world_rotation = config.rotation * rotation; + let local_position = config.rotation * (position * inner_half_size); + self.gizmos + .rounded_rect( + config.position + local_position, + world_rotation, + size, + config.color, + ) + .arc_segments(config.arc_segments) + .corner_radius(edge_radius); + + self.gizmos + .rounded_rect( + config.position - local_position, + world_rotation, + size, + config.color, + ) + .arc_segments(config.arc_segments) + .corner_radius(edge_radius); + } + } +} + +impl<'w, 's, T: GizmoConfigGroup> Gizmos<'w, 's, T> { + /// Draw a wireframe rectangle with rounded corners in 3D. + /// + /// This should be called for each frame the rectangle needs to be rendered. + /// + /// # Arguments + /// + /// - `position`: The center point of the rectangle. + /// - `rotation`: defines orientation of the rectangle, by default we assume the rectangle is contained in a plane parallel to the XY plane. + /// - `size`: defines the size of the rectangle. This refers to the 'outer size', similar to a bounding box. + /// - `color`: color of the rectangle + /// + /// # Builder methods + /// + /// - The corner radius can be adjusted with the `.corner_radius(...)` method. + /// - The number of segments of the arcs at each corner (i.e. the level of detail) can be adjusted with the + /// `.arc_segments(...)` method. + /// + /// # Example + /// ``` + /// # use bevy_gizmos::prelude::*; + /// # use bevy_render::prelude::*; + /// # use bevy_math::prelude::*; + /// fn system(mut gizmos: Gizmos) { + /// gizmos.rounded_rect( + /// Vec3::ZERO, + /// Quat::IDENTITY, + /// Vec2::ONE, + /// Color::GREEN + /// ) + /// .corner_radius(0.25) + /// .arc_segments(10); + /// } + /// # bevy_ecs::system::assert_is_system(system); + /// ``` + pub fn rounded_rect( + &mut self, + position: Vec3, + rotation: Quat, + size: Vec2, + color: Color, + ) -> RoundedRectBuilder<'_, 'w, 's, T> { + let corner_radius = size.min_element() / 10.0; + RoundedRectBuilder { + gizmos: self, + config: RoundedBoxConfig { + position, + rotation, + color, + corner_radius, + arc_segments: DEFAULT_ARC_SEGMENTS, + }, + size, + } + } + + /// Draw a wireframe rectangle with rounded corners in 2D. + /// + /// This should be called for each frame the rectangle needs to be rendered. + /// + /// # Arguments + /// + /// - `position`: The center point of the rectangle. + /// - `rotation`: defines orientation of the rectangle. + /// - `size`: defines the size of the rectangle. This refers to the 'outer size', similar to a bounding box. + /// - `color`: color of the rectangle + /// + /// # Builder methods + /// + /// - The corner radius can be adjusted with the `.corner_radius(...)` method. + /// - The number of segments of the arcs at each corner (i.e. the level of detail) can be adjusted with the + /// `.arc_segments(...)` method. + /// + /// # Example + /// ``` + /// # use bevy_gizmos::prelude::*; + /// # use bevy_render::prelude::*; + /// # use bevy_math::prelude::*; + /// fn system(mut gizmos: Gizmos) { + /// gizmos.rounded_rect_2d( + /// Vec2::ZERO, + /// 0., + /// Vec2::ONE, + /// Color::GREEN + /// ) + /// .corner_radius(0.25) + /// .arc_segments(10); + /// } + /// # bevy_ecs::system::assert_is_system(system); + /// ``` + pub fn rounded_rect_2d( + &mut self, + position: Vec2, + rotation: f32, + size: Vec2, + color: Color, + ) -> RoundedRectBuilder<'_, 'w, 's, T> { + let corner_radius = size.min_element() / 10.0; + RoundedRectBuilder { + gizmos: self, + config: RoundedBoxConfig { + position: position.extend(0.), + rotation: Quat::from_rotation_z(rotation), + color, + corner_radius, + arc_segments: DEFAULT_ARC_SEGMENTS, + }, + size, + } + } + + /// Draw a wireframe cuboid with rounded corners in 3D. + /// + /// This should be called for each frame the cuboid needs to be rendered. + /// + /// # Arguments + /// + /// - `position`: The center point of the cuboid. + /// - `rotation`: defines orientation of the cuboid. + /// - `size`: defines the size of the cuboid. This refers to the 'outer size', similar to a bounding box. + /// - `color`: color of the cuboid + /// + /// # Builder methods + /// + /// - The edge radius can be adjusted with the `.edge_radius(...)` method. + /// - The number of segments of the arcs at each edge (i.e. the level of detail) can be adjusted with the + /// `.arc_segments(...)` method. + /// + /// # Example + /// ``` + /// # use bevy_gizmos::prelude::*; + /// # use bevy_render::prelude::*; + /// # use bevy_math::prelude::*; + /// fn system(mut gizmos: Gizmos) { + /// gizmos.rounded_cuboid( + /// Vec3::ZERO, + /// Quat::IDENTITY, + /// Vec3::ONE, + /// Color::GREEN + /// ) + /// .edge_radius(0.25) + /// .arc_segments(10); + /// } + /// # bevy_ecs::system::assert_is_system(system); + /// ``` + pub fn rounded_cuboid( + &mut self, + position: Vec3, + rotation: Quat, + size: Vec3, + color: Color, + ) -> RoundedCuboidBuilder<'_, 'w, 's, T> { + let corner_radius = size.min_element() / 10.0; + RoundedCuboidBuilder { + gizmos: self, + config: RoundedBoxConfig { + position, + rotation, + color, + corner_radius, + arc_segments: DEFAULT_ARC_SEGMENTS, + }, + size, + } + } +} + +const DEFAULT_ARC_SEGMENTS: usize = 8; From 7fd9cba6df74a3360a074542b2023a7fc2ddb1c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lynn=20B=C3=BCttgenbach?= <62256001+solis-lumine-vorago@users.noreply.github.com> Date: Sun, 18 Feb 2024 15:38:52 +0100 Subject: [PATCH 02/13] Add rounded cuboid to 3d_gizmos example --- examples/3d/3d_gizmos.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/examples/3d/3d_gizmos.rs b/examples/3d/3d_gizmos.rs index 543db056c43ed..25206bfa274ae 100644 --- a/examples/3d/3d_gizmos.rs +++ b/examples/3d/3d_gizmos.rs @@ -98,6 +98,15 @@ fn draw_example_collection( ); my_gizmos.sphere(Vec3::new(1., 0.5, 0.), Quat::IDENTITY, 0.5, Color::RED); + my_gizmos + .rounded_cuboid( + Vec3::new(-2.0, 0.75, -0.75), + Quat::IDENTITY, + Vec3::splat(0.9), + Color::TURQUOISE, + ) + .edge_radius(0.1) + .arc_segments(4); for y in [0., 0.5, 1.] { gizmos.ray( From 515cf19fb69fc64d253f44326118524b7bd8e096 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lynn=20B=C3=BCttgenbach?= <62256001+solis-lumine-vorago@users.noreply.github.com> Date: Sun, 18 Feb 2024 16:18:52 +0100 Subject: [PATCH 03/13] Allow negative corner and edge radius --- crates/bevy_gizmos/src/rounded_box.rs | 39 +++++++++++++++++++-------- 1 file changed, 28 insertions(+), 11 deletions(-) diff --git a/crates/bevy_gizmos/src/rounded_box.rs b/crates/bevy_gizmos/src/rounded_box.rs index aae332c27a4b6..3ef5ae60229f6 100644 --- a/crates/bevy_gizmos/src/rounded_box.rs +++ b/crates/bevy_gizmos/src/rounded_box.rs @@ -69,10 +69,15 @@ impl Drop for RoundedRectBuilder<'_, '_, '_, T> { let config = &self.config; // Calculate inner and outer half size and ensure that the ede_radius is <= any half_length - let outer_half_size = self.size.abs() / 2.0; - let inner_half_size = (outer_half_size - Vec2::splat(config.corner_radius)).max(Vec2::ZERO); + let mut outer_half_size = self.size.abs() / 2.0; + let inner_half_size = + (outer_half_size - Vec2::splat(config.corner_radius.abs())).max(Vec2::ZERO); let corner_radius = (outer_half_size - inner_half_size).min_element(); - let inner_half_size = outer_half_size - Vec2::splat(corner_radius); + let mut inner_half_size = outer_half_size - Vec2::splat(corner_radius); + + if config.corner_radius < 0. { + std::mem::swap(&mut outer_half_size, &mut inner_half_size); + } // Handle cases where the rectangle collapses into simpler shapes if outer_half_size.x * outer_half_size.y == 0. { @@ -115,12 +120,22 @@ impl Drop for RoundedRectBuilder<'_, '_, '_, T> { .segments(config.arc_segments); } - let edges = [ - (vertices[2], vertices[3]), - (vertices[5], vertices[6]), - (vertices[8], vertices[9]), - (vertices[11], vertices[0]), - ]; + let edges = if config.corner_radius > 0. { + [ + (vertices[2], vertices[3]), + (vertices[5], vertices[6]), + (vertices[8], vertices[9]), + (vertices[11], vertices[0]), + ] + } else { + [ + (vertices[0], vertices[5]), + (vertices[3], vertices[8]), + (vertices[6], vertices[11]), + (vertices[9], vertices[2]), + ] + }; + for (start, end) in edges { self.gizmos.line(start, end, config.color); } @@ -136,9 +151,11 @@ impl Drop for RoundedCuboidBuilder<'_, '_, '_, T> { // Calculate inner and outer half size and ensure that the ede_radius is <= any half_length let outer_half_size = self.size.abs() / 2.0; - let inner_half_size = (outer_half_size - Vec3::splat(config.corner_radius)).max(Vec3::ZERO); - let edge_radius = (outer_half_size - inner_half_size).min_element(); + let inner_half_size = + (outer_half_size - Vec3::splat(config.corner_radius.abs())).max(Vec3::ZERO); + let mut edge_radius = (outer_half_size - inner_half_size).min_element(); let inner_half_size = outer_half_size - Vec3::splat(edge_radius); + edge_radius *= config.corner_radius.signum(); // Handle cases where the rounded cuboid collapses into simpler shapes if edge_radius == 0. { From 81b07bd4c35a2b41d36707873101c7c7bb95e6ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lynn=20B=C3=BCttgenbach?= <62256001+solis-lumine-vorago@users.noreply.github.com> Date: Sun, 18 Feb 2024 16:19:05 +0100 Subject: [PATCH 04/13] Add 2d example --- examples/2d/2d_gizmos.rs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/examples/2d/2d_gizmos.rs b/examples/2d/2d_gizmos.rs index fb414201edcf4..79758658b69cc 100644 --- a/examples/2d/2d_gizmos.rs +++ b/examples/2d/2d_gizmos.rs @@ -52,7 +52,7 @@ fn draw_example_collection( gizmos.rect_2d( Vec2::ZERO, time.elapsed_seconds() / 3., - Vec2::splat(300.), + Vec2::splat(400.), Color::BLACK, ); @@ -73,6 +73,15 @@ fn draw_example_collection( // 1 and 32, using the arc length as scalar. my_gizmos.arc_2d(Vec2::ZERO, sin / 10., PI / 2., 350., Color::ORANGE_RED); + my_gizmos + .rounded_rect_2d( + Vec2::ZERO, + time.elapsed_seconds() / -3., + Vec2::splat(300.), + Color::BLACK, + ) + .corner_radius((time.elapsed_seconds() / 3.).cos() * 100.); + gizmos.arrow_2d( Vec2::ZERO, Vec2::from_angle(sin / -10. + PI / 2.) * 50., From 3c82063c99feca466fbf8a9516538280ff373708 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lynn=20B=C3=BCttgenbach?= <62256001+solis-lumine-vorago@users.noreply.github.com> Date: Sun, 18 Feb 2024 17:01:21 +0100 Subject: [PATCH 05/13] Fix module docs --- crates/bevy_gizmos/src/rounded_box.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/bevy_gizmos/src/rounded_box.rs b/crates/bevy_gizmos/src/rounded_box.rs index 3ef5ae60229f6..a471519f5b2dc 100644 --- a/crates/bevy_gizmos/src/rounded_box.rs +++ b/crates/bevy_gizmos/src/rounded_box.rs @@ -1,6 +1,6 @@ //! Additional [`Gizmos`] Functions -- Rounded cuboids and rectangles //! -//! Includes the implementation of [`Gizmos::rounded_cuboid`] and [`Gizmos::rounded_c`], +//! Includes the implementation of[`Gizmos::rounded_rect`], [`Gizmos::rounded_rect_2d`] and [`Gizmos::rounded_cuboid`]. //! and assorted support items. use std::f32::consts::FRAC_PI_2; From aedb0b1a0c04d448dea7679e8e7b6485aab29c3c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lynn=20B=C3=BCttgenbach?= <62256001+solis-lumine-vorago@users.noreply.github.com> Date: Wed, 28 Feb 2024 01:38:31 +0100 Subject: [PATCH 06/13] Update `Color` to be `LegacyColor` and move example --- crates/bevy_gizmos/src/rounded_box.rs | 16 ++++++++-------- examples/gizmos/2d_gizmos.rs | 11 ++++++++++- examples/gizmos/3d_gizmos.rs | 10 ++++++++++ 3 files changed, 28 insertions(+), 9 deletions(-) diff --git a/crates/bevy_gizmos/src/rounded_box.rs b/crates/bevy_gizmos/src/rounded_box.rs index a471519f5b2dc..1575560609189 100644 --- a/crates/bevy_gizmos/src/rounded_box.rs +++ b/crates/bevy_gizmos/src/rounded_box.rs @@ -7,7 +7,7 @@ use std::f32::consts::FRAC_PI_2; use crate::prelude::{GizmoConfigGroup, Gizmos}; use bevy_math::{Quat, Vec2, Vec3}; -use bevy_render::color::Color; +use bevy_render::color::LegacyColor; use bevy_transform::components::Transform; /// A builder returned by [`Gizmos::rounded_rect`] and [`Gizmos::rounded_rect_2d`] @@ -25,7 +25,7 @@ pub struct RoundedCuboidBuilder<'a, 'w, 's, T: GizmoConfigGroup> { struct RoundedBoxConfig { position: Vec3, rotation: Quat, - color: Color, + color: LegacyColor, corner_radius: f32, arc_segments: usize, } @@ -234,7 +234,7 @@ impl<'w, 's, T: GizmoConfigGroup> Gizmos<'w, 's, T> { /// Vec3::ZERO, /// Quat::IDENTITY, /// Vec2::ONE, - /// Color::GREEN + /// LegacyColor::GREEN /// ) /// .corner_radius(0.25) /// .arc_segments(10); @@ -246,7 +246,7 @@ impl<'w, 's, T: GizmoConfigGroup> Gizmos<'w, 's, T> { position: Vec3, rotation: Quat, size: Vec2, - color: Color, + color: LegacyColor, ) -> RoundedRectBuilder<'_, 'w, 's, T> { let corner_radius = size.min_element() / 10.0; RoundedRectBuilder { @@ -289,7 +289,7 @@ impl<'w, 's, T: GizmoConfigGroup> Gizmos<'w, 's, T> { /// Vec2::ZERO, /// 0., /// Vec2::ONE, - /// Color::GREEN + /// LegacyColor::GREEN /// ) /// .corner_radius(0.25) /// .arc_segments(10); @@ -301,7 +301,7 @@ impl<'w, 's, T: GizmoConfigGroup> Gizmos<'w, 's, T> { position: Vec2, rotation: f32, size: Vec2, - color: Color, + color: LegacyColor, ) -> RoundedRectBuilder<'_, 'w, 's, T> { let corner_radius = size.min_element() / 10.0; RoundedRectBuilder { @@ -344,7 +344,7 @@ impl<'w, 's, T: GizmoConfigGroup> Gizmos<'w, 's, T> { /// Vec3::ZERO, /// Quat::IDENTITY, /// Vec3::ONE, - /// Color::GREEN + /// LegacyColor::GREEN /// ) /// .edge_radius(0.25) /// .arc_segments(10); @@ -356,7 +356,7 @@ impl<'w, 's, T: GizmoConfigGroup> Gizmos<'w, 's, T> { position: Vec3, rotation: Quat, size: Vec3, - color: Color, + color: LegacyColor, ) -> RoundedCuboidBuilder<'_, 'w, 's, T> { let corner_radius = size.min_element() / 10.0; RoundedCuboidBuilder { diff --git a/examples/gizmos/2d_gizmos.rs b/examples/gizmos/2d_gizmos.rs index 2072ebe01168c..23a36abeeded7 100644 --- a/examples/gizmos/2d_gizmos.rs +++ b/examples/gizmos/2d_gizmos.rs @@ -52,7 +52,7 @@ fn draw_example_collection( gizmos.rect_2d( Vec2::ZERO, time.elapsed_seconds() / 3., - Vec2::splat(300.), + Vec2::splat(400.), LegacyColor::BLACK, ); @@ -79,6 +79,15 @@ fn draw_example_collection( LegacyColor::ORANGE_RED, ); + my_gizmos + .rounded_rect_2d( + Vec2::ZERO, + time.elapsed_seconds() / -3., + Vec2::splat(300.), + LegacyColor::BLACK, + ) + .corner_radius((time.elapsed_seconds() / 3.).cos() * 100.); + gizmos.arrow_2d( Vec2::ZERO, Vec2::from_angle(sin / -10. + PI / 2.) * 50., diff --git a/examples/gizmos/3d_gizmos.rs b/examples/gizmos/3d_gizmos.rs index 4e97e0e8e8358..bd05399b458a5 100644 --- a/examples/gizmos/3d_gizmos.rs +++ b/examples/gizmos/3d_gizmos.rs @@ -104,6 +104,16 @@ fn draw_example_collection( LegacyColor::RED, ); + my_gizmos + .rounded_cuboid( + Vec3::new(-2.0, 0.75, -0.75), + Quat::IDENTITY, + Vec3::splat(0.9), + LegacyColor::TURQUOISE, + ) + .edge_radius(0.1) + .arc_segments(4); + for y in [0., 0.5, 1.] { gizmos.ray( Vec3::new(1., y, 0.), From 88d4ac9d0a88cbce53822ed4240469cda97a5c2a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lynn=20B=C3=BCttgenbach?= <62256001+solis-lumine-vorago@users.noreply.github.com> Date: Wed, 28 Feb 2024 01:43:50 +0100 Subject: [PATCH 07/13] Fix typo --- crates/bevy_gizmos/src/rounded_box.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/bevy_gizmos/src/rounded_box.rs b/crates/bevy_gizmos/src/rounded_box.rs index 1575560609189..74d34acf2cebe 100644 --- a/crates/bevy_gizmos/src/rounded_box.rs +++ b/crates/bevy_gizmos/src/rounded_box.rs @@ -68,7 +68,7 @@ impl Drop for RoundedRectBuilder<'_, '_, '_, T> { } let config = &self.config; - // Calculate inner and outer half size and ensure that the ede_radius is <= any half_length + // Calculate inner and outer half size and ensure that the edge_radius is <= any half_length let mut outer_half_size = self.size.abs() / 2.0; let inner_half_size = (outer_half_size - Vec2::splat(config.corner_radius.abs())).max(Vec2::ZERO); @@ -149,7 +149,7 @@ impl Drop for RoundedCuboidBuilder<'_, '_, '_, T> { } let config = &self.config; - // Calculate inner and outer half size and ensure that the ede_radius is <= any half_length + // Calculate inner and outer half size and ensure that the edge_radius is <= any half_length let outer_half_size = self.size.abs() / 2.0; let inner_half_size = (outer_half_size - Vec3::splat(config.corner_radius.abs())).max(Vec3::ZERO); From 6a5e085c30ca888295f26faa7b70703debc5237a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lynn=20B=C3=BCttgenbach?= <62256001+solis-lumine-vorago@users.noreply.github.com> Date: Sat, 9 Mar 2024 23:51:49 +0100 Subject: [PATCH 08/13] Use new color system --- crates/bevy_gizmos/src/rounded_box.rs | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/crates/bevy_gizmos/src/rounded_box.rs b/crates/bevy_gizmos/src/rounded_box.rs index 74d34acf2cebe..3d9290a8f6967 100644 --- a/crates/bevy_gizmos/src/rounded_box.rs +++ b/crates/bevy_gizmos/src/rounded_box.rs @@ -6,8 +6,8 @@ use std::f32::consts::FRAC_PI_2; use crate::prelude::{GizmoConfigGroup, Gizmos}; +use bevy_color::Color; use bevy_math::{Quat, Vec2, Vec3}; -use bevy_render::color::LegacyColor; use bevy_transform::components::Transform; /// A builder returned by [`Gizmos::rounded_rect`] and [`Gizmos::rounded_rect_2d`] @@ -25,7 +25,7 @@ pub struct RoundedCuboidBuilder<'a, 'w, 's, T: GizmoConfigGroup> { struct RoundedBoxConfig { position: Vec3, rotation: Quat, - color: LegacyColor, + color: Color, corner_radius: f32, arc_segments: usize, } @@ -234,7 +234,7 @@ impl<'w, 's, T: GizmoConfigGroup> Gizmos<'w, 's, T> { /// Vec3::ZERO, /// Quat::IDENTITY, /// Vec2::ONE, - /// LegacyColor::GREEN + /// GREEN /// ) /// .corner_radius(0.25) /// .arc_segments(10); @@ -246,7 +246,7 @@ impl<'w, 's, T: GizmoConfigGroup> Gizmos<'w, 's, T> { position: Vec3, rotation: Quat, size: Vec2, - color: LegacyColor, + color: impl Into, ) -> RoundedRectBuilder<'_, 'w, 's, T> { let corner_radius = size.min_element() / 10.0; RoundedRectBuilder { @@ -254,7 +254,7 @@ impl<'w, 's, T: GizmoConfigGroup> Gizmos<'w, 's, T> { config: RoundedBoxConfig { position, rotation, - color, + color: color.into(), corner_radius, arc_segments: DEFAULT_ARC_SEGMENTS, }, @@ -289,7 +289,7 @@ impl<'w, 's, T: GizmoConfigGroup> Gizmos<'w, 's, T> { /// Vec2::ZERO, /// 0., /// Vec2::ONE, - /// LegacyColor::GREEN + /// GREEN /// ) /// .corner_radius(0.25) /// .arc_segments(10); @@ -301,7 +301,7 @@ impl<'w, 's, T: GizmoConfigGroup> Gizmos<'w, 's, T> { position: Vec2, rotation: f32, size: Vec2, - color: LegacyColor, + color: impl Into, ) -> RoundedRectBuilder<'_, 'w, 's, T> { let corner_radius = size.min_element() / 10.0; RoundedRectBuilder { @@ -309,7 +309,7 @@ impl<'w, 's, T: GizmoConfigGroup> Gizmos<'w, 's, T> { config: RoundedBoxConfig { position: position.extend(0.), rotation: Quat::from_rotation_z(rotation), - color, + color: color.into(), corner_radius, arc_segments: DEFAULT_ARC_SEGMENTS, }, @@ -344,7 +344,7 @@ impl<'w, 's, T: GizmoConfigGroup> Gizmos<'w, 's, T> { /// Vec3::ZERO, /// Quat::IDENTITY, /// Vec3::ONE, - /// LegacyColor::GREEN + /// ColorGREEN /// ) /// .edge_radius(0.25) /// .arc_segments(10); @@ -356,7 +356,7 @@ impl<'w, 's, T: GizmoConfigGroup> Gizmos<'w, 's, T> { position: Vec3, rotation: Quat, size: Vec3, - color: LegacyColor, + color: impl Into, ) -> RoundedCuboidBuilder<'_, 'w, 's, T> { let corner_radius = size.min_element() / 10.0; RoundedCuboidBuilder { @@ -364,7 +364,7 @@ impl<'w, 's, T: GizmoConfigGroup> Gizmos<'w, 's, T> { config: RoundedBoxConfig { position, rotation, - color, + color: color.into(), corner_radius, arc_segments: DEFAULT_ARC_SEGMENTS, }, From 4744f7cfd8476322eff3c5102af6721cf1208662 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lynn=20B=C3=BCttgenbach?= <62256001+solis-lumine-vorago@users.noreply.github.com> Date: Sat, 9 Mar 2024 23:52:00 +0100 Subject: [PATCH 09/13] Update examples --- examples/gizmos/2d_gizmos.rs | 9 +++++++++ examples/gizmos/3d_gizmos.rs | 2 +- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/examples/gizmos/2d_gizmos.rs b/examples/gizmos/2d_gizmos.rs index a4604ed1b46c8..86e192317546b 100644 --- a/examples/gizmos/2d_gizmos.rs +++ b/examples/gizmos/2d_gizmos.rs @@ -67,6 +67,15 @@ fn draw_example_collection( BLACK, ); + my_gizmos + .rounded_rect_2d( + Vec2::ZERO, + time.elapsed_seconds() / -3., + Vec2::splat(300.), + BLACK, + ) + .corner_radius((time.elapsed_seconds() / 3.).cos() * 100.); + // The circles have 32 line-segments by default. my_gizmos.circle_2d(Vec2::ZERO, 120., BLACK); my_gizmos.ellipse_2d( diff --git a/examples/gizmos/3d_gizmos.rs b/examples/gizmos/3d_gizmos.rs index 95ef6026f55b8..de34676f634b7 100644 --- a/examples/gizmos/3d_gizmos.rs +++ b/examples/gizmos/3d_gizmos.rs @@ -113,7 +113,7 @@ fn draw_example_collection( Vec3::new(-2.0, 0.75, -0.75), Quat::IDENTITY, Vec3::splat(0.9), - LegacyColor::TURQUOISE, + TURQUOISE, ) .edge_radius(0.1) .arc_segments(4); From 84fa62563e4b876e6b2a6c62e9c027fdb8442865 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lynn=20B=C3=BCttgenbach?= <62256001+solis-lumine-vorago@users.noreply.github.com> Date: Sat, 9 Mar 2024 23:59:35 +0100 Subject: [PATCH 10/13] Fix doc-tests --- crates/bevy_gizmos/src/rounded_box.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/crates/bevy_gizmos/src/rounded_box.rs b/crates/bevy_gizmos/src/rounded_box.rs index 3d9290a8f6967..3b1592533dcfc 100644 --- a/crates/bevy_gizmos/src/rounded_box.rs +++ b/crates/bevy_gizmos/src/rounded_box.rs @@ -229,6 +229,7 @@ impl<'w, 's, T: GizmoConfigGroup> Gizmos<'w, 's, T> { /// # use bevy_gizmos::prelude::*; /// # use bevy_render::prelude::*; /// # use bevy_math::prelude::*; + /// # use bevy_color::palettes::css::GREEN; /// fn system(mut gizmos: Gizmos) { /// gizmos.rounded_rect( /// Vec3::ZERO, @@ -284,6 +285,7 @@ impl<'w, 's, T: GizmoConfigGroup> Gizmos<'w, 's, T> { /// # use bevy_gizmos::prelude::*; /// # use bevy_render::prelude::*; /// # use bevy_math::prelude::*; + /// # use bevy_color::palettes::css::GREEN; /// fn system(mut gizmos: Gizmos) { /// gizmos.rounded_rect_2d( /// Vec2::ZERO, @@ -339,12 +341,13 @@ impl<'w, 's, T: GizmoConfigGroup> Gizmos<'w, 's, T> { /// # use bevy_gizmos::prelude::*; /// # use bevy_render::prelude::*; /// # use bevy_math::prelude::*; + /// # use bevy_color::palettes::css::GREEN; /// fn system(mut gizmos: Gizmos) { /// gizmos.rounded_cuboid( /// Vec3::ZERO, /// Quat::IDENTITY, /// Vec3::ONE, - /// ColorGREEN + /// GREEN /// ) /// .edge_radius(0.25) /// .arc_segments(10); From 6c3ca39f79f28cd903eb8cb90c6730178051ba62 Mon Sep 17 00:00:00 2001 From: Lynn <62256001+solis-lumine-vorago@users.noreply.github.com> Date: Thu, 25 Apr 2024 09:52:18 +0200 Subject: [PATCH 11/13] Fix typo Co-authored-by: JMS55 <47158642+JMS55@users.noreply.github.com> --- crates/bevy_gizmos/src/rounded_box.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/bevy_gizmos/src/rounded_box.rs b/crates/bevy_gizmos/src/rounded_box.rs index 3b1592533dcfc..7155493bc8d89 100644 --- a/crates/bevy_gizmos/src/rounded_box.rs +++ b/crates/bevy_gizmos/src/rounded_box.rs @@ -1,6 +1,6 @@ //! Additional [`Gizmos`] Functions -- Rounded cuboids and rectangles //! -//! Includes the implementation of[`Gizmos::rounded_rect`], [`Gizmos::rounded_rect_2d`] and [`Gizmos::rounded_cuboid`]. +//! Includes the implementation of [`Gizmos::rounded_rect`], [`Gizmos::rounded_rect_2d`] and [`Gizmos::rounded_cuboid`]. //! and assorted support items. use std::f32::consts::FRAC_PI_2; From 395a0132cca830a7d7817db27cf13ebbd61ddc42 Mon Sep 17 00:00:00 2001 From: Lynn <62256001+solis-lumine-vorago@users.noreply.github.com> Date: Thu, 25 Apr 2024 09:52:39 +0200 Subject: [PATCH 12/13] Add decimal Co-authored-by: JMS55 <47158642+JMS55@users.noreply.github.com> --- crates/bevy_gizmos/src/rounded_box.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/bevy_gizmos/src/rounded_box.rs b/crates/bevy_gizmos/src/rounded_box.rs index 7155493bc8d89..75a2d155d420a 100644 --- a/crates/bevy_gizmos/src/rounded_box.rs +++ b/crates/bevy_gizmos/src/rounded_box.rs @@ -158,7 +158,7 @@ impl Drop for RoundedCuboidBuilder<'_, '_, '_, T> { edge_radius *= config.corner_radius.signum(); // Handle cases where the rounded cuboid collapses into simpler shapes - if edge_radius == 0. { + if edge_radius == 0.0 { let transform = Transform::from_translation(config.position) .with_rotation(config.rotation) .with_scale(self.size); From 0c94f1520cb539eda2469595f2845a3612dc1563 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Raphael=20B=C3=BCttgenbach?= <62256001+solis-lumine-vorago@users.noreply.github.com> Date: Fri, 3 May 2024 11:33:50 +0200 Subject: [PATCH 13/13] Make default corner radius constant Co-Authored-By: James Gayfer <10660608+jgayfer@users.noreply.github.com> --- crates/bevy_gizmos/src/rounded_box.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/crates/bevy_gizmos/src/rounded_box.rs b/crates/bevy_gizmos/src/rounded_box.rs index 75a2d155d420a..cef07d960ca74 100644 --- a/crates/bevy_gizmos/src/rounded_box.rs +++ b/crates/bevy_gizmos/src/rounded_box.rs @@ -249,7 +249,7 @@ impl<'w, 's, T: GizmoConfigGroup> Gizmos<'w, 's, T> { size: Vec2, color: impl Into, ) -> RoundedRectBuilder<'_, 'w, 's, T> { - let corner_radius = size.min_element() / 10.0; + let corner_radius = size.min_element() * DEFAULT_CORNER_RADIUS; RoundedRectBuilder { gizmos: self, config: RoundedBoxConfig { @@ -305,7 +305,7 @@ impl<'w, 's, T: GizmoConfigGroup> Gizmos<'w, 's, T> { size: Vec2, color: impl Into, ) -> RoundedRectBuilder<'_, 'w, 's, T> { - let corner_radius = size.min_element() / 10.0; + let corner_radius = size.min_element() * DEFAULT_CORNER_RADIUS; RoundedRectBuilder { gizmos: self, config: RoundedBoxConfig { @@ -361,7 +361,7 @@ impl<'w, 's, T: GizmoConfigGroup> Gizmos<'w, 's, T> { size: Vec3, color: impl Into, ) -> RoundedCuboidBuilder<'_, 'w, 's, T> { - let corner_radius = size.min_element() / 10.0; + let corner_radius = size.min_element() * DEFAULT_CORNER_RADIUS; RoundedCuboidBuilder { gizmos: self, config: RoundedBoxConfig { @@ -377,3 +377,4 @@ impl<'w, 's, T: GizmoConfigGroup> Gizmos<'w, 's, T> { } const DEFAULT_ARC_SEGMENTS: usize = 8; +const DEFAULT_CORNER_RADIUS: f32 = 0.1;