use super::*;
pub(crate) fn splitmix64_next(state: &mut u64) -> u64 {
*state = state.wrapping_add(0x9E37_79B9_7F4A_7C15);
let mut z: u64 = *state;
z = (z ^ (z >> 30)).wrapping_mul(0xBF58_476D_1CE4_E5B9);
z = (z ^ (z >> 27)).wrapping_mul(0x94D0_49BB_1331_11EB);
z ^ (z >> 31)
}
pub(crate) fn noise_fade(t: f32) -> f32 {
t * t * t * (t * (t * 6.0 - 15.0) + 10.0)
}
pub(crate) fn noise_lerp(a: f32, b: f32, t: f32) -> f32 {
a + t * (b - a)
}
pub(crate) fn grad_2d(hash: u8, x: f32, y: f32) -> f32 {
match hash & 7 {
0 => x + y,
1 => -x + y,
2 => x - y,
3 => -x - y,
4 => x,
5 => -x,
6 => y,
_ => -y,
}
}
pub(crate) fn grad_3d(hash: u8, x: f32, y: f32, z: f32) -> f32 {
let h: u8 = hash & 15;
let u: f32 = if h < 8 { x } else { y };
let v: f32 = if h < 4 {
y
} else if h == 12 || h == 14 {
x
} else {
z
};
let first: f32 = if (h & 1) == 0 { u } else { -u };
let second: f32 = if (h & 2) == 0 { v } else { -v };
first + second
}
pub(crate) fn worley_hash_2d(cx: i32, cy: i32, seed: u64) -> (f32, f32) {
let mut s: u64 = seed
^ (cx as i64 as u64).wrapping_mul(0x9E37_79B9_7F4A_7C15)
^ (cy as i64 as u64).wrapping_mul(0xBF58_476D_1CE4_E5B9);
let h1: u64 = splitmix64_next(&mut s);
let h2: u64 = splitmix64_next(&mut s);
let fx: f32 = ((h1 >> 40) as f32) * (1.0 / 16_777_216.0);
let fy: f32 = ((h2 >> 40) as f32) * (1.0 / 16_777_216.0);
(fx, fy)
}
pub(crate) fn worley_hash_3d(cx: i32, cy: i32, cz: i32, seed: u64) -> (f32, f32, f32) {
let mut s: u64 = seed
^ (cx as i64 as u64).wrapping_mul(0x9E37_79B9_7F4A_7C15)
^ (cy as i64 as u64).wrapping_mul(0xBF58_476D_1CE4_E5B9)
^ (cz as i64 as u64).wrapping_mul(0x94D0_49BB_1331_11EB);
let h1: u64 = splitmix64_next(&mut s);
let h2: u64 = splitmix64_next(&mut s);
let h3: u64 = splitmix64_next(&mut s);
let inv: f32 = 1.0 / 16_777_216.0;
(
((h1 >> 40) as f32) * inv,
((h2 >> 40) as f32) * inv,
((h3 >> 40) as f32) * inv,
)
}
pub(crate) fn worley_dist_2d(dx: f32, dy: f32, metric: WorleyMetric) -> f32 {
match metric {
WorleyMetric::Euclidean => (dx * dx + dy * dy).sqrt(),
WorleyMetric::Manhattan => dx.abs() + dy.abs(),
WorleyMetric::Chebyshev => dx.abs().max(dy.abs()),
}
}
pub(crate) fn worley_dist_3d(dx: f32, dy: f32, dz: f32, metric: WorleyMetric) -> f32 {
match metric {
WorleyMetric::Euclidean => (dx * dx + dy * dy + dz * dz).sqrt(),
WorleyMetric::Manhattan => dx.abs() + dy.abs() + dz.abs(),
WorleyMetric::Chebyshev => dx.abs().max(dy.abs()).max(dz.abs()),
}
}