use std::f32::consts::PI;
#[derive(Clone, Copy, Debug, PartialEq, PartialOrd)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(transparent))]
pub struct Lux(pub f32);
#[derive(Clone, Copy, Debug, PartialEq, PartialOrd)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(transparent))]
pub struct Candela(pub f32);
#[derive(Clone, Copy, Debug, PartialEq, PartialOrd)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(transparent))]
pub struct Lumen(pub f32);
impl Lux {
pub const FULL_DAYLIGHT: Lux = Lux(100_000.0);
pub const OVERCAST: Lux = Lux(1_000.0);
pub const SUNRISE_SUNSET: Lux = Lux(400.0);
pub const OFFICE: Lux = Lux(400.0);
pub const LIVING_ROOM: Lux = Lux(50.0);
pub const FULL_MOON: Lux = Lux(0.25);
}
impl Candela {
pub const CANDLE: Candela = Candela(1.0);
}
impl Lumen {
pub const CANDLE: Lumen = Lumen(12.0);
pub const INCANDESCENT_40W: Lumen = Lumen(450.0);
pub const INCANDESCENT_60W: Lumen = Lumen(800.0);
pub const INCANDESCENT_100W: Lumen = Lumen(1_600.0);
pub const LED_10W: Lumen = Lumen(900.0);
pub fn to_point_candela(self) -> Candela {
Candela(self.0 / (4.0 * PI))
}
pub fn to_spot_candela(self, outer_angle: f32) -> Candela {
let solid_angle = 2.0 * PI * (1.0 - outer_angle.cos());
Candela(self.0 / solid_angle.max(1e-6))
}
}
impl From<Lux> for f32 {
fn from(v: Lux) -> f32 {
v.0
}
}
impl From<Candela> for f32 {
fn from(v: Candela) -> f32 {
v.0
}
}
impl From<Lumen> for f32 {
fn from(v: Lumen) -> f32 {
v.0
}
}
#[derive(Clone, Copy, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[non_exhaustive]
pub enum LightKind {
Directional {
direction: [f32; 3],
},
Point {
position: glam::DVec3,
range: f32,
radius: f32,
},
Spot {
position: glam::DVec3,
direction: [f32; 3],
range: f32,
inner_angle: f32,
outer_angle: f32,
radius: f32,
},
}
#[non_exhaustive]
#[derive(Clone, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct LightSource {
pub kind: LightKind,
pub colour: crate::colour::Colour,
pub intensity: f32,
pub importance: f32,
pub cast_shadows: bool,
pub channel_mask: u32,
}
impl Default for LightSource {
fn default() -> Self {
Self {
kind: LightKind::Directional {
direction: [0.4, 0.3, 1.5],
},
colour: crate::colour::Colour::WHITE,
intensity: core::f32::consts::PI,
importance: 1.0,
cast_shadows: true,
channel_mask: !0,
}
}
}
impl LightSource {
pub fn directional_lux(direction: [f32; 3], illuminance: Lux) -> Self {
Self {
kind: LightKind::Directional { direction },
intensity: illuminance.0,
..Self::default()
}
}
pub fn point_candela(
position: impl crate::scene::IntoWorldPosition,
intensity: Candela,
range: f32,
radius: f32,
) -> Self {
Self {
kind: LightKind::Point {
position: position.into_world_position(),
range,
radius,
},
intensity: intensity.0,
..Self::default()
}
}
pub fn point_lumens(
position: impl crate::scene::IntoWorldPosition,
flux: Lumen,
range: f32,
radius: f32,
) -> Self {
Self::point_candela(position, flux.to_point_candela(), range, radius)
}
#[allow(clippy::too_many_arguments)]
pub fn spot_candela(
position: impl crate::scene::IntoWorldPosition,
direction: [f32; 3],
intensity: Candela,
range: f32,
inner_angle: f32,
outer_angle: f32,
radius: f32,
) -> Self {
Self {
kind: LightKind::Spot {
position: position.into_world_position(),
direction,
range,
inner_angle,
outer_angle,
radius,
},
intensity: intensity.0,
..Self::default()
}
}
#[allow(clippy::too_many_arguments)]
pub fn spot_lumens(
position: impl crate::scene::IntoWorldPosition,
direction: [f32; 3],
flux: Lumen,
range: f32,
inner_angle: f32,
outer_angle: f32,
radius: f32,
) -> Self {
Self::spot_candela(
position,
direction,
flux.to_spot_candela(outer_angle),
range,
inner_angle,
outer_angle,
radius,
)
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum ShadowFilter {
Hard,
#[default]
Pcf,
PcfHigh,
Pcss,
PcssFast,
}
#[non_exhaustive]
#[derive(Clone, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct LightingSettings {
pub lights: Vec<LightSource>,
pub sky_colour: crate::colour::Colour,
pub ground_colour: crate::colour::Colour,
pub hemisphere_intensity: f32,
pub shadows: ShadowSettings,
#[cfg_attr(feature = "serde", serde(default))]
pub environment_intensity: crate::effects::environment::EnvironmentIntensity,
#[cfg_attr(feature = "serde", serde(default = "default_multi_bounce_ao"))]
pub multi_bounce_ao: bool,
#[cfg_attr(feature = "serde", serde(default))]
pub ao_micro_shadowing: bool,
}
#[cfg(feature = "serde")]
fn default_multi_bounce_ao() -> bool {
true
}
impl Default for LightingSettings {
fn default() -> Self {
Self {
lights: vec![LightSource::default()],
sky_colour: crate::colour::Colour::WHITE,
ground_colour: crate::colour::Colour::linear_rgb(0.6, 0.6, 0.6),
hemisphere_intensity: 0.55,
shadows: ShadowSettings::default(),
environment_intensity: Default::default(),
multi_bounce_ao: true,
ao_micro_shadowing: false,
}
}
}
#[non_exhaustive]
#[derive(Clone, Copy, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct ShadowSettings {
pub enabled: bool,
pub bias: f32,
pub extent_override: Option<f32>,
pub cascade_count: u32,
pub atlas_resolution: u32,
pub filter: ShadowFilter,
#[cfg_attr(feature = "serde", serde(default = "default_sun_angular_radius"))]
pub sun_angular_radius: f32,
#[cfg_attr(feature = "serde", serde(default = "default_punctual_shadow_layers"))]
pub punctual_shadow_layers: u32,
#[cfg_attr(
feature = "serde",
serde(default = "default_punctual_shadow_resolution")
)]
pub punctual_shadow_resolution: u32,
#[cfg_attr(feature = "serde", serde(default))]
pub shadow_distance: Option<f32>,
#[cfg_attr(feature = "serde", serde(default))]
pub cascade_split: CascadeSplit,
#[cfg_attr(feature = "serde", serde(default = "default_fade_fraction"))]
pub fade_fraction: f32,
}
#[derive(Clone, Copy, Debug, PartialEq)]
#[non_exhaustive]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum CascadeSplit {
Practical {
lambda: f32,
},
Fractions([f32; 3]),
}
#[cfg(feature = "serde")]
fn default_fade_fraction() -> f32 {
ShadowSettings::default().fade_fraction
}
impl Default for CascadeSplit {
fn default() -> Self {
Self::Practical { lambda: 0.75 }
}
}
#[cfg(feature = "serde")]
fn default_sun_angular_radius() -> f32 {
ShadowSettings::default().sun_angular_radius
}
#[cfg(feature = "serde")]
fn default_punctual_shadow_layers() -> u32 {
ShadowSettings::default().punctual_shadow_layers
}
#[cfg(feature = "serde")]
fn default_punctual_shadow_resolution() -> u32 {
ShadowSettings::default().punctual_shadow_resolution
}
impl Default for ShadowSettings {
fn default() -> Self {
Self {
enabled: true,
bias: 0.0,
extent_override: None,
cascade_count: 4,
atlas_resolution: 4096,
filter: ShadowFilter::Pcf,
sun_angular_radius: 0.01,
punctual_shadow_layers: 48,
punctual_shadow_resolution: 1024,
shadow_distance: None,
cascade_split: CascadeSplit::default(),
fade_fraction: 0.1,
}
}
}
impl LightingSettings {
pub fn daylight() -> Self {
let mut sun = LightSource::default();
sun.intensity = Lux::FULL_DAYLIGHT.0;
Self {
lights: vec![sun],
hemisphere_intensity: 8_000.0,
environment_intensity: crate::effects::environment::EnvironmentIntensity::Lux(
8_000.0 * PI,
),
..Self::default()
}
}
}