noiz 0.4.0

A simple, configurable, blazingly fast noise library built for and with Bevy.
Documentation
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
#![cfg_attr(not(test), no_std)]
#![allow(
    clippy::doc_markdown,
    reason = "These rules should not apply to the readme."
)]
#![doc = include_str!("../README.md")]

pub mod cell_noise;
pub mod cells;
pub mod curves;
pub mod layering;
pub mod lengths;
pub mod math_noise;
pub mod misc_noise;
pub mod prelude;
pub mod rng;

use bevy_math::VectorSpace;
use rng::NoiseRng;

/// Represents a simple noise function with an input `I` and an output.
///
/// This is the powerhouse of this library.
/// This just represents a function that produces a value based only on its input type, input values, and some random number generator.
pub trait NoiseFunction<I> {
    /// The output of the function.
    /// This can be different for different inputs, which is especially useful for calculating gradients.
    type Output;

    /// Evaluates the function at some `input` with this [`NoiseRng`].
    /// This function should be deterministic.
    fn evaluate(&self, input: I, seeds: &mut NoiseRng) -> Self::Output;
}

impl<I, T0: NoiseFunction<I>> NoiseFunction<I> for (T0,) {
    type Output = T0::Output;
    #[inline]
    fn evaluate(&self, input: I, seeds: &mut NoiseRng) -> Self::Output {
        self.0.evaluate(input, seeds)
    }
}

macro_rules! impl_noise_function_tuple {
    ($($l:ident-$t:ident-$i:tt),*) => {
        impl<
            I,
            T0: NoiseFunction<I>,
            $($t: NoiseFunction<$l::Output>,)*
        > NoiseFunction<I> for (T0, $($t,)*)
        {
            type Output = <impl_noise_function_tuple!(last $($t),*)>::Output;

            #[inline]
            fn evaluate(&self, input: I, seeds: &mut NoiseRng) -> Self::Output {
                let input = self.0.evaluate(input, seeds);
                $(let input = self.$i.evaluate(input, seeds);)*
                input
            }
        }
    };


    (last $f:ident $(,)? ) => {
        $f
    };

    (last $f:ident, $($items:ident),+ $(,)?) => {
        impl_noise_function_tuple!(last $($items),+)
    };
}

#[rustfmt::skip]
mod function_impls {
    use super::*;
    impl_noise_function_tuple!(T0-T1-1);
    impl_noise_function_tuple!(T0-T1-1, T1-T2-2);
    impl_noise_function_tuple!(T0-T1-1, T1-T2-2, T2-T3-3);
    impl_noise_function_tuple!(T0-T1-1, T1-T2-2, T2-T3-3, T3-T4-4);
    impl_noise_function_tuple!(T0-T1-1, T1-T2-2, T2-T3-3, T3-T4-4, T4-T5-5);
    impl_noise_function_tuple!(T0-T1-1, T1-T2-2, T2-T3-3, T3-T4-4, T4-T5-5, T5-T6-6);
    impl_noise_function_tuple!(T0-T1-1, T1-T2-2, T2-T3-3, T3-T4-4, T4-T5-5, T5-T6-6, T6-T7-7);
    impl_noise_function_tuple!(T0-T1-1, T1-T2-2, T2-T3-3, T3-T4-4, T4-T5-5, T5-T6-6, T6-T7-7, T7-T8-8);
    impl_noise_function_tuple!(T0-T1-1, T1-T2-2, T2-T3-3, T3-T4-4, T4-T5-5, T5-T6-6, T6-T7-7, T7-T8-8, T8-T9-9);
    impl_noise_function_tuple!(T0-T1-1, T1-T2-2, T2-T3-3, T3-T4-4, T4-T5-5, T5-T6-6, T6-T7-7, T7-T8-8, T8-T9-9, T9-T10-10);
    impl_noise_function_tuple!(T0-T1-1, T1-T2-2, T2-T3-3, T3-T4-4, T4-T5-5, T5-T6-6, T6-T7-7, T7-T8-8, T8-T9-9, T9-T10-10, T10-T11-11);
    impl_noise_function_tuple!(T0-T1-1, T1-T2-2, T2-T3-3, T3-T4-4, T4-T5-5, T5-T6-6, T6-T7-7, T7-T8-8, T8-T9-9, T9-T10-10, T10-T11-11, T11-T12-12);
    impl_noise_function_tuple!(T0-T1-1, T1-T2-2, T2-T3-3, T3-T4-4, T4-T5-5, T5-T6-6, T6-T7-7, T7-T8-8, T8-T9-9, T9-T10-10, T10-T11-11, T11-T12-12, T12-T13-13);
    impl_noise_function_tuple!(T0-T1-1, T1-T2-2, T2-T3-3, T3-T4-4, T4-T5-5, T5-T6-6, T6-T7-7, T7-T8-8, T8-T9-9, T9-T10-10, T10-T11-11, T11-T12-12, T12-T13-13, T13-T14-14);
    impl_noise_function_tuple!(T0-T1-1, T1-T2-2, T2-T3-3, T3-T4-4, T4-T5-5, T5-T6-6, T6-T7-7, T7-T8-8, T8-T9-9, T9-T10-10, T10-T11-11, T11-T12-12, T12-T13-13, T13-T14-14, T14-T15-15);
}

