noise_functions/modifiers/
frequency.rs

1#[cfg(feature = "nightly-simd")]
2use core::simd::{LaneCount, Simd, SupportedLaneCount};
3
4use crate::{Noise, Sample};
5
6/// Modifies a noise with a frequency multiplier.
7///
8/// This multiplies the point by the provided `frequency` before sampling.
9#[derive(Debug, Clone, Copy, PartialEq)]
10pub struct Frequency<N, F> {
11    pub noise: N,
12    pub frequency: F,
13}
14
15impl<N, F> Noise for Frequency<N, F> {}
16
17impl<const DIM: usize, N, F> Sample<DIM> for Frequency<N, F>
18where
19    N: Sample<DIM>,
20    F: Sample<DIM>,
21{
22    fn sample_with_seed(&self, mut point: [f32; DIM], seed: i32) -> f32 {
23        let frequency = self.frequency.sample_with_seed(point, seed);
24
25        for x in &mut point {
26            *x *= frequency;
27        }
28
29        self.noise.sample_with_seed(point, seed)
30    }
31}
32
33#[cfg(feature = "nightly-simd")]
34impl<const DIM: usize, const LANES: usize, N, F> Sample<DIM, Simd<f32, LANES>> for Frequency<N, F>
35where
36    N: Sample<DIM, Simd<f32, LANES>>,
37    F: Sample<DIM, Simd<f32, LANES>>,
38    LaneCount<LANES>: SupportedLaneCount,
39{
40    fn sample_with_seed(&self, mut point: Simd<f32, LANES>, seed: i32) -> f32 {
41        point *= Simd::splat(self.frequency.sample_with_seed(point, seed));
42        self.noise.sample_with_seed(point, seed)
43    }
44}