Skip to content

[DRAFT] Bug fix in Gizmo grid #19697

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 26 additions & 7 deletions crates/bevy_gizmos/src/grid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -388,9 +388,27 @@ fn draw_grid<Config, Clear>(
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);
Expand All @@ -415,11 +433,12 @@ fn draw_grid<Config, Clear>(
}

// 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)
Expand Down