noise/noise_fns/generators/
simplex.rs

1use crate::{
2    core::simplex::*,
3    noise_fns::{NoiseFn, Seedable},
4    permutationtable::PermutationTable,
5};
6
7/// Noise function that outputs N-dimensional Simplex noise.
8///
9#[derive(Clone, Copy, Debug)]
10pub struct Simplex {
11    seed: u32,
12    hasher: PermutationTable,
13}
14
15impl Simplex {
16    pub const DEFAULT_SEED: u32 = 0;
17
18    pub fn new(seed: u32) -> Self {
19        Simplex {
20            seed,
21            hasher: PermutationTable::new(seed),
22        }
23    }
24}
25
26impl Default for Simplex {
27    fn default() -> Self {
28        Self::new(Self::DEFAULT_SEED)
29    }
30}
31
32impl Seedable for Simplex {
33    /// Sets the seed value for Simplex noise
34    fn set_seed(self, seed: u32) -> Self {
35        // If the new seed is the same as the current seed, just return self.
36        if self.seed == seed {
37            return self;
38        }
39
40        // Otherwise, regenerate the permutation table based on the new seed.
41        Simplex {
42            seed,
43            hasher: PermutationTable::new(seed),
44        }
45    }
46
47    fn seed(&self) -> u32 {
48        self.seed
49    }
50}
51
52/// 2-dimensional Simplex noise
53impl NoiseFn<f64, 2> for Simplex {
54    fn get(&self, point: [f64; 2]) -> f64 {
55        let (result, _) = simplex_2d(point.into(), &self.hasher);
56
57        result
58    }
59}
60
61/// 3-dimensional Simplex noise
62impl NoiseFn<f64, 3> for Simplex {
63    fn get(&self, point: [f64; 3]) -> f64 {
64        let (result, _) = simplex_3d(point.into(), &self.hasher);
65
66        result
67    }
68}
69
70/// 4-dimensional Simplex noise
71impl NoiseFn<f64, 4> for Simplex {
72    fn get(&self, point: [f64; 4]) -> f64 {
73        let (result, _) = simplex_4d(point.into(), &self.hasher);
74
75        result
76    }
77}