noise_functions/modifiers/
frequency.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
#[cfg(feature = "nightly-simd")]
use core::simd::{LaneCount, Simd, SupportedLaneCount};

use crate::{Noise, Sample};

/// Modifies a noise with a frequency multiplier.
///
/// This multiplies the point by the provided `frequency` before sampling.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Frequency<Noise> {
    pub noise: Noise,
    pub frequency: f32,
}

impl<N> Noise for Frequency<N> {}

impl<const DIM: usize, Noise> Sample<DIM> for Frequency<Noise>
where
    Noise: Sample<DIM>,
{
    fn sample(&self, mut point: [f32; DIM]) -> f32 {
        let frequency = self.frequency;

        for x in &mut point {
            *x *= frequency;
        }

        self.noise.sample(point)
    }
}

#[cfg(feature = "nightly-simd")]
impl<const DIM: usize, const LANES: usize, Noise> Sample<DIM, Simd<f32, LANES>> for Frequency<Noise>
where
    Noise: Sample<DIM, Simd<f32, LANES>>,
    LaneCount<LANES>: SupportedLaneCount,
{
    fn sample(&self, mut point: Simd<f32, LANES>) -> f32 {
        point *= Simd::splat(self.frequency);
        self.noise.sample(point)
    }
}