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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
//! Contains common math-based [`NoiseFunction`]s.
//! These are some of the smallest but most powerful noise functions.
//! Note that some of them have specific requirements for the domain of their inputs.
//! To see some examples of this, see the "show_noise" example.

use core::ops::{Mul, Neg};

use bevy_math::{Curve, Vec2, Vec3, Vec3A, Vec4};

use crate::{NoiseFunction, cells::WithGradient, lengths::LengthFunction};

/// A [`NoiseFunction`] that maps vectors from (-1,1) to (0, 1).
#[derive(Default, 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 SNormToUNorm;

/// A [`NoiseFunction`] that maps vectors from (0, 1) to (-1,1).
#[derive(Default, 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 UNormToSNorm;

/// A [`NoiseFunction`] that raises the input to the second power.
#[derive(Default, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "bevy_reflect", derive(bevy_reflect::Reflect))]
#[cfg_attr(feature = "serialize", derive(serde::Serialize))]
#[cfg_attr(feature = "debug", derive(Debug))]
pub struct Pow2;

/// A [`NoiseFunction`] that raises the input to the third power.
#[derive(Default, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "bevy_reflect", derive(bevy_reflect::Reflect))]
#[cfg_attr(feature = "serialize", derive(serde::Serialize))]
#[cfg_attr(feature = "debug", derive(Debug))]
pub struct Pow3;

/// A [`NoiseFunction`] that raises the input to the fourth power.
#[derive(Default, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "bevy_reflect", derive(bevy_reflect::Reflect))]
#[cfg_attr(feature = "serialize", derive(serde::Serialize))]
#[cfg_attr(feature = "debug", derive(Debug))]
pub struct Pow4;

/// A [`NoiseFunction`] that raises the input to some power.
#[derive(Default, Clone, Copy, PartialEq)]
#[cfg_attr(feature = "bevy_reflect", derive(bevy_reflect::Reflect))]
#[cfg_attr(feature = "serialize", derive(serde::Serialize))]
#[cfg_attr(feature = "debug", derive(Debug))]
pub struct PowF(pub f32);

/// A [`NoiseFunction`] that raises the input to some integer power.
#[derive(Default, Clone, Copy, PartialEq)]
#[cfg_attr(feature = "bevy_reflect", derive(bevy_reflect::Reflect))]
#[cfg_attr(feature = "serialize", derive(serde::Serialize))]
#[cfg_attr(feature = "debug", derive(Debug))]
pub struct PowI(pub i32);

/// A [`NoiseFunction`] that takes the square root of its input.
#[derive(Default, Clone, Copy, PartialEq)]
#[cfg_attr(feature = "bevy_reflect", derive(bevy_reflect::Reflect))]
#[cfg_attr(feature = "serialize", derive(serde::Serialize))]
#[cfg_attr(feature = "debug", derive(Debug))]
pub struct Sqrt;

/// A [`NoiseFunction`] makes more positive numbers get closer to 0.
/// Negative numbers are meaningless. Positive numbers will produce UNorm results.
#[derive(Default, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "bevy_reflect", derive(bevy_reflect::Reflect))]
#[cfg_attr(feature = "serialize", derive(serde::Serialize))]
#[cfg_attr(feature = "debug", derive(Debug))]
pub struct PositiveApproachZero;

/// A [`NoiseFunction`] that takes the absolute value of its input.
///
/// Note that differentiation is implemented for this, but it is not smooth and is not mathematically rigorous.
#[derive(Default, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "bevy_reflect", derive(bevy_reflect::Reflect))]
#[cfg_attr(feature = "serialize", derive(serde::Serialize))]
#[cfg_attr(feature = "debug", derive(Debug))]
pub struct Abs;

/// A [`NoiseFunction`] that divides 1.0 by its input, ex: `1.0 / input`.
#[derive(Default, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "bevy_reflect", derive(bevy_reflect::Reflect))]
#[cfg_attr(feature = "serialize", derive(serde::Serialize))]
#[cfg_attr(feature = "debug", derive(Debug))]
pub struct Inverse;

