From 2d2d463487efd4a4c5423fd8468a1dd936313f9c Mon Sep 17 00:00:00 2001 From: Phil Ellison Date: Tue, 17 Jun 2025 19:56:58 +0100 Subject: [PATCH 1/8] Bug fix in Gizmo grid --- crates/bevy_gizmos/src/grid.rs | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/crates/bevy_gizmos/src/grid.rs b/crates/bevy_gizmos/src/grid.rs index cdcfc41236..3ab0cd9c82 100644 --- a/crates/bevy_gizmos/src/grid.rs +++ b/crates/bevy_gizmos/src/grid.rs @@ -388,9 +388,18 @@ fn draw_grid( let cell_count_half = cell_count.as_vec3() * 0.5; let grid_start = -cell_count_half.x * dx - cell_count_half.y * dy - cell_count_half.z * dz; - let outer_edges_u32 = UVec3::from(outer_edges.map(|v| v as u32)); - let line_count = outer_edges_u32 * cell_count.saturating_add(UVec3::ONE) - + (UVec3::ONE - outer_edges_u32) * cell_count.saturating_sub(UVec3::ONE); + #[inline] + fn adj(cond: bool, val: u32) -> u32 { + if cond { + val.saturating_add(1) + } else { + val.saturating_sub(1).max(1) + } + } + + let x_line_count = UVec2::new(adj(outer_edges[0], cell_count.y), adj(outer_edges[0], cell_count.z)); + let y_line_count = UVec2::new(adj(outer_edges[1], cell_count.z), adj(outer_edges[1], cell_count.x)); + let z_line_count = UVec2::new(adj(outer_edges[2], cell_count.x), adj(outer_edges[2], cell_count.y)); let x_start = grid_start + or_zero(!outer_edges[0], dy + dz); let y_start = grid_start + or_zero(!outer_edges[1], dx + dz); @@ -415,11 +424,12 @@ fn draw_grid( } // Lines along the x direction - let x_lines = iter_lines(dx, dy, dz, line_count.yz(), cell_count.x, x_start); + let x_lines = iter_lines(dx, dy, dz, x_line_count, cell_count.x, x_start); // Lines along the y direction - let y_lines = iter_lines(dy, dz, dx, line_count.zx(), cell_count.y, y_start); + let y_lines = iter_lines(dy, dz, dx, y_line_count, cell_count.y, y_start); // Lines along the z direction - let z_lines = iter_lines(dz, dx, dy, line_count.xy(), cell_count.z, z_start); + let z_lines = iter_lines(dz, dx, dy, z_line_count, cell_count.z, z_start); + x_lines .chain(y_lines) .chain(z_lines) From 237f54ad27d32673374ba97b7632a13e2266c053 Mon Sep 17 00:00:00 2001 From: Phil Ellison Date: Tue, 17 Jun 2025 20:02:33 +0100 Subject: [PATCH 2/8] fmt --- crates/bevy_gizmos/src/grid.rs | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/crates/bevy_gizmos/src/grid.rs b/crates/bevy_gizmos/src/grid.rs index 3ab0cd9c82..bdf5c08188 100644 --- a/crates/bevy_gizmos/src/grid.rs +++ b/crates/bevy_gizmos/src/grid.rs @@ -397,9 +397,18 @@ fn draw_grid( } } - let x_line_count = UVec2::new(adj(outer_edges[0], cell_count.y), adj(outer_edges[0], cell_count.z)); - let y_line_count = UVec2::new(adj(outer_edges[1], cell_count.z), adj(outer_edges[1], cell_count.x)); - let z_line_count = UVec2::new(adj(outer_edges[2], cell_count.x), adj(outer_edges[2], cell_count.y)); + let x_line_count = UVec2::new( + adj(outer_edges[0], cell_count.y), + adj(outer_edges[0], cell_count.z), + ); + let y_line_count = UVec2::new( + adj(outer_edges[1], cell_count.z), + adj(outer_edges[1], cell_count.x), + ); + let z_line_count = UVec2::new( + adj(outer_edges[2], cell_count.x), + adj(outer_edges[2], cell_count.y), + ); let x_start = grid_start + or_zero(!outer_edges[0], dy + dz); let y_start = grid_start + or_zero(!outer_edges[1], dx + dz); From 92f69456e7d5e6d0e836d495d0c911640ee4315a Mon Sep 17 00:00:00 2001 From: Phil Ellison Date: Tue, 17 Jun 2025 20:10:09 +0100 Subject: [PATCH 3/8] Unused --- crates/bevy_gizmos/src/grid.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/bevy_gizmos/src/grid.rs b/crates/bevy_gizmos/src/grid.rs index bdf5c08188..d5b067b031 100644 --- a/crates/bevy_gizmos/src/grid.rs +++ b/crates/bevy_gizmos/src/grid.rs @@ -5,7 +5,7 @@ use crate::{gizmos::GizmoBuffer, prelude::GizmoConfigGroup}; use bevy_color::Color; -use bevy_math::{ops, Isometry2d, Isometry3d, Quat, UVec2, UVec3, Vec2, Vec3, Vec3Swizzles}; +use bevy_math::{ops, Isometry2d, Isometry3d, Quat, UVec2, UVec3, Vec2, Vec3}; /// A builder returned by [`GizmoBuffer::grid_3d`] pub struct GridBuilder3d<'a, Config, Clear> From 2c94b50ee914f001f4e611fd186f4882d35f50a5 Mon Sep 17 00:00:00 2001 From: Phil Ellison Date: Thu, 26 Jun 2025 20:16:29 +0100 Subject: [PATCH 4/8] Clarify doc document, add grids to gizmo testbed --- crates/bevy_gizmos/src/grid.rs | 10 ++++---- examples/testbed/3d.rs | 47 +++++++++++++++++++++++++++++++--- 2 files changed, 49 insertions(+), 8 deletions(-) diff --git a/crates/bevy_gizmos/src/grid.rs b/crates/bevy_gizmos/src/grid.rs index d5b067b031..e67e40cd94 100644 --- a/crates/bevy_gizmos/src/grid.rs +++ b/crates/bevy_gizmos/src/grid.rs @@ -66,19 +66,19 @@ where self } - /// Declare that the outer edges of the grid along the x axis should be drawn. + /// Declare that the outer edges of the grid parallel to the x axis should be drawn. /// By default, the outer edges will not be drawn. pub fn outer_edges_x(mut self) -> Self { self.outer_edges[0] = true; self } - /// Declare that the outer edges of the grid along the y axis should be drawn. + /// Declare that the outer edges of the grid paralllel to the y axis should be drawn. /// By default, the outer edges will not be drawn. pub fn outer_edges_y(mut self) -> Self { self.outer_edges[1] = true; self } - /// Declare that the outer edges of the grid along the z axis should be drawn. + /// Declare that the outer edges of the grid parallel to the z axis should be drawn. /// By default, the outer edges will not be drawn. pub fn outer_edges_z(mut self) -> Self { self.outer_edges[2] = true; @@ -116,13 +116,13 @@ where self } - /// Declare that the outer edges of the grid along the x axis should be drawn. + /// Declare that the outer edges of the grid parallel to the x axis should be drawn. /// By default, the outer edges will not be drawn. pub fn outer_edges_x(mut self) -> Self { self.outer_edges[0] = true; self } - /// Declare that the outer edges of the grid along the y axis should be drawn. + /// Declare that the outer edges of the grid parallel to the y axis should be drawn. /// By default, the outer edges will not be drawn. pub fn outer_edges_y(mut self) -> Self { self.outer_edges[1] = true; diff --git a/examples/testbed/3d.rs b/examples/testbed/3d.rs index 8770cd2f6b..cfabe14aee 100644 --- a/examples/testbed/3d.rs +++ b/examples/testbed/3d.rs @@ -304,18 +304,59 @@ mod gizmos { pub fn setup(mut commands: Commands) { commands.spawn(( Camera3d::default(), - Transform::from_xyz(-2.0, 2.5, 5.0).looking_at(Vec3::ZERO, Vec3::Y), + Transform::from_xyz(-1.0, 2.5, 6.5).looking_at(Vec3::ZERO, Vec3::Y), DespawnOnExitState(super::Scene::Gizmos), )); } pub fn draw_gizmos(mut gizmos: Gizmos) { gizmos.cuboid( - Transform::from_translation(Vec3::X * 2.0).with_scale(Vec3::splat(2.0)), + Transform::from_translation(Vec3::X * -1.75).with_scale(Vec3::splat(1.25)), RED, ); gizmos - .sphere(Isometry3d::from_translation(Vec3::X * -2.0), 1.0, GREEN) + .sphere(Isometry3d::from_translation(Vec3::X * -3.5), 0.75, GREEN) .resolution(30_000 / 3); + + fn grid_position(row: usize, col: usize) -> Vec3 { + Vec3::new(1.5 * col as f32, (1.0 - row as f32) * 1.25, 0.) + } + + // Display 2d and 3d grids with all variations of outer edges on or off + for i in 0..4 { + let mut grid = gizmos.grid( + grid_position(0, i), + UVec2::new(5, 4), + Vec2::splat(0.175), + Color::WHITE, + ); + if i & 1 > 0 { + grid = grid.outer_edges_x(); + } + if i & 2 > 0 { + grid.outer_edges_y(); + } + } + + for i in 0..8 { + let mut grid = gizmos.grid_3d( + Isometry3d::new( + grid_position(1 + i / 4, i % 4), + Quat::IDENTITY, + ), + UVec3::new(5, 4, 3), + Vec3::splat(0.175), + Color::WHITE, + ); + if i & 1 > 0 { + grid = grid.outer_edges_x(); + } + if i & 2 > 0 { + grid = grid.outer_edges_y(); + } + if i & 4 > 0 { + grid.outer_edges_z(); + } + } } } From e936da5e602dd9534123c37566ba885cb8b3af63 Mon Sep 17 00:00:00 2001 From: Phil Ellison Date: Thu, 26 Jun 2025 20:20:54 +0100 Subject: [PATCH 5/8] Oops --- crates/bevy_gizmos/src/grid.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/bevy_gizmos/src/grid.rs b/crates/bevy_gizmos/src/grid.rs index e67e40cd94..d63d7aed3a 100644 --- a/crates/bevy_gizmos/src/grid.rs +++ b/crates/bevy_gizmos/src/grid.rs @@ -72,7 +72,7 @@ where self.outer_edges[0] = true; self } - /// Declare that the outer edges of the grid paralllel to the y axis should be drawn. + /// Declare that the outer edges of the grid parallel to the y axis should be drawn. /// By default, the outer edges will not be drawn. pub fn outer_edges_y(mut self) -> Self { self.outer_edges[1] = true; From 22774257be462e4c702c35528185a3d055f5562b Mon Sep 17 00:00:00 2001 From: Phil Ellison Date: Thu, 26 Jun 2025 20:29:05 +0100 Subject: [PATCH 6/8] fmt --- examples/testbed/3d.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/examples/testbed/3d.rs b/examples/testbed/3d.rs index cfabe14aee..8d02a4a317 100644 --- a/examples/testbed/3d.rs +++ b/examples/testbed/3d.rs @@ -340,10 +340,7 @@ mod gizmos { for i in 0..8 { let mut grid = gizmos.grid_3d( - Isometry3d::new( - grid_position(1 + i / 4, i % 4), - Quat::IDENTITY, - ), + Isometry3d::new(grid_position(1 + i / 4, i % 4), Quat::IDENTITY), UVec3::new(5, 4, 3), Vec3::splat(0.175), Color::WHITE, From ace451e318126329768e80f9c0298ec2a35409e5 Mon Sep 17 00:00:00 2001 From: Phil Ellison Date: Thu, 26 Jun 2025 20:37:29 +0100 Subject: [PATCH 7/8] Clearer names --- crates/bevy_gizmos/src/grid.rs | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/crates/bevy_gizmos/src/grid.rs b/crates/bevy_gizmos/src/grid.rs index 20144a1283..453c98845b 100644 --- a/crates/bevy_gizmos/src/grid.rs +++ b/crates/bevy_gizmos/src/grid.rs @@ -390,25 +390,25 @@ fn draw_grid( let grid_start = -cell_count_half.x * dx - cell_count_half.y * dy - cell_count_half.z * dz; #[inline] - fn adj(cond: bool, val: u32) -> u32 { - if cond { - val.saturating_add(1) + fn cell_count_to_line_count(include_outer: bool, cell_count: u32) -> u32 { + if include_outer { + cell_count.saturating_add(1) } else { - val.saturating_sub(1).max(1) + cell_count.saturating_sub(1).max(1) } } let x_line_count = UVec2::new( - adj(outer_edges[0], cell_count.y), - adj(outer_edges[0], cell_count.z), + cell_count_to_line_count(outer_edges[0], cell_count.y), + cell_count_to_line_count(outer_edges[0], cell_count.z), ); let y_line_count = UVec2::new( - adj(outer_edges[1], cell_count.z), - adj(outer_edges[1], cell_count.x), + cell_count_to_line_count(outer_edges[1], cell_count.z), + cell_count_to_line_count(outer_edges[1], cell_count.x), ); let z_line_count = UVec2::new( - adj(outer_edges[2], cell_count.x), - adj(outer_edges[2], cell_count.y), + cell_count_to_line_count(outer_edges[2], cell_count.x), + cell_count_to_line_count(outer_edges[2], cell_count.y), ); let x_start = grid_start + or_zero(!outer_edges[0], dy + dz); From 974f3d9a3d3bd40f27477518b4b08c1c62c98aab Mon Sep 17 00:00:00 2001 From: Phil Ellison Date: Tue, 1 Jul 2025 07:40:00 +0100 Subject: [PATCH 8/8] Move 2d grid into 2d.rs --- examples/testbed/2d.rs | 30 ++++++++++++++++++++++++++++-- examples/testbed/3d.rs | 25 ++++--------------------- 2 files changed, 32 insertions(+), 23 deletions(-) diff --git a/examples/testbed/2d.rs b/examples/testbed/2d.rs index 4d53daf507..4084b921de 100644 --- a/examples/testbed/2d.rs +++ b/examples/testbed/2d.rs @@ -288,9 +288,35 @@ mod gizmos { } pub fn draw_gizmos(mut gizmos: Gizmos) { - gizmos.rect_2d(Isometry2d::IDENTITY, Vec2::new(200.0, 200.0), RED); + gizmos.rect_2d( + Isometry2d::from_translation(Vec2::new(-200.0, 0.0)), + Vec2::new(200.0, 200.0), + RED, + ); gizmos - .circle_2d(Isometry2d::IDENTITY, 200.0, GREEN) + .circle_2d( + Isometry2d::from_translation(Vec2::new(-200.0, 0.0)), + 200.0, + GREEN, + ) .resolution(64); + + // 2d grids with all variations of outer edges on or off + for i in 0..4 { + let x = 200.0 * (1.0 + (i % 2) as f32); + let y = 150.0 * (0.5 - (i / 2) as f32); + let mut grid = gizmos.grid( + Vec3::new(x, y, 0.0), + UVec2::new(5, 4), + Vec2::splat(30.), + Color::WHITE, + ); + if i & 1 > 0 { + grid = grid.outer_edges_x(); + } + if i & 2 > 0 { + grid.outer_edges_y(); + } + } } } diff --git a/examples/testbed/3d.rs b/examples/testbed/3d.rs index 8d02a4a317..0a897b85b0 100644 --- a/examples/testbed/3d.rs +++ b/examples/testbed/3d.rs @@ -318,29 +318,12 @@ mod gizmos { .sphere(Isometry3d::from_translation(Vec3::X * -3.5), 0.75, GREEN) .resolution(30_000 / 3); - fn grid_position(row: usize, col: usize) -> Vec3 { - Vec3::new(1.5 * col as f32, (1.0 - row as f32) * 1.25, 0.) - } - - // Display 2d and 3d grids with all variations of outer edges on or off - for i in 0..4 { - let mut grid = gizmos.grid( - grid_position(0, i), - UVec2::new(5, 4), - Vec2::splat(0.175), - Color::WHITE, - ); - if i & 1 > 0 { - grid = grid.outer_edges_x(); - } - if i & 2 > 0 { - grid.outer_edges_y(); - } - } - + // 3d grids with all variations of outer edges on or off for i in 0..8 { + let x = 1.5 * (i % 4) as f32; + let y = 1.0 * (0.5 - (i / 4) as f32); let mut grid = gizmos.grid_3d( - Isometry3d::new(grid_position(1 + i / 4, i % 4), Quat::IDENTITY), + Isometry3d::from_translation(Vec3::new(x, y, 0.0)), UVec3::new(5, 4, 3), Vec3::splat(0.175), Color::WHITE,