//! 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/// The grid's default orientation aligns with the XY-plane.183///184/// # Arguments185///186/// - `isometry` defines the translation and rotation of the grid.187/// - the translation specifies the center of the grid188/// - defines the orientation of the grid, by default we assume the grid is contained in a189/// plane parallel to the XY plane190/// - `cell_count`: defines the amount of cells in the x and y axes191/// - `spacing`: defines the distance between cells along the x and y axes192/// - `color`: color of the grid193///194/// # Builder methods195///196/// - 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.197/// - 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.198///199/// # Example200/// ```201/// # use bevy_gizmos::prelude::*;202/// # use bevy_math::prelude::*;203/// # use bevy_color::palettes::basic::GREEN;204/// fn system(mut gizmos: Gizmos) {205/// gizmos.grid(206/// Isometry3d::IDENTITY,207/// UVec2::new(10, 10),208/// Vec2::splat(2.),209/// GREEN210/// )211/// .skew_x(0.25)212/// .outer_edges();213/// }214/// # bevy_ecs::system::assert_is_system(system);215/// ```216pub fn grid(217&mut self,218isometry: impl Into<Isometry3d>,219cell_count: UVec2,220spacing: Vec2,221color: impl Into<Color>,222) -> GridBuilder2d<'_, Config, Clear> {223GridBuilder2d {224gizmos: self,225isometry: isometry.into(),226spacing,227cell_count,228skew: Vec2::ZERO,229outer_edges: [false, false],230color: color.into(),231}232}233234/// Draw a 3D grid of voxel-like cells.235///236/// # Arguments237///238/// - `isometry` defines the translation and rotation of the grid.239/// - the translation specifies the center of the grid240/// - defines the orientation of the grid, by default we assume the grid is aligned with all axes241/// - `cell_count`: defines the amount of cells in the x, y and z axes242/// - `spacing`: defines the distance between cells along the x, y and z axes243/// - `color`: color of the grid244///245/// # Builder methods246///247/// - 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.248/// - 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.249///250/// # Example251/// ```252/// # use bevy_gizmos::prelude::*;253/// # use bevy_math::prelude::*;254/// # use bevy_color::palettes::basic::GREEN;255/// fn system(mut gizmos: Gizmos) {256/// gizmos.grid_3d(257/// Isometry3d::IDENTITY,258/// UVec3::new(10, 2, 10),259/// Vec3::splat(2.),260/// GREEN261/// )262/// .skew_x(0.25)263/// .outer_edges();264/// }265/// # bevy_ecs::system::assert_is_system(system);266/// ```267pub fn grid_3d(268&mut self,269isometry: impl Into<Isometry3d>,270cell_count: UVec3,271spacing: Vec3,272color: impl Into<Color>,273) -> GridBuilder3d<'_, Config, Clear> {274GridBuilder3d {275gizmos: self,276isometry: isometry.into(),277spacing,278cell_count,279skew: Vec3::ZERO,280outer_edges: [false, false, false],281color: color.into(),282}283}284285/// Draw a grid in 2D.286///287/// # Arguments288///289/// - `isometry` defines the translation and rotation of the grid.290/// - the translation specifies the center of the grid291/// - defines the orientation of the grid, by default we assume the grid is aligned with all axes292/// - `cell_count`: defines the amount of cells in the x and y axes293/// - `spacing`: defines the distance between cells along the x and y axes294/// - `color`: color of the grid295///296/// # Builder methods297///298/// - 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.299/// - 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.300///301/// # Example302/// ```303/// # use bevy_gizmos::prelude::*;304/// # use bevy_math::prelude::*;305/// # use bevy_color::palettes::basic::GREEN;306/// fn system(mut gizmos: Gizmos) {307/// gizmos.grid_2d(308/// Isometry2d::IDENTITY,309/// UVec2::new(10, 10),310/// Vec2::splat(1.),311/// GREEN312/// )313/// .skew_x(0.25)314/// .outer_edges();315/// }316/// # bevy_ecs::system::assert_is_system(system);317/// ```318pub fn grid_2d(319&mut self,320isometry: impl Into<Isometry2d>,321cell_count: UVec2,322spacing: Vec2,323color: impl Into<Color>,324) -> GridBuilder2d<'_, Config, Clear> {325let isometry = isometry.into();326GridBuilder2d {327gizmos: self,328isometry: Isometry3d::new(329isometry.translation.extend(0.0),330Quat::from_rotation_z(isometry.rotation.as_radians()),331),332spacing,333cell_count,334skew: Vec2::ZERO,335outer_edges: [false, false],336color: color.into(),337}338}339}340341fn draw_grid<Config, Clear>(342gizmos: &mut GizmoBuffer<Config, Clear>,343isometry: Isometry3d,344spacing: Vec3,345cell_count: UVec3,346skew: Vec3,347outer_edges: [bool; 3],348color: Color,349) where350Config: GizmoConfigGroup,351Clear: 'static + Send + Sync,352{353if !gizmos.enabled {354return;355}356357#[inline]358fn or_zero(cond: bool, val: Vec3) -> Vec3 {359if cond {360val361} else {362Vec3::ZERO363}364}365366// Offset between two adjacent grid cells along the x/y-axis and accounting for skew.367let skew_tan = Vec3::from(skew.to_array().map(ops::tan));368let dx = or_zero(369cell_count.x != 0,370spacing.x * Vec3::new(1., skew_tan.y, skew_tan.z),371);372let dy = or_zero(373cell_count.y != 0,374spacing.y * Vec3::new(skew_tan.x, 1., skew_tan.z),375);376let dz = or_zero(377cell_count.z != 0,378spacing.z * Vec3::new(skew_tan.x, skew_tan.y, 1.),379);380381// Bottom-left-front corner of the grid382let cell_count_half = cell_count.as_vec3() * 0.5;383let grid_start = -cell_count_half.x * dx - cell_count_half.y * dy - cell_count_half.z * dz;384385#[inline]386fn cell_count_to_line_count(include_outer: bool, cell_count: u32) -> u32 {387if include_outer {388cell_count.saturating_add(1)389} else {390cell_count.saturating_sub(1).max(1)391}392}393394let x_line_count = UVec2::new(395cell_count_to_line_count(outer_edges[0], cell_count.y),396cell_count_to_line_count(outer_edges[0], cell_count.z),397);398let y_line_count = UVec2::new(399cell_count_to_line_count(outer_edges[1], cell_count.z),400cell_count_to_line_count(outer_edges[1], cell_count.x),401);402let z_line_count = UVec2::new(403cell_count_to_line_count(outer_edges[2], cell_count.x),404cell_count_to_line_count(outer_edges[2], cell_count.y),405);406407let x_start = grid_start + or_zero(!outer_edges[0], dy + dz);408let y_start = grid_start + or_zero(!outer_edges[1], dx + dz);409let z_start = grid_start + or_zero(!outer_edges[2], dx + dy);410411fn iter_lines(412delta_a: Vec3,413delta_b: Vec3,414delta_c: Vec3,415line_count: UVec2,416cell_count: u32,417start: Vec3,418) -> impl Iterator<Item = [Vec3; 2]> {419let dline = delta_a * cell_count as f32;420(0..line_count.x).map(|v| v as f32).flat_map(move |b| {421(0..line_count.y).map(|v| v as f32).map(move |c| {422let line_start = start + b * delta_b + c * delta_c;423let line_end = line_start + dline;424[line_start, line_end]425})426})427}428429// Lines along the x direction430let x_lines = iter_lines(dx, dy, dz, x_line_count, cell_count.x, x_start);431// Lines along the y direction432let y_lines = iter_lines(dy, dz, dx, y_line_count, cell_count.y, y_start);433// Lines along the z direction434let z_lines = iter_lines(dz, dx, dy, z_line_count, cell_count.z, z_start);435436x_lines437.chain(y_lines)438.chain(z_lines)439.map(|vec3s| vec3s.map(|vec3| isometry * vec3))440.for_each(|[start, end]| {441gizmos.line(start, end, color);442});443}444445446