/// Specifies that this noise is seedable.
///
/// ```
/// # use noiz::prelude::*;
/// let mut noise = Noise::<common_noise::Perlin>::default();
/// noise.set_seed(1234);
/// # let val = noise.sample_for::<f32>(bevy_math::Vec2::ZERO);
/// ```
pub trait SeedableNoise {
    /// Sets the seed of the noise.
    /// This seed can be absolutely any value.
    /// Even 0 is fine!
    fn set_seed(&mut self, seed: u32);

    /// Gets the seed of the noise.
    fn get_seed(&self) -> u32;
}

/// Specifies that this noise is scalable.
///
/// ```
/// # use noiz::prelude::*;
/// let mut noise = Noise::<common_noise::Perlin>::default();
/// noise.set_period(30.0);
/// # let val = noise.sample_for::<f32>(bevy_math::Vec2::ZERO);
/// ```
pub trait ScalableNoise {
    /// Sets the scale of the noise via its frequency.
    /// The frequency scales noise directly.
    /// At a frequency of `5`, an input of `2.0` will really sample at `10.0`.
    /// A frequency of `0.0`, infinity, or any other unusual value is not recommended.
    ///
    /// ```
    /// # use noiz::prelude::*;
    /// # let mut heightmap = Noise::<common_noise::Perlin>::default();
    /// // I want each sample to represent data from 30 units apart.
    /// heightmap.set_frequency(30.0);
    /// # let val = heightmap.sample_for::<f32>(bevy_math::Vec2::ZERO);
    /// ```
    fn set_frequency(&mut self, frequency: f32);

    /// Gets the scale of the noise via its frequency.
    /// See also [`get_frequency`](ScalableNoise::get_frequency).
    fn get_frequency(&self) -> f32;

    /// Sets the scale of the noise via its period.
    /// The `period` is the inverse of the [`frequency`](ScalableNoise::set_frequency).
    /// A period of `5` means an input will need to progress by `5` beffore it's inner sample can progress by `1.0`.
    /// A period of `0.0`, infinity, or any other unusual value is not recommended.
    ///
    /// ```
    /// # use noiz::prelude::*;
    /// # let mut heightmap = Noise::<common_noise::Perlin>::default();
    /// // I want each big mountain to be 30 units apart.
    /// heightmap.set_period(30.0);
    /// # let val = heightmap.sample_for::<f32>(bevy_math::Vec2::ZERO);
    /// ```
    fn set_period(&mut self, period: f32) {
        self.set_frequency(1.0 / period);
    }

    /// Gets the scale of the noise via its period.
    /// See also [`set_period`](ScalableNoise::set_period).
    fn get_period(&self) -> f32 {
        1.0 / self.get_frequency()
    }
}

/// Indicates that this noise is samplable by type `I`.
///
/// This differs from a [`NoiseFunction<I>`] because this is self contained.
/// It provides it's own random number generator, with its own seed, etc.
///
/// ```
/// # use noiz::prelude::*;
/// # use bevy_math::prelude::*;
/// let noise = Noise::<common_noise::Perlin>::default();
/// let value = noise.sample_for::<f32>(Vec2::new(1.0, -1.0));
/// ```
pub trait Sampleable<I> {
    /// Represents the raw result of the sample.
    /// This could be a pretty type like `f32` or it could encode additional information, like gradients, etc.
    type Result;

    /// Samples the noise at `loc`, returning the raw [`Sampleable::Result`] and the resulting [`NoiseRng`] state.
    /// This result may be incomplete and may depend on some cleanup to make the result meaningful.
    /// Use this with caution.
    ///
    /// ```
    /// # use noiz::prelude::*;
    /// # use bevy_math::prelude::*;
    /// let noise = Noise::<common_noise::Perlin>::default();
    /// let (wip_result, rng) = noise.sample_raw(Vec2::new(1.0, -1.0));
    /// // mutate wip_result and pass it to another noise function with rng.
    /// ```
    fn sample_raw(&self, loc: I) -> (Self::Result, NoiseRng);

