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::*;

/// Converts a cubemap face index (`0..6`) and normalized texel UV `(u, v)` in `[0.0, 1.0]^2` into a unit 3D direction.
///
/// # Arguments
///
/// - `usize` - Cubemap face index (`0=+X, 1=-X, 2=+Y, 3=-Y, 4=+Z, 5=-Z`).
/// - `f32` - Horizontal face coordinate in `[0.0, 1.0]`.
/// - `f32` - Vertical face coordinate in `[0.0, 1.0]`.
///
/// # Returns
///
/// - `Vec3` - Unit 3D world direction corresponding to `(face, u, v)`.
#[must_use]
pub fn cubemap_face_uv_to_dir(face: usize, u: f32, v: f32) -> Vec3 {
    let su: f32 = u * 2.0 - 1.0;
    let sv: f32 = v * 2.0 - 1.0;
    let raw: Vec3 = match face {
        0 => Vec3::new(1.0, -sv, -su),
        1 => Vec3::new(-1.0, -sv, su),
        2 => Vec3::new(su, 1.0, sv),
        3 => Vec3::new(su, -1.0, -sv),
        4 => Vec3::new(su, -sv, 1.0),
        _ => Vec3::new(-su, -sv, -1.0),
    };
    raw.normalize()
}

/// Maps a 3D direction vector to its dominant cubemap face index (`0..6`) and normalized UV `(u, v)` in `[0.0, 1.0]^2`.
///
/// # Arguments
///
/// - `Vec3` - Input 3D direction vector.
///
/// # Returns
///
/// - `(usize, f32, f32)` - Tuple `(face_index, u, v)` matching `cubemap_face_uv_to_dir`.
#[must_use]
pub fn dir_to_cubemap_face_uv(direction: Vec3) -> (usize, f32, f32) {
    let d: Vec3 = direction.normalize();
    let ax: f32 = d.get_x().abs();
    let ay: f32 = d.get_y().abs();
    let az: f32 = d.get_z().abs();
    if ax >= ay && ax >= az {
        let inv: f32 = 1.0 / ax.max(1.0e-6);
        if d.get_x() >= 0.0 {
            (
                0,
                (0.5 * (1.0 - d.get_z() * inv)).clamp(0.0, 1.0),
                (0.5 * (1.0 - d.get_y() * inv)).clamp(0.0, 1.0),
            )
        } else {
            (
                1,
                (0.5 * (1.0 + d.get_z() * inv)).clamp(0.0, 1.0),
                (0.5 * (1.0 - d.get_y() * inv)).clamp(0.0, 1.0),
            )
        }
    } else if ay >= ax && ay >= az {
        let inv: f32 = 1.0 / ay.max(1.0e-6);
        if d.get_y() >= 0.0 {
            (
                2,
                (0.5 * (1.0 + d.get_x() * inv)).clamp(0.0, 1.0),
                (0.5 * (1.0 + d.get_z() * inv)).clamp(0.0, 1.0),
            )
        } else {
            (
                3,
                (0.5 * (1.0 + d.get_x() * inv)).clamp(0.0, 1.0),
                (0.5 * (1.0 - d.get_z() * inv)).clamp(0.0, 1.0),
            )
        }
    } else {
        let inv: f32 = 1.0 / az.max(1.0e-6);
        if d.get_z() >= 0.0 {
            (
                4,
                (0.5 * (1.0 + d.get_x() * inv)).clamp(0.0, 1.0),
                (0.5 * (1.0 - d.get_y() * inv)).clamp(0.0, 1.0),
            )
        } else {
            (
                5,
                (0.5 * (1.0 - d.get_x() * inv)).clamp(0.0, 1.0),
                (0.5 * (1.0 - d.get_y() * inv)).clamp(0.0, 1.0),
            )
        }
    }
}

/// Projects a single directional radiance impulse into cosine-convolved L2 Spherical Harmonics coefficients.
///
/// # Arguments
///
/// - `&mut [[f32; 3]; SH9_BAND_COUNT]` - Mutable SH9 irradiance coefficient accumulator.
/// - `Vec3` - Unit direction vector pointing toward the incoming light.
/// - `Vec3` - Incoming linear HDR RGB radiance scaled by solid-angle weight.
pub fn accumulate_sh9_directional(
    coeffs: &mut [[f32; 3]; SH9_BAND_COUNT],
    direction: Vec3,
    radiance_weight: Vec3,
) {
    let basis: [f32; 9] = eval_sh_l2_basis(direction.normalize());
    let lobe_norm: [f32; 9] = [
        1.0,
        2.0 / 3.0,
        2.0 / 3.0,
        2.0 / 3.0,
        0.25,
        0.25,
        0.25,
        0.25,
        0.25,
    ];
    let rx: f32 = if radiance_weight.get_x().is_finite() {
        radiance_weight.get_x().max(0.0)
    } else {
        0.0
    };
    let ry: f32 = if radiance_weight.get_y().is_finite() {
        radiance_weight.get_y().max(0.0)
    } else {
        0.0
    };
    let rz: f32 = if radiance_weight.get_z().is_finite() {
        radiance_weight.get_z().max(0.0)
    } else {
        0.0
    };
    for band in 0..SH9_BAND_COUNT {
        let w: f32 = basis[band] * lobe_norm[band];
        coeffs[band][0] += rx * w;
        coeffs[band][1] += ry * w;
        coeffs[band][2] += rz * w;
    }
}