/// A [`NoiseFunction`] that subtracts its input from 1.0, ex: `1.0 - input`.
#[derive(Default, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "bevy_reflect", derive(bevy_reflect::Reflect))]
#[cfg_attr(feature = "serialize", derive(serde::Serialize))]
#[cfg_attr(feature = "debug", derive(Debug))]
pub struct ReverseUNorm;

/// A [`NoiseFunction`] that negates its input, ex: `-input`.
#[derive(Default, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "bevy_reflect", derive(bevy_reflect::Reflect))]
#[cfg_attr(feature = "serialize", derive(serde::Serialize))]
#[cfg_attr(feature = "debug", derive(Debug))]
pub struct Negate;

/// A [`NoiseFunction`] that produces a billowing effect for SNorm values.
/// Inspired by [libnoise](https://docs.rs/libnoise/latest/libnoise/).
///
/// Note that differentiation is implemented for this, but it is not smooth and is not mathematically rigorous.
pub type Billow = (Abs, UNormToSNorm);

/// A [`NoiseFunction`] that wraps values over this one back below it.
/// This can produce a ridging effect.
#[derive(Clone, Copy, PartialEq)]
#[cfg_attr(feature = "bevy_reflect", derive(bevy_reflect::Reflect))]
#[cfg_attr(feature = "serialize", derive(serde::Serialize))]
#[cfg_attr(feature = "debug", derive(Debug))]
pub struct Wrapped(pub f32);

macro_rules! impl_vector_spaces {
    (scalar $n:ty) => {
        impl_vector_spaces!(both $n);

        impl NoiseFunction<$n> for Abs {
            type Output = $n;

            #[inline]
            fn evaluate(&self, input: $n, _seeds: &mut crate::rng::NoiseRng) -> Self::Output {
                bevy_math::ops::abs(input)
            }
        }

        impl NoiseFunction<$n> for PowF {
            type Output = $n;

            #[inline]
            fn evaluate(&self, input: $n, _seeds: &mut crate::rng::NoiseRng) -> Self::Output {
                bevy_math::ops::powf(input, self.0)
            }
        }

        impl NoiseFunction<$n> for PowI {
            type Output = $n;

            #[inline]
            fn evaluate(&self, input: $n, _seeds: &mut crate::rng::NoiseRng) -> Self::Output {
                input.powi(self.0)
            }
        }

        impl NoiseFunction<$n> for Sqrt {
            type Output = $n;

            #[inline]
            fn evaluate(&self, input: $n, _seeds: &mut crate::rng::NoiseRng) -> Self::Output {
                bevy_math::ops::sqrt(input)
            }
        }
    };

    (vec $n:ty) => {
        impl_vector_spaces!(both $n);

        impl NoiseFunction<$n> for Abs {
            type Output = $n;

            #[inline]
            fn evaluate(&self, input: $n, _seeds: &mut crate::rng::NoiseRng) -> Self::Output {
                input.abs()
            }
        }

        impl NoiseFunction<$n> for PowF {
            type Output = $n;

            #[inline]
            fn evaluate(&self, input: $n, _seeds: &mut crate::rng::NoiseRng) -> Self::Output {
                input.powf(self.0)
            }
        }

        impl NoiseFunction<$n> for Sqrt {
            type Output = $n;

            #[inline]
            fn evaluate(&self, input: $n, _seeds: &mut crate::rng::NoiseRng) -> Self::Output {
                input.map(bevy_math::ops::sqrt)
            }
        }

        impl NoiseFunction<$n> for PowI {
            type Output = $n;

            #[inline]
            fn evaluate(&self, input: $n, _seeds: &mut crate::rng::NoiseRng) -> Self::Output {
                input.map(|v| v.powi(self.0))
            }
        }
    };

    (both $n:ty) => {
        impl NoiseFunction<$n> for SNormToUNorm {
            type Output = $n;

            #[inline]
            fn evaluate(&self, input: $n, _seeds: &mut crate::rng::NoiseRng) -> Self::Output {
                input * 0.5 + 0.5
            }
        }

        impl NoiseFunction<$n> for UNormToSNorm {
            type Output = $n;

            #[inline]
            fn evaluate(&self, input: $n, _seeds: &mut crate::rng::NoiseRng) -> Self::Output {
                (input - 0.5) * 2.0
            }
        }

        impl NoiseFunction<$n> for Pow2 {
            type Output = $n;

            #[inline]
            fn evaluate(&self, input: $n, _seeds: &mut crate::rng::NoiseRng) -> Self::Output {
                input * input
            }
        }

        impl NoiseFunction<$n> for Pow3 {
            type Output = $n;

            #[inline]
            fn evaluate(&self, input: $n, _seeds: &mut crate::rng::NoiseRng) -> Self::Output {
                input * input * input
            }
        }

        impl NoiseFunction<$n> for Pow4 {
            type Output = $n;

            #[inline]
            fn evaluate(&self, input: $n, _seeds: &mut crate::rng::NoiseRng) -> Self::Output {
                (input * input) * (input * input)
            }
        }

        impl NoiseFunction<$n> for PositiveApproachZero {
            type Output = $n;

            #[inline]
            fn evaluate(&self, input: $n, _seeds: &mut crate::rng::NoiseRng) -> Self::Output {
                1.0 / (input + 1.0)
            }
        }

        impl NoiseFunction<$n> for Inverse {
            type Output = $n;

            #[inline]
            fn evaluate(&self, input: $n, _seeds: &mut crate::rng::NoiseRng) -> Self::Output {
                1.0 / input
            }
        }

        impl NoiseFunction<$n> for ReverseUNorm {
            type Output = $n;

            #[inline]
            fn evaluate(&self, input: $n, _seeds: &mut crate::rng::NoiseRng) -> Self::Output {
                1.0 - input
            }
        }

        impl NoiseFunction<$n> for Negate {
            type Output = $n;

            #[inline]
            fn evaluate(&self, input: $n, _seeds: &mut crate::rng::NoiseRng) -> Self::Output {
                -input
            }
        }

        impl NoiseFunction<$n> for Wrapped {
            type Output = $n;

            #[inline]
            fn evaluate(&self, input: $n, _seeds: &mut crate::rng::NoiseRng) -> Self::Output {
                input % self.0
            }
        }
    };
}

