Skip to content

armgit / raea

Public Imported from GitHub
0

Render Anything Engine: tell your AI what to make. Videos, motion graphics, 3D and games as React and TypeScript, rendered on your own GPU. Rust + Bevy, macOS, Linux and Windows.

LT21: the sun's shadows fade out at their distance instead of ending in a line; the extract-order line out again

shinyobjectze35a7769c0ef
diff --git a/Cargo.lock b/Cargo.lock
index 867a050f..896e163e 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -16124,6 +16124,7 @@ name = "raea-sys-lod"
version = "0.1.0"
dependencies = [
"bevy",
+ "raea-sys-frame",
]
[[package]]
diff --git a/docs/realism.feature b/docs/realism.feature
index 672c839f..631fad42 100644
--- a/docs/realism.feature
+++ b/docs/realism.feature
@@ -363,13 +363,22 @@ Feature: raea Light: raea's own real-time realism renderer, measured against rae
And so far (11-17, branch realism; not done): a Model's `detail` (near, far, max_px, min_px, edge, fade,
shadow) is raea-sys-lod's Detail. Bevy's dither now runs in raea's own fragments (ShaderExt, ShaderMaterial).
Ranges are measured from each mesh's origin, as Bevy's GPU measures them, and moved to the part's nearest
- edge. Meshes are extracted after the ranges. The live diff keeps its fast path with Detail rows (it had run in
- full every frame, +19 ms a step). gambit's City draws `levels="detail"`, its tiles' levels cross-faded by
+ edge. The live diff keeps its fast path with Detail rows (it had run in full every frame, +19 ms a step). gambit's City draws `levels="detail"`, its tiles' levels cross-faded by
distance, streamed or not
And measured on the 540p fly over Berlin (240 frames; a one-frame spike is an 8x8 block whose change is over
12/255 and 3 times the frames' either side): detail's level switches at frames 41, 117, 122 and 157 (45, 20,
19 and 17 blocks) are gone, and JS levels' at 58, 106, 112 and 148 (41, 15, 16, 13) are unchanged. Two left
- at 35 and 226 (29 and 12 blocks) came from Bevy's extract order; their fix waits for a rebuild to be checked
+ at 35 and 226 (29 and 12 blocks) aren't Bevy's extract order: ordering meshes after the ranges changed no
+ pixel (Bevy 0.20 already orders them, bevy_pbr mesh.rs:200), and it came out again. At 35 a held tile's
+ arcaded row fades out half stippled for three frames with the sky showing through, no level behind it, then
+ drops in one frame: open
+ And the sun's shadows no longer end in a line (Shane, 11-17, on the fly: "the shadow having a clear cull
+ distance"): Bevy draws past its last cascade fully lit, so raea-sys-lod swaps its shadows.wesl (hash checked)
+ for one whose last cascade fades to lit over the cascades' overlap, the last 30% of the distance; the fly's
+ frames change on about 2% of their pixels, all lighter. Reaching 900 m, where DAY's fog begins, costs too
+ much as it is: live at 2560x1440, median of 3, work p50 walk 45.9 ms at 300 m against 53.3 at 900, fly
+ 42.5 against 54.3. Far shadows wait on the far cascades cast by coarse levels only and redrawn every few
+ frames
And measured live at 2560x1440 on an M-series Mac, work p50 JS levels against detail: walk 51.7 against 48.9
ms (p99 144.8 against 78.2), fly 50.3 against 50.1 (four runs each, about 5 ms apart run to run), streamed
run 34.7 against 31.8 (p99 143.3 against 114.6). Detail adds 0.4 to 1.3 ms of step. Footprint: walk 6.97
diff --git a/systems/lod/Cargo.toml b/systems/lod/Cargo.toml
index 662f7cda..15aa630c 100644
--- a/systems/lod/Cargo.toml
+++ b/systems/lod/Cargo.toml
@@ -4,7 +4,9 @@ version = "0.1.0"
edition = "2024"
license = "FSL-1.1-ALv2"
publish = false
-description = "Detail by distance and size on screen: where each part is drawn, its levels cross-faded, its shadow left to a coarser level, nothing drawn past its last (docs/realism.feature LT21)"
+description = "Detail by distance and size on screen: where each part is drawn, its levels cross-faded, its shadow left to a coarser level, nothing drawn past its last, the sun's shadows faded out at their distance (docs/realism.feature LT21)"
[dependencies]
bevy = { version = "=0.20.0", default-features = false, features = ["std", "default_app", "3d_bevy_render"] }
+# swapping Bevy's shadows.wesl for the one whose last cascade fades out (shadow.rs)
+raea-sys-frame = { path = "../frame" }
diff --git a/systems/lod/src/lib.rs b/systems/lod/src/lib.rs
index 48074450..20334d72 100644
--- a/systems/lod/src/lib.rs
+++ b/systems/lod/src/lib.rs
@@ -7,6 +7,9 @@
//! camera's vertical field of view and its target's height in pixels, so a level looks the same at any output size.
//! `shadow: false` keeps a level out of the shadow maps, for a coarser level to cast its shadow.
+// the sun's shadows fade out at their distance rather than end in a line
+mod shadow;
+
use bevy::{
camera::{
primitives::Aabb,
@@ -49,13 +52,7 @@ struct ShadowLeft;
pub fn install(app: &mut App) {
app.register_type::<Detail>().add_systems(PostUpdate, apply.after(VisibilitySystems::CalculateBounds).after(bevy::transform::TransformSystems::Propagate).before(VisibilitySystems::CheckVisibility));
- // Bevy 0.20 extracts meshes and visibility ranges in no set order. A mesh re-extracted for its changed range before
- // the ranges are read bakes the range's previous slot into its uniform and keeps it until its range changes again:
- // the GPU dithers it by a range the CPU no longer culls by (on the 540p fly a tile's LOD1, in range at 14% faded,
- // drew nothing for two frames, then snapped in). Meshes are extracted after the ranges.
- if let Some(render) = app.get_sub_app_mut(bevy::render::RenderApp) {
- render.configure_sets(bevy::render::ExtractSchedule, bevy::pbr::MeshExtractionSystems.after(bevy::render::view::extract_visibility_ranges));
- }
+ shadow::install(app);
match std::env::var("RAEA_LOD_DEBUG").as_deref() {
Ok("time") => {
app.init_resource::<LodTime>();
diff --git a/systems/lod/src/shadow.rs b/systems/lod/src/shadow.rs
new file mode 100644
index 00000000..b47eb2c1
--- /dev/null
+++ b/systems/lod/src/shadow.rs
@@ -0,0 +1,64 @@
+//! The sun's shadows fade out where they end (docs/realism.feature LT21). Bevy blends each shadow cascade into the next
+//! over their overlap, but draws everything past the last cascade's far bound fully lit, so a city's shadows stop in a
+//! straight line across the street (Shane, 11-17, on the 540p fly: "the shadow having a clear cull distance"). Bevy's
+//! shadows.wesl is swapped for one whose last cascade fades to lit over the same overlap (raea_sys_frame's
+//! swap_embedded_shader, which checks Bevy's file is still the one this was written against).
+
+use bevy::prelude::*;
+
+/// FNV-1a of bevy_pbr 0.20's render/shadows.wesl, the file the fade was written against
+const UPSTREAM: u64 = 0xcd13_e017_3c8e_a62c;
+
+/// the end of fetch_directional_shadow's blend into the next cascade, where the last cascade's fade goes
+const FROM: &str = " shadow = mix(shadow, next_shadow, (-view_z - next_near_bound) / (this_far_bound - next_near_bound));
+ }
+ }
+ return shadow;
+}";
+
+const TO: &str = " shadow = mix(shadow, next_shadow, (-view_z - next_near_bound) / (this_far_bound - next_near_bound));
+ }
+ } else {
+ // raea: the last cascade fades to lit over the cascades' overlap, so the shadows don't end in a line
+ let far_bound = (*light).cascades[cascade_index].far_bound;
+ let fade_from = (1.0 - (*light).cascades_overlap_proportion) * far_bound;
+ if (-view_z >= fade_from) {
+ shadow = mix(shadow, 1.0, (-view_z - fade_from) / max(far_bound - fade_from, 0.0001));
+ }
+ }
+ return shadow;
+}";
+
+/// Bevy's shadows.wesl with the last cascade's fade
+fn faded(bevy: &str) -> String {
+ assert_eq!(bevy.matches(FROM).count(), 1, "raea: Bevy's shadows.wesl no longer ends its cascade blend where raea's fade goes (docs/bevy-upgrade.md)");
+ bevy.replace(FROM, TO)
+}
+
+/// Swaps Bevy's shadows.wesl for the faded one; after PbrPlugin, which embeds it. Without PBR there's nothing to fade.
+pub fn install(app: &mut App) {
+ if !raea_sys_frame::bevy_api::swap_embedded_shader(app, "bevy_pbr/render/shadows.wesl", UPSTREAM, faded) {
+ debug!("raea-sys-lod: no bevy_pbr shadows.wesl to fade");
+ return;
+ }
+ // PbrPlugin started loading Bevy's file when it was built, and an embedded file is read on the IO threads at once,
+ // so by the time this plugin is built it already holds Bevy's bytes (on 11-17 the fly drew bit for bit as before
+ // the swap): read again, from the swapped file
+ if let Some(server) = app.world().get_resource::<AssetServer>() {
+ server.reload("embedded://bevy_pbr/render/shadows.wesl");
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ /// The fade replaces the end of the cascade blend once and adds the last cascade's branch.
+ #[test]
+ fn the_last_cascade_fades() {
+ let bevy = format!("fn fetch_directional_shadow() -> f32 {{\n if (next_cascade_index < n) {{\n if (x) {{\n{FROM}\n");
+ let ours = faded(&bevy);
+ assert_eq!(ours.matches("raea: the last cascade fades").count(), 1);
+ assert!(ours.contains("shadow = mix(shadow, 1.0,"));
+ }
+}