noise/noise_fns/generators/
super_simplex.rs

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