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: [f32; 3],
range: f32,
radius: f32,
},
Spot {
position: [f32; 3],
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: [f32; 3],
pub intensity: f32,
pub importance: f32,
pub cast_shadows: bool,
}
impl Default for LightSource {
fn default() -> Self {
Self {
kind: LightKind::Directional {
direction: [0.4, 0.3, 1.5],
},
colour: [1.0, 1.0, 1.0],
intensity: core::f32::consts::PI,
importance: 1.0,
cast_shadows: true,
}
}
}
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: [f32; 3], intensity: Candela, range: f32, radius: f32) -> Self {
Self {
kind: LightKind::Point {
position,
range,
radius,
},
intensity: intensity.0,
..Self::default()
}
}
pub fn point_lumens(position: [f32; 3], 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: [f32; 3],
direction: [f32; 3],
intensity: Candela,
range: f32,
inner_angle: f32,
outer_angle: f32,
radius: f32,
) -> Self {
Self {
kind: LightKind::Spot {
position,
direction,
range,
inner_angle,
outer_angle,
radius,
},
intensity: intensity.0,
..Self::default()
}
}
#[allow(clippy::too_many_arguments)]
pub fn spot_lumens(
position: [f32; 3],
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)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum PointShadowMode {
Cone,
#[default]
Cube,
}
pub const MAX_POINT_SHADOW_LIGHTS: u32 = 8;
pub const POINT_SHADOW_FACE_SIZE: u32 = 1024;
#[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: [f32; 3],
pub ground_colour: [f32; 3],
pub hemisphere_intensity: f32,
pub shadows: ShadowSettings,
}
impl Default for LightingSettings {
fn default() -> Self {
Self {
lights: vec![LightSource::default()],
sky_colour: [1.0, 1.0, 1.0],
ground_colour: [0.6, 0.6, 0.6],
hemisphere_intensity: 0.4,
shadows: ShadowSettings::default(),
}
}
}
#[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,
pub pcss_light_radius: f32,
pub point_shadow_mode: PointShadowMode,
}
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,
pcss_light_radius: 0.02,
point_shadow_mode: PointShadowMode::default(),
}
}
}
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,
..Self::default()
}
}
}