euv-core 0.57.0

Core cross-platform runtime for euv featuring arena-based fine-grained reactivity, deterministic layout, flat scene graph, multi-mode rendering, and direct OS platform adapters.
Documentation
use super::*;

/// Advances a deterministic SplitMix64 PRNG state and returns the next 64-bit value.
///
/// # Arguments
///
/// - `&mut u64` - Mutable 64-bit PRNG state.
///
/// # Returns
///
/// - `u64` - Next pseudo-random unsigned 64-bit integer.
#[must_use]
pub(crate) fn terrain_next_u64(state: &mut u64) -> u64 {
    *state = state.wrapping_add(0x9E37_79B9_7F4A_7C15);
    let mut z: u64 = *state;
    z = (z ^ (z >> 30)).wrapping_mul(0xBF58_476D_1CE4_E5B9);
    z = (z ^ (z >> 27)).wrapping_mul(0x94D0_49BB_1331_11EB);
    z ^ (z >> 31)
}

/// Generates a deterministic unit float in `[0.0, 1.0)` from a SplitMix64 PRNG state.
///
/// # Arguments
///
/// - `&mut u64` - Mutable 64-bit PRNG state.
///
/// # Returns
///
/// - `f32` - Uniformly distributed float in `[0.0, 1.0)`.
#[must_use]
pub(crate) fn terrain_next_f32(state: &mut u64) -> f32 {
    let bits: u64 = terrain_next_u64(state) >> 40;
    (bits as f32) * (1.0 / 16_777_216.0)
}

/// Computes a deterministic 2D integer lattice hash in `[0.0, 1.0)`.
///
/// # Arguments
///
/// - `i32` - Lattice X coordinate.
/// - `i32` - Lattice Z coordinate.
/// - `u64` - Noise seed.
///
/// # Returns
///
/// - `f32` - Deterministic pseudo-random scalar in `[0.0, 1.0)`.
#[must_use]
fn hash_lattice_2d(ix: i32, iz: i32, seed: u64) -> f32 {
    let mut h: u64 = seed
        ^ (ix as i64 as u64).wrapping_mul(0x9E37_79B9_7F4A_7C15)
        ^ (iz as i64 as u64).wrapping_mul(0xBF58_476D_1CE4_E5B9);
    h = (h ^ (h >> 30)).wrapping_mul(0x94D0_49BB_1331_11EB);
    h ^= h >> 31;
    ((h >> 40) as f32) * (1.0 / 16_777_216.0)
}

/// Evaluates smooth quintic-interpolated 2D value noise in `[-1.0, 1.0]`.
///
/// # Arguments
///
/// - `f32` - Continuous X coordinate.
/// - `f32` - Continuous Z coordinate.
/// - `u64` - Noise seed.
///
/// # Returns
///
/// - `f32` - Smooth 2D value noise sample in `[-1.0, 1.0]`.
#[must_use]
pub(crate) fn value_noise_2d(x: f32, z: f32, seed: u64) -> f32 {
    let ix: i32 = x.floor() as i32;
    let iz: i32 = z.floor() as i32;
    let fx: f32 = x - (ix as f32);
    let fz: f32 = z - (iz as f32);
    let ux: f32 = fx * fx * fx * (fx * (fx * 6.0 - 15.0) + 10.0);
    let uz: f32 = fz * fz * fz * (fz * (fz * 6.0 - 15.0) + 10.0);
    let v00: f32 = hash_lattice_2d(ix, iz, seed);
    let v10: f32 = hash_lattice_2d(ix + 1, iz, seed);
    let v01: f32 = hash_lattice_2d(ix, iz + 1, seed);
    let v11: f32 = hash_lattice_2d(ix + 1, iz + 1, seed);
    let nx0: f32 = v00 + (v10 - v00) * ux;
    let nx1: f32 = v01 + (v11 - v01) * ux;
    (nx0 + (nx1 - nx0) * uz) * 2.0 - 1.0
}

/// Evaluates multi-octave fractal Brownian motion (fBm) in `[-1.0, 1.0]`.
///
/// # Arguments
///
/// - `f32` - X coordinate.
/// - `f32` - Z coordinate.
/// - `u32` - Octave count.
/// - `f32` - Lacunarity frequency multiplier.
/// - `f32` - Persistence amplitude multiplier.
/// - `u64` - Seed value.
///
/// # Returns
///
/// - `f32` - Normalized multi-octave fBm height value.
#[must_use]
pub(crate) fn fbm_2d(
    x: f32,
    z: f32,
    octaves: u32,
    lacunarity: f32,
    persistence: f32,
    seed: u64,
) -> f32 {
    let oct: u32 = octaves.clamp(1, TERRAIN_MAX_OCTAVES);
    let mut amp: f32 = 1.0;
    let mut freq: f32 = 1.0;
    let mut sum: f32 = 0.0;
    let mut norm: f32 = 0.0;
    for i in 0..oct {
        let oct_seed: u64 = seed.wrapping_add((i as u64).wrapping_mul(0x517C_C1B7_2722_0A95));
        sum += value_noise_2d(x * freq, z * freq, oct_seed) * amp;
        norm += amp;
        freq *= lacunarity;
        amp *= persistence;
    }
    if norm > 1e-6 { sum / norm } else { 0.0 }
}

