1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
use super::{RandomField, Sampler};
use std::{f32, ops::Add};
use vek::*;

pub struct FastNoise {
    noise: RandomField,
}

impl FastNoise {
    pub const fn new(seed: u32) -> Self {
        Self {
            noise: RandomField::new(seed),
        }
    }

    #[allow(clippy::excessive_precision)] // TODO: Pending review in #587
    fn noise_at(&self, pos: Vec3<i32>) -> f32 {
        (self.noise.get(pos) & 4095) as f32 * 0.000244140625
    }
}

impl Sampler<'static> for FastNoise {
    type Index = Vec3<f64>;
    type Sample = f32;

    fn get(&self, pos: Self::Index) -> Self::Sample {
        // let align_pos = pos.map(|e| e.floor());
        // let near_pos = align_pos.map(|e| e as i32);

        // let v000 = self.noise_at(near_pos + Vec3::new(0, 0, 0));
        // let v100 = self.noise_at(near_pos + Vec3::new(1, 0, 0));
        // let v010 = self.noise_at(near_pos + Vec3::new(0, 1, 0));
        // let v110 = self.noise_at(near_pos + Vec3::new(1, 1, 0));
        // let v001 = self.noise_at(near_pos + Vec3::new(0, 0, 1));
        // let v101 = self.noise_at(near_pos + Vec3::new(1, 0, 1));
        // let v011 = self.noise_at(near_pos + Vec3::new(0, 1, 1));
        // let v111 = self.noise_at(near_pos + Vec3::new(1, 1, 1));

        // let factor = (pos - align_pos).map(|e| e as f32);

        // let v00 = v000 + factor.z * (v001 - v000);
        // let v10 = v010 + factor.z * (v011 - v010);
        // let v01 = v100 + factor.z * (v101 - v100);
        // let v11 = v110 + factor.z * (v111 - v110);

        // let v0 = v00 + factor.y * (v01 - v00);
        // let v1 = v10 + factor.y * (v11 - v10);

        // (v0 + factor.x * (v1 - v0)) * 2.0 - 1.0

        let near_pos = pos.map(|e| e.floor() as i32);

        let v000 = self.noise_at(near_pos + Vec3::new(0, 0, 0));
        let v100 = self.noise_at(near_pos + Vec3::new(1, 0, 0));
        let v010 = self.noise_at(near_pos + Vec3::new(0, 1, 0));
        let v110 = self.noise_at(near_pos + Vec3::new(1, 1, 0));
        let v001 = self.noise_at(near_pos + Vec3::new(0, 0, 1));
        let v101 = self.noise_at(near_pos + Vec3::new(1, 0, 1));
        let v011 = self.noise_at(near_pos + Vec3::new(0, 1, 1));
        let v111 = self.noise_at(near_pos + Vec3::new(1, 1, 1));

        let factor = pos.map(|e| {
            let f = e.fract().add(1.0).fract() as f32;
            f.powi(2) * (3.0 - 2.0 * f)
        });

        let x00 = v000 + factor.x * (v100 - v000);
        let x10 = v010 + factor.x * (v110 - v010);
        let x01 = v001 + factor.x * (v101 - v001);
        let x11 = v011 + factor.x * (v111 - v011);

        let y0 = x00 + factor.y * (x10 - x00);
        let y1 = x01 + factor.y * (x11 - x01);

        (y0 + factor.z * (y1 - y0)) * 2.0 - 1.0
    }
}

pub struct FastNoise2d {
    noise: RandomField,
}

impl FastNoise2d {
    pub const fn new(seed: u32) -> Self {
        Self {
            noise: RandomField::new(seed),
        }
    }

    #[allow(clippy::excessive_precision)] // TODO: Pending review in #587
    fn noise_at(&self, pos: Vec2<i32>) -> f32 {
        (self.noise.get(Vec3::new(pos.x, pos.y, 0)) & 4095) as f32 * 0.000244140625
    }
}

impl Sampler<'static> for FastNoise2d {
    type Index = Vec2<f64>;
    type Sample = f32;

    fn get(&self, pos: Self::Index) -> Self::Sample {
        let near_pos = pos.map(|e| e.floor() as i32);

        let v00 = self.noise_at(near_pos + Vec2::new(0, 0));
        let v10 = self.noise_at(near_pos + Vec2::new(1, 0));
        let v01 = self.noise_at(near_pos + Vec2::new(0, 1));
        let v11 = self.noise_at(near_pos + Vec2::new(1, 1));

        let factor = pos.map(|e| {
            let f = e.fract().add(1.0).fract() as f32;
            f.powi(2) * (3.0 - 2.0 * f)
        });

        let v0 = v00 + factor.x * (v10 - v00);
        let v1 = v01 + factor.x * (v11 - v01);

        (v0 + factor.y * (v1 - v0)) * 2.0 - 1.0
    }
}