impl_vector_spaces!(scalar f32);
impl_vector_spaces!(vec Vec2);
impl_vector_spaces!(vec Vec3);
impl_vector_spaces!(vec Vec3A);
impl_vector_spaces!(vec Vec4);

/// A [`NoiseFunction`] produces a ping ponging effect for UNorm values.
/// The inner value represents the strength of the ping pong.
/// Inspired by [fastnoise_lite](https://docs.rs/fastnoise-lite/latest/fastnoise_lite/).
#[derive(Clone, Copy, PartialEq)]
#[cfg_attr(feature = "bevy_reflect", derive(bevy_reflect::Reflect))]
#[cfg_attr(feature = "serialize", derive(serde::Serialize))]
#[cfg_attr(feature = "debug", derive(Debug))]
pub struct PingPong(pub f32);

impl Default for PingPong {
    fn default() -> Self {
        Self(1.0)
    }
}

impl NoiseFunction<f32> for PingPong {
    type Output = f32;

    #[inline]
    fn evaluate(&self, input: f32, _seeds: &mut crate::rng::NoiseRng) -> Self::Output {
        let t = (input + 1.0) * self.0;
        let t = t - (t * 0.5).trunc() * 2.;

        if t < 1.0 { t } else { 2. - t }
    }
}

/// A [`NoiseFunction`] that samples some [`Curve`] directly.
#[derive(Default, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "bevy_reflect", derive(bevy_reflect::Reflect))]
#[cfg_attr(feature = "serialize", derive(serde::Serialize))]
#[cfg_attr(feature = "debug", derive(Debug))]
pub struct NoiseCurve<C>(pub C);

impl<C: Curve<f32>> NoiseFunction<f32> for NoiseCurve<C> {
    type Output = f32;

    #[inline]
    fn evaluate(&self, input: f32, _seeds: &mut crate::rng::NoiseRng) -> Self::Output {
        self.0.sample_unchecked(input)
    }
}

