Skip to main content

terrain_forge/noise/
mod.rs

1//! Noise generation module with composable generators and modifiers
2
3mod fbm;
4mod modifiers;
5mod perlin;
6mod ridged;
7mod simplex;
8mod value;
9mod worley;
10
11pub use fbm::Fbm;
12pub use modifiers::*;
13pub use perlin::Perlin;
14pub use ridged::Ridged;
15pub use simplex::Simplex;
16pub use value::Value;
17pub use worley::Worley;
18
19/// Trait for noise sources that can be sampled at 2D coordinates
20pub trait NoiseSource {
21    /// Sample noise at 2D coordinates, returns value typically in [-1, 1]
22    fn sample(&self, x: f64, y: f64) -> f64;
23}
24
25/// Extension trait for composing noise sources
26pub trait NoiseExt: NoiseSource + Sized {
27    /// Scale output by a factor
28    fn scale(self, factor: f64) -> Scale<Self> {
29        Scale {
30            source: self,
31            factor,
32        }
33    }
34
35    /// Add offset to output
36    fn offset(self, amount: f64) -> Offset<Self> {
37        Offset {
38            source: self,
39            amount,
40        }
41    }
42
43    /// Clamp output to range
44    fn clamp(self, min: f64, max: f64) -> Clamp<Self> {
45        Clamp {
46            source: self,
47            min,
48            max,
49        }
50    }
51
52    /// Take absolute value
53    fn abs(self) -> Abs<Self> {
54        Abs { source: self }
55    }
56
57    /// Apply fractal brownian motion
58    fn fbm(self, octaves: u32, lacunarity: f64, persistence: f64) -> Fbm<Self> {
59        Fbm::new(self, octaves, lacunarity, persistence)
60    }
61}
62
63impl<T: NoiseSource> NoiseExt for T {}