    /// Samples the noise at `loc` for a result of type `T`.
    /// This is a convenience over [`SampleableFor`] since it doesn't require `T` to be written in the trait.
    ///
    /// This is generally inlined.
    /// This is useful to enable additional optimizations when sampling similar `loc`s in a tight loop.
    /// If you are not calling this from a tight loop, maybe try [`sample_dyn_for`](Sampleable::sample_dyn_for) instead.
    #[inline]
    fn sample_for<T>(&self, loc: I) -> T
    where
        Self: SampleableFor<I, T>,
    {
        self.sample(loc)
    }

    /// Samples the noise at `loc` for a result of type `T`.
    /// This is a convenience over [`DynamicSampleable`] since it doesn't require `T` to be written in the trait.
    ///
    /// This is generally not inlined.
    #[inline]
    fn sample_dyn_for<T>(&self, loc: I) -> T
    where
        Self: DynamicSampleable<I, T>,
    {
        self.sample_dyn(loc)
    }
}

/// Indicates that this noise is samplable by type `I` for type `T`. See also [`Sampleable`].
pub trait SampleableFor<I, T> {
    /// Samples the noise at `loc` for a result of type `T`.
    /// If both the result and input type can be inferred (as is often the case in practice), this can be more convenient than [`Sampleable::sample_for`].
    ///
    /// ```
    /// # use noiz::prelude::*;
    /// # use bevy_math::prelude::*;
    /// let noise = Noise::<common_noise::Perlin>::default();
    /// let value: f32 = noise.sample(Vec2::new(1.0, -1.0));
    /// ```
    ///
    /// This is generally inlined.
    /// This is useful to enable additional optimizations when sampling similar `loc`s in a tight loop.
    /// If you are not calling this from a tight loop, maybe try [`DynamicSampleable::sample_dyn`] instead.
    fn sample(&self, loc: I) -> T;
}

/// A version of [`Sampleable<I, Result=T>`] that is dyn-compatible.
/// Generally, `noize` uses exact types whenever possible to enable more inlining and optimizations,
/// but this trait focuses instead on usability at the expense of speed.
///
/// Use [`Sampleable`] when you need performance and [`DynamicSampleable`] when you need dyn compatibility or don't want to bloat binary size with more inlining.
///
/// ```
/// # use noiz::prelude::*;
/// # use bevy_math::prelude::*;
/// let noise: Box<dyn DynamicSampleable<Vec2, f32>> = Box::new(Noise::<common_noise::Perlin>::default());
/// let value = noise.sample_dyn(Vec2::new(1.0, -1.0));
/// ```
pub trait DynamicSampleable<I, T>: SampleableFor<I, T> {
    /// This is the same as [`SampleableFor::sample`] but it is not inlined.
    /// If you need a "one off" sample, and don't want it to be inlined, use this.
    fn sample_dyn(&self, loc: I) -> T {
        self.sample(loc)
    }
}

impl<T, I, N> DynamicSampleable<I, T> for N where N: SampleableFor<I, T> + Sampleable<I> {}

/// This is a convenience trait that merges [`DynamicSampleable`], [`ScalableNoise`] and [`SeedableNoise`].
/// ```
/// # use noiz::prelude::*;
/// # use bevy_math::prelude::*;
/// let mut noise: Box<dyn DynamicConfigurableSampleable<Vec2, f32>> = Box::new(Noise::<common_noise::Perlin>::default());
/// noise.set_seed(1234);
/// let value = noise.sample_dyn(Vec2::new(1.0, -1.0));
/// ```
pub trait DynamicConfigurableSampleable<I, T>:
    SeedableNoise + ScalableNoise + DynamicSampleable<I, T>
{
}

impl<I, T, N: SeedableNoise + ScalableNoise + DynamicSampleable<I, T>>
    DynamicConfigurableSampleable<I, T> for N
{
}

/// This is the standard [`Sampleable`] of a [`NoiseFunction`] `N`.
/// It wraps `N` with a self contained random number generator and frequency.
/// This currently only supports sampling from [`VectorSpace`] types.
///
/// ```
/// # use noiz::prelude::*;
/// # use bevy_math::prelude::*;
/// let mut noise = Noise::from(common_noise::Perlin::default());
/// noise.set_seed(1234);
/// let value = noise.sample_for::<f32>(Vec2::new(1.0, -1.0));
/// ```
///
/// See also [`Sampleable`], [`DynamicSampleable`], [`SampleableFor`], [`SeedableNoise`], [`ScalableNoise`], and [`DynamicConfigurableSampleable`].
///
/// See the "show_noise" example to see a few ways you can use this.
#[derive(PartialEq, Clone, Copy)]
#[cfg_attr(feature = "bevy_reflect", derive(bevy_reflect::Reflect))]
#[cfg_attr(feature = "serialize", derive(serde::Serialize))]
#[cfg_attr(feature = "debug", derive(Debug))]
pub struct Noise<N> {
    /// The [`NoiseFunction`] powering this noise.
    pub noise: N,
    /// The seed of the [`Noise`].
    pub seed: NoiseRng,
    /// The frequency or scale of the [`Noise`].
    pub frequency: f32,
}

