2024-02-28 00:18:26 +00:00
//! Additional [`Gizmos`] Functions -- Grids
//!
2024-06-17 15:45:32 +00:00
//! Includes the implementation of [`Gizmos::grid`] and [`Gizmos::grid_2d`].
2024-02-28 00:18:26 +00:00
//! and assorted support items.
use crate ::prelude ::{ GizmoConfigGroup , Gizmos } ;
2024-08-23 02:54:45 +00:00
use bevy_color ::Color ;
2024-08-28 01:37:19 +00:00
use bevy_math ::Vec3Swizzles ;
use bevy_math ::{ Isometry2d , Isometry3d , Quat , UVec2 , UVec3 , Vec2 , Vec3 } ;
2024-02-28 00:18:26 +00:00
2024-03-13 18:51:53 +00:00
/// A builder returned by [`Gizmos::grid_3d`]
2024-04-23 00:16:12 +00:00
pub struct GridBuilder3d < ' a , ' w , ' s , Config , Clear >
where
Config : GizmoConfigGroup ,
Clear : 'static + Send + Sync ,
{
gizmos : & ' a mut Gizmos < ' w , ' s , Config , Clear > ,
2024-08-28 01:37:19 +00:00
isometry : Isometry3d ,
2024-03-13 18:51:53 +00:00
spacing : Vec3 ,
cell_count : UVec3 ,
skew : Vec3 ,
outer_edges : [ bool ; 3 ] ,
2024-08-23 02:54:45 +00:00
color : Color ,
2024-03-13 18:51:53 +00:00
}
2024-02-28 00:18:26 +00:00
/// A builder returned by [`Gizmos::grid`] and [`Gizmos::grid_2d`]
2024-04-23 00:16:12 +00:00
pub struct GridBuilder2d < ' a , ' w , ' s , Config , Clear >
where
Config : GizmoConfigGroup ,
Clear : 'static + Send + Sync ,
{
gizmos : & ' a mut Gizmos < ' w , ' s , Config , Clear > ,
2024-08-28 01:37:19 +00:00
isometry : Isometry3d ,
2024-02-28 00:18:26 +00:00
spacing : Vec2 ,
cell_count : UVec2 ,
skew : Vec2 ,
2024-03-13 18:51:53 +00:00
outer_edges : [ bool ; 2 ] ,
2024-08-23 02:54:45 +00:00
color : Color ,
2024-02-28 00:18:26 +00:00
}
2024-04-23 00:16:12 +00:00
impl < Config , Clear > GridBuilder3d < '_ , '_ , '_ , Config , Clear >
where
Config : GizmoConfigGroup ,
Clear : 'static + Send + Sync ,
{
2024-02-28 00:18:26 +00:00
/// Skews the grid by `tan(skew)` in the x direction.
/// `skew` is in radians
pub fn skew_x ( mut self , skew : f32 ) -> Self {
self . skew . x = skew ;
self
}
2024-03-13 18:51:53 +00:00
/// Skews the grid by `tan(skew)` in the y direction.
/// `skew` is in radians
pub fn skew_y ( mut self , skew : f32 ) -> Self {
self . skew . y = skew ;
self
}
/// Skews the grid by `tan(skew)` in the z direction.
/// `skew` is in radians
pub fn skew_z ( mut self , skew : f32 ) -> Self {
self . skew . z = skew ;
self
}
/// Skews the grid by `tan(skew)` in the x, y and z directions.
/// `skew` is in radians
pub fn skew ( mut self , skew : Vec3 ) -> Self {
self . skew = skew ;
self
}
/// Declare that the outer edges of the grid along 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.
/// 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.
/// By default, the outer edges will not be drawn.
pub fn outer_edges_z ( mut self ) -> Self {
self . outer_edges [ 2 ] = true ;
self
}
/// Declare that all outer edges of the grid should be drawn.
/// By default, the outer edges will not be drawn.
pub fn outer_edges ( mut self ) -> Self {
self . outer_edges . fill ( true ) ;
self
}
}
2024-04-23 00:16:12 +00:00
impl < Config , Clear > GridBuilder2d < '_ , '_ , '_ , Config , Clear >
where
Config : GizmoConfigGroup ,
Clear : 'static + Send + Sync ,
{
2024-02-28 00:18:26 +00:00
/// Skews the grid by `tan(skew)` in the x direction.
/// `skew` is in radians
2024-03-13 18:51:53 +00:00
pub fn skew_x ( mut self , skew : f32 ) -> Self {
self . skew . x = skew ;
self
}
/// Skews the grid by `tan(skew)` in the y direction.
/// `skew` is in radians
2024-02-28 00:18:26 +00:00
pub fn skew_y ( mut self , skew : f32 ) -> Self {
self . skew . y = skew ;
self
}
/// Skews the grid by `tan(skew)` in the x and y directions.
/// `skew` is in radians
pub fn skew ( mut self , skew : Vec2 ) -> Self {
self . skew = skew ;
self
}
2024-03-13 18:51:53 +00:00
/// Declare that the outer edges of the grid along 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.
2024-02-28 00:18:26 +00:00
/// By default, the outer edges will not be drawn.
2024-03-13 18:51:53 +00:00
pub fn outer_edges_y ( mut self ) -> Self {
self . outer_edges [ 1 ] = true ;
self
}
/// Declare that all outer edges of the grid should be drawn.
/// By default, the outer edges will not be drawn.
pub fn outer_edges ( mut self ) -> Self {
self . outer_edges . fill ( true ) ;
2024-02-28 00:18:26 +00:00
self
}
}
2024-04-23 00:16:12 +00:00
impl < Config , Clear > Drop for GridBuilder3d < '_ , '_ , '_ , Config , Clear >
where
Config : GizmoConfigGroup ,
Clear : 'static + Send + Sync ,
{
/// Draws a grid, by drawing lines with the stored [`Gizmos`]
2024-02-28 00:18:26 +00:00
fn drop ( & mut self ) {
2024-03-13 18:51:53 +00:00
draw_grid (
self . gizmos ,
2024-08-28 01:37:19 +00:00
self . isometry ,
2024-03-13 18:51:53 +00:00
self . spacing ,
self . cell_count ,
self . skew ,
self . outer_edges ,
self . color ,
) ;
}
}
2024-04-23 00:16:12 +00:00
impl < Config , Clear > Drop for GridBuilder2d < '_ , '_ , '_ , Config , Clear >
where
Config : GizmoConfigGroup ,
Clear : 'static + Send + Sync ,
{
2024-03-13 18:51:53 +00:00
fn drop ( & mut self ) {
draw_grid (
self . gizmos ,
2024-08-28 01:37:19 +00:00
self . isometry ,
2024-03-13 18:51:53 +00:00
self . spacing . extend ( 0. ) ,
self . cell_count . extend ( 0 ) ,
self . skew . extend ( 0. ) ,
[ self . outer_edges [ 0 ] , self . outer_edges [ 1 ] , true ] ,
self . color ,
) ;
2024-02-28 00:18:26 +00:00
}
}
2024-04-23 00:16:12 +00:00
impl < ' w , ' s , Config , Clear > Gizmos < ' w , ' s , Config , Clear >
where
Config : GizmoConfigGroup ,
Clear : 'static + Send + Sync ,
{
2024-02-28 00:18:26 +00:00
/// Draw a 2D grid in 3D.
///
/// This should be called for each frame the grid needs to be rendered.
///
/// # Arguments
///
2024-08-28 01:37:19 +00:00
/// - `isometry` defines the translation and rotation of the grid.
/// - the translation specifies the center of the grid
/// - defines the orientation of the grid, by default
/// we assume the grid is contained in a plane parallel
/// to the XY plane
2024-02-28 00:18:26 +00:00
/// - `cell_count`: defines the amount of cells in the x and y axes
/// - `spacing`: defines the distance between cells along the x and y axes
/// - `color`: color of the grid
///
/// # Builder methods
///
/// - 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.
2024-03-13 18:51:53 +00:00
/// - 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.
2024-02-28 00:18:26 +00:00
///
/// # Example
/// ```
/// # use bevy_gizmos::prelude::*;
/// # use bevy_math::prelude::*;
Migrate from `LegacyColor` to `bevy_color::Color` (#12163)
# Objective
- As part of the migration process we need to a) see the end effect of
the migration on user ergonomics b) check for serious perf regressions
c) actually migrate the code
- To accomplish this, I'm going to attempt to migrate all of the
remaining user-facing usages of `LegacyColor` in one PR, being careful
to keep a clean commit history.
- Fixes #12056.
## Solution
I've chosen to use the polymorphic `Color` type as our standard
user-facing API.
- [x] Migrate `bevy_gizmos`.
- [x] Take `impl Into<Color>` in all `bevy_gizmos` APIs
- [x] Migrate sprites
- [x] Migrate UI
- [x] Migrate `ColorMaterial`
- [x] Migrate `MaterialMesh2D`
- [x] Migrate fog
- [x] Migrate lights
- [x] Migrate StandardMaterial
- [x] Migrate wireframes
- [x] Migrate clear color
- [x] Migrate text
- [x] Migrate gltf loader
- [x] Register color types for reflection
- [x] Remove `LegacyColor`
- [x] Make sure CI passes
Incidental improvements to ease migration:
- added `Color::srgba_u8`, `Color::srgba_from_array` and friends
- added `set_alpha`, `is_fully_transparent` and `is_fully_opaque` to the
`Alpha` trait
- add and immediately deprecate (lol) `Color::rgb` and friends in favor
of more explicit and consistent `Color::srgb`
- standardized on white and black for most example text colors
- added vector field traits to `LinearRgba`: ~~`Add`, `Sub`,
`AddAssign`, `SubAssign`,~~ `Mul<f32>` and `Div<f32>`. Multiplications
and divisions do not scale alpha. `Add` and `Sub` have been cut from
this PR.
- added `LinearRgba` and `Srgba` `RED/GREEN/BLUE`
- added `LinearRgba_to_f32_array` and `LinearRgba::to_u32`
## Migration Guide
Bevy's color types have changed! Wherever you used a
`bevy::render::Color`, a `bevy::color::Color` is used instead.
These are quite similar! Both are enums storing a color in a specific
color space (or to be more precise, using a specific color model).
However, each of the different color models now has its own type.
TODO...
- `Color::rgba`, `Color::rgb`, `Color::rbga_u8`, `Color::rgb_u8`,
`Color::rgb_from_array` are now `Color::srgba`, `Color::srgb`,
`Color::srgba_u8`, `Color::srgb_u8` and `Color::srgb_from_array`.
- `Color::set_a` and `Color::a` is now `Color::set_alpha` and
`Color::alpha`. These are part of the `Alpha` trait in `bevy_color`.
- `Color::is_fully_transparent` is now part of the `Alpha` trait in
`bevy_color`
- `Color::r`, `Color::set_r`, `Color::with_r` and the equivalents for
`g`, `b` `h`, `s` and `l` have been removed due to causing silent
relatively expensive conversions. Convert your `Color` into the desired
color space, perform your operations there, and then convert it back
into a polymorphic `Color` enum.
- `Color::hex` is now `Srgba::hex`. Call `.into` or construct a
`Color::Srgba` variant manually to convert it.
- `WireframeMaterial`, `ExtractedUiNode`, `ExtractedDirectionalLight`,
`ExtractedPointLight`, `ExtractedSpotLight` and `ExtractedSprite` now
store a `LinearRgba`, rather than a polymorphic `Color`
- `Color::rgb_linear` and `Color::rgba_linear` are now
`Color::linear_rgb` and `Color::linear_rgba`
- The various CSS color constants are no longer stored directly on
`Color`. Instead, they're defined in the `Srgba` color space, and
accessed via `bevy::color::palettes::css`. Call `.into()` on them to
convert them into a `Color` for quick debugging use, and consider using
the much prettier `tailwind` palette for prototyping.
- The `LIME_GREEN` color has been renamed to `LIMEGREEN` to comply with
the standard naming.
- Vector field arithmetic operations on `Color` (add, subtract, multiply
and divide by a f32) have been removed. Instead, convert your colors
into `LinearRgba` space, and perform your operations explicitly there.
This is particularly relevant when working with emissive or HDR colors,
whose color channel values are routinely outside of the ordinary 0 to 1
range.
- `Color::as_linear_rgba_f32` has been removed. Call
`LinearRgba::to_f32_array` instead, converting if needed.
- `Color::as_linear_rgba_u32` has been removed. Call
`LinearRgba::to_u32` instead, converting if needed.
- Several other color conversion methods to transform LCH or HSL colors
into float arrays or `Vec` types have been removed. Please reimplement
these externally or open a PR to re-add them if you found them
particularly useful.
- Various methods on `Color` such as `rgb` or `hsl` to convert the color
into a specific color space have been removed. Convert into
`LinearRgba`, then to the color space of your choice.
- Various implicitly-converting color value methods on `Color` such as
`r`, `g`, `b` or `h` have been removed. Please convert it into the color
space of your choice, then check these properties.
- `Color` no longer implements `AsBindGroup`. Store a `LinearRgba`
internally instead to avoid conversion costs.
---------
Co-authored-by: Alice Cecile <alice.i.cecil@gmail.com>
Co-authored-by: Afonso Lage <lage.afonso@gmail.com>
Co-authored-by: Rob Parrett <robparrett@gmail.com>
Co-authored-by: Zachary Harrold <zac@harrold.com.au>
2024-02-29 19:35:12 +00:00
/// # use bevy_color::palettes::basic::GREEN;
2024-02-28 00:18:26 +00:00
/// fn system(mut gizmos: Gizmos) {
/// gizmos.grid(
2024-08-28 01:37:19 +00:00
/// Isometry3d::IDENTITY,
2024-02-28 00:18:26 +00:00
/// UVec2::new(10, 10),
/// Vec2::splat(2.),
Migrate from `LegacyColor` to `bevy_color::Color` (#12163)
# Objective
- As part of the migration process we need to a) see the end effect of
the migration on user ergonomics b) check for serious perf regressions
c) actually migrate the code
- To accomplish this, I'm going to attempt to migrate all of the
remaining user-facing usages of `LegacyColor` in one PR, being careful
to keep a clean commit history.
- Fixes #12056.
## Solution
I've chosen to use the polymorphic `Color` type as our standard
user-facing API.
- [x] Migrate `bevy_gizmos`.
- [x] Take `impl Into<Color>` in all `bevy_gizmos` APIs
- [x] Migrate sprites
- [x] Migrate UI
- [x] Migrate `ColorMaterial`
- [x] Migrate `MaterialMesh2D`
- [x] Migrate fog
- [x] Migrate lights
- [x] Migrate StandardMaterial
- [x] Migrate wireframes
- [x] Migrate clear color
- [x] Migrate text
- [x] Migrate gltf loader
- [x] Register color types for reflection
- [x] Remove `LegacyColor`
- [x] Make sure CI passes
Incidental improvements to ease migration:
- added `Color::srgba_u8`, `Color::srgba_from_array` and friends
- added `set_alpha`, `is_fully_transparent` and `is_fully_opaque` to the
`Alpha` trait
- add and immediately deprecate (lol) `Color::rgb` and friends in favor
of more explicit and consistent `Color::srgb`
- standardized on white and black for most example text colors
- added vector field traits to `LinearRgba`: ~~`Add`, `Sub`,
`AddAssign`, `SubAssign`,~~ `Mul<f32>` and `Div<f32>`. Multiplications
and divisions do not scale alpha. `Add` and `Sub` have been cut from
this PR.
- added `LinearRgba` and `Srgba` `RED/GREEN/BLUE`
- added `LinearRgba_to_f32_array` and `LinearRgba::to_u32`
## Migration Guide
Bevy's color types have changed! Wherever you used a
`bevy::render::Color`, a `bevy::color::Color` is used instead.
These are quite similar! Both are enums storing a color in a specific
color space (or to be more precise, using a specific color model).
However, each of the different color models now has its own type.
TODO...
- `Color::rgba`, `Color::rgb`, `Color::rbga_u8`, `Color::rgb_u8`,
`Color::rgb_from_array` are now `Color::srgba`, `Color::srgb`,
`Color::srgba_u8`, `Color::srgb_u8` and `Color::srgb_from_array`.
- `Color::set_a` and `Color::a` is now `Color::set_alpha` and
`Color::alpha`. These are part of the `Alpha` trait in `bevy_color`.
- `Color::is_fully_transparent` is now part of the `Alpha` trait in
`bevy_color`
- `Color::r`, `Color::set_r`, `Color::with_r` and the equivalents for
`g`, `b` `h`, `s` and `l` have been removed due to causing silent
relatively expensive conversions. Convert your `Color` into the desired
color space, perform your operations there, and then convert it back
into a polymorphic `Color` enum.
- `Color::hex` is now `Srgba::hex`. Call `.into` or construct a
`Color::Srgba` variant manually to convert it.
- `WireframeMaterial`, `ExtractedUiNode`, `ExtractedDirectionalLight`,
`ExtractedPointLight`, `ExtractedSpotLight` and `ExtractedSprite` now
store a `LinearRgba`, rather than a polymorphic `Color`
- `Color::rgb_linear` and `Color::rgba_linear` are now
`Color::linear_rgb` and `Color::linear_rgba`
- The various CSS color constants are no longer stored directly on
`Color`. Instead, they're defined in the `Srgba` color space, and
accessed via `bevy::color::palettes::css`. Call `.into()` on them to
convert them into a `Color` for quick debugging use, and consider using
the much prettier `tailwind` palette for prototyping.
- The `LIME_GREEN` color has been renamed to `LIMEGREEN` to comply with
the standard naming.
- Vector field arithmetic operations on `Color` (add, subtract, multiply
and divide by a f32) have been removed. Instead, convert your colors
into `LinearRgba` space, and perform your operations explicitly there.
This is particularly relevant when working with emissive or HDR colors,
whose color channel values are routinely outside of the ordinary 0 to 1
range.
- `Color::as_linear_rgba_f32` has been removed. Call
`LinearRgba::to_f32_array` instead, converting if needed.
- `Color::as_linear_rgba_u32` has been removed. Call
`LinearRgba::to_u32` instead, converting if needed.
- Several other color conversion methods to transform LCH or HSL colors
into float arrays or `Vec` types have been removed. Please reimplement
these externally or open a PR to re-add them if you found them
particularly useful.
- Various methods on `Color` such as `rgb` or `hsl` to convert the color
into a specific color space have been removed. Convert into
`LinearRgba`, then to the color space of your choice.
- Various implicitly-converting color value methods on `Color` such as
`r`, `g`, `b` or `h` have been removed. Please convert it into the color
space of your choice, then check these properties.
- `Color` no longer implements `AsBindGroup`. Store a `LinearRgba`
internally instead to avoid conversion costs.
---------
Co-authored-by: Alice Cecile <alice.i.cecil@gmail.com>
Co-authored-by: Afonso Lage <lage.afonso@gmail.com>
Co-authored-by: Rob Parrett <robparrett@gmail.com>
Co-authored-by: Zachary Harrold <zac@harrold.com.au>
2024-02-29 19:35:12 +00:00
/// GREEN
2024-02-28 00:18:26 +00:00
/// )
/// .skew_x(0.25)
2024-03-13 18:51:53 +00:00
/// .outer_edges();
2024-02-28 00:18:26 +00:00
/// }
/// # bevy_ecs::system::assert_is_system(system);
/// ```
pub fn grid (
& mut self ,
2024-08-28 01:37:19 +00:00
isometry : Isometry3d ,
2024-02-28 00:18:26 +00:00
cell_count : UVec2 ,
spacing : Vec2 ,
2024-08-23 02:54:45 +00:00
color : impl Into < Color > ,
2024-04-23 00:16:12 +00:00
) -> GridBuilder2d < '_ , ' w , ' s , Config , Clear > {
2024-03-13 18:51:53 +00:00
GridBuilder2d {
2024-02-28 00:18:26 +00:00
gizmos : self ,
2024-08-28 01:37:19 +00:00
isometry ,
2024-02-28 00:18:26 +00:00
spacing ,
cell_count ,
skew : Vec2 ::ZERO ,
2024-03-13 18:51:53 +00:00
outer_edges : [ false , false ] ,
color : color . into ( ) ,
}
}
/// Draw a 3D grid of voxel-like cells.
///
/// This should be called for each frame the grid needs to be rendered.
///
/// # Arguments
///
2024-08-28 01:37:19 +00:00
/// - `isometry` defines the translation and rotation of the grid.
/// - the translation specifies the center of the grid
/// - defines the orientation of the grid, by default
/// we assume the grid is aligned with all axes
2024-03-13 18:51:53 +00:00
/// - `cell_count`: defines the amount of cells in the x, y and z axes
/// - `spacing`: defines the distance between cells along the x, y and z axes
/// - `color`: color of the grid
///
/// # Builder methods
///
/// - 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.
/// - 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.
///
/// # Example
/// ```
/// # use bevy_gizmos::prelude::*;
/// # use bevy_math::prelude::*;
/// # use bevy_color::palettes::basic::GREEN;
/// fn system(mut gizmos: Gizmos) {
/// gizmos.grid_3d(
2024-08-28 01:37:19 +00:00
/// Isometry3d::IDENTITY,
2024-03-13 18:51:53 +00:00
/// UVec3::new(10, 2, 10),
/// Vec3::splat(2.),
/// GREEN
/// )
/// .skew_x(0.25)
/// .outer_edges();
/// }
/// # bevy_ecs::system::assert_is_system(system);
/// ```
pub fn grid_3d (
& mut self ,
2024-08-28 01:37:19 +00:00
isometry : Isometry3d ,
2024-03-13 18:51:53 +00:00
cell_count : UVec3 ,
spacing : Vec3 ,
2024-08-23 02:54:45 +00:00
color : impl Into < Color > ,
2024-04-23 00:16:12 +00:00
) -> GridBuilder3d < '_ , ' w , ' s , Config , Clear > {
2024-03-13 18:51:53 +00:00
GridBuilder3d {
gizmos : self ,
2024-08-28 01:37:19 +00:00
isometry ,
2024-03-13 18:51:53 +00:00
spacing ,
cell_count ,
skew : Vec3 ::ZERO ,
outer_edges : [ false , false , false ] ,
Migrate from `LegacyColor` to `bevy_color::Color` (#12163)
# Objective
- As part of the migration process we need to a) see the end effect of
the migration on user ergonomics b) check for serious perf regressions
c) actually migrate the code
- To accomplish this, I'm going to attempt to migrate all of the
remaining user-facing usages of `LegacyColor` in one PR, being careful
to keep a clean commit history.
- Fixes #12056.
## Solution
I've chosen to use the polymorphic `Color` type as our standard
user-facing API.
- [x] Migrate `bevy_gizmos`.
- [x] Take `impl Into<Color>` in all `bevy_gizmos` APIs
- [x] Migrate sprites
- [x] Migrate UI
- [x] Migrate `ColorMaterial`
- [x] Migrate `MaterialMesh2D`
- [x] Migrate fog
- [x] Migrate lights
- [x] Migrate StandardMaterial
- [x] Migrate wireframes
- [x] Migrate clear color
- [x] Migrate text
- [x] Migrate gltf loader
- [x] Register color types for reflection
- [x] Remove `LegacyColor`
- [x] Make sure CI passes
Incidental improvements to ease migration:
- added `Color::srgba_u8`, `Color::srgba_from_array` and friends
- added `set_alpha`, `is_fully_transparent` and `is_fully_opaque` to the
`Alpha` trait
- add and immediately deprecate (lol) `Color::rgb` and friends in favor
of more explicit and consistent `Color::srgb`
- standardized on white and black for most example text colors
- added vector field traits to `LinearRgba`: ~~`Add`, `Sub`,
`AddAssign`, `SubAssign`,~~ `Mul<f32>` and `Div<f32>`. Multiplications
and divisions do not scale alpha. `Add` and `Sub` have been cut from
this PR.
- added `LinearRgba` and `Srgba` `RED/GREEN/BLUE`
- added `LinearRgba_to_f32_array` and `LinearRgba::to_u32`
## Migration Guide
Bevy's color types have changed! Wherever you used a
`bevy::render::Color`, a `bevy::color::Color` is used instead.
These are quite similar! Both are enums storing a color in a specific
color space (or to be more precise, using a specific color model).
However, each of the different color models now has its own type.
TODO...
- `Color::rgba`, `Color::rgb`, `Color::rbga_u8`, `Color::rgb_u8`,
`Color::rgb_from_array` are now `Color::srgba`, `Color::srgb`,
`Color::srgba_u8`, `Color::srgb_u8` and `Color::srgb_from_array`.
- `Color::set_a` and `Color::a` is now `Color::set_alpha` and
`Color::alpha`. These are part of the `Alpha` trait in `bevy_color`.
- `Color::is_fully_transparent` is now part of the `Alpha` trait in
`bevy_color`
- `Color::r`, `Color::set_r`, `Color::with_r` and the equivalents for
`g`, `b` `h`, `s` and `l` have been removed due to causing silent
relatively expensive conversions. Convert your `Color` into the desired
color space, perform your operations there, and then convert it back
into a polymorphic `Color` enum.
- `Color::hex` is now `Srgba::hex`. Call `.into` or construct a
`Color::Srgba` variant manually to convert it.
- `WireframeMaterial`, `ExtractedUiNode`, `ExtractedDirectionalLight`,
`ExtractedPointLight`, `ExtractedSpotLight` and `ExtractedSprite` now
store a `LinearRgba`, rather than a polymorphic `Color`
- `Color::rgb_linear` and `Color::rgba_linear` are now
`Color::linear_rgb` and `Color::linear_rgba`
- The various CSS color constants are no longer stored directly on
`Color`. Instead, they're defined in the `Srgba` color space, and
accessed via `bevy::color::palettes::css`. Call `.into()` on them to
convert them into a `Color` for quick debugging use, and consider using
the much prettier `tailwind` palette for prototyping.
- The `LIME_GREEN` color has been renamed to `LIMEGREEN` to comply with
the standard naming.
- Vector field arithmetic operations on `Color` (add, subtract, multiply
and divide by a f32) have been removed. Instead, convert your colors
into `LinearRgba` space, and perform your operations explicitly there.
This is particularly relevant when working with emissive or HDR colors,
whose color channel values are routinely outside of the ordinary 0 to 1
range.
- `Color::as_linear_rgba_f32` has been removed. Call
`LinearRgba::to_f32_array` instead, converting if needed.
- `Color::as_linear_rgba_u32` has been removed. Call
`LinearRgba::to_u32` instead, converting if needed.
- Several other color conversion methods to transform LCH or HSL colors
into float arrays or `Vec` types have been removed. Please reimplement
these externally or open a PR to re-add them if you found them
particularly useful.
- Various methods on `Color` such as `rgb` or `hsl` to convert the color
into a specific color space have been removed. Convert into
`LinearRgba`, then to the color space of your choice.
- Various implicitly-converting color value methods on `Color` such as
`r`, `g`, `b` or `h` have been removed. Please convert it into the color
space of your choice, then check these properties.
- `Color` no longer implements `AsBindGroup`. Store a `LinearRgba`
internally instead to avoid conversion costs.
---------
Co-authored-by: Alice Cecile <alice.i.cecil@gmail.com>
Co-authored-by: Afonso Lage <lage.afonso@gmail.com>
Co-authored-by: Rob Parrett <robparrett@gmail.com>
Co-authored-by: Zachary Harrold <zac@harrold.com.au>
2024-02-29 19:35:12 +00:00
color : color . into ( ) ,
2024-02-28 00:18:26 +00:00
}
}
/// Draw a grid in 2D.
///
/// This should be called for each frame the grid needs to be rendered.
///
/// # Arguments
///
2024-08-28 01:37:19 +00:00
/// - `isometry` defines the translation and rotation of the grid.
/// - the translation specifies the center of the grid
/// - defines the orientation of the grid, by default
/// we assume the grid is aligned with all axes
2024-02-28 00:18:26 +00:00
/// - `cell_count`: defines the amount of cells in the x and y axes
/// - `spacing`: defines the distance between cells along the x and y axes
/// - `color`: color of the grid
///
/// # Builder methods
///
/// - 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.
2024-03-13 18:51:53 +00:00
/// - 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.
2024-02-28 00:18:26 +00:00
///
/// # Example
/// ```
/// # use bevy_gizmos::prelude::*;
/// # use bevy_math::prelude::*;
Migrate from `LegacyColor` to `bevy_color::Color` (#12163)
# Objective
- As part of the migration process we need to a) see the end effect of
the migration on user ergonomics b) check for serious perf regressions
c) actually migrate the code
- To accomplish this, I'm going to attempt to migrate all of the
remaining user-facing usages of `LegacyColor` in one PR, being careful
to keep a clean commit history.
- Fixes #12056.
## Solution
I've chosen to use the polymorphic `Color` type as our standard
user-facing API.
- [x] Migrate `bevy_gizmos`.
- [x] Take `impl Into<Color>` in all `bevy_gizmos` APIs
- [x] Migrate sprites
- [x] Migrate UI
- [x] Migrate `ColorMaterial`
- [x] Migrate `MaterialMesh2D`
- [x] Migrate fog
- [x] Migrate lights
- [x] Migrate StandardMaterial
- [x] Migrate wireframes
- [x] Migrate clear color
- [x] Migrate text
- [x] Migrate gltf loader
- [x] Register color types for reflection
- [x] Remove `LegacyColor`
- [x] Make sure CI passes
Incidental improvements to ease migration:
- added `Color::srgba_u8`, `Color::srgba_from_array` and friends
- added `set_alpha`, `is_fully_transparent` and `is_fully_opaque` to the
`Alpha` trait
- add and immediately deprecate (lol) `Color::rgb` and friends in favor
of more explicit and consistent `Color::srgb`
- standardized on white and black for most example text colors
- added vector field traits to `LinearRgba`: ~~`Add`, `Sub`,
`AddAssign`, `SubAssign`,~~ `Mul<f32>` and `Div<f32>`. Multiplications
and divisions do not scale alpha. `Add` and `Sub` have been cut from
this PR.
- added `LinearRgba` and `Srgba` `RED/GREEN/BLUE`
- added `LinearRgba_to_f32_array` and `LinearRgba::to_u32`
## Migration Guide
Bevy's color types have changed! Wherever you used a
`bevy::render::Color`, a `bevy::color::Color` is used instead.
These are quite similar! Both are enums storing a color in a specific
color space (or to be more precise, using a specific color model).
However, each of the different color models now has its own type.
TODO...
- `Color::rgba`, `Color::rgb`, `Color::rbga_u8`, `Color::rgb_u8`,
`Color::rgb_from_array` are now `Color::srgba`, `Color::srgb`,
`Color::srgba_u8`, `Color::srgb_u8` and `Color::srgb_from_array`.
- `Color::set_a` and `Color::a` is now `Color::set_alpha` and
`Color::alpha`. These are part of the `Alpha` trait in `bevy_color`.
- `Color::is_fully_transparent` is now part of the `Alpha` trait in
`bevy_color`
- `Color::r`, `Color::set_r`, `Color::with_r` and the equivalents for
`g`, `b` `h`, `s` and `l` have been removed due to causing silent
relatively expensive conversions. Convert your `Color` into the desired
color space, perform your operations there, and then convert it back
into a polymorphic `Color` enum.
- `Color::hex` is now `Srgba::hex`. Call `.into` or construct a
`Color::Srgba` variant manually to convert it.
- `WireframeMaterial`, `ExtractedUiNode`, `ExtractedDirectionalLight`,
`ExtractedPointLight`, `ExtractedSpotLight` and `ExtractedSprite` now
store a `LinearRgba`, rather than a polymorphic `Color`
- `Color::rgb_linear` and `Color::rgba_linear` are now
`Color::linear_rgb` and `Color::linear_rgba`
- The various CSS color constants are no longer stored directly on
`Color`. Instead, they're defined in the `Srgba` color space, and
accessed via `bevy::color::palettes::css`. Call `.into()` on them to
convert them into a `Color` for quick debugging use, and consider using
the much prettier `tailwind` palette for prototyping.
- The `LIME_GREEN` color has been renamed to `LIMEGREEN` to comply with
the standard naming.
- Vector field arithmetic operations on `Color` (add, subtract, multiply
and divide by a f32) have been removed. Instead, convert your colors
into `LinearRgba` space, and perform your operations explicitly there.
This is particularly relevant when working with emissive or HDR colors,
whose color channel values are routinely outside of the ordinary 0 to 1
range.
- `Color::as_linear_rgba_f32` has been removed. Call
`LinearRgba::to_f32_array` instead, converting if needed.
- `Color::as_linear_rgba_u32` has been removed. Call
`LinearRgba::to_u32` instead, converting if needed.
- Several other color conversion methods to transform LCH or HSL colors
into float arrays or `Vec` types have been removed. Please reimplement
these externally or open a PR to re-add them if you found them
particularly useful.
- Various methods on `Color` such as `rgb` or `hsl` to convert the color
into a specific color space have been removed. Convert into
`LinearRgba`, then to the color space of your choice.
- Various implicitly-converting color value methods on `Color` such as
`r`, `g`, `b` or `h` have been removed. Please convert it into the color
space of your choice, then check these properties.
- `Color` no longer implements `AsBindGroup`. Store a `LinearRgba`
internally instead to avoid conversion costs.
---------
Co-authored-by: Alice Cecile <alice.i.cecil@gmail.com>
Co-authored-by: Afonso Lage <lage.afonso@gmail.com>
Co-authored-by: Rob Parrett <robparrett@gmail.com>
Co-authored-by: Zachary Harrold <zac@harrold.com.au>
2024-02-29 19:35:12 +00:00
/// # use bevy_color::palettes::basic::GREEN;
2024-02-28 00:18:26 +00:00
/// fn system(mut gizmos: Gizmos) {
/// gizmos.grid_2d(
2024-08-28 01:37:19 +00:00
/// Isometry2d::IDENTITY,
2024-02-28 00:18:26 +00:00
/// UVec2::new(10, 10),
/// Vec2::splat(1.),
Migrate from `LegacyColor` to `bevy_color::Color` (#12163)
# Objective
- As part of the migration process we need to a) see the end effect of
the migration on user ergonomics b) check for serious perf regressions
c) actually migrate the code
- To accomplish this, I'm going to attempt to migrate all of the
remaining user-facing usages of `LegacyColor` in one PR, being careful
to keep a clean commit history.
- Fixes #12056.
## Solution
I've chosen to use the polymorphic `Color` type as our standard
user-facing API.
- [x] Migrate `bevy_gizmos`.
- [x] Take `impl Into<Color>` in all `bevy_gizmos` APIs
- [x] Migrate sprites
- [x] Migrate UI
- [x] Migrate `ColorMaterial`
- [x] Migrate `MaterialMesh2D`
- [x] Migrate fog
- [x] Migrate lights
- [x] Migrate StandardMaterial
- [x] Migrate wireframes
- [x] Migrate clear color
- [x] Migrate text
- [x] Migrate gltf loader
- [x] Register color types for reflection
- [x] Remove `LegacyColor`
- [x] Make sure CI passes
Incidental improvements to ease migration:
- added `Color::srgba_u8`, `Color::srgba_from_array` and friends
- added `set_alpha`, `is_fully_transparent` and `is_fully_opaque` to the
`Alpha` trait
- add and immediately deprecate (lol) `Color::rgb` and friends in favor
of more explicit and consistent `Color::srgb`
- standardized on white and black for most example text colors
- added vector field traits to `LinearRgba`: ~~`Add`, `Sub`,
`AddAssign`, `SubAssign`,~~ `Mul<f32>` and `Div<f32>`. Multiplications
and divisions do not scale alpha. `Add` and `Sub` have been cut from
this PR.
- added `LinearRgba` and `Srgba` `RED/GREEN/BLUE`
- added `LinearRgba_to_f32_array` and `LinearRgba::to_u32`
## Migration Guide
Bevy's color types have changed! Wherever you used a
`bevy::render::Color`, a `bevy::color::Color` is used instead.
These are quite similar! Both are enums storing a color in a specific
color space (or to be more precise, using a specific color model).
However, each of the different color models now has its own type.
TODO...
- `Color::rgba`, `Color::rgb`, `Color::rbga_u8`, `Color::rgb_u8`,
`Color::rgb_from_array` are now `Color::srgba`, `Color::srgb`,
`Color::srgba_u8`, `Color::srgb_u8` and `Color::srgb_from_array`.
- `Color::set_a` and `Color::a` is now `Color::set_alpha` and
`Color::alpha`. These are part of the `Alpha` trait in `bevy_color`.
- `Color::is_fully_transparent` is now part of the `Alpha` trait in
`bevy_color`
- `Color::r`, `Color::set_r`, `Color::with_r` and the equivalents for
`g`, `b` `h`, `s` and `l` have been removed due to causing silent
relatively expensive conversions. Convert your `Color` into the desired
color space, perform your operations there, and then convert it back
into a polymorphic `Color` enum.
- `Color::hex` is now `Srgba::hex`. Call `.into` or construct a
`Color::Srgba` variant manually to convert it.
- `WireframeMaterial`, `ExtractedUiNode`, `ExtractedDirectionalLight`,
`ExtractedPointLight`, `ExtractedSpotLight` and `ExtractedSprite` now
store a `LinearRgba`, rather than a polymorphic `Color`
- `Color::rgb_linear` and `Color::rgba_linear` are now
`Color::linear_rgb` and `Color::linear_rgba`
- The various CSS color constants are no longer stored directly on
`Color`. Instead, they're defined in the `Srgba` color space, and
accessed via `bevy::color::palettes::css`. Call `.into()` on them to
convert them into a `Color` for quick debugging use, and consider using
the much prettier `tailwind` palette for prototyping.
- The `LIME_GREEN` color has been renamed to `LIMEGREEN` to comply with
the standard naming.
- Vector field arithmetic operations on `Color` (add, subtract, multiply
and divide by a f32) have been removed. Instead, convert your colors
into `LinearRgba` space, and perform your operations explicitly there.
This is particularly relevant when working with emissive or HDR colors,
whose color channel values are routinely outside of the ordinary 0 to 1
range.
- `Color::as_linear_rgba_f32` has been removed. Call
`LinearRgba::to_f32_array` instead, converting if needed.
- `Color::as_linear_rgba_u32` has been removed. Call
`LinearRgba::to_u32` instead, converting if needed.
- Several other color conversion methods to transform LCH or HSL colors
into float arrays or `Vec` types have been removed. Please reimplement
these externally or open a PR to re-add them if you found them
particularly useful.
- Various methods on `Color` such as `rgb` or `hsl` to convert the color
into a specific color space have been removed. Convert into
`LinearRgba`, then to the color space of your choice.
- Various implicitly-converting color value methods on `Color` such as
`r`, `g`, `b` or `h` have been removed. Please convert it into the color
space of your choice, then check these properties.
- `Color` no longer implements `AsBindGroup`. Store a `LinearRgba`
internally instead to avoid conversion costs.
---------
Co-authored-by: Alice Cecile <alice.i.cecil@gmail.com>
Co-authored-by: Afonso Lage <lage.afonso@gmail.com>
Co-authored-by: Rob Parrett <robparrett@gmail.com>
Co-authored-by: Zachary Harrold <zac@harrold.com.au>
2024-02-29 19:35:12 +00:00
/// GREEN
2024-02-28 00:18:26 +00:00
/// )
/// .skew_x(0.25)
2024-03-13 18:51:53 +00:00
/// .outer_edges();
2024-02-28 00:18:26 +00:00
/// }
/// # bevy_ecs::system::assert_is_system(system);
/// ```
pub fn grid_2d (
& mut self ,
2024-08-28 01:37:19 +00:00
isometry : Isometry2d ,
2024-02-28 00:18:26 +00:00
cell_count : UVec2 ,
spacing : Vec2 ,
2024-08-23 02:54:45 +00:00
color : impl Into < Color > ,
2024-04-23 00:16:12 +00:00
) -> GridBuilder2d < '_ , ' w , ' s , Config , Clear > {
2024-03-13 18:51:53 +00:00
GridBuilder2d {
2024-02-28 00:18:26 +00:00
gizmos : self ,
2024-08-28 01:37:19 +00:00
isometry : Isometry3d ::new (
isometry . translation . extend ( 0.0 ) ,
Quat ::from_rotation_z ( isometry . rotation . as_radians ( ) ) ,
) ,
2024-02-28 00:18:26 +00:00
spacing ,
cell_count ,
skew : Vec2 ::ZERO ,
2024-03-13 18:51:53 +00:00
outer_edges : [ false , false ] ,
Migrate from `LegacyColor` to `bevy_color::Color` (#12163)
# Objective
- As part of the migration process we need to a) see the end effect of
the migration on user ergonomics b) check for serious perf regressions
c) actually migrate the code
- To accomplish this, I'm going to attempt to migrate all of the
remaining user-facing usages of `LegacyColor` in one PR, being careful
to keep a clean commit history.
- Fixes #12056.
## Solution
I've chosen to use the polymorphic `Color` type as our standard
user-facing API.
- [x] Migrate `bevy_gizmos`.
- [x] Take `impl Into<Color>` in all `bevy_gizmos` APIs
- [x] Migrate sprites
- [x] Migrate UI
- [x] Migrate `ColorMaterial`
- [x] Migrate `MaterialMesh2D`
- [x] Migrate fog
- [x] Migrate lights
- [x] Migrate StandardMaterial
- [x] Migrate wireframes
- [x] Migrate clear color
- [x] Migrate text
- [x] Migrate gltf loader
- [x] Register color types for reflection
- [x] Remove `LegacyColor`
- [x] Make sure CI passes
Incidental improvements to ease migration:
- added `Color::srgba_u8`, `Color::srgba_from_array` and friends
- added `set_alpha`, `is_fully_transparent` and `is_fully_opaque` to the
`Alpha` trait
- add and immediately deprecate (lol) `Color::rgb` and friends in favor
of more explicit and consistent `Color::srgb`
- standardized on white and black for most example text colors
- added vector field traits to `LinearRgba`: ~~`Add`, `Sub`,
`AddAssign`, `SubAssign`,~~ `Mul<f32>` and `Div<f32>`. Multiplications
and divisions do not scale alpha. `Add` and `Sub` have been cut from
this PR.
- added `LinearRgba` and `Srgba` `RED/GREEN/BLUE`
- added `LinearRgba_to_f32_array` and `LinearRgba::to_u32`
## Migration Guide
Bevy's color types have changed! Wherever you used a
`bevy::render::Color`, a `bevy::color::Color` is used instead.
These are quite similar! Both are enums storing a color in a specific
color space (or to be more precise, using a specific color model).
However, each of the different color models now has its own type.
TODO...
- `Color::rgba`, `Color::rgb`, `Color::rbga_u8`, `Color::rgb_u8`,
`Color::rgb_from_array` are now `Color::srgba`, `Color::srgb`,
`Color::srgba_u8`, `Color::srgb_u8` and `Color::srgb_from_array`.
- `Color::set_a` and `Color::a` is now `Color::set_alpha` and
`Color::alpha`. These are part of the `Alpha` trait in `bevy_color`.
- `Color::is_fully_transparent` is now part of the `Alpha` trait in
`bevy_color`
- `Color::r`, `Color::set_r`, `Color::with_r` and the equivalents for
`g`, `b` `h`, `s` and `l` have been removed due to causing silent
relatively expensive conversions. Convert your `Color` into the desired
color space, perform your operations there, and then convert it back
into a polymorphic `Color` enum.
- `Color::hex` is now `Srgba::hex`. Call `.into` or construct a
`Color::Srgba` variant manually to convert it.
- `WireframeMaterial`, `ExtractedUiNode`, `ExtractedDirectionalLight`,
`ExtractedPointLight`, `ExtractedSpotLight` and `ExtractedSprite` now
store a `LinearRgba`, rather than a polymorphic `Color`
- `Color::rgb_linear` and `Color::rgba_linear` are now
`Color::linear_rgb` and `Color::linear_rgba`
- The various CSS color constants are no longer stored directly on
`Color`. Instead, they're defined in the `Srgba` color space, and
accessed via `bevy::color::palettes::css`. Call `.into()` on them to
convert them into a `Color` for quick debugging use, and consider using
the much prettier `tailwind` palette for prototyping.
- The `LIME_GREEN` color has been renamed to `LIMEGREEN` to comply with
the standard naming.
- Vector field arithmetic operations on `Color` (add, subtract, multiply
and divide by a f32) have been removed. Instead, convert your colors
into `LinearRgba` space, and perform your operations explicitly there.
This is particularly relevant when working with emissive or HDR colors,
whose color channel values are routinely outside of the ordinary 0 to 1
range.
- `Color::as_linear_rgba_f32` has been removed. Call
`LinearRgba::to_f32_array` instead, converting if needed.
- `Color::as_linear_rgba_u32` has been removed. Call
`LinearRgba::to_u32` instead, converting if needed.
- Several other color conversion methods to transform LCH or HSL colors
into float arrays or `Vec` types have been removed. Please reimplement
these externally or open a PR to re-add them if you found them
particularly useful.
- Various methods on `Color` such as `rgb` or `hsl` to convert the color
into a specific color space have been removed. Convert into
`LinearRgba`, then to the color space of your choice.
- Various implicitly-converting color value methods on `Color` such as
`r`, `g`, `b` or `h` have been removed. Please convert it into the color
space of your choice, then check these properties.
- `Color` no longer implements `AsBindGroup`. Store a `LinearRgba`
internally instead to avoid conversion costs.
---------
Co-authored-by: Alice Cecile <alice.i.cecil@gmail.com>
Co-authored-by: Afonso Lage <lage.afonso@gmail.com>
Co-authored-by: Rob Parrett <robparrett@gmail.com>
Co-authored-by: Zachary Harrold <zac@harrold.com.au>
2024-02-29 19:35:12 +00:00
color : color . into ( ) ,
2024-02-28 00:18:26 +00:00
}
}
}
2024-03-13 18:51:53 +00:00
#[ allow(clippy::too_many_arguments) ]
2024-04-23 00:16:12 +00:00
fn draw_grid < Config , Clear > (
gizmos : & mut Gizmos < '_ , '_ , Config , Clear > ,
2024-08-28 01:37:19 +00:00
isometry : Isometry3d ,
2024-03-13 18:51:53 +00:00
spacing : Vec3 ,
cell_count : UVec3 ,
skew : Vec3 ,
outer_edges : [ bool ; 3 ] ,
2024-08-23 02:54:45 +00:00
color : Color ,
2024-04-23 00:16:12 +00:00
) where
Config : GizmoConfigGroup ,
Clear : 'static + Send + Sync ,
{
2024-03-13 18:51:53 +00:00
if ! gizmos . enabled {
return ;
}
2024-08-16 23:40:06 +00:00
#[ inline ]
fn or_zero ( cond : bool , val : Vec3 ) -> Vec3 {
if cond {
val
} else {
Vec3 ::ZERO
}
}
2024-03-13 18:51:53 +00:00
// Offset between two adjacent grid cells along the x/y-axis and accounting for skew.
2024-08-16 23:40:06 +00:00
let skew_tan = Vec3 ::from ( skew . to_array ( ) . map ( f32 ::tan ) ) ;
let dx = or_zero (
cell_count . x ! = 0 ,
spacing . x * Vec3 ::new ( 1. , skew_tan . y , skew_tan . z ) ,
) ;
let dy = or_zero (
cell_count . y ! = 0 ,
spacing . y * Vec3 ::new ( skew_tan . x , 1. , skew_tan . z ) ,
) ;
let dz = or_zero (
cell_count . z ! = 0 ,
spacing . z * Vec3 ::new ( skew_tan . x , skew_tan . y , 1. ) ,
) ;
2024-03-13 18:51:53 +00:00
// Bottom-left-front corner of the grid
2024-08-16 23:40:06 +00:00
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 ;
2024-03-13 18:51:53 +00:00
2024-08-16 23:40:06 +00:00
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 ) ;
2024-03-13 18:51:53 +00:00
2024-08-16 23:40:06 +00:00
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 z_start = grid_start + or_zero ( ! outer_edges [ 2 ] , dx + dy ) ;
2024-03-13 18:51:53 +00:00
2024-08-16 23:40:06 +00:00
fn iter_lines (
delta_a : Vec3 ,
delta_b : Vec3 ,
delta_c : Vec3 ,
line_count : UVec2 ,
cell_count : u32 ,
start : Vec3 ,
) -> impl Iterator < Item = [ Vec3 ; 2 ] > {
let dline = delta_a * cell_count as f32 ;
( 0 .. line_count . x ) . map ( | v | v as f32 ) . flat_map ( move | b | {
( 0 .. line_count . y ) . map ( | v | v as f32 ) . map ( move | c | {
let line_start = start + b * delta_b + c * delta_c ;
let line_end = line_start + dline ;
[ line_start , line_end ]
} )
} )
2024-03-13 18:51:53 +00:00
}
2024-08-16 23:40:06 +00:00
// Lines along the x direction
let x_lines = iter_lines ( dx , dy , dz , line_count . yz ( ) , 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 ) ;
2024-03-13 18:51:53 +00:00
// Lines along the z direction
2024-08-16 23:40:06 +00:00
let z_lines = iter_lines ( dz , dx , dy , line_count . xy ( ) , cell_count . z , z_start ) ;
x_lines
. chain ( y_lines )
. chain ( z_lines )
2024-08-28 01:37:19 +00:00
. map ( | vec3s | vec3s . map ( | vec3 | isometry * vec3 ) )
2024-08-16 23:40:06 +00:00
. for_each ( | [ start , end ] | {
gizmos . line ( start , end , color ) ;
} ) ;
2024-03-13 18:51:53 +00:00
}