Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace AHash with a good sequence for entity AABB colors #9175

Merged
merged 4 commits into from
Jul 21, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions crates/bevy_gizmos/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
nicopap marked this conversation as resolved.
Show resolved Hide resolved
}

fn aabb_transform(aabb: Aabb, transform: GlobalTransform) -> GlobalTransform {
Expand Down