noise/noise_fns/generators/
perlin_surflet.rs

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