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
/// Revised Perlin noise (3D input, 1D output)
use stb_sys as sys;

/// This function computes a random value at the coordinate (x,y,z).
/// Adjacent random values are continuous but the noise fluctuates
/// its randomness with period 1, i.e. takes on wholly unrelated values
/// at integer points. Specifically, this implements Ken Perlin's
/// revised noise function from 2002.
///
/// The "wrap" parameters can be used to create wraparound noise that
/// wraps at powers of two. The numbers MUST be powers of two. Specify
/// 0 to mean "don't care". (The noise always wraps every 256 due
/// details of the implementation, even if you ask for larger or no
/// wrapping.)
pub fn stb_perlin_noise3(x: f32, y: f32, z: f32, x_wrap: i32, y_wrap: i32, z_wrap: i32) -> f32 {
    unsafe { sys::stb_perlin_noise3(x, y, z, x_wrap, y_wrap, z_wrap) }
}

/// Same as `stb_perlin_noise3`, but `seed` selects from multiple different variations of the
/// noise function.
/// The current implementation only uses the bottom 8 bits of 'seed', but possibly in the future
/// more bits will be used.
pub fn stb_perlin_noise3_seed(
    x: f32,
    y: f32,
    z: f32,
    x_wrap: i32,
    y_wrap: i32,
    z_wrap: i32,
    seed: i32,
) -> f32 {
    unsafe { sys::stb_perlin_noise3_seed(x, y, z, x_wrap, y_wrap, z_wrap, seed) }
}

pub fn stb_perlin_ridge_noise3(
    x: f32,
    y: f32,
    z: f32,
    lacunarity: f32,
    gain: f32,
    offset: f32,
    octaves: i32,
) -> f32 {
    unsafe { sys::stb_perlin_ridge_noise3(x, y, z, lacunarity, gain, offset, octaves) }
}

pub fn stb_perlin_fbm_noise3(
    x: f32,
    y: f32,
    z: f32,
    lacunarity: f32,
    gain: f32,
    octaves: i32,
) -> f32 {
    unsafe { sys::stb_perlin_fbm_noise3(x, y, z, lacunarity, gain, octaves) }
}

pub fn stb_perlin_turbulence_noise3(
    x: f32,
    y: f32,
    z: f32,
    lacunarity: f32,
    gain: f32,
    octaves: i32,
) -> f32 {
    unsafe { sys::stb_perlin_turbulence_noise3(x, y, z, lacunarity, gain, octaves) }
}

pub fn stb_perlin_noise3_wrap_nonpow2(
    x: f32,
    y: f32,
    z: f32,
    x_wrap: i32,
    y_wrap: i32,
    z_wrap: i32,
    seed: u8,
) -> f32 {
    unsafe { sys::stb_perlin_noise3_wrap_nonpow2(x, y, z, x_wrap, y_wrap, z_wrap, seed) }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn perlin_noise3() {
        let n = stb_perlin_noise3(0.5, 0.0, 0.01, 0, 0, 0);
        assert!(n > 0.0);
    }

    #[test]
    fn perlin_noise3_seed() {
        let n = stb_perlin_noise3_seed(0.01, 0.02, 0.03, 0, 0, 0, 123);
        assert!(n > 0.0);
    }

    #[test]
    fn perlin_ridge_noise3() {
        let n = stb_perlin_ridge_noise3(0.02, 0.04, 0.02, 2.0, 0.5, 1.0, 6);
        assert!(n > 0.0);
    }

    #[test]
    fn perlin_fbm_noise3() {
        let n = stb_perlin_fbm_noise3(0.4, 0.1, 0.3, 2.0, 0.5, 6);
        assert!(n > 0.0);
    }

    #[test]
    fn perlin_turbulence_noise3() {
        let n = stb_perlin_turbulence_noise3(0.04, 0.09, 0.91, 2.0, 0.5, 6);
        assert!(n > 0.0);
    }

    #[test]
    fn perlin_noise3_wrap_nonpow2() {
        let n = stb_perlin_noise3_wrap_nonpow2(0.04, 0.09, 0.91, 1, 2, 3, 123);
        assert!(n > 0.0);
    }
}