use crate::content::lighting::Sky;
use glam::Vec3;
pub(crate) static SKY_BRUNETON: super::Module = super::Module {
name: "sky_bruneton",
source: include_str!("sky_bruneton.wgsl"),
deps: &[],
};
pub(crate) static SKY: super::Module = super::Module {
name: "sky",
source: include_str!("sky.wgsl"),
deps: &[&SKY_BRUNETON],
};
pub(crate) const SKY_GROUND_RADIUS: f32 = 6_360_000.;
pub(crate) const SKY_TOP_RADIUS: f32 = 6_460_000.;
pub(crate) const SKY_RAYLEIGH_SCATTERING: [f32; 3] = [5.802e-6, 13.558e-6, 33.1e-6];
pub(crate) const SKY_RAYLEIGH_SCALE_HEIGHT: f32 = 8000.;
pub(crate) const SKY_MIE_SCATTERING: f32 = 3.996e-6;
pub(crate) const SKY_MIE_EXTINCTION: f32 = 4.440e-6;
pub(crate) const SKY_MIE_SCALE_HEIGHT: f32 = 1200.;
pub(crate) const SKY_OZONE_ABSORPTION: [f32; 3] = [0.650e-6, 1.881e-6, 0.085e-6];
pub(crate) const SKY_OZONE_PEAK: f32 = 25_000.;
pub(crate) const SKY_OZONE_HALF_WIDTH: f32 = 15_000.;
pub(crate) const SKY_TRANSMITTANCE_SAMPLES: u32 = 40;
pub(crate) const AERIAL_SIDE: u32 = 32;
pub(crate) const AERIAL_SLICES: u32 = 32;
pub(crate) const AERIAL_REACH: f32 = 32_000.;
const SKY_EPSILON: f32 = 1.;
#[repr(C)]
#[derive(Clone, Copy, Debug, PartialEq, bytemuck::Pod, bytemuck::Zeroable)]
pub(crate) struct SkyAtmosphereUniform {
pub ground_albedo: [f32; 3],
pub rayleigh: f32,
pub mie: f32,
pub ozone: f32,
pub padding: [f32; 2],
}
impl SkyAtmosphereUniform {
pub fn of(sky: &Sky) -> Self {
let sky = sky.resolved();
Self {
ground_albedo: sky.ground_albedo,
rayleigh: sky.rayleigh,
mie: sky.mie,
ozone: sky.ozone,
padding: [0.; 2],
}
}
}
pub(crate) fn camera_radius(sky: &Sky, eye: Vec3) -> f32 {
let altitude = eye.y - sky.resolved().ground_height;
let altitude = if altitude.is_finite() {
altitude.max(SKY_EPSILON)
} else {
SKY_EPSILON
};
SKY_GROUND_RADIUS + altitude
}
fn densities(atm: &SkyAtmosphereUniform, r: f32) -> Vec3 {
let h = (r - SKY_GROUND_RADIUS).clamp(0., SKY_TOP_RADIUS - SKY_GROUND_RADIUS);
let rayleigh = (-h / SKY_RAYLEIGH_SCALE_HEIGHT).exp();
let mie = (-h / SKY_MIE_SCALE_HEIGHT).exp();
let ozone = (1. - (h - SKY_OZONE_PEAK).abs() / SKY_OZONE_HALF_WIDTH).max(0.);
Vec3::new(rayleigh * atm.rayleigh, mie * atm.mie, ozone * atm.ozone)
}
fn extinction(atm: &SkyAtmosphereUniform, r: f32) -> Vec3 {
let density = densities(atm, r);
let absorption = Vec3::splat((SKY_MIE_EXTINCTION - SKY_MIE_SCATTERING) * density.y)
+ Vec3::from(SKY_OZONE_ABSORPTION) * density.z;
let scattering = Vec3::from(SKY_RAYLEIGH_SCATTERING) * density.x
+ Vec3::splat(SKY_MIE_SCATTERING * density.y);
absorption + scattering
}
fn distance_to_top_atmosphere_boundary(r: f32, mu: f32) -> f32 {
let discriminant = (r * r * (mu * mu - 1.) + SKY_TOP_RADIUS * SKY_TOP_RADIUS).max(0.);
(-r * mu + discriminant.sqrt()).max(0.)
}
fn horizon_mu(r: f32) -> f32 {
-(1. - (SKY_GROUND_RADIUS * SKY_GROUND_RADIUS) / (r * r))
.max(0.)
.sqrt()
}
fn optical_depth(atm: &SkyAtmosphereUniform, r: f32, mu: f32) -> Vec3 {
let t_max = distance_to_top_atmosphere_boundary(r, mu);
let dt = t_max / SKY_TRANSMITTANCE_SAMPLES as f32;
(0..SKY_TRANSMITTANCE_SAMPLES)
.map(|i| {
let t = dt * (i as f32 + 0.5);
let r_i = (t * t + 2. * r * mu * t + r * r).sqrt();
extinction(atm, r_i) * dt
})
.sum()
}
pub(crate) fn transmittance(atm: &SkyAtmosphereUniform, r: f32, mu: f32) -> Vec3 {
let mu = mu.max(horizon_mu(r));
(-optical_depth(atm, r, mu)).exp()
}
pub(crate) fn visible_sun_ratio(r: f32, mu: f32, angular_diameter: f32) -> f32 {
let horizon_angle = horizon_mu(r).acos();
let sun_zenith_angle = mu.clamp(-1., 1.).acos();
let half = angular_diameter * 0.5;
if sun_zenith_angle + half <= horizon_angle {
return 1.;
}
if sun_zenith_angle - half >= horizon_angle {
return 0.;
}
let d = (horizon_angle - sun_zenith_angle) / half;
(0.5 + d * 0.5).clamp(0., 1.)
}
pub(crate) fn disc_solid_angle(angular_diameter: f32) -> f32 {
angular_diameter * angular_diameter * 0.25 * std::f32::consts::PI
}
#[cfg(test)]
pub(crate) fn mirrors() -> Vec<crate::shading::layout_tests::Mirror> {
use crate::shading::layout_tests::mirror;
vec![mirror!(
"sky_transmittance",
"SkyAtmosphere",
SkyAtmosphereUniform,
[ground_albedo, rayleigh, mie, ozone]
)]
}
#[cfg(test)]
pub(crate) fn constants() -> Vec<crate::shading::layout_tests::Constant> {
use crate::shading::layout_tests::Constant;
let f = |name, value| Constant::new("sky_transmittance", name, naga::Literal::F32(value));
vec![
f("SKY_GROUND_RADIUS", SKY_GROUND_RADIUS),
f("SKY_TOP_RADIUS", SKY_TOP_RADIUS),
f("SKY_RAYLEIGH_SCALE_HEIGHT", SKY_RAYLEIGH_SCALE_HEIGHT),
f("SKY_MIE_SCATTERING", SKY_MIE_SCATTERING),
f("SKY_MIE_EXTINCTION", SKY_MIE_EXTINCTION),
f("SKY_MIE_SCALE_HEIGHT", SKY_MIE_SCALE_HEIGHT),
f("SKY_OZONE_PEAK", SKY_OZONE_PEAK),
f("SKY_OZONE_HALF_WIDTH", SKY_OZONE_HALF_WIDTH),
Constant::new(
"sky_transmittance",
"SKY_TRANSMITTANCE_SAMPLES",
naga::Literal::U32(SKY_TRANSMITTANCE_SAMPLES),
),
Constant::new(
"sky_transmittance",
"AERIAL_SIDE",
naga::Literal::U32(AERIAL_SIDE),
),
Constant::new(
"sky_transmittance",
"AERIAL_SLICES",
naga::Literal::U32(AERIAL_SLICES),
),
f("AERIAL_REACH", AERIAL_REACH),
]
}
#[cfg(test)]
pub(crate) fn vector_constants() -> Vec<(&'static str, &'static str, [f32; 3])> {
vec![
(
"sky_transmittance",
"SKY_RAYLEIGH_SCATTERING",
SKY_RAYLEIGH_SCATTERING,
),
(
"sky_transmittance",
"SKY_OZONE_ABSORPTION",
SKY_OZONE_ABSORPTION,
),
]
}
#[cfg(all(test, not(target_arch = "wasm32")))]
mod tests;