pub struct SimplexNoise;
const NORMALISE: f32 = 45.231;
impl SimplexNoise {
pub fn noise2(x: f32, y: f32) -> f32 {
const F2: f32 = 0.366_025_4; const G2: f32 = 0.211_324_87; let s = (x + y) * F2;
let i = (x + s).floor();
let j = (y + s).floor();
let t = (i + j) * G2;
let x0 = x - (i - t);
let y0 = y - (j - t);
let (i1, j1) = if x0 > y0 { (1.0, 0.0) } else { (0.0, 1.0) };
let x1 = x0 - i1 + G2;
let y1 = y0 - j1 + G2;
let x2 = x0 - 1.0 + 2.0 * G2;
let y2 = y0 - 1.0 + 2.0 * G2;
let n0 = corner_contrib(x0, y0, i as i32, j as i32);
let n1 = corner_contrib(x1, y1, (i + i1) as i32, (j + j1) as i32);
let n2 = corner_contrib(x2, y2, (i + 1.0) as i32, (j + 1.0) as i32);
(NORMALISE * (n0 + n1 + n2)).clamp(-1.0, 1.0)
}
pub fn noise3(x: f32, y: f32, z: f32) -> f32 {
let a = Self::noise2(x + z * 1.317, y - z * 0.713);
let b = Self::noise2(y + z * 0.911, z - x * 1.123);
let c = Self::noise2(z + x * 0.577, x - y * 1.491);
((a + b + c) * 0.5).clamp(-1.0, 1.0)
}
pub fn fbm2(x: f32, y: f32, octaves: u32, lacunarity: f32, gain: f32) -> f32 {
let mut sum = 0.0;
let mut amp = 1.0;
let mut total = 0.0;
let (mut px, mut py) = (x, y);
for o in 0..octaves.max(1) {
let (ox, oy) = OCTAVE_OFFSETS[o as usize % OCTAVE_OFFSETS.len()];
sum += Self::noise2(px + ox, py + oy) * amp;
total += amp;
amp *= gain;
px *= lacunarity;
py *= lacunarity;
}
if total > 0.0 {
sum / total
} else {
0.0
}
}
pub fn fbm3(x: f32, y: f32, z: f32, octaves: u32, lacunarity: f32, gain: f32) -> f32 {
let mut sum = 0.0;
let mut amp = 1.0;
let mut total = 0.0;
let (mut px, mut py, mut pz) = (x, y, z);
for o in 0..octaves.max(1) {
let (ox, oy) = OCTAVE_OFFSETS[o as usize % OCTAVE_OFFSETS.len()];
sum += Self::noise3(px + ox, py + oy, pz - ox) * amp;
total += amp;
amp *= gain;
px *= lacunarity;
py *= lacunarity;
pz *= lacunarity;
}
if total > 0.0 {
sum / total
} else {
0.0
}
}
pub fn turbulence2(x: f32, y: f32, octaves: u32) -> f32 {
let mut sum = 0.0;
let mut amp = 1.0;
let mut total = 0.0;
let (mut px, mut py) = (x, y);
for o in 0..octaves.max(1) {
let (ox, oy) = OCTAVE_OFFSETS[o as usize % OCTAVE_OFFSETS.len()];
sum += Self::noise2(px + ox, py + oy).abs() * amp;
total += amp;
amp *= 0.5;
px *= 2.0;
py *= 2.0;
}
if total > 0.0 {
sum / total
} else {
0.0
}
}
pub fn ridged2(x: f32, y: f32, octaves: u32) -> f32 {
1.0 - Self::turbulence2(x, y, octaves)
}
pub fn curl2(x: f32, y: f32, epsilon: f32) -> (f32, f32) {
let e = epsilon.max(1e-4);
let dx = (Self::noise2(x, y + e) - Self::noise2(x, y - e)) / (2.0 * e);
let dy = (Self::noise2(x + e, y) - Self::noise2(x - e, y)) / (2.0 * e);
(dx, -dy)
}
}
const OCTAVE_OFFSETS: [(f32, f32); 8] = [
(0.0, 0.0),
(37.19, -11.73),
(-61.37, 83.11),
(103.57, 29.41),
(-17.83, -97.29),
(71.03, 53.67),
(-131.11, 19.87),
(23.41, -67.53),
];
fn corner_contrib(x: f32, y: f32, i: i32, j: i32) -> f32 {
let t = 0.5 - x * x - y * y;
if t < 0.0 {
return 0.0;
}
let t2 = t * t;
let g = grad(hash(i, j), x, y);
t2 * t2 * g
}
fn hash(i: i32, j: i32) -> u32 {
let mut h = (i as u32)
.wrapping_mul(374761393)
.wrapping_add((j as u32).wrapping_mul(668265263));
h = (h ^ (h >> 13)).wrapping_mul(1274126177);
h ^ (h >> 16)
}
fn grad(hash: u32, x: f32, y: f32) -> f32 {
let h = hash & 7;
let u = if h < 4 { x } else { y };
let v = if h < 4 { y } else { x };
let sx = if h & 1 != 0 { -u } else { u };
let sy = if h & 2 != 0 { -2.0 * v } else { 2.0 * v };
sx + sy
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn every_variant_stays_in_range() {
for i in 0..400 {
let (x, y, z) = (i as f32 * 0.37, i as f32 * -0.21, i as f32 * 0.13);
assert!((-1.0..=1.0).contains(&SimplexNoise::noise2(x, y)), "noise2");
assert!(
(-1.0..=1.0).contains(&SimplexNoise::noise3(x, y, z)),
"noise3"
);
for octaves in [1u32, 2, 4, 8, 12] {
let f = SimplexNoise::fbm2(x, y, octaves, 2.0, 0.5);
assert!((-1.0..=1.0).contains(&f), "fbm2 {octaves} octaves: {f}");
let f3 = SimplexNoise::fbm3(x, y, z, octaves, 2.0, 0.5);
assert!((-1.0..=1.0).contains(&f3), "fbm3 {octaves} octaves: {f3}");
let t = SimplexNoise::turbulence2(x, y, octaves);
assert!((0.0..=1.0).contains(&t), "turbulence {octaves}: {t}");
let r = SimplexNoise::ridged2(x, y, octaves);
assert!((0.0..=1.0).contains(&r), "ridged {octaves}: {r}");
}
}
}
#[test]
fn more_octaves_add_finer_detail() {
let roughness = |octaves: u32| {
let step = 0.004;
let mut sum = 0.0;
let mut prev = SimplexNoise::fbm2(0.0, 0.5, octaves, 2.0, 0.5);
for i in 1..20_000 {
let v = SimplexNoise::fbm2(i as f32 * step, 0.5, octaves, 2.0, 0.5);
sum += (v - prev).abs();
prev = v;
}
sum / 20_000.0
};
let series: Vec<f32> = [2u32, 3, 4, 6, 8].iter().map(|o| roughness(*o)).collect();
for w in series.windows(2) {
assert!(w[1] > w[0], "roughness fell: {:.5} then {:.5}", w[0], w[1]);
}
let growth = series[series.len() - 1] / series[0];
assert!(
growth > 1.35,
"eight octaves are only {growth:.2}x as rough as two"
);
}
#[test]
fn the_octaves_do_not_all_share_an_origin() {
let at_origin: Vec<f32> = (1..=6)
.map(|o| SimplexNoise::fbm2(0.0, 0.0, o, 2.0, 0.5))
.collect();
let distinct = at_origin
.windows(2)
.filter(|w| (w[0] - w[1]).abs() > 1e-4)
.count();
assert!(
distinct >= 4,
"fbm at the origin barely moved with octave count: {at_origin:?}"
);
}
#[test]
fn turbulence_creases_where_plain_noise_does_not() {
let near_zero = |f: &dyn Fn(f32) -> f32| {
(0..4000)
.filter(|i| f(*i as f32 * 0.01).abs() < 0.02)
.count()
};
let turb = near_zero(&|x| SimplexNoise::turbulence2(x, 0.0, 1));
let plain = near_zero(&|x| SimplexNoise::noise2(x, 0.0) * 0.5 + 0.5);
assert!(
turb > plain * 3 + 5,
"turbulence reached zero {turb} times, plain noise {plain}"
);
}
#[test]
fn curl_is_divergence_free_and_two_noise_channels_are_not() {
let e = 0.01;
let divergence = |f: &dyn Fn(f32, f32) -> (f32, f32), x: f32, y: f32| {
let (ax, _) = f(x + e, y);
let (bx, _) = f(x - e, y);
let (_, ay) = f(x, y + e);
let (_, by) = f(x, y - e);
((ax - bx) + (ay - by)).abs() / (2.0 * e)
};
let curl = |x: f32, y: f32| SimplexNoise::curl2(x, y, 0.01);
let naive = |x: f32, y: f32| {
(
SimplexNoise::noise2(x, y),
SimplexNoise::noise2(x + 31.4, y - 17.2),
)
};
let mean = |f: &dyn Fn(f32, f32) -> (f32, f32)| {
let mut sum = 0.0;
for i in 0..600 {
sum += divergence(f, i as f32 * 0.07, i as f32 * -0.11);
}
sum / 600.0
};
let (c, n) = (mean(&curl), mean(&naive));
assert!(
c < n * 0.2,
"curl divergence {c:.4} is not clearly below the naive field's {n:.4}"
);
}
#[test]
fn noise3_varies_along_z_without_repeating() {
let a = SimplexNoise::noise3(1.0, 2.0, 0.0);
let b = SimplexNoise::noise3(1.0, 2.0, 0.05);
let far = SimplexNoise::noise3(1.0, 2.0, 7.3);
assert!((a - b).abs() < 0.3, "a small step in z jumped: {a} to {b}");
assert!((a - far).abs() > 0.02, "z barely changes anything");
}
}