From 129b657987d22d9fbbd2eeab182f85eb2281dbcb Mon Sep 17 00:00:00 2001 From: Nicola Papale Date: Sun, 16 Jul 2023 16:19:29 +0200 Subject: [PATCH 1/4] Use a good sequence for entity AABB colors --- crates/bevy_gizmos/src/lib.rs | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/crates/bevy_gizmos/src/lib.rs b/crates/bevy_gizmos/src/lib.rs index 15e99651092a1..9a9a49d733ab9 100644 --- a/crates/bevy_gizmos/src/lib.rs +++ b/crates/bevy_gizmos/src/lib.rs @@ -232,15 +232,21 @@ fn draw_all_aabbs( } } +fn gold_kronecker(bits: u64) -> f32 { + // from https://extremelearning.com.au/unreasonable-effectiveness-of-quasirandom-sequences/ + // + // See https://en.wikipedia.org/wiki/Low-discrepancy_sequence + // Map a sequence of integers (eg: 154, 155, 156, 157, 158) into the [0.0..1.0] range, + // so that the closer the numbers are, the larger the difference of their image. + + // (u64::MAX / Φ) rounded down + // see https://probablydance.com/2018/06/16/fibonacci-hashing-the-optimization-that-the-world-forgot-or-a-better-alternative-to-integer-modulo/ + const FRAC_U64MAX_GOLDEN_RATIO: u64 = 11400714819323198485; + const RATIO_360: f32 = u64::MAX as f32 / 360.0; + bits.wrapping_mul(FRAC_U64MAX_GOLDEN_RATIO) as f32 / RATIO_360 +} fn color_from_entity(entity: Entity) -> Color { - use bevy_utils::RandomState; - const U64_TO_DEGREES: f32 = 360.0 / u64::MAX as f32; - const STATE: RandomState = - RandomState::with_seeds(5952553601252303067, 16866614500153072625, 0, 0); - - let hash = STATE.hash_one(entity); - let hue = hash as f32 * U64_TO_DEGREES; - Color::hsl(hue, 1., 0.5) + Color::hsl(gold_kronecker(entity.to_bits()), 1., 0.5) } fn aabb_transform(aabb: Aabb, transform: GlobalTransform) -> GlobalTransform { From a7df420bb29fc1804da777eb405f0c69c210d89e Mon Sep 17 00:00:00 2001 From: Nicola Papale Date: Sun, 16 Jul 2023 19:21:28 +0200 Subject: [PATCH 2/4] Use index u32 over to_bits --- crates/bevy_gizmos/src/lib.rs | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/crates/bevy_gizmos/src/lib.rs b/crates/bevy_gizmos/src/lib.rs index 9a9a49d733ab9..7ea99bb272930 100644 --- a/crates/bevy_gizmos/src/lib.rs +++ b/crates/bevy_gizmos/src/lib.rs @@ -232,21 +232,18 @@ fn draw_all_aabbs( } } -fn gold_kronecker(bits: u64) -> f32 { +fn gold_kronecker(bits: u32) -> f32 { // from https://extremelearning.com.au/unreasonable-effectiveness-of-quasirandom-sequences/ // // See https://en.wikipedia.org/wiki/Low-discrepancy_sequence // Map a sequence of integers (eg: 154, 155, 156, 157, 158) into the [0.0..1.0] range, // so that the closer the numbers are, the larger the difference of their image. - - // (u64::MAX / Φ) rounded down - // see https://probablydance.com/2018/06/16/fibonacci-hashing-the-optimization-that-the-world-forgot-or-a-better-alternative-to-integer-modulo/ - const FRAC_U64MAX_GOLDEN_RATIO: u64 = 11400714819323198485; - const RATIO_360: f32 = u64::MAX as f32 / 360.0; - bits.wrapping_mul(FRAC_U64MAX_GOLDEN_RATIO) as f32 / RATIO_360 + const FRAC_U32MAX_GOLDEN_RATIO: u32 = 2654435769; // (u32::MAX / Φ) rounded up + const RATIO_360: f32 = u32::MAX as f32 / 360.0; + bits.wrapping_mul(FRAC_U32MAX_GOLDEN_RATIO) as f32 / RATIO_360 } fn color_from_entity(entity: Entity) -> Color { - Color::hsl(gold_kronecker(entity.to_bits()), 1., 0.5) + Color::hsl(gold_kronecker(entity.index()), 1., 0.5) } fn aabb_transform(aabb: Aabb, transform: GlobalTransform) -> GlobalTransform { From b18adef8d307d1502966592fde74a5e4ffb41427 Mon Sep 17 00:00:00 2001 From: Nicola Papale Date: Sun, 16 Jul 2023 19:38:49 +0200 Subject: [PATCH 3/4] Use multiplication over division --- crates/bevy_gizmos/src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/bevy_gizmos/src/lib.rs b/crates/bevy_gizmos/src/lib.rs index 7ea99bb272930..9d8a49de005fc 100644 --- a/crates/bevy_gizmos/src/lib.rs +++ b/crates/bevy_gizmos/src/lib.rs @@ -239,8 +239,8 @@ fn gold_kronecker(bits: u32) -> f32 { // Map a sequence of integers (eg: 154, 155, 156, 157, 158) into the [0.0..1.0] range, // so that the closer the numbers are, the larger the difference of their image. const FRAC_U32MAX_GOLDEN_RATIO: u32 = 2654435769; // (u32::MAX / Φ) rounded up - const RATIO_360: f32 = u32::MAX as f32 / 360.0; - bits.wrapping_mul(FRAC_U32MAX_GOLDEN_RATIO) as f32 / RATIO_360 + const RATIO_360: f32 = 360.0 / u32::MAX as f32; + bits.wrapping_mul(FRAC_U32MAX_GOLDEN_RATIO) as f32 * RATIO_360 } fn color_from_entity(entity: Entity) -> Color { Color::hsl(gold_kronecker(entity.index()), 1., 0.5) From 5b41bdce8c6af99b3d08b88499aeefce649e5358 Mon Sep 17 00:00:00 2001 From: Nicola Papale Date: Fri, 21 Jul 2023 09:02:50 +0200 Subject: [PATCH 4/4] Merge gold_kronecker & color_from_entity --- crates/bevy_gizmos/src/lib.rs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/crates/bevy_gizmos/src/lib.rs b/crates/bevy_gizmos/src/lib.rs index 9d8a49de005fc..3faf6c6305ca4 100644 --- a/crates/bevy_gizmos/src/lib.rs +++ b/crates/bevy_gizmos/src/lib.rs @@ -232,7 +232,9 @@ fn draw_all_aabbs( } } -fn gold_kronecker(bits: u32) -> f32 { +fn color_from_entity(entity: Entity) -> Color { + let index = entity.index(); + // from https://extremelearning.com.au/unreasonable-effectiveness-of-quasirandom-sequences/ // // See https://en.wikipedia.org/wiki/Low-discrepancy_sequence @@ -240,10 +242,9 @@ fn gold_kronecker(bits: u32) -> f32 { // so that the closer the numbers are, the larger the difference of their image. const FRAC_U32MAX_GOLDEN_RATIO: u32 = 2654435769; // (u32::MAX / Φ) rounded up const RATIO_360: f32 = 360.0 / u32::MAX as f32; - bits.wrapping_mul(FRAC_U32MAX_GOLDEN_RATIO) as f32 * RATIO_360 -} -fn color_from_entity(entity: Entity) -> Color { - Color::hsl(gold_kronecker(entity.index()), 1., 0.5) + let hue = index.wrapping_mul(FRAC_U32MAX_GOLDEN_RATIO) as f32 * RATIO_360; + + Color::hsl(hue, 1., 0.5) } fn aabb_transform(aabb: Aabb, transform: GlobalTransform) -> GlobalTransform {