use super::blend::ProbeBlendMode;
use super::error::LightProbeError;
use super::probe::LightProbe;
#[derive(Debug, Clone)]
pub struct LightProbeConfig {
pub n_samples_projection: usize,
pub max_probes: usize,
pub blend_mode: ProbeBlendMode,
}
impl Default for LightProbeConfig {
fn default() -> Self {
Self {
n_samples_projection: 10_000,
max_probes: 64,
blend_mode: ProbeBlendMode::WeightedAverage,
}
}
}
#[derive(Debug, Clone)]
pub struct LightProbeStats {
pub n_probes: usize,
pub mean_intensity: f32,
pub max_coefficient: f32,
pub ambient_rgb: [f32; 3],
pub sh_energy: f32,
}
pub fn lp_compute_stats(probes: &[LightProbe]) -> Result<LightProbeStats, LightProbeError> {
if probes.is_empty() {
return Err(LightProbeError::EmptyProbeList);
}
let n = probes.len();
let mean_intensity = probes.iter().map(|p| p.intensity).sum::<f32>() / n as f32;
let mut max_coefficient = 0.0_f32;
let mut sh_energy = 0.0_f32;
let mut ambient_sum = [0.0_f32; 3];
for probe in probes {
for &c in &probe.irradiance.coefficients {
let abs = c.abs();
if abs > max_coefficient {
max_coefficient = abs;
}
sh_energy += c * c;
}
let amb = probe.irradiance.ambient();
ambient_sum[0] += amb[0];
ambient_sum[1] += amb[1];
ambient_sum[2] += amb[2];
}
let inv_n = 1.0 / n as f32;
Ok(LightProbeStats {
n_probes: n,
mean_intensity,
max_coefficient,
ambient_rgb: [
ambient_sum[0] * inv_n,
ambient_sum[1] * inv_n,
ambient_sum[2] * inv_n,
],
sh_energy,
})
}
pub fn lp_format_stats(stats: &LightProbeStats) -> String {
format!(
"LightProbeStats {{ n_probes: {}, mean_intensity: {:.4}, max_coefficient: {:.6}, \
ambient_rgb: [{:.4}, {:.4}, {:.4}], sh_energy: {:.6} }}",
stats.n_probes,
stats.mean_intensity,
stats.max_coefficient,
stats.ambient_rgb[0],
stats.ambient_rgb[1],
stats.ambient_rgb[2],
stats.sh_energy,
)
}
pub fn lp_format_config(config: &LightProbeConfig) -> String {
let mode_str = match config.blend_mode {
ProbeBlendMode::Nearest => "Nearest",
ProbeBlendMode::WeightedAverage => "WeightedAverage",
ProbeBlendMode::VolumeWeighted => "VolumeWeighted",
};
format!(
"LightProbeConfig {{ n_samples_projection: {}, max_probes: {}, blend_mode: {} }}",
config.n_samples_projection, config.max_probes, mode_str,
)
}