/// Evaluates multi-octave ridged multifractal noise in `[0.0, 1.0]` for sharp alpine crests.
///
/// # Arguments
///
/// - `f32` - X coordinate.
/// - `f32` - Z coordinate.
/// - `u32` - Octave count.
/// - `f32` - Lacunarity frequency multiplier.
/// - `f32` - Persistence amplitude multiplier.
/// - `u64` - Seed value.
///
/// # Returns
///
/// - `f32` - Normalized ridged multifractal sample in `[0.0, 1.0]`.
#[must_use]
pub(crate) fn ridged_multifractal_2d(
    x: f32,
    z: f32,
    octaves: u32,
    lacunarity: f32,
    persistence: f32,
    seed: u64,
) -> f32 {
    let oct: u32 = octaves.clamp(1, TERRAIN_MAX_OCTAVES);
    let mut amp: f32 = 1.0;
    let mut freq: f32 = 1.0;
    let mut weight: f32 = 1.0;
    let mut sum: f32 = 0.0;
    let mut norm: f32 = 0.0;
    for i in 0..oct {
        let oct_seed: u64 =
            seed.wrapping_add(((i + 17) as u64).wrapping_mul(0x9E37_79B9_7F4A_7C15));
        let n: f32 = value_noise_2d(x * freq, z * freq, oct_seed);
        let mut ridge: f32 = 1.0 - n.abs();
        ridge = ridge * ridge * weight;
        weight = (ridge * 1.8).clamp(0.0, 1.0);
        sum += ridge * amp;
        norm += amp;
        freq *= lacunarity;
        amp *= persistence;
    }
    if norm > 1e-6 {
        (sum / norm).clamp(0.0, 1.0)
    } else {
        0.0
    }
}

/// Evaluates Worley cellular ridge-channel distance `F2 - F1` in `[0.0, 1.0]` for canyon carving.
///
/// # Arguments
///
/// - `f32` - X coordinate.
/// - `f32` - Z coordinate.
/// - `u64` - Seed value.
///
/// # Returns
///
/// - `f32` - Normalized canyon channel factor (`0.0` at canyon floor, `1.0` on mesa interior).
#[must_use]
pub(crate) fn worley_canyon_2d(x: f32, z: f32, seed: u64) -> f32 {
    let cx: i32 = x.floor() as i32;
    let cz: i32 = z.floor() as i32;
    let mut f1: f32 = f32::INFINITY;
    let mut f2: f32 = f32::INFINITY;
    for dz in -1_i32..=1_i32 {
        for dx in -1_i32..=1_i32 {
            let nx: i32 = cx + dx;
            let nz: i32 = cz + dz;
            let jx: f32 = hash_lattice_2d(nx, nz, seed.wrapping_add(101));
            let jz: f32 = hash_lattice_2d(nx, nz, seed.wrapping_add(211));
            let px: f32 = (nx as f32) + jx;
            let pz: f32 = (nz as f32) + jz;
            let ddx: f32 = px - x;
            let ddz: f32 = pz - z;
            let dist: f32 = (ddx * ddx + ddz * ddz).sqrt();
            if dist < f1 {
                f2 = f1;
                f1 = dist;
            } else if dist < f2 {
                f2 = dist;
            }
        }
    }
    let gap: f32 = (f2 - f1).clamp(0.0, 1.0);
    let t: f32 = (gap / 0.35).clamp(0.0, 1.0);
    t * t * (3.0 - 2.0 * t)
}

/// Applies smooth step terracing to a normalized height value in `[0.0, 1.0]`.
///
/// # Arguments
///
/// - `f32` - Normalized elevation in `[0.0, 1.0]`.
/// - `u32` - Number of plateau steps.
/// - `f32` - Blending strength in `[0.0, 1.0]`.
///
/// # Returns
///
/// - `f32` - Terraced normalized height.
#[must_use]
pub(crate) fn apply_terracing(norm_h: f32, steps: u32, strength: f32) -> f32 {
    if steps < 2 || strength <= 1e-4 {
        return norm_h;
    }
    let s: f32 = steps as f32;
    let scaled: f32 = norm_h.clamp(0.0, 1.0) * s;
    let base: f32 = scaled.floor();
    let frac: f32 = scaled - base;
    let smooth_step: f32 = frac * frac * (3.0 - 2.0 * frac);
    let sharp: f32 = smooth_step * smooth_step;
    let terraced: f32 = (base + sharp) / s;
    norm_h + (terraced - norm_h) * strength.clamp(0.0, 1.0)
}