2019-06-15 10:36:26 +00:00
|
|
|
use super::Sampler;
|
|
|
|
use vek::*;
|
|
|
|
|
|
|
|
pub struct RandomField {
|
|
|
|
seed: u32,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl RandomField {
|
2019-07-07 21:31:47 +00:00
|
|
|
pub const fn new(seed: u32) -> Self {
|
2019-06-15 10:36:26 +00:00
|
|
|
Self { seed }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-23 19:56:21 +00:00
|
|
|
impl Sampler<'static> for RandomField {
|
2019-06-15 10:36:26 +00:00
|
|
|
type Index = Vec3<i32>;
|
|
|
|
type Sample = u32;
|
|
|
|
|
|
|
|
fn get(&self, pos: Self::Index) -> Self::Sample {
|
|
|
|
let pos = pos.map(|e| (e * 13 + (1 << 31)) as u32);
|
|
|
|
|
2019-06-22 17:37:48 +00:00
|
|
|
let mut a = self.seed;
|
|
|
|
a = (a ^ 61) ^ (a >> 16);
|
|
|
|
a = a + (a << 3);
|
|
|
|
a = a ^ pos.x;
|
|
|
|
a = a ^ (a >> 4);
|
|
|
|
a = a * 0x27d4eb2d;
|
|
|
|
a = a ^ (a >> 15);
|
|
|
|
a = a ^ pos.y;
|
|
|
|
a = (a ^ 61) ^ (a >> 16);
|
|
|
|
a = a + (a << 3);
|
|
|
|
a = a ^ (a >> 4);
|
|
|
|
a = a ^ pos.z;
|
|
|
|
a = a * 0x27d4eb2d;
|
|
|
|
a = a ^ (a >> 15);
|
|
|
|
a
|
2019-06-15 10:36:26 +00:00
|
|
|
}
|
|
|
|
}
|
2019-07-07 21:31:47 +00:00
|
|
|
|
|
|
|
pub struct RandomPerm {
|
|
|
|
seed: u32,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl RandomPerm {
|
|
|
|
pub const fn new(seed: u32) -> Self {
|
|
|
|
Self { seed }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-23 19:56:21 +00:00
|
|
|
impl Sampler<'static> for RandomPerm {
|
2019-07-07 21:31:47 +00:00
|
|
|
type Index = u32;
|
|
|
|
type Sample = u32;
|
|
|
|
|
|
|
|
fn get(&self, perm: Self::Index) -> Self::Sample {
|
|
|
|
let mut a = perm;
|
|
|
|
a = (a ^ 61) ^ (a >> 16);
|
|
|
|
a = a + (a << 3);
|
|
|
|
a = a ^ (a >> 4);
|
|
|
|
a = a * 0x27d4eb2d;
|
|
|
|
a = a ^ (a >> 15);
|
|
|
|
a
|
|
|
|
}
|
|
|
|
}
|