use crate::ids::EnvironmentMapId;
#[non_exhaustive]
#[derive(Clone, Copy, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct EnvironmentLighting {
pub environment: EnvironmentMapId,
pub rotation: f32,
pub diffuse_scale: f32,
pub specular_scale: f32,
}
impl EnvironmentLighting {
pub fn new(environment: EnvironmentMapId) -> Self {
Self {
environment,
rotation: 0.0,
diffuse_scale: 1.0,
specular_scale: 1.0,
}
}
pub fn with_rotation(mut self, radians: f32) -> Self {
self.rotation = radians;
self
}
}
#[non_exhaustive]
#[derive(Clone, Copy, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum EnvironmentIntensity {
Multiplier(f32),
Ev100(f32),
Lux(f32),
}
impl EnvironmentIntensity {
pub fn multiplier(self, measured_lux: f32) -> f32 {
match self {
Self::Multiplier(m) => m,
Self::Ev100(ev) => ev.exp2(),
Self::Lux(target) => {
if measured_lux > 1e-6 {
target / measured_lux
} else {
0.0
}
}
}
}
}
impl Default for EnvironmentIntensity {
fn default() -> Self {
Self::Multiplier(1.0)
}
}
#[non_exhaustive]
#[derive(Clone, Copy, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct EnvironmentBackground {
pub source: BackgroundSource,
pub intensity: Option<EnvironmentIntensity>,
pub rotation: Option<f32>,
pub blur: f32,
}
impl Default for EnvironmentBackground {
fn default() -> Self {
Self {
source: BackgroundSource::LightingEnvironment,
intensity: None,
rotation: None,
blur: 0.0,
}
}
}
impl EnvironmentBackground {
pub fn colour() -> Self {
Self {
source: BackgroundSource::Colour,
..Self::default()
}
}
pub fn environment(environment: EnvironmentMapId) -> Self {
Self {
source: BackgroundSource::Environment(environment),
..Self::default()
}
}
}
#[non_exhaustive]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum BackgroundSource {
LightingEnvironment,
Colour,
Environment(EnvironmentMapId),
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn intensity_modes_give_their_multiplier() {
assert_eq!(EnvironmentIntensity::Multiplier(3.0).multiplier(10.0), 3.0);
assert_eq!(EnvironmentIntensity::Ev100(3.0).multiplier(10.0), 8.0);
assert_eq!(EnvironmentIntensity::Lux(1000.0).multiplier(4.0), 250.0);
assert_eq!(EnvironmentIntensity::Lux(1000.0).multiplier(0.0), 0.0);
}
}