This commit is contained in:
theotherphil 2025-07-16 17:44:25 -04:00 committed by GitHub
commit db912970fe
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 83 additions and 17 deletions

View File

@ -5,7 +5,7 @@
use crate::{gizmos::GizmoBuffer, prelude::GizmoConfigGroup}; use crate::{gizmos::GizmoBuffer, prelude::GizmoConfigGroup};
use bevy_color::Color; 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`] /// A builder returned by [`GizmoBuffer::grid_3d`]
pub struct GridBuilder3d<'a, Config, Clear> pub struct GridBuilder3d<'a, Config, Clear>
@ -66,19 +66,19 @@ where
self 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. /// By default, the outer edges will not be drawn.
pub fn outer_edges_x(mut self) -> Self { pub fn outer_edges_x(mut self) -> Self {
self.outer_edges[0] = true; self.outer_edges[0] = true;
self 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. /// By default, the outer edges will not be drawn.
pub fn outer_edges_y(mut self) -> Self { pub fn outer_edges_y(mut self) -> Self {
self.outer_edges[1] = true; self.outer_edges[1] = true;
self 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. /// By default, the outer edges will not be drawn.
pub fn outer_edges_z(mut self) -> Self { pub fn outer_edges_z(mut self) -> Self {
self.outer_edges[2] = true; self.outer_edges[2] = true;
@ -116,13 +116,13 @@ where
self 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. /// By default, the outer edges will not be drawn.
pub fn outer_edges_x(mut self) -> Self { pub fn outer_edges_x(mut self) -> Self {
self.outer_edges[0] = true; self.outer_edges[0] = true;
self 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. /// By default, the outer edges will not be drawn.
pub fn outer_edges_y(mut self) -> Self { pub fn outer_edges_y(mut self) -> Self {
self.outer_edges[1] = true; self.outer_edges[1] = true;
@ -389,9 +389,27 @@ fn draw_grid<Config, Clear>(
let cell_count_half = cell_count.as_vec3() * 0.5; 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 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)); #[inline]
let line_count = outer_edges_u32 * cell_count.saturating_add(UVec3::ONE) fn cell_count_to_line_count(include_outer: bool, cell_count: u32) -> u32 {
+ (UVec3::ONE - outer_edges_u32) * cell_count.saturating_sub(UVec3::ONE); if include_outer {
cell_count.saturating_add(1)
} else {
cell_count.saturating_sub(1).max(1)
}
}
let x_line_count = UVec2::new(
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(
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(
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); let x_start = grid_start + or_zero(!outer_edges[0], dy + dz);
let y_start = grid_start + or_zero(!outer_edges[1], dx + dz); let y_start = grid_start + or_zero(!outer_edges[1], dx + dz);
@ -416,11 +434,12 @@ fn draw_grid<Config, Clear>(
} }
// Lines along the x direction // 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 // 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 // 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 x_lines
.chain(y_lines) .chain(y_lines)
.chain(z_lines) .chain(z_lines)

View File

@ -291,9 +291,35 @@ mod gizmos {
} }
pub fn draw_gizmos(mut gizmos: 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 gizmos
.circle_2d(Isometry2d::IDENTITY, 200.0, GREEN) .circle_2d(
Isometry2d::from_translation(Vec2::new(-200.0, 0.0)),
200.0,
GREEN,
)
.resolution(64); .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();
}
}
} }
} }

View File

@ -304,18 +304,39 @@ mod gizmos {
pub fn setup(mut commands: Commands) { pub fn setup(mut commands: Commands) {
commands.spawn(( commands.spawn((
Camera3d::default(), 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), DespawnOnExitState(super::Scene::Gizmos),
)); ));
} }
pub fn draw_gizmos(mut gizmos: Gizmos) { pub fn draw_gizmos(mut gizmos: Gizmos) {
gizmos.cuboid( 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, RED,
); );
gizmos 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); .resolution(30_000 / 3);
// 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::from_translation(Vec3::new(x, y, 0.0)),
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();
}
}
} }
} }