use super::*;
#[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()
}
#[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),
)
}
}
}
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;
}
}