use super::error::LightProbeError;
use super::sh_math::{lp_normalize_dir, lp_sh_full_9, LP_SH_C0};
#[derive(Debug, Clone, PartialEq)]
pub struct IrradianceSH {
pub coefficients: [f32; 27],
pub order: usize,
}
impl IrradianceSH {
pub fn new() -> Self {
Self {
coefficients: [0.0_f32; 27],
order: 2,
}
}
pub fn from_coefficients(coeffs: [f32; 27]) -> Self {
Self {
coefficients: coeffs,
order: 2,
}
}
pub fn evaluate(&self, dir: [f32; 3]) -> Result<[f32; 3], LightProbeError> {
let d = lp_normalize_dir(dir)?;
let basis = lp_sh_full_9(d);
let mut rgb = [0.0_f32; 3];
for (i, &y) in basis.iter().enumerate() {
for (c, channel) in rgb.iter_mut().enumerate() {
*channel += y * self.coefficients[i * 3 + c];
}
}
Ok(rgb)
}
pub fn scale(&self, factor: f32) -> Self {
let mut out = self.clone();
for v in out.coefficients.iter_mut() {
*v *= factor;
}
out
}
pub fn add(&self, other: &IrradianceSH) -> Self {
let mut out = self.clone();
for i in 0..27 {
out.coefficients[i] += other.coefficients[i];
}
out
}
pub fn ambient(&self) -> [f32; 3] {
[
self.coefficients[0] * LP_SH_C0,
self.coefficients[1] * LP_SH_C0,
self.coefficients[2] * LP_SH_C0,
]
}
}
impl Default for IrradianceSH {
fn default() -> Self {
Self::new()
}
}