Add more Glam types and constructors to prelude (#16261)

# Objective

Glam has some common and useful types and helpers that are not in the
prelude of `bevy_math`. This includes shorthand constructors like
`vec3`, or even `Vec3A`, the aligned version of `Vec3`.

```rust
// The "normal" way to create a 3D vector
let vec = Vec3::new(2.0, 1.0, -3.0);

// Shorthand version
let vec = vec3(2.0, 1.0, -3.0);
```

## Solution

Add the following types and methods to the prelude:

- `vec2`, `vec3`, `vec3a`, `vec4`
- `uvec2`, `uvec3`, `uvec4`
- `ivec2`, `ivec3`, `ivec4`
- `bvec2`, `bvec3`, `bvec3a`, `bvec4`, `bvec4a`
- `mat2`, `mat3`, `mat3a`, `mat4`
- `quat` (not sure if anyone uses this, but for consistency)
- `Vec3A`
- `BVec3A`, `BVec4A`
- `Mat3A`

I did not add the u16, i16, or f64 variants like `dvec2`, since there
are currently no existing types like those in the prelude.

The shorthand constructors are currently used a lot in some places in
Bevy, and not at all in others. In a follow-up, we might want to
consider if we have a preference for the shorthand, and make a PR to
change the codebase to use it more consistently.
This commit is contained in:
Joona Aalto 2024-11-11 20:47:16 +02:00 committed by GitHub
parent b83c0e106e
commit 3ada15ee1c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 8 additions and 10 deletions

View file

@ -1,6 +1,6 @@
use criterion::{black_box, criterion_group, criterion_main, Criterion}; use criterion::{black_box, criterion_group, criterion_main, Criterion};
use bevy_math::{prelude::*, *}; use bevy_math::prelude::*;
fn easing(c: &mut Criterion) { fn easing(c: &mut Criterion) {
let cubic_bezier = CubicSegment::new_bezier(vec2(0.25, 0.1), vec2(0.25, 1.0)); let cubic_bezier = CubicSegment::new_bezier(vec2(0.25, 0.1), vec2(0.25, 1.0));

View file

@ -1,7 +1,5 @@
//! Contains [`Bounded3d`] implementations for [geometric primitives](crate::primitives). //! Contains [`Bounded3d`] implementations for [geometric primitives](crate::primitives).
use glam::Vec3A;
use crate::{ use crate::{
bounding::{Bounded2d, BoundingCircle}, bounding::{Bounded2d, BoundingCircle},
ops, ops,
@ -9,7 +7,7 @@ use crate::{
BoxedPolyline3d, Capsule3d, Cone, ConicalFrustum, Cuboid, Cylinder, InfinitePlane3d, BoxedPolyline3d, Capsule3d, Cone, ConicalFrustum, Cuboid, Cylinder, InfinitePlane3d,
Line3d, Polyline3d, Segment3d, Sphere, Torus, Triangle2d, Triangle3d, Line3d, Polyline3d, Segment3d, Sphere, Torus, Triangle2d, Triangle3d,
}, },
Isometry2d, Isometry3d, Mat3, Vec2, Vec3, Isometry2d, Isometry3d, Mat3, Vec2, Vec3, Vec3A,
}; };
use super::{Aabb3d, Bounded3d, BoundingSphere}; use super::{Aabb3d, Bounded3d, BoundingSphere};

View file

@ -58,17 +58,19 @@ pub use sampling::{FromRng, ShapeSample};
pub mod prelude { pub mod prelude {
#[doc(hidden)] #[doc(hidden)]
pub use crate::{ pub use crate::{
bvec2, bvec3, bvec3a, bvec4, bvec4a,
cubic_splines::{ cubic_splines::{
CubicBSpline, CubicBezier, CubicCardinalSpline, CubicCurve, CubicGenerator, CubicBSpline, CubicBezier, CubicCardinalSpline, CubicCurve, CubicGenerator,
CubicHermite, CubicNurbs, CubicNurbsError, CubicSegment, CyclicCubicGenerator, CubicHermite, CubicNurbs, CubicNurbsError, CubicSegment, CyclicCubicGenerator,
RationalCurve, RationalGenerator, RationalSegment, RationalCurve, RationalGenerator, RationalSegment,
}, },
direction::{Dir2, Dir3, Dir3A}, direction::{Dir2, Dir3, Dir3A},
ops, ivec2, ivec3, ivec4, mat2, mat3, mat3a, mat4, ops,
primitives::*, primitives::*,
BVec2, BVec3, BVec4, EulerRot, FloatExt, IRect, IVec2, IVec3, IVec4, Isometry2d, quat, uvec2, uvec3, uvec4, vec2, vec3, vec3a, vec4, BVec2, BVec3, BVec3A, BVec4, BVec4A,
Isometry3d, Mat2, Mat3, Mat4, Quat, Ray2d, Ray3d, Rect, Rot2, StableInterpolate, URect, EulerRot, FloatExt, IRect, IVec2, IVec3, IVec4, Isometry2d, Isometry3d, Mat2, Mat3, Mat3A,
UVec2, UVec3, UVec4, Vec2, Vec2Swizzles, Vec3, Vec3Swizzles, Vec4, Vec4Swizzles, Mat4, Quat, Ray2d, Ray3d, Rect, Rot2, StableInterpolate, URect, UVec2, UVec3, UVec4, Vec2,
Vec2Swizzles, Vec3, Vec3A, Vec3Swizzles, Vec4, Vec4Swizzles,
}; };
#[doc(hidden)] #[doc(hidden)]

View file

@ -13,7 +13,6 @@ use bevy::{
query::ROQueryItem, query::ROQueryItem,
system::{lifetimeless::SRes, SystemParamItem}, system::{lifetimeless::SRes, SystemParamItem},
}, },
math::{vec3, Vec3A},
prelude::*, prelude::*,
render::{ render::{
extract_component::{ExtractComponent, ExtractComponentPlugin}, extract_component::{ExtractComponent, ExtractComponentPlugin},

View file

@ -8,7 +8,6 @@
//! If you want to hot reload asset changes, enable the `file_watcher` cargo feature. //! If you want to hot reload asset changes, enable the `file_watcher` cargo feature.
use bevy::{ use bevy::{
math::Vec3A,
prelude::*, prelude::*,
render::primitives::{Aabb, Sphere}, render::primitives::{Aabb, Sphere},
}; };