//! Additional [`GizmoBuffer`] Functions -- Grids1//!2//! Includes the implementation of [`GizmoBuffer::grid`] and [`GizmoBuffer::grid_2d`].3//! and assorted support items.45use crate::{gizmos::GizmoBuffer, prelude::GizmoConfigGroup};6use bevy_color::Color;7use bevy_math::{ops, Isometry2d, Isometry3d, Quat, UVec2, UVec3, Vec2, Vec3};89/// A builder returned by [`GizmoBuffer::grid_3d`]10pub struct GridBuilder3d<'a, Config, Clear>11where12Config: GizmoConfigGroup,13Clear: 'static + Send + Sync,14{15gizmos: &'a mut GizmoBuffer<Config, Clear>,16isometry: Isometry3d,17spacing: Vec3,18cell_count: UVec3,19skew: Vec3,20outer_edges: [bool; 3],21color: Color,22}23/// A builder returned by [`GizmoBuffer::grid`] and [`GizmoBuffer::grid_2d`]24pub struct GridBuilder2d<'a, Config, Clear>25where26Config: GizmoConfigGroup,27Clear: 'static + Send + Sync,28{29gizmos: &'a mut GizmoBuffer<Config, Clear>,30isometry: Isometry3d,31spacing: Vec2,32cell_count: UVec2,33skew: Vec2,34outer_edges: [bool; 2],35color: Color,36}3738impl<Config, Clear> GridBuilder3d<'_, Config, Clear>39where40Config: GizmoConfigGroup,41Clear: 'static + Send + Sync,42{43/// Skews the grid by `tan(skew)` in the x direction.44/// `skew` is in radians45pub fn skew_x(mut self, skew: f32) -> Self {46self.skew.x = skew;47self48}49/// Skews the grid by `tan(skew)` in the y direction.50/// `skew` is in radians51pub fn skew_y(mut self, skew: f32) -> Self {52self.skew.y = skew;53self54}55/// Skews the grid by `tan(skew)` in the z direction.56/// `skew` is in radians57pub fn skew_z(mut self, skew: f32) -> Self {58self.skew.z = skew;59self60}61/// Skews the grid by `tan(skew)` in the x, y and z directions.62/// `skew` is in radians63pub fn skew(mut self, skew: Vec3) -> Self {64self.skew = skew;65self66}6768/// Declare that the outer edges of the grid parallel to the x axis should be drawn.69/// By default, the outer edges will not be drawn.70pub fn outer_edges_x(mut self) -> Self {71self.outer_edges[0] = true;72self73}74/// Declare that the outer edges of the grid parallel to the y axis should be drawn.75/// By default, the outer edges will not be drawn.76pub fn outer_edges_y(mut self) -> Self {77self.outer_edges[1] = true;78self79}80/// Declare that the outer edges of the grid parallel to the z axis should be drawn.81/// By default, the outer edges will not be drawn.82pub fn outer_edges_z(mut self) -> Self {83self.outer_edges[2] = true;84self85}86/// Declare that all outer edges of the grid should be drawn.87/// By default, the outer edges will not be drawn.88pub fn outer_edges(mut self) -> Self {89self.outer_edges.fill(true);90self91}92}9394impl<Config, Clear> GridBuilder2d<'_, Config, Clear>95where96Config: GizmoConfigGroup,97Clear: 'static + Send + Sync,98{99/// Skews the grid by `tan(skew)` in the x direction.100/// `skew` is in radians101pub fn skew_x(mut self, skew: f32) -> Self {102self.skew.x = skew;103self104}105/// Skews the grid by `tan(skew)` in the y direction.106/// `skew` is in radians107pub fn skew_y(mut self, skew: f32) -> Self {108self.skew.y = skew;109self110}111/// Skews the grid by `tan(skew)` in the x and y directions.112/// `skew` is in radians113pub fn skew(mut self, skew: Vec2) -> Self {114self.skew = skew;115self116}117118/// Declare that the outer edges of the grid parallel to the x axis should be drawn.119/// By default, the outer edges will not be drawn.120pub fn outer_edges_x(mut self) -> Self {121self.outer_edges[0] = true;122self123}124/// Declare that the outer edges of the grid parallel to the y axis should be drawn.125/// By default, the outer edges will not be drawn.126pub fn outer_edges_y(mut self) -> Self {127self.outer_edges[1] = true;128self129}130/// Declare that all outer edges of the grid should be drawn.131/// By default, the outer edges will not be drawn.132pub fn outer_edges(mut self) -> Self {133self.outer_edges.fill(true);134self135}136}137138impl<Config, Clear> Drop for GridBuilder3d<'_, Config, Clear>139where140Config: GizmoConfigGroup,141Clear: 'static + Send + Sync,142{143/// Draws a grid, by drawing lines with the stored [`GizmoBuffer`]144fn drop(&mut self) {145draw_grid(146self.gizmos,147self.isometry,148self.spacing,149self.cell_count,150self.skew,151self.outer_edges,152self.color,153);154}155}156157impl<Config, Clear> Drop for GridBuilder2d<'_, Config, Clear>158where159Config: GizmoConfigGroup,160Clear: 'static + Send + Sync,161{162fn drop(&mut self) {163draw_grid(164self.gizmos,165self.isometry,166self.spacing.extend(0.),167self.cell_count.extend(0),168self.skew.extend(0.),169[self.outer_edges[0], self.outer_edges[1], true],170self.color,171);172}173}174175impl<Config, Clear> GizmoBuffer<Config, Clear>176where177Config: GizmoConfigGroup,178Clear: 'static + Send + Sync,179{180/// Draw a 2D grid in 3D.181///182/// This should be called for each frame the grid needs to be rendered.183///184/// The grid's default orientation aligns with the XY-plane.185///186/// # Arguments187///188/// - `isometry` defines the translation and rotation of the grid.189/// - the translation specifies the center of the grid190/// - defines the orientation of the grid, by default we assume the grid is contained in a191/// plane parallel to the XY plane192/// - `cell_count`: defines the amount of cells in the x and y axes193/// - `spacing`: defines the distance between cells along the x and y axes194/// - `color`: color of the grid195///196/// # Builder methods197///198/// - The skew of the grid can be adjusted using the `.skew(...)`, `.skew_x(...)` or `.skew_y(...)` methods. They behave very similar to their CSS equivalents.199/// - All outer edges can be toggled on or off using `.outer_edges(...)`. Alternatively you can use `.outer_edges_x(...)` or `.outer_edges_y(...)` to toggle the outer edges along an axis.200///201/// # Example202/// ```203/// # use bevy_gizmos::prelude::*;204/// # use bevy_math::prelude::*;205/// # use bevy_color::palettes::basic::GREEN;206/// fn system(mut gizmos: Gizmos) {207/// gizmos.grid(208/// Isometry3d::IDENTITY,209/// UVec2::new(10, 10),210/// Vec2::splat(2.),211/// GREEN212/// )213/// .skew_x(0.25)214/// .outer_edges();215/// }216/// # bevy_ecs::system::assert_is_system(system);217/// ```218pub fn grid(219&mut self,220isometry: impl Into<Isometry3d>,221cell_count: UVec2,222spacing: Vec2,223color: impl Into<Color>,224) -> GridBuilder2d<'_, Config, Clear> {225GridBuilder2d {226gizmos: self,227isometry: isometry.into(),228spacing,229cell_count,230skew: Vec2::ZERO,231outer_edges: [false, false],232color: color.into(),233}234}235236/// Draw a 3D grid of voxel-like cells.237///238/// This should be called for each frame the grid needs to be rendered.239///240/// # Arguments241///242/// - `isometry` defines the translation and rotation of the grid.243/// - the translation specifies the center of the grid244/// - defines the orientation of the grid, by default we assume the grid is aligned with all axes245/// - `cell_count`: defines the amount of cells in the x, y and z axes246/// - `spacing`: defines the distance between cells along the x, y and z axes247/// - `color`: color of the grid248///249/// # Builder methods250///251/// - The skew of the grid can be adjusted using the `.skew(...)`, `.skew_x(...)`, `.skew_y(...)` or `.skew_z(...)` methods. They behave very similar to their CSS equivalents.252/// - All outer edges can be toggled on or off using `.outer_edges(...)`. Alternatively you can use `.outer_edges_x(...)`, `.outer_edges_y(...)` or `.outer_edges_z(...)` to toggle the outer edges along an axis.253///254/// # Example255/// ```256/// # use bevy_gizmos::prelude::*;257/// # use bevy_math::prelude::*;258/// # use bevy_color::palettes::basic::GREEN;259/// fn system(mut gizmos: Gizmos) {260/// gizmos.grid_3d(261/// Isometry3d::IDENTITY,262/// UVec3::new(10, 2, 10),263/// Vec3::splat(2.),264/// GREEN265/// )266/// .skew_x(0.25)267/// .outer_edges();268/// }269/// # bevy_ecs::system::assert_is_system(system);270/// ```271pub fn grid_3d(272&mut self,273isometry: impl Into<Isometry3d>,274cell_count: UVec3,275spacing: Vec3,276color: impl Into<Color>,277) -> GridBuilder3d<'_, Config, Clear> {278GridBuilder3d {279gizmos: self,280isometry: isometry.into(),281spacing,282cell_count,283skew: Vec3::ZERO,284outer_edges: [false, false, false],285color: color.into(),286}287}288289/// Draw a grid in 2D.290///291/// This should be called for each frame the grid needs to be rendered.292///293/// # Arguments294///295/// - `isometry` defines the translation and rotation of the grid.296/// - the translation specifies the center of the grid297/// - defines the orientation of the grid, by default we assume the grid is aligned with all axes298/// - `cell_count`: defines the amount of cells in the x and y axes299/// - `spacing`: defines the distance between cells along the x and y axes300/// - `color`: color of the grid301///302/// # Builder methods303///304/// - The skew of the grid can be adjusted using the `.skew(...)`, `.skew_x(...)` or `.skew_y(...)` methods. They behave very similar to their CSS equivalents.305/// - All outer edges can be toggled on or off using `.outer_edges(...)`. Alternatively you can use `.outer_edges_x(...)` or `.outer_edges_y(...)` to toggle the outer edges along an axis.306///307/// # Example308/// ```309/// # use bevy_gizmos::prelude::*;310/// # use bevy_math::prelude::*;311/// # use bevy_color::palettes::basic::GREEN;312/// fn system(mut gizmos: Gizmos) {313/// gizmos.grid_2d(314/// Isometry2d::IDENTITY,315/// UVec2::new(10, 10),316/// Vec2::splat(1.),317/// GREEN318/// )319/// .skew_x(0.25)320/// .outer_edges();321/// }322/// # bevy_ecs::system::assert_is_system(system);323/// ```324pub fn grid_2d(325&mut self,326isometry: impl Into<Isometry2d>,327cell_count: UVec2,328spacing: Vec2,329color: impl Into<Color>,330) -> GridBuilder2d<'_, Config, Clear> {331let isometry = isometry.into();332GridBuilder2d {333gizmos: self,334isometry: Isometry3d::new(335isometry.translation.extend(0.0),336Quat::from_rotation_z(isometry.rotation.as_radians()),337),338spacing,339cell_count,340skew: Vec2::ZERO,341outer_edges: [false, false],342color: color.into(),343}344}345}346347fn draw_grid<Config, Clear>(348gizmos: &mut GizmoBuffer<Config, Clear>,349isometry: Isometry3d,350spacing: Vec3,351cell_count: UVec3,352skew: Vec3,353outer_edges: [bool; 3],354color: Color,355) where356Config: GizmoConfigGroup,357Clear: 'static + Send + Sync,358{359if !gizmos.enabled {360return;361}362363#[inline]364fn or_zero(cond: bool, val: Vec3) -> Vec3 {365if cond {366val367} else {368Vec3::ZERO369}370}371372// Offset between two adjacent grid cells along the x/y-axis and accounting for skew.373let skew_tan = Vec3::from(skew.to_array().map(ops::tan));374let dx = or_zero(375cell_count.x != 0,376spacing.x * Vec3::new(1., skew_tan.y, skew_tan.z),377);378let dy = or_zero(379cell_count.y != 0,380spacing.y * Vec3::new(skew_tan.x, 1., skew_tan.z),381);382let dz = or_zero(383cell_count.z != 0,384spacing.z * Vec3::new(skew_tan.x, skew_tan.y, 1.),385);386387// Bottom-left-front corner of the grid388let cell_count_half = cell_count.as_vec3() * 0.5;389let grid_start = -cell_count_half.x * dx - cell_count_half.y * dy - cell_count_half.z * dz;390391#[inline]392fn cell_count_to_line_count(include_outer: bool, cell_count: u32) -> u32 {393if include_outer {394cell_count.saturating_add(1)395} else {396cell_count.saturating_sub(1).max(1)397}398}399400let x_line_count = UVec2::new(401cell_count_to_line_count(outer_edges[0], cell_count.y),402cell_count_to_line_count(outer_edges[0], cell_count.z),403);404let y_line_count = UVec2::new(405cell_count_to_line_count(outer_edges[1], cell_count.z),406cell_count_to_line_count(outer_edges[1], cell_count.x),407);408let z_line_count = UVec2::new(409cell_count_to_line_count(outer_edges[2], cell_count.x),410cell_count_to_line_count(outer_edges[2], cell_count.y),411);412413let x_start = grid_start + or_zero(!outer_edges[0], dy + dz);414let y_start = grid_start + or_zero(!outer_edges[1], dx + dz);415let z_start = grid_start + or_zero(!outer_edges[2], dx + dy);416417fn iter_lines(418delta_a: Vec3,419delta_b: Vec3,420delta_c: Vec3,421line_count: UVec2,422cell_count: u32,423start: Vec3,424) -> impl Iterator<Item = [Vec3; 2]> {425let dline = delta_a * cell_count as f32;426(0..line_count.x).map(|v| v as f32).flat_map(move |b| {427(0..line_count.y).map(|v| v as f32).map(move |c| {428let line_start = start + b * delta_b + c * delta_c;429let line_end = line_start + dline;430[line_start, line_end]431})432})433}434435// Lines along the x direction436let x_lines = iter_lines(dx, dy, dz, x_line_count, cell_count.x, x_start);437// Lines along the y direction438let y_lines = iter_lines(dy, dz, dx, y_line_count, cell_count.y, y_start);439// Lines along the z direction440let z_lines = iter_lines(dz, dx, dy, z_line_count, cell_count.z, z_start);441442x_lines443.chain(y_lines)444.chain(z_lines)445.map(|vec3s| vec3s.map(|vec3| isometry * vec3))446.for_each(|[start, end]| {447gizmos.line(start, end, color);448});449}450451452