/// A [`NoiseFunction`] that samples some [`Curve`] in the proper range by clamping.
#[derive(Default, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "bevy_reflect", derive(bevy_reflect::Reflect))]
#[cfg_attr(feature = "serialize", derive(serde::Serialize))]
#[cfg_attr(feature = "debug", derive(Debug))]
pub struct NoiseCurveClamped<C>(pub C);

impl<C: Curve<f32>> NoiseFunction<f32> for NoiseCurveClamped<C> {
    type Output = f32;

    #[inline]
    fn evaluate(&self, input: f32, _seeds: &mut crate::rng::NoiseRng) -> Self::Output {
        self.0.sample_clamped(input)
    }
}

macro_rules! impl_mapped_vector_spaces {
    ($n:ty) => {
        impl NoiseFunction<$n> for PingPong {
            type Output = $n;

            #[inline]
            fn evaluate(&self, input: $n, _seeds: &mut crate::rng::NoiseRng) -> Self::Output {
                input.map(|v| self.evaluate(v, &mut crate::rng::NoiseRng(0)))
            }
        }

        impl<C: Curve<f32>> NoiseFunction<$n> for NoiseCurveClamped<C> {
            type Output = $n;

            #[inline]
            fn evaluate(&self, input: $n, _seeds: &mut crate::rng::NoiseRng) -> Self::Output {
                input.map(|v| self.evaluate(v, &mut crate::rng::NoiseRng(0)))
            }
        }

        impl<C: Curve<f32>> NoiseFunction<$n> for NoiseCurve<C> {
            type Output = $n;

            #[inline]
            fn evaluate(&self, input: $n, _seeds: &mut crate::rng::NoiseRng) -> Self::Output {
                input.map(|v| self.evaluate(v, &mut crate::rng::NoiseRng(0)))
            }
        }
    };
}

impl_mapped_vector_spaces!(Vec2);
impl_mapped_vector_spaces!(Vec3);
impl_mapped_vector_spaces!(Vec3A);
impl_mapped_vector_spaces!(Vec4);

/// A [`NoiseFunction`] that turns a cartesian coordinate into a polar coordinate.
/// Contains a [`LengthFunction`] and a scale for radial cells.
#[derive(Clone, Copy, PartialEq)]
#[cfg_attr(feature = "bevy_reflect", derive(bevy_reflect::Reflect))]
#[cfg_attr(feature = "serialize", derive(serde::Serialize))]
#[cfg_attr(feature = "debug", derive(Debug))]
pub struct Spiral<L>(pub L, f32);

impl<L: Default> Default for Spiral<L> {
    fn default() -> Self {
        Self(L::default(), 1.0)
    }
}

impl<L: LengthFunction<Vec2>> NoiseFunction<Vec2> for Spiral<L> {
    type Output = Vec2;

    #[inline]
    fn evaluate(&self, input: Vec2, _seeds: &mut crate::rng::NoiseRng) -> Self::Output {
        let len = self.0.length_of(input);
        let theta = input.to_angle() * core::f32::consts::FRAC_1_PI * self.1;
        Vec2::new(theta * len.floor(), len)
    }
}

impl<T, G: Mul<f32, Output = G>> NoiseFunction<WithGradient<T, G>> for SNormToUNorm
where
    Self: NoiseFunction<T, Output = T>,
{
    type Output = WithGradient<T, G>;
    #[inline]
    fn evaluate(
        &self,
        input: WithGradient<T, G>,
        seeds: &mut crate::rng::NoiseRng,
    ) -> Self::Output {
        WithGradient {
            value: self.evaluate(input.value, seeds),
            gradient: input.gradient * 0.5,
        }
    }
}

impl<T, G: Mul<f32, Output = G>> NoiseFunction<WithGradient<T, G>> for UNormToSNorm
where
    Self: NoiseFunction<T, Output = T>,
{
    type Output = WithGradient<T, G>;
    #[inline]
    fn evaluate(
        &self,
        input: WithGradient<T, G>,
        seeds: &mut crate::rng::NoiseRng,
    ) -> Self::Output {
        WithGradient {
            value: self.evaluate(input.value, seeds),
            gradient: input.gradient * 2.0,
        }
    }
}

