noise/noise_fns/generators/open_simplex.rs
1//! Note that this is NOT Ken Perlin's simplex noise, as that is patent encumbered.
2//! Instead, these functions use the `OpenSimplex` algorithm, as detailed here:
3//! <http://uniblock.tumblr.com/post/97868843242/noise>
4
5use crate::{
6 core::open_simplex::{open_simplex_2d, open_simplex_3d, open_simplex_4d},
7 noise_fns::{NoiseFn, Seedable},
8 permutationtable::PermutationTable,
9};
10
11/// Noise function that outputs 2/3/4-dimensional Open Simplex noise.
12#[derive(Clone, Copy, Debug)]
13pub struct OpenSimplex {
14 seed: u32,
15 perm_table: PermutationTable,
16}
17
18impl OpenSimplex {
19 const DEFAULT_SEED: u32 = 0;
20
21 pub fn new(seed: u32) -> Self {
22 Self {
23 seed,
24 perm_table: PermutationTable::new(seed),
25 }
26 }
27}
28
29impl Default for OpenSimplex {
30 fn default() -> Self {
31 Self::new(Self::DEFAULT_SEED)
32 }
33}
34
35impl Seedable for OpenSimplex {
36 /// Sets the seed value for Open Simplex noise
37 fn set_seed(self, seed: u32) -> Self {
38 // If the new seed is the same as the current seed, just return self.
39 if self.seed == seed {
40 return self;
41 }
42
43 // Otherwise, regenerate the permutation table based on the new seed.
44 Self {
45 seed,
46 perm_table: PermutationTable::new(seed),
47 }
48 }
49
50 fn seed(&self) -> u32 {
51 self.seed
52 }
53}
54
55/// 2-dimensional [`OpenSimplex` Noise](http://uniblock.tumblr.com/post/97868843242/noise)
56///
57/// This is a slower but higher quality form of gradient noise than `Perlin` 2D.
58impl NoiseFn<f64, 2> for OpenSimplex {
59 fn get(&self, point: [f64; 2]) -> f64 {
60 open_simplex_2d(point.into(), &self.perm_table)
61 }
62}
63
64/// 3-dimensional [`OpenSimplex` Noise](http://uniblock.tumblr.com/post/97868843242/noise)
65///
66/// This is a slower but higher quality form of gradient noise than `Perlin` 3D.
67impl NoiseFn<f64, 3> for OpenSimplex {
68 fn get(&self, point: [f64; 3]) -> f64 {
69 open_simplex_3d(point.into(), &self.perm_table)
70 }
71}
72
73/// 4-dimensional [`OpenSimplex` Noise](http://uniblock.tumblr.com/post/97868843242/noise)
74///
75/// This is a slower but higher quality form of gradient noise than `Perlin` 4D.
76impl NoiseFn<f64, 4> for OpenSimplex {
77 fn get(&self, point: [f64; 4]) -> f64 {
78 open_simplex_4d(point.into(), &self.perm_table)
79 }
80}