From bb16087b0b4b4e66cc12b24620546c2951d6774a Mon Sep 17 00:00:00 2001 From: Jerome Humbert Date: Fri, 1 Mar 2024 21:44:01 +0000 Subject: [PATCH] Change `VectorValue::new_uvecX()` to take `UVecX` (#290) Change the signature of the `VectorValue::new_uvecX()` functions to directly take a glam math type `UVecX`, like all other vectors of other scalar types. --- CHANGELOG.md | 1 + src/graph/mod.rs | 102 +++++++++++++---------------------------------- 2 files changed, 28 insertions(+), 75 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ac80368d..e76b21bf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - `ExprHandle` is now `#[repr(transparent)]`, which guarantees that `Option` has the same size as `ExprHandle` itself (4 bytes). - `EffectProperties::set_if_changed()` now returns the `Mut` variable it takes as input, to allow subsequent calls. +- `VectorValue::new_uvecX()` now take a `UVecX` instead of individual components, like for all other scalar types. ### Removed diff --git a/src/graph/mod.rs b/src/graph/mod.rs index a803ac79..b757f633 100644 --- a/src/graph/mod.rs +++ b/src/graph/mod.rs @@ -42,7 +42,10 @@ use std::fmt::Debug; use bevy::{ - math::{BVec2, BVec3, BVec4, IVec2, IVec3, IVec4, Mat2, Mat3, Mat4, Vec2, Vec3, Vec3A, Vec4}, + math::{ + BVec2, BVec3, BVec4, IVec2, IVec3, IVec4, Mat2, Mat3, Mat4, UVec2, UVec3, UVec4, Vec2, + Vec3, Vec3A, Vec4, + }, reflect::Reflect, utils::FloatOrd, }; @@ -191,13 +194,7 @@ impl ScalarValue { /// not guaranteed to be stable. fn as_storage(&self) -> u32 { match self { - ScalarValue::Bool(b) => { - if *b { - Self::BOOL_TRUE_STORAGE - } else { - 0u32 - } - } + ScalarValue::Bool(b) => Self::BOOL_STORAGE[*b as usize], ScalarValue::Float(f) => bytemuck::cast::(*f), ScalarValue::Int(i) => bytemuck::cast::(*i), ScalarValue::Uint(u) => *u, @@ -452,16 +449,8 @@ impl VectorValue { Self { vector_type: VectorType::VEC2B, storage: [ - if value.x { - ScalarValue::BOOL_TRUE_STORAGE - } else { - 0u32 - }, - if value.y { - ScalarValue::BOOL_TRUE_STORAGE - } else { - 0u32 - }, + ScalarValue::BOOL_STORAGE[value.x as usize], + ScalarValue::BOOL_STORAGE[value.y as usize], 0u32, 0u32, ], @@ -474,21 +463,9 @@ impl VectorValue { Self { vector_type: VectorType::VEC3B, storage: [ - if value.x { - ScalarValue::BOOL_TRUE_STORAGE - } else { - 0u32 - }, - if value.y { - ScalarValue::BOOL_TRUE_STORAGE - } else { - 0u32 - }, - if value.z { - ScalarValue::BOOL_TRUE_STORAGE - } else { - 0u32 - }, + ScalarValue::BOOL_STORAGE[value.x as usize], + ScalarValue::BOOL_STORAGE[value.y as usize], + ScalarValue::BOOL_STORAGE[value.z as usize], 0u32, ], } @@ -500,26 +477,10 @@ impl VectorValue { Self { vector_type: VectorType::VEC4B, storage: [ - if value.x { - ScalarValue::BOOL_TRUE_STORAGE - } else { - 0u32 - }, - if value.y { - ScalarValue::BOOL_TRUE_STORAGE - } else { - 0u32 - }, - if value.z { - ScalarValue::BOOL_TRUE_STORAGE - } else { - 0u32 - }, - if value.w { - ScalarValue::BOOL_TRUE_STORAGE - } else { - 0u32 - }, + ScalarValue::BOOL_STORAGE[value.x as usize], + ScalarValue::BOOL_STORAGE[value.y as usize], + ScalarValue::BOOL_STORAGE[value.z as usize], + ScalarValue::BOOL_STORAGE[value.w as usize], ], } } @@ -598,36 +559,27 @@ impl VectorValue { } } - /// Create a new [`VectorValue`] from a 2D vector of `u32`. - /// - /// Note that due to the lack of `UVec2` in glam, this method takes - /// individual vector components instead. - pub const fn new_uvec2(x: u32, y: u32) -> Self { + /// Workaround for `impl const From`. + pub const fn new_uvec2(v: UVec2) -> Self { Self { vector_type: VectorType::VEC2U, - storage: [x, y, 0, 0], + storage: [v.x, v.y, 0, 0], } } - /// Create a new [`VectorValue`] from a 3D vector of `u32`. - /// - /// Note that due to the lack of `UVec3` in glam, this method takes - /// individual vector components instead. - pub const fn new_uvec3(x: u32, y: u32, z: u32) -> Self { + /// Workaround for `impl const From`. + pub const fn new_uvec3(v: UVec3) -> Self { Self { vector_type: VectorType::VEC3U, - storage: [x, y, z, 0], + storage: [v.x, v.y, v.z, 0], } } - /// Create a new [`VectorValue`] from a 4D vector of `u32`. - /// - /// Note that due to the lack of `UVec4` in glam, this method takes - /// individual vector components instead. - pub const fn new_uvec4(x: u32, y: u32, z: u32, w: u32) -> Self { + /// Workaround for `impl const From`. + pub const fn new_uvec4(v: UVec4) -> Self { Self { vector_type: VectorType::VEC4U, - storage: [x, y, z, w], + storage: v.to_array(), } } @@ -1932,7 +1884,7 @@ mod tests { let vecs = [ VectorValue::new_vec2(Vec2::new(1., 0.)), VectorValue::new_ivec2(IVec2::new(1, 0)), - VectorValue::new_uvec2(1, 0), + VectorValue::new_uvec2(UVec2::new(1, 0)), VectorValue::new_bvec2(BVec2::new(true, false)), ]; for i in 0..=3 { @@ -1994,15 +1946,15 @@ mod tests { ); assert_eq!( - calc_hash(&VectorValue::new_uvec2(3, 42)), + calc_hash(&VectorValue::new_uvec2(UVec2::new(3, 42))), calc_u32_vector_hash(VectorType::VEC2U, &[3, 42]) ); assert_eq!( - calc_hash(&VectorValue::new_uvec3(3, 42, 999)), + calc_hash(&VectorValue::new_uvec3(UVec3::new(3, 42, 999))), calc_u32_vector_hash(VectorType::VEC3U, &[3, 42, 999]) ); assert_eq!( - calc_hash(&VectorValue::new_uvec4(3, 42, 999, 1)), + calc_hash(&VectorValue::new_uvec4(UVec4::new(3, 42, 999, 1))), calc_u32_vector_hash(VectorType::VEC4U, &[3, 42, 999, 1]) ); }