noise_functions/
modifiers.rs

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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
mod fbm;
mod frequency;
pub mod open_simplex_2;
mod seeded;
mod tileable;

pub use fbm::Fbm;
pub use frequency::Frequency;
pub use seeded::Seeded;
pub use tileable::Tileable;

use crate::{
    math::{abs, const_abs},
    Noise,
};

/// Modifies a fractal noise to make successive octaves have less impact the lower the output value of the previous one was.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Weighted<Fractal> {
    pub fractal: Fractal,
    pub strength: f32,
}

impl<Fractal> Noise for Weighted<Fractal> {}

impl<Fractal> Weighted<Fractal> {
    #[inline(always)]
    pub const fn seed(self, seed: i32) -> Seeded<Self> {
        Seeded { noise: self, seed }
    }

    #[inline(always)]
    pub const fn frequency(self, frequency: f32) -> Frequency<Self> {
        Frequency { noise: self, frequency }
    }
}

macro_rules! or_else {
    ({ } else { $($else:tt)* }) => { $($else)* };
    ({ $($tokens:tt)+ } else { $($else:tt)* }) => { $($tokens)* };
}

macro_rules! modifier_map {
    (
        $(#[$attr:meta])*
        pub struct $struct:ident {
            $($fields:tt)*
        }

        $self:ident, $seed:ident, $point:ident, $value:ident;
        $(map_seed: { $($map_seed:tt)* })?
        $(map_value: { $($map_value:tt)* })?
    ) => {
        $(#[$attr])*
        pub struct $struct<Noise> {
            pub noise: Noise,
            $($fields)*
        }

        const _: () = {
            use crate::{ Noise, Sample, SampleWithSeed };

            #[cfg(feature = "nightly-simd")]
            use core::simd::{ Simd, LaneCount, SupportedLaneCount };

            impl<N> Noise for $struct<N> {}

            impl<Noise, const DIM: usize> Sample<DIM> for $struct<Noise>
            where
                Noise: SampleWithSeed<DIM>,
            {
                #[inline]
                fn sample(&$self, $point: [f32; DIM]) -> f32 {
                    let $value = $self.noise.sample($point);
                    or_else!({$({$($map_value)*})?} else { $value })
                }
            }

            impl<Noise, const DIM: usize> SampleWithSeed<DIM> for $struct<Noise>
            where
                Noise: SampleWithSeed<DIM>,
            {
                #[inline]
                fn sample_with_seed(&$self, $point: [f32; DIM], $seed: i32) -> f32 {
                    let $value = $self.noise.sample_with_seed($point, or_else!({$({$($map_seed)*})?} else { $seed }));
                    or_else!({$({$($map_value)*})?} else { $value })
                }
            }

            #[cfg(feature = "nightly-simd")]
            impl<Noise, const DIM: usize, const LANES: usize> Sample<DIM, Simd<f32, LANES>> for $struct<Noise>
            where
                Noise: SampleWithSeed<DIM, Simd<f32, LANES>>,
                LaneCount<LANES>: SupportedLaneCount,
            {
                #[inline]
                fn sample(&$self, $point: Simd<f32, LANES>) -> f32 {
                    let $value = $self.noise.sample($point);
                    or_else!({$({$($map_value)*})?} else { $value })
                }
            }

            #[cfg(feature = "nightly-simd")]
            impl<Noise, const DIM: usize, const LANES: usize> SampleWithSeed<DIM, Simd<f32, LANES>> for $struct<Noise>
            where
                Noise: SampleWithSeed<DIM, Simd<f32, LANES>>,
                LaneCount<LANES>: SupportedLaneCount,
            {
                #[inline]
                fn sample_with_seed(&$self, $point: Simd<f32, LANES>, $seed: i32) -> f32 {
                    let $value = $self.noise.sample_with_seed($point, or_else!({$({$($map_seed)*})?} else { $seed }));
                    or_else!({$({$($map_value)*})?} else { $value })
                }
            }
        };
    };
}

pub(crate) use modifier_map;

use crate::math::floor;

modifier_map! {
    /// Modifies a noise to create a peak at 0.
    ///
    /// This outputs values is in the [-1, 1] range.
    ///
    /// **Note:** This modifier assumes the base noise returns values in the [-1, 1] range.
    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
    pub struct Ridged {}

    self, seed, point, value;

    map_value: {
        abs(value) * -2.0 + 1.0
    }
}

modifier_map! {
    /// Applies a triangle wave to the output of a base noise function.
    ///
    /// This outputs values is in the [-1, 1] range.
    ///
    /// **Note:** This modifier assumes the base noise returns values in the [-1, 1] range.
    #[derive(Debug, Clone, Copy, PartialEq)]
    pub struct TriangleWave {
        pub frequency: f32,
    }

    self, seed, point, value;

    map_value: {
        let v = (value + 1.0) * self.frequency;
        let v = v - floor(v * 0.5) * 2.0;
        let v = if v < 1.0 {
            v
        } else {
            2.0 - v
        };
        (v - 0.5) * 2.0
    }
}

modifier_map! {
    /// Multiplies the seed by `value`.
    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
    pub struct MulSeed {
        pub value: i32,
    }

    self, seed, point, value;

    map_seed: {
        seed * self.value
    }
}

/// Calculates the `fractal_bounding` property for [`Fbm`].
///
#[inline(always)]
pub const fn fractal_bounding(octaves: u32, gain: f32) -> f32 {
    let gain = const_abs(gain);
    let mut amp = gain;
    let mut amp_fractal = 1.0;
    let mut i = 0;

    while i < octaves {
        amp_fractal += amp;
        amp *= gain;
        i += 1;
    }

    1.0 / amp_fractal
}