The native runtime for three.js: three.js, down to the metal. 45× smaller than Electron, on a Sony PS3, and path-traced at 60 fps on ray-tracing GPUs.
Rust electron-alternativegame-engineglobal-illuminationgraphicsjavascript-runtimemetalpath-tracingray-tracingreal-time-renderingrenderingrustthreejstypescriptvoxelvulkanwebgpuwgpu
Cloud cover, in place: scene.cloudCover turns an atmosphere sky overcast (CIE standard overcast dome lit by the clear day), the sun's direct light dimmed with it
Evan (Yifeng) Wang03f667929767
diff --git a/packages/scene/src/core.ts b/packages/scene/src/core.tsindex 4f4e997..e60db45 100644--- a/packages/scene/src/core.ts+++ b/packages/scene/src/core.ts@@ -878,6 +878,12 @@ export class Scene extends Object3D {wetness = 0;/** Rain intensity 0..1 (ripples on wet ground). */rain = 0;+ /**+ * Cloud cover 0..1 of a `SkyAtmosphere` sky, changed in place: the sky goes over to an overcast dome (CIE standard+ * overcast, lit by what the clear sky and sun would give the ground), the sun's direct light dims to nothing, and a+ * city's night glow brightens off the cloud base.+ */+ cloudCover = 0;}// ---------------------------------------------------------------------------------------------diff --git a/packages/scene/src/engine.ts b/packages/scene/src/engine.tsindex 227d3c6..28a2e88 100644--- a/packages/scene/src/engine.ts+++ b/packages/scene/src/engine.ts@@ -98,6 +98,7 @@ export interface EnvironmentDesc {fog?: FogDesc;wetness?: number;rain?: number;+ cloudCover?: number;}export interface MaterialDesc {@@ -1372,6 +1373,7 @@ export class SceneBinding {}if (scene.wetness > 0) e.wetness = scene.wetness;if (scene.rain > 0) e.rain = scene.rain;+ if (scene.cloudCover > 0) e.cloudCover = Math.min(1, scene.cloudCover);return e;}diff --git a/renderer/src/desc.rs b/renderer/src/desc.rsindex 37b4392..5b64b39 100644--- a/renderer/src/desc.rs+++ b/renderer/src/desc.rs@@ -110,6 +110,10 @@ pub struct Environment {/// Rain intensity 0..1 (material functions' `i.rain`: ripples on wet surfaces).#[serde(default)]pub rain: f32,+ /// Cloud cover 0..1 of an atmosphere sky (`sky::bake_atmosphere_tables`: an overcast dome, the sun dimmed), changed+ /// in place.+ #[serde(default, rename = "cloudCover")]+ pub cloud_cover: f32,}/// Exponential height fog (UE ExponentialHeightFog): extinction `density * exp(-heightFalloff * (y - height))`.diff --git a/renderer/src/lib.rs b/renderer/src/lib.rsindex 293d369..4480ed4 100644--- a/renderer/src/lib.rs+++ b/renderer/src/lib.rs@@ -485,14 +485,14 @@ pub fn prepare(g: &gpu::Gpu, sc: &mut scene::Scene, timings: &mut Vec<(String, fstatic LAST: std::sync::Mutex<Option<(Vec<u32>, std::sync::Arc<sky::SkyBake>)>> = std::sync::Mutex::new(None);let alt = sc.camera.position.y.max(0.0);let (bw, bh) = if *dynamic { sky::DYNAMIC_SKY } else { (1024, 512) };- let key: Vec<u32> = [*rayleigh, *mie, *mie_g, ground.x, ground.y, ground.z, sun_dir.x, sun_dir.y, sun_dir.z, sun_lux.x, sun_lux.y, sun_lux.z, alt, glow.uniform.x, glow.uniform.y, glow.uniform.z, glow.horizon.x, glow.horizon.y, glow.horizon.z, glow.height, bw as f32].iter().map(|f| f.to_bits()).collect();+ let key: Vec<u32> = [*rayleigh, *mie, *mie_g, ground.x, ground.y, ground.z, sun_dir.x, sun_dir.y, sun_dir.z, sun_lux.x, sun_lux.y, sun_lux.z, alt, glow.uniform.x, glow.uniform.y, glow.uniform.z, glow.horizon.x, glow.horizon.y, glow.horizon.z, glow.height, bw as f32, sc.cloud_cover].iter().map(|f| f.to_bits()).collect();let mut last = LAST.lock().unwrap();let bake = match last.as_ref() {Some((k, b)) if *k == key => b.clone(),_ => {let atm = sky::Atmosphere::earth(*rayleigh, *mie, *mie_g, *ground);- let b = std::sync::Arc::new(if *dynamic || !glow.is_zero() {- sky::bake_atmosphere_tables(&atm, &sky::SkyTables::new(&atm), *sun_dir, *sun_lux, alt, bw, bh, *glow)+ let b = std::sync::Arc::new(if *dynamic || !glow.is_zero() || sc.cloud_cover > 0.0 {+ sky::bake_atmosphere_tables(&atm, &sky::SkyTables::new(&atm), *sun_dir, *sun_lux, alt, bw, bh, *glow, sc.cloud_cover)} else {sky::bake_atmosphere(&atm, *sun_dir, *sun_lux, alt, bw, bh)});diff --git a/renderer/src/live.rs b/renderer/src/live.rsindex 7ec315e..d727c19 100644--- a/renderer/src/live.rs+++ b/renderer/src/live.rs@@ -441,7 +441,8 @@ const SKY_THREADS: usize = 3;/// trails the sun by one bake (~10–40 ms at `sky::DYNAMIC_SKY`). Lockstep runs wait for it, so they repeat.struct DynamicSky {atm: Arc<crate::sky::Atmosphere>,- ask: mpsc::Sender<(glam::Vec3, glam::Vec3)>,+ /// (sun direction, sun illuminance above the atmosphere, cloud cover)+ ask: mpsc::Sender<(glam::Vec3, glam::Vec3, f32)>,done: mpsc::Receiver<(gpu::EnvMap, Vec<f32>, f32)>,/// The brightest sky texel from which the environment is light-sampled (`Prepared::env_nee_floor`).env_nee_floor: f32,@@ -454,7 +455,7 @@ impl DynamicSky {};let atm = Arc::new(crate::sky::Atmosphere::earth(*rayleigh_scale, *mie_scale, *mie_anisotropy, glam::Vec3::from(*ground_albedo)));let glow = crate::sky::glow_of(glow, horizon_glow);- let (ask, asked) = mpsc::channel::<(glam::Vec3, glam::Vec3)>();+ let (ask, asked) = mpsc::channel::<(glam::Vec3, glam::Vec3, f32)>();let (tx, done) = mpsc::channel();let a = atm.clone();std::thread::Builder::new()@@ -465,8 +466,8 @@ impl DynamicSky {Some(p) => p.install(|| crate::sky::SkyTables::new(&a)),None => crate::sky::SkyTables::new(&a),};- let bake = |sun: (glam::Vec3, glam::Vec3)| {- let b = crate::sky::bake_atmosphere_tables(&a, &tables, sun.0, sun.1, altitude, w, h, glow);+ let bake = |sun: (glam::Vec3, glam::Vec3, f32)| {+ let b = crate::sky::bake_atmosphere_tables(&a, &tables, sun.0, sun.1, altitude, w, h, glow, sun.2);let env = gpu::EnvMap { width: b.width, height: b.height, texels: b.texels };let cdf = crate::build_env_cdf(&env);let max = env.texels.iter().map(|t| 0.2126 * t[0] + 0.7152 * t[1] + 0.0722 * t[2]).fold(0.0f32, f32::max);@@ -1107,6 +1108,8 @@ pub struct LiveCore {pub gpu_blas_ms: f32,/// CPU time the last `render` spent waiting for the oldest frame in flight (ms; frame log `wait`): GPU-bound frames.pub cpu_wait_ms: f32,+ /// The cloud cover the sky and the sun light were made with (`move_sun`).+ sky_cover: f32,}/// GPU time per frame from timestamp queries (start of the first trace pass .. end of the post pass), read back a few@@ -1459,8 +1462,9 @@ impl LiveCore {let restir = std::env::var("THREERT_RESTIR").is_ok_and(|v| v == "1");let resv = gpu::reservoirs(&g.device, if restir { w as u64 * h as u64 } else { 1 });let kernel_bg = gpu::vis_bind_group(&g.device, &g.vis_layout, raster.as_ref().map_or(&g.no_vis_view, |r| &r.vis), &cache.as_ref().map_or_else(|| gpu::CacheBuffers::new(&g.device, 1), |c| gpu::CacheBuffers { keys: c.bufs.keys.clone(), acc: c.bufs.acc.clone(), val: c.bufs.val.clone() }), &resv, &targets.refl, &targets.motion, &targets.direct);- let mut core = LiveCore { particle_spawns: Default::default(), grid_sources: Default::default(), g, passes, targets, sc, presets: presets.to_path_buf(), assets: assets.to_path_buf(), w, h, seq: 0, aspect: w as f32 / h as f32, cam, prev_cam: cam, frame: 0, history_len: 0.0, reset: true, cap_next: None, bloom: ([0.0; 6], [0.0; 6]), vignette: 0.0, post: [0.0; 4], blur: (0.0, 0.0), still: 0, inflight: Default::default(), gpu_ms: 0.0, gpu_frame: 0, gpu_trace_ms: 0.0, edit_ms: 0.0, instanced_ms: 0.0, edit_split: EditSplit::default(), rebuild_ms: 0.0, fallback: None, fallbacks: 0, bounces: 4, view_mode: std::env::var("THREERT_VIEW_MODE").ok().and_then(|v| v.parse().ok()).unwrap_or(0), fixed_dt: None, motion_blur_off: false, ghost_fix_off: false, triangles, scene_name, time: 0.0, frame_dt: 1.0 / 60.0, last_frame: Instant::now(), post_key: String::new(), render_scale: 1.0, dynamic_res: false, over_budget: (0, 0), profile, timer, raster, cache, restir, restir_prev: None, kernel_bg, prev_span: None, reposed: false, emitters_moved: false, cold_frames: 0, pose_span: None, pose_reposed: false, inst_moving: vec![], inst_moved: false, prev_isz: (0, 0), lit_changed: 0, spark_lights: false, cpu_skin_ms: 0.0, cpu_upload_ms: 0.0, gpu_blas_ms: 0.0, cpu_wait_ms: 0.0 };+ let mut core = LiveCore { particle_spawns: Default::default(), grid_sources: Default::default(), g, passes, targets, sc, presets: presets.to_path_buf(), assets: assets.to_path_buf(), w, h, seq: 0, aspect: w as f32 / h as f32, cam, prev_cam: cam, frame: 0, history_len: 0.0, reset: true, cap_next: None, bloom: ([0.0; 6], [0.0; 6]), vignette: 0.0, post: [0.0; 4], blur: (0.0, 0.0), still: 0, inflight: Default::default(), gpu_ms: 0.0, gpu_frame: 0, gpu_trace_ms: 0.0, edit_ms: 0.0, instanced_ms: 0.0, edit_split: EditSplit::default(), rebuild_ms: 0.0, fallback: None, fallbacks: 0, bounces: 4, view_mode: std::env::var("THREERT_VIEW_MODE").ok().and_then(|v| v.parse().ok()).unwrap_or(0), fixed_dt: None, motion_blur_off: false, ghost_fix_off: false, triangles, scene_name, time: 0.0, frame_dt: 1.0 / 60.0, last_frame: Instant::now(), post_key: String::new(), render_scale: 1.0, dynamic_res: false, over_budget: (0, 0), profile, timer, raster, cache, restir, restir_prev: None, kernel_bg, prev_span: None, reposed: false, emitters_moved: false, cold_frames: 0, pose_span: None, pose_reposed: false, inst_moving: vec![], inst_moved: false, prev_isz: (0, 0), lit_changed: 0, spark_lights: false, cpu_skin_ms: 0.0, cpu_upload_ms: 0.0, gpu_blas_ms: 0.0, cpu_wait_ms: 0.0, sky_cover: 0.0 };core.spark_lights = core.sc.tables.emitter_tris.iter().any(|e| e.nominal);+ core.sky_cover = core.sc.tables.cloud_cover;core.apply_post(&pp);Ok(core)}@@ -1736,7 +1740,9 @@ impl LiveCore {self.cap_next = Some(self.cap_next.map_or(16.0, |c| c.min(16.0)));self.lit_changed = self.frame.max(1);}- if changes.sun {+ // (A new cloud cover re-bakes the sky and dims the sun as a sun move does.)+ if changes.sun || self.sc.tables.cloud_cover != self.sky_cover {+ self.sky_cover = self.sc.tables.cloud_cover;self.move_sun(world);}if changes.exposure {@@ -1755,7 +1761,8 @@ impl LiveCore {self.sc.sky = DynamicSky::new(world, size, alt, floor);}let Some(sky) = self.sc.sky.as_ref() else { return };- let colour = crate::sky::sun_illuminance(&sky.atm, dir, lux);+ let cover = self.sc.tables.cloud_cover;+ let colour = crate::sky::sun_illuminance(&sky.atm, dir, lux, cover);let e = &mut self.sc.tables;if let (Some(idx), Some(o)) = (e.light_index.get(&id), world.objects.get(&id)) {if let crate::world::Kind::Light(l) = &o.kind {@@ -1768,7 +1775,7 @@ impl LiveCore {}}}- let _ = sky.ask.send((dir, lux));+ let _ = sky.ask.send((dir, lux, cover));if self.fixed_dt.is_some() {if let Ok(b) = sky.done.recv() {self.put_sky(b);diff --git a/renderer/src/scene.rs b/renderer/src/scene.rsindex 401fc03..3e34f40 100644--- a/renderer/src/scene.rs+++ b/renderer/src/scene.rs@@ -445,6 +445,8 @@ pub struct Scene {/// Global wetness and rain intensity (0..1).pub wetness: f32,pub rain: f32,+ /// Cloud cover (0..1) of an atmosphere sky.+ pub cloud_cover: f32,pub preset: desc::Preset,pub stats: HashMap<&'static str, f64>,/// Triangle ranges per object (name, first, end), in `tris` order: picking and selection outlines.@@ -1783,6 +1785,7 @@ pub fn build(w: &World, assets: &Path, presets_dir: &Path, overrides: Option<&sefog: env.fog.clone(),wetness: env.wetness.clamp(0.0, 1.0),rain: env.rain.clamp(0.0, 1.0),+ cloud_cover: env.cloud_cover.clamp(0.0, 1.0),preset,stats,tri_nodes,diff --git a/renderer/src/sky.rs b/renderer/src/sky.rsindex c219608..019f5c0 100644--- a/renderer/src/sky.rs+++ b/renderer/src/sky.rs@@ -269,7 +269,7 @@ const SKY_LIGHT_CUBE: f32 = 128.0;/// sky fades to black across one texel row of the side faces at the horizon (0.9 deg at a face's centre, 0.63 at its/// edges). UE's path tracer shows that fade below every horizon (Khronos layer B: the band beyond a 2 km ground plane).pub fn bake_atmosphere(a: &Atmosphere, sun_dir: Vec3, sun_lux: Vec3, altitude_m: f32, width: u32, height: u32) -> SkyBake {- bake_atmosphere_with(a, &Luts::build(a), sun_dir, sun_lux, altitude_m, width, height, Glow::default())+ bake_atmosphere_with(a, &Luts::build(a), sun_dir, sun_lux, altitude_m, width, height, Glow::default(), 0.0)}/// The atmosphere's transmittance and multiple-scattering tables: they depend on the atmosphere alone, so a sky whose@@ -310,19 +310,44 @@ impl Glow {}}-/// `bake_atmosphere` with tables built once (`SkyTables`), plus the night sky's `glow` above the horizon.-pub fn bake_atmosphere_tables(a: &Atmosphere, t: &SkyTables, sun_dir: Vec3, sun_lux: Vec3, altitude_m: f32, width: u32, height: u32, glow: Glow) -> SkyBake {- bake_atmosphere_with(a, &t.0, sun_dir, sun_lux, altitude_m, width, height, glow)+/// `bake_atmosphere` with tables built once (`SkyTables`), plus the night sky's `glow` above the horizon and a cloud+/// `cover` (0..1, `overcast`).+#[allow(clippy::too_many_arguments)]+pub fn bake_atmosphere_tables(a: &Atmosphere, t: &SkyTables, sun_dir: Vec3, sun_lux: Vec3, altitude_m: f32, width: u32, height: u32, glow: Glow, cover: f32) -> SkyBake {+ bake_atmosphere_with(a, &t.0, sun_dir, sun_lux, altitude_m, width, height, glow, cover)+}++/// The sun's illuminance at the ground (lux, RGB) for a sun of `sun_lux` above the atmosphere under a cloud `cover`,+/// as the bake gives it: what a directional sun light carries when it moves without a new bake.+pub fn sun_illuminance(a: &Atmosphere, sun_dir: Vec3, sun_lux: Vec3, cover: f32) -> Vec3 {+ sun_lux * a.sun_transmittance_ue(sun_dir) * cloud_transmittance(cover)+}++/// The sun's direct light through a cloud deck of `cover` (0..1): the share of time it is not behind a cloud, as a mean+/// (a thick deck passes no direct light).+pub fn cloud_transmittance(cover: f32) -> f32 {+ 1.0 - cover.clamp(0.0, 1.0)}-/// The sun's illuminance at the ground (lux, RGB) for a sun of `sun_lux` above the atmosphere, as the bake gives it:-/// what a directional sun light carries when it moves without a new bake.-pub fn sun_illuminance(a: &Atmosphere, sun_dir: Vec3, sun_lux: Vec3) -> Vec3 {- sun_lux * a.sun_transmittance_ue(sun_dir)+/// An overcast deck's diffuse transmittance: the ground's illuminance under full cover against what the clear sky and+/// sun give it (stratus / nimbostratus pass 0.1-0.4 of it; a mid value).+pub const OVERCAST_TRANSMITTANCE: f32 = 0.3;++/// A city's light dome off a cloud base: clouds scatter the upward light back, overcast city skies read several times+/// as bright as clear ones at night (Kyba et al. 2011, Berlin: up to ~10x at the zenith; a low value).+pub const OVERCAST_GLOW: f32 = 3.0;++/// The CIE standard overcast sky (ISO 15469 type 1): radiance at elevation `e` (radians) for a horizontal illuminance+/// `e_h`: L(e) = L_z (1 + 2 sin e) / 3, with E_h = 7 pi L_z / 9.+pub fn cie_overcast(e_h: Vec3, e: f32) -> Vec3 {+ e_h * (9.0 / (7.0 * PI)) * ((1.0 + 2.0 * e.max(0.0).sin()) / 3.0)}+/// With a cloud `cover`: the atmosphere's sky (and glow) blended with an overcast dome (`cie_overcast`) whose+/// illuminance is OVERCAST_TRANSMITTANCE of what the clear sky and sun give the ground (their luminance, white), the+/// glow at OVERCAST_GLOW x.#[allow(clippy::too_many_arguments)]-fn bake_atmosphere_with(a: &Atmosphere, luts: &Luts, sun_dir: Vec3, sun_lux: Vec3, altitude_m: f32, width: u32, height: u32, glow: Glow) -> SkyBake {+fn bake_atmosphere_with(a: &Atmosphere, luts: &Luts, sun_dir: Vec3, sun_lux: Vec3, altitude_m: f32, width: u32, height: u32, glow: Glow, cover: f32) -> SkyBake {// Planet frame: Z up. Map Y-up world -> planet frame (x, z, y).let to_planet = |d: Vec3| Vec3::new(d.x, d.z, d.y);let sun_p = to_planet(sun_dir).normalize();@@ -339,15 +364,45 @@ fn bake_atmosphere_with(a: &Atmosphere, luts: &Luts, sun_dir: Vec3, sun_lux: Veclet side = d.x.abs().max(d.z.abs()).max(1e-6);let r = d.y / side * (0.5 * SKY_LIGHT_CUBE);if r <= -0.5 {- return [0.0, 0.0, 0.0, 1.0];+ return [0.0, 0.0, 0.0, 0.0];}let (dir, k) = if r < 0.5 { (Vec3::new(d.x, side / SKY_LIGHT_CUBE, d.z).normalize(), r + 0.5) } else { (d, 1.0) };let (l, _) = luts.integrate(a, pos, to_planet(dir), sun_p, 40, false, Some(()), false);let l = (l * sun_lux + glow.at(dir.y.clamp(-1.0, 1.0).asin())) * k;- [l.x, l.y, l.z, 1.0]+ // (w: the horizon fade's factor until the overcast blend below; 1 in the bake.)+ [l.x, l.y, l.z, k]}).collect();- SkyBake { width, height, texels, sun_illuminance: sun_illuminance(a, sun_dir, sun_lux) }+ let mut texels = texels;+ let cover = cover.clamp(0.0, 1.0);+ if cover > 0.0 {+ // What the clear sky and the sun give a horizontal surface (texel solid angles of the equirectangular map).+ let (dt, dp) = (PI / height as f32, 2.0 * PI / width as f32);+ let e_sky: Vec3 = texels.iter().enumerate().map(|(i, t)| {+ let theta = ((i as u32 / width) as f32 + 0.5) * dt;+ Vec3::new(t[0], t[1], t[2]) * (theta.cos().max(0.0) * theta.sin() * dt * dp)+ }).sum();+ let e_sun = sun_illuminance(a, sun_dir, sun_lux, 0.0) * sun_dir.normalize().y.max(0.0);+ let lum = |v: Vec3| 0.2126 * v.x + 0.7152 * v.y + 0.0722 * v.z;+ // (The glow's share is the night's, scaled apart.)+ let e_glow: Vec3 = if glow.is_zero() { Vec3::ZERO } else {+ (0..height).map(|y| {+ let theta = (y as f32 + 0.5) * dt;+ glow.at((PI / 2.0 - theta).max(-PI / 2.0)) * (theta.cos().max(0.0) * theta.sin() * dt * dp * width as f32)+ }).sum()+ };+ let e_oc = Vec3::splat(lum(e_sky - e_glow + e_sun).max(0.0) * OVERCAST_TRANSMITTANCE);+ for (i, t) in texels.iter_mut().enumerate() {+ let e = PI / 2.0 - ((i as u32 / width) as f32 + 0.5) * dt;+ let oc = (cie_overcast(e_oc, e) + glow.at(e.max(-PI / 2.0)) * OVERCAST_GLOW) * t[3];+ let l = Vec3::new(t[0], t[1], t[2]).lerp(oc, cover);+ *t = [l.x, l.y, l.z, t[3]];+ }+ }+ for t in &mut texels {+ t[3] = 1.0;+ }+ SkyBake { width, height, texels, sun_illuminance: sun_illuminance(a, sun_dir, sun_lux, cover) }}/// Analytic dome for night / overcast scenes (UE session: gradient + light-pollution glow + clouds as a sky light).@@ -476,3 +531,50 @@ pub fn bake_gradient(g: &Gradient, width: u32, height: u32) -> SkyBake {.collect();SkyBake { width, height, texels, sun_illuminance: Vec3::ZERO }}++#[cfg(test)]+mod overcast_tests {+ use super::*;++ #[test]+ fn full_cover_is_the_cie_overcast_sky_lit_by_the_clear_day() {+ let a = Atmosphere::earth(1.0, 10.0, 0.8, Vec3::splat(0.3));+ let t = SkyTables::new(&a);+ let (sun, lux) = (Vec3::new(0.3, 0.5, -0.8).normalize(), Vec3::splat(100_000.0));+ let (w, h) = (256u32, 128u32);+ let clear = bake_atmosphere_tables(&a, &t, sun, lux, 10.0, w, h, Glow::default(), 0.0);+ let oc = bake_atmosphere_tables(&a, &t, sun, lux, 10.0, w, h, Glow::default(), 1.0);+ let lum = |v: [f32; 4]| 0.2126 * v[0] + 0.7152 * v[1] + 0.0722 * v[2];+ // Horizontal illuminance of a bake (+ its sun).+ let e_h = |b: &SkyBake| -> f32 {+ let (dt, dp) = (PI / h as f32, 2.0 * PI / w as f32);+ let sky: f32 = b.texels.iter().enumerate().map(|(i, t)| {+ let th = ((i as u32 / w) as f32 + 0.5) * dt;+ lum(*t) * th.cos().max(0.0) * th.sin() * dt * dp+ }).sum();+ let s = b.sun_illuminance * sun.y;+ sky + 0.2126 * s.x + 0.7152 * s.y + 0.0722 * s.z+ };+ // No direct sun; the ground gets OVERCAST_TRANSMITTANCE of the clear day's light.+ assert_eq!(oc.sun_illuminance, Vec3::ZERO);+ let ratio = e_h(&oc) / e_h(&clear);+ assert!((ratio - OVERCAST_TRANSMITTANCE).abs() < 0.01, "{ratio}");+ // The dome's shape: (1 + 2 sin e) / 3, zenith over 10 degrees up, the same in every azimuth; white.+ let row = |e_deg: f32| (((90.0 - e_deg) / 180.0 * h as f32) as u32).min(h - 1);+ let at = |r: u32, x: u32| oc.texels[(r * w + x) as usize];+ let e_of = |r: u32| PI / 2.0 - (r as f32 + 0.5) * PI / h as f32;+ let (z, ten) = (row(89.0), row(10.0));+ let expect = (1.0 + 2.0 * e_of(z).sin()) / (1.0 + 2.0 * e_of(ten).sin());+ for x in [0, w / 3, 2 * w / 3] {+ let got = lum(at(z, x)) / lum(at(ten, x));+ assert!((got / expect - 1.0).abs() < 1e-3, "{got} {expect}");+ let c = at(ten, x);+ assert!((c[0] - c[2]).abs() < 1e-3 * c[1], "{c:?}");+ }+ // Half cover: halfway in every texel, the sun at half.+ let half = bake_atmosphere_tables(&a, &t, sun, lux, 10.0, w, h, Glow::default(), 0.5);+ assert!((half.sun_illuminance - clear.sun_illuminance * 0.5).length() < 1e-3 * clear.sun_illuminance.length());+ let k = (ten * w + 7) as usize;+ assert!((lum(half.texels[k]) - 0.5 * (lum(clear.texels[k]) + lum(oc.texels[k]))).abs() < 1e-3 * lum(clear.texels[k]));+ }+}diff --git a/renderer/src/tables.rs b/renderer/src/tables.rsindex 5ae254b..0d9afcb 100644--- a/renderer/src/tables.rs+++ b/renderer/src/tables.rs@@ -43,6 +43,8 @@ pub struct Tables {pub(crate) emissive: HashSet<u32>,pub wetness: f32,pub rain: f32,+ /// Cloud cover (0..1): a change re-bakes the sky and dims the sun (live.rs `move_sun`).+ pub cloud_cover: f32,pub fog: Option<desc::Fog>,/// three.js `scene.background` (`Params::background`).pub background: Option<[f32; 3]>,@@ -82,6 +84,7 @@ impl Tables {light_tree,wetness: sc.wetness,rain: sc.rain,+ cloud_cover: sc.cloud_cover,fog: sc.fog.clone(),background: sc.background.map(|b| b.to_array()),emit_in_place: false,@@ -167,6 +170,7 @@ impl Tables {if c.environment {self.wetness = w.environment.wetness.clamp(0.0, 1.0);self.rain = w.environment.rain.clamp(0.0, 1.0);+ self.cloud_cover = w.environment.cloud_cover.clamp(0.0, 1.0);self.fog = w.environment.fog.clone();self.background = w.environment.background;self.aerial = scene::aerial_params(&w.environment.sky);diff --git a/renderer/src/world.rs b/renderer/src/world.rsindex 7f9980a..af8df9e 100644--- a/renderer/src/world.rs+++ b/renderer/src/world.rs@@ -386,7 +386,7 @@ impl Default for World {geometry_fns: BTreeMap::new(),proc_geometries: HashMap::new(),instanced: HashMap::new(),- environment: desc::Environment { sky: desc::Sky::Color { color: [0.0; 3], intensity: 0.0, sky_light_intensity: 1.0 }, fog: None, background: None, wetness: 0.0, rain: 0.0 },+ environment: desc::Environment { sky: desc::Sky::Color { color: [0.0; 3], intensity: 0.0, sky_light_intensity: 1.0 }, fog: None, background: None, wetness: 0.0, rain: 0.0, cloud_cover: 0.0 },settings: Settings::default(),changes: Changes { structural: true, ..Default::default() },}