impl<N: Default> Default for Noise<N> {
    fn default() -> Self {
        Self {
            noise: N::default(),
            seed: NoiseRng(0),
            frequency: 1.0,
        }
    }
}

impl<N> From<N> for Noise<N> {
    fn from(value: N) -> Self {
        Self {
            noise: value,
            seed: NoiseRng(0),
            frequency: 1.0,
        }
    }
}

impl<I: VectorSpace<Scalar = f32>, N: NoiseFunction<I>> NoiseFunction<I> for Noise<N> {
    type Output = N::Output;

    #[inline]
    fn evaluate(&self, input: I, seeds: &mut NoiseRng) -> Self::Output {
        seeds.0 ^= self.seed.0;
        self.noise.evaluate(input * self.frequency, seeds)
    }
}

impl<N> ScalableNoise for Noise<N> {
    fn set_frequency(&mut self, frequency: f32) {
        self.frequency = frequency;
    }

    fn get_frequency(&self) -> f32 {
        self.frequency
    }
}

impl<N> SeedableNoise for Noise<N> {
    fn set_seed(&mut self, seed: u32) {
        self.seed = NoiseRng(seed);
    }

    fn get_seed(&self) -> u32 {
        self.seed.0
    }
}

impl<I: VectorSpace<Scalar = f32>, N: NoiseFunction<I>> Sampleable<I> for Noise<N> {
    type Result = N::Output;

    #[inline]
    fn sample_raw(&self, loc: I) -> (Self::Result, NoiseRng) {
        let mut seeds = self.seed;
        let result = self.noise.evaluate(loc * self.frequency, &mut seeds);
        (result, seeds)
    }
}

impl<T, I: VectorSpace<Scalar = f32>, N: NoiseFunction<I, Output: Into<T>>> SampleableFor<I, T>
    for Noise<N>
{
    #[inline]
    fn sample(&self, loc: I) -> T {
        let (result, _rng) = self.sample_raw(loc);
        result.into()
    }
}

/// This is an alternative to [`Noise`] for when scaling an sample location is not desired or is impossible.
/// In general, [`Noise`] is easier to use, but this offers more control if desired.
#[derive(PartialEq, Clone, Copy)]
#[cfg_attr(feature = "bevy_reflect", derive(bevy_reflect::Reflect))]
#[cfg_attr(feature = "serialize", derive(serde::Serialize))]
#[cfg_attr(feature = "debug", derive(Debug))]
pub struct RawNoise<N> {
    /// The [`NoiseFunction`] powering this noise.
    pub noise: N,
    /// The seed of the [`Noise`].
    pub seed: NoiseRng,
}

impl<N: Default> Default for RawNoise<N> {
    fn default() -> Self {
        Self {
            noise: N::default(),
            seed: NoiseRng(0),
        }
    }
}

impl<N> From<N> for RawNoise<N> {
    fn from(value: N) -> Self {
        Self {
            noise: value,
            seed: NoiseRng(0),
        }
    }
}

impl<I, N: NoiseFunction<I>> NoiseFunction<I> for RawNoise<N> {
    type Output = N::Output;

    #[inline]
    fn evaluate(&self, input: I, seeds: &mut NoiseRng) -> Self::Output {
        seeds.0 ^= self.seed.0;
        self.noise.evaluate(input, seeds)
    }
}

impl<N> SeedableNoise for RawNoise<N> {
    fn set_seed(&mut self, seed: u32) {
        self.seed = NoiseRng(seed);
    }

    fn get_seed(&self) -> u32 {
        self.seed.0
    }
}

impl<I, N: NoiseFunction<I>> Sampleable<I> for RawNoise<N> {
    type Result = N::Output;

    #[inline]
    fn sample_raw(&self, loc: I) -> (Self::Result, NoiseRng) {
        let mut seeds = self.seed;
        let result = self.noise.evaluate(loc, &mut seeds);
        (result, seeds)
    }
}

impl<T, I, N: NoiseFunction<I, Output: Into<T>>> SampleableFor<I, T> for RawNoise<N> {
    #[inline]
    fn sample(&self, loc: I) -> T {
        let (result, _rng) = self.sample_raw(loc);
        result.into()
    }
}