impl<T, G> NoiseFunction<WithGradient<T, G>> for Negate
where
    Self: NoiseFunction<T> + NoiseFunction<G>,
{
    type Output =
        WithGradient<<Self as NoiseFunction<T>>::Output, <Self as NoiseFunction<G>>::Output>;
    #[inline]
    fn evaluate(
        &self,
        input: WithGradient<T, G>,
        seeds: &mut crate::rng::NoiseRng,
    ) -> Self::Output {
        WithGradient {
            value: self.evaluate(input.value, seeds),
            gradient: self.evaluate(input.gradient, seeds),
        }
    }
}

impl<G: Neg<Output = G>> NoiseFunction<WithGradient<f32, G>> for Abs {
    type Output = WithGradient<f32, G>;

    #[inline]
    fn evaluate(
        &self,
        input: WithGradient<f32, G>,
        _seeds: &mut crate::rng::NoiseRng,
    ) -> Self::Output {
        if input.value > 0.0 {
            input
        } else {
            WithGradient {
                value: -input.value,
                gradient: -input.gradient,
            }
        }
    }
}

impl<G: Mul<f32, Output = G>> NoiseFunction<WithGradient<f32, G>> for Inverse {
    type Output = WithGradient<f32, G>;

    #[inline]
    fn evaluate(
        &self,
        input: WithGradient<f32, G>,
        _seeds: &mut crate::rng::NoiseRng,
    ) -> Self::Output {
        WithGradient {
            value: 1.0 / input.value,
            gradient: input.gradient * (-1.0 / (input.value * input.value)),
        }
    }
}

impl<G: Mul<f32, Output = G>> NoiseFunction<WithGradient<f32, G>> for Pow2 {
    type Output = WithGradient<f32, G>;

    #[inline]
    fn evaluate(
        &self,
        input: WithGradient<f32, G>,
        _seeds: &mut crate::rng::NoiseRng,
    ) -> Self::Output {
        WithGradient {
            value: input.value * input.value,
            gradient: input.gradient * (2.0 * input.value),
        }
    }
}

impl<G: Mul<f32, Output = G>> NoiseFunction<WithGradient<f32, G>> for Pow3 {
    type Output = WithGradient<f32, G>;

    #[inline]
    fn evaluate(
        &self,
        input: WithGradient<f32, G>,
        _seeds: &mut crate::rng::NoiseRng,
    ) -> Self::Output {
        WithGradient {
            value: input.value * input.value * input.value,
            gradient: input.gradient * (3.0 * input.value * input.value),
        }
    }
}

impl<G: Mul<f32, Output = G>> NoiseFunction<WithGradient<f32, G>> for Pow4 {
    type Output = WithGradient<f32, G>;

    #[inline]
    fn evaluate(
        &self,
        input: WithGradient<f32, G>,
        _seeds: &mut crate::rng::NoiseRng,
    ) -> Self::Output {
        WithGradient {
            value: (input.value * input.value) * (input.value * input.value),
            gradient: input.gradient * (4.0 * (input.value * input.value) * input.value),
        }
    }
}

impl<G: Mul<f32, Output = G>> NoiseFunction<WithGradient<f32, G>> for PowF {
    type Output = WithGradient<f32, G>;

    #[inline]
    fn evaluate(
        &self,
        input: WithGradient<f32, G>,
        _seeds: &mut crate::rng::NoiseRng,
    ) -> Self::Output {
        WithGradient {
            value: bevy_math::ops::powf(input.value, self.0),
            gradient: input.gradient * (self.0 * bevy_math::ops::powf(input.value, self.0 - 1.0)),
        }
    }
}

impl<T, G: Neg<Output = G>> NoiseFunction<WithGradient<T, G>> for ReverseUNorm
where
    Self: NoiseFunction<T>,
{
    type Output = WithGradient<<Self as NoiseFunction<T>>::Output, G>;

    #[inline]
    fn evaluate(
        &self,
        input: WithGradient<T, G>,
        seeds: &mut crate::rng::NoiseRng,
    ) -> Self::Output {
        WithGradient {
            value: self.evaluate(input.value, seeds),
            gradient: -input.gradient,
        }
    }
}