use super::identity::EnvironmentId;
use glam::Vec3;
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub enum FrameEnvironment {
#[default]
None,
Map(EnvironmentId),
Sky(Sky),
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Sky {
pub ground_height: f32,
pub rayleigh: f32,
pub mie: f32,
pub ozone: f32,
pub ground_albedo: [f32; 3],
pub sun_disc: f32,
pub aerial_perspective: f32,
}
impl Default for Sky {
fn default() -> Self {
Self {
ground_height: 0.,
rayleigh: 1.,
mie: 1.,
ozone: 1.,
ground_albedo: [0.3; 3],
sun_disc: 1.,
aerial_perspective: 1.,
}
}
}
impl Sky {
pub(crate) fn resolved(&self) -> Self {
let scale = |value: f32| {
if value.is_finite() && value > 0. {
value
} else {
0.
}
};
let albedo = |value: f32| {
if value.is_nan() {
0.
} else {
value.clamp(0., 1.)
}
};
Self {
ground_height: if self.ground_height.is_finite() {
self.ground_height
} else {
0.
},
rayleigh: scale(self.rayleigh),
mie: scale(self.mie),
ozone: scale(self.ozone),
ground_albedo: self.ground_albedo.map(albedo),
sun_disc: scale(self.sun_disc),
aerial_perspective: scale(self.aerial_perspective),
}
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct DirectionalLight {
pub direction: Vec3,
pub color: [f32; 3],
pub illuminance: f32,
pub shadow: Option<DirectionalShadow>,
pub fog_energy: f32,
pub shadow_opacity: f32,
pub angular_diameter: f32,
}
impl DirectionalLight {
pub const SUN_ANGULAR_DIAMETER: f32 = 0.53 * std::f32::consts::PI / 180.;
}
impl Default for DirectionalLight {
fn default() -> Self {
Self {
direction: Vec3::NEG_Z,
color: [1.; 3],
illuminance: std::f32::consts::PI,
shadow: None,
fog_energy: 1.,
shadow_opacity: 1.,
angular_diameter: Self::SUN_ANGULAR_DIAMETER,
}
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct DirectionalShadow {
pub distance: f32,
pub cascades: u32,
}
impl DirectionalShadow {
pub const DEFAULT: Self = Self {
distance: 150.,
cascades: 4,
};
}
impl Default for DirectionalShadow {
fn default() -> Self {
Self::DEFAULT
}
}
impl DirectionalLight {
pub(crate) fn is_on(&self) -> bool {
self.illuminance > 0. && self.direction.try_normalize().is_some()
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct HemisphereLight {
pub sky_color: [f32; 3],
pub ground_color: [f32; 3],
pub intensity: f32,
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct EnvironmentLight {
pub yaw: f32,
pub intensity: f32,
}
impl Default for EnvironmentLight {
fn default() -> Self {
Self {
yaw: 0.,
intensity: 1.,
}
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Backdrop {
Environment { yaw: f32, brightness: f32 },
Color([f32; 3]),
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Mist {
pub thin_color: [f32; 3],
pub dense_color: [f32; 3],
pub opacity: f32,
pub width: f32,
pub height: f32,
pub drift: [f32; 2],
}
impl Default for Mist {
fn default() -> Self {
Self {
thin_color: [0.; 3],
dense_color: [0.; 3],
opacity: 0.,
width: 1.,
height: 1.,
drift: [-0.025 / 7., 0.035 / 3.],
}
}
}