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.
TypeScript 3dagent-skillsai-agentsai-videobevyclaude-codegame-enginegpulinuxmacosmotion-designmotion-graphicsreactrusttypescriptvideo-editingvideo-renderingwgpuwindows
LT21: detail cross-fades on every surface and from where the GPU measures. raea's generated fragments for ShaderExt (surfaceShader: surface and lit hooks) and ShaderMaterial (shade) never called Bevy's visibility-range dither, so a part drawn with them (the city's facades, glass and ground) drew both levels fully inside a fade band and cut one at the band's edge, a pop: on the 540p fly over 1917 Berlin, detail made one-frame spikes of 45, 20, 19 and 17 tiles at frames 41, 117, 122 and 157 where the JS levels made theirs at 58, 106, 112 and 148. Both fragments now call visibility_range_dither under @if(VISIBILITY_RANGE_DITHER), as Bevy's own pbr fragment does (a source with its own @fragment calls it itself). And Bevy's GPU dither measures from a mesh's origin (mesh_world_from_local[3]) while its CPU check measured from the box's centre under use_aabb, which raea-sys-lod set for meshes baked in world coordinates (a city tile's origin is the world's, its geometry hundreds of metres off), so the two disagreed. Now every range is measured from the origin (use_aabb off) and moved by how much farther the origin is than what the part is measured to: its nearest edge (edge), the mesh's own middle when its origin lies outside its bounds, else nothing; the move can be negative, and a range never goes below 0. A moved range is rewritten once the eye has gone a third of its way to the nearest fading band, which bounds a stale move's error (the distance moves as far as the eye, a move twice as far). RAEA_LOD_DEBUG=time now times apply inside it: between two systems ordered around it others ran, so it read 1 ms with no Details at all.
shinyobjectzc77c4858c064
diff --git a/systems/lod/src/lib.rs b/systems/lod/src/lib.rsindex d68f4df5..729c9f9a 100644--- a/systems/lod/src/lib.rs+++ b/systems/lod/src/lib.rs@@ -52,7 +52,7 @@ pub fn install(app: &mut App) {match std::env::var("RAEA_LOD_DEBUG").as_deref() {Ok("time") => {app.init_resource::<LodTime>();- app.add_systems(PostUpdate, (stamp.before(apply), timed.after(apply)));+ app.add_systems(PostUpdate, timed.after(apply));}Ok(_) => {app.add_systems(PostUpdate, report.after(VisibilitySystems::CheckVisibility));@@ -61,21 +61,16 @@ pub fn install(app: &mut App) {}}-/// RAEA_LOD_DEBUG=time: apply's own time and the Details changed this frame, averaged every 100 frames, on stderr-fn stamp(mut t: ResMut<LodTime>) {- t.0 = Some(std::time::Instant::now());-}-+/// RAEA_LOD_DEBUG=time: apply's own time (timed inside it, as other systems may run between two of its neighbours)+/// and the Details changed this frame, averaged every 100 frames, on stderr#[derive(Resource, Default)]-struct LodTime(Option<std::time::Instant>, f64, u32, u32);+struct LodTime(f64, u32, u32);fn timed(mut t: ResMut<LodTime>, changed: Query<(), Changed<Detail>>, parts: Query<(), With<Detail>>) {- let Some(at) = t.0 else { return };- t.1 += at.elapsed().as_secs_f64() * 1000.0;- t.2 += 1;- t.3 += changed.iter().count() as u32;- if t.2 == 100 {- eprintln!("raea lod time: apply {:.2} ms a frame, {:.0} of {} Details changed a frame", t.1 / 100.0, t.3 as f64 / 100.0, parts.iter().count());+ t.1 += 1;+ t.2 += changed.iter().count() as u32;+ if t.1 == 100 {+ eprintln!("raea lod time: apply {:.2} ms a frame, {:.0} of {} Details changed a frame", t.0 / 100.0, t.2 as f64 / 100.0, parts.iter().count());*t = LodTime::default();}}@@ -110,27 +105,27 @@ pub fn range(start: f32, end: f32, fade: f32, use_aabb: bool) -> VisibilityRangeVisibilityRange { start_margin: s.start.min(e.start)..s.end.min(e.start), end_margin: e, use_aabb }}-/// A range moved out by `by` metres (a mesh's measuring point that far behind its part's nearest edge); a start at 0-/// and an endless end stay as they are.+/// A range moved out by `by` metres (a mesh's origin that much farther than what its part is measured to; nearer when+/// negative), never below 0; a start at 0 and an endless end stay as they are.pub fn shifted(mut r: VisibilityRange, by: f32) -> VisibilityRange {if r.start_margin.end > 0.0 {- r.start_margin = r.start_margin.start + by..r.start_margin.end + by;+ r.start_margin = (r.start_margin.start + by).max(0.0)..(r.start_margin.end + by).max(0.0);}if r.end_margin.end < f32::MAX {- r.end_margin = r.end_margin.start + by..r.end_margin.end + by;+ r.end_margin = (r.end_margin.start + by).max(0.0)..(r.end_margin.end + by).max(0.0);}r}-/// how much farther `point` is from `eye` than the nearest point of the box `lo`..`hi` is (with the eye inside the box,-/// all of `point`'s distance: a camera standing in a part is 0 m from it)+/// how much farther `point` is from `eye` than the nearest point of the box `lo`..`hi` is, negative when it's nearer (with+/// the eye inside the box, all of `point`'s distance: a camera standing in a part is 0 m from it)pub fn edge_gap(eye: Vec3, point: Vec3, lo: Vec3, hi: Vec3) -> f32 {- (eye.distance(point) - eye.distance(eye.clamp(lo, hi))).max(0.0)+ eye.distance(point) - eye.distance(eye.clamp(lo, hi))}-/// how far a part's nearest edge (`d`) is from the bands its range fades over (0 inside one): the eye can move a third-/// of this before a range written for it could draw the part differently (the edge and each mesh's gap move at most-/// as far as the eye)+/// how far a part's measured distance (`d`) is from the bands its range fades over (0 inside one): the eye can move a+/// third of this before a range written for it could draw the part differently (the distance moves at most as far as+/// the eye, and a mesh's gap, two distances' difference, twice as far)pub fn slack(d: f32, start: f32, end: f32, fade: f32) -> f32 {let h = fade.clamp(0.0, 1.0) * 0.5;[start, end].into_iter().filter(|b| *b > 0.0 && b.is_finite()).fold(f32::INFINITY, |s, b| {@@ -152,10 +147,14 @@ struct Held {focal: Option<f32>,eye: Vec3,slack: f32,+ /// a mesh's origin lies outside its own bounds (a mesh baked in world coordinates, as the city's tiles are), so its+ /// range is measured to its middle and moves with the eye+ outside: bool,}#[allow(clippy::too_many_arguments)]-fn apply(cams: Query<(&Camera, &Projection, &GlobalTransform, Has<bevy::camera::ShadowLodOrigin>), With<Camera3d>>, parts: Query<(Entity, &Detail, Ref<GlobalTransform>)>, added: Query<(), Added<Mesh3d>>, children: Query<&Children>, meshes: Query<MeshRow, With<Mesh3d>>, mut held: Local<std::collections::HashMap<Entity, Held>>, mut commands: Commands) {+fn apply(cams: Query<(&Camera, &Projection, &GlobalTransform, Has<bevy::camera::ShadowLodOrigin>), With<Camera3d>>, parts: Query<(Entity, &Detail, Ref<GlobalTransform>)>, added: Query<(), Added<Mesh3d>>, children: Query<&Children>, meshes: Query<MeshRow, With<Mesh3d>>, mut held: Local<std::collections::HashMap<Entity, Held>>, mut commands: Commands, time: Option<ResMut<LodTime>>) {+ let t0 = std::time::Instant::now();// the shot's camera (raea gives it the shadow LOD origin), else the first active 3D one: a range is one per mesh,// so sizes on screen and nearest edges are measured for one cameralet shot = cams.iter().filter(|(c, ..)| c.is_active).max_by_key(|(.., origin)| *origin);@@ -179,6 +178,7 @@ fn apply(cams: Query<(&Camera, &Projection, &GlobalTransform, Has<bevy::camera::}if walk || at.is_changed() {h.bounds = bounds(&h.meshes, &meshes);+ h.outside = h.meshes.iter().filter_map(|e| meshes.get(*e).ok()).any(|(_, a, ..)| a.is_some_and(outside));}let size = if d.size > 0.0 { d.size } else { h.bounds.map_or(0.0, |(lo, hi)| (hi - lo).length()) };let (start, end) = span(d, size, focal);@@ -186,27 +186,33 @@ fn apply(cams: Query<(&Camera, &Projection, &GlobalTransform, Has<bevy::camera::(true, Some(eye), Some((lo, hi))) => Some((eye, lo, hi)),_ => None,};- let moved = edge.is_some_and(|(eye, ..)| eye.distance(h.eye) >= (h.slack / 3.0).max(0.5));+ let follows = eye.filter(|_| edge.is_some() || h.outside);+ let moved = follows.is_some_and(|eye| eye.distance(h.eye) >= (h.slack / 3.0).max(0.5));if !(walk || at.is_changed() || h.focal != focal || moved) {continue;}h.detail = Some(d.clone());h.focal = focal;- if let Some((eye, lo, hi)) = edge {+ h.slack = f32::INFINITY;+ if let Some(eye) = follows {h.eye = eye;- h.slack = slack(eye.distance(eye.clamp(lo, hi)), start, end, d.fade);}for &e in &h.meshes {let Ok((gt, aabb, now, no_shadow, ours)) = meshes.get(e) else { continue };- // measured from the mesh's origin, unless that lies outside its own bounds (a mesh baked in world- // coordinates, as the city's tiles are): then from its bounds' centre- let use_aabb = aabb.is_some_and(|a| a.center.length() > a.half_extents.length());- let point = match aabb.filter(|_| use_aabb) {- Some(a) => gt.affine().transform_point3(Vec3::from(a.center)),- None => gt.translation(),+ // Bevy's GPU dither measures from a mesh's origin, so its range does too (its CPU check, use_aabb off),+ // moved by how much farther the origin is than what the part is measured to: its nearest edge (`edge`), or+ // the mesh's own middle when its origin lies outside its bounds; else the origin itself+ let origin = gt.translation();+ let middle = aabb.filter(|a| outside(a)).map(|a| gt.affine().transform_point3(Vec3::from(a.center)));+ let (gap, dist) = match (follows, edge, middle) {+ (Some(eye), Some((_, lo, hi)), _) => (edge_gap(eye, origin, lo, hi), eye.distance(eye.clamp(lo, hi))),+ (Some(eye), None, Some(m)) => (eye.distance(origin) - eye.distance(m), eye.distance(m)),+ _ => (0.0, f32::INFINITY),};- let gap = edge.map_or(0.0, |(eye, lo, hi)| edge_gap(eye, point, lo, hi).round());- let want = shifted(range(start, end, d.fade, use_aabb), gap);+ if dist.is_finite() {+ h.slack = h.slack.min(slack(dist, start, end, d.fade));+ }+ let want = shifted(range(start, end, d.fade, false), gap.round());if now != Some(&want) {commands.entity(e).insert(want);}@@ -221,6 +227,14 @@ fn apply(cams: Query<(&Camera, &Projection, &GlobalTransform, Has<bevy::camera::}}}+ if let Some(mut t) = time {+ t.0 += t0.elapsed().as_secs_f64() * 1000.0;+ }+}++/// a mesh's origin outside its own bounds+fn outside(a: &Aabb) -> bool {+ a.center.length() > a.half_extents.length()}/// the meshes' bounds together, in the world; None before any has bounds@@ -284,6 +298,10 @@ mod tests {// standing inside it, 3 m from its middle: 0 m from it, so the whole 3 m is the gap; an endless end stays endlessassert_eq!(edge_gap(Vec3::new(12.0, 0.0, 0.0), Vec3::new(15.0, 0.0, 0.0), Vec3::new(10.0, -1.0, -1.0), Vec3::new(20.0, 1.0, 1.0)), 3.0);assert_eq!(shifted(range(30.0, f32::INFINITY, 0.0, true), 5.0).end_margin, f32::MAX..f32::MAX);+ // an origin nearer than the box's nearest point: a negative gap, and the range moved in, never below 0+ assert_eq!(edge_gap(Vec3::ZERO, Vec3::new(4.0, 0.0, 0.0), Vec3::new(10.0, -1.0, -1.0), Vec3::new(20.0, 1.0, 1.0)), -6.0);+ let r = shifted(range(5.0, 12.0, 0.0, false), -6.0);+ assert_eq!((r.start_margin, r.end_margin), (0.0..0.0, 6.0..6.0));}#[test]diff --git a/systems/material/src/shader_material.rs b/systems/material/src/shader_material.rsindex be2eb2a1..72bb93c8 100644--- a/systems/material/src/shader_material.rs+++ b/systems/material/src/shader_material.rs@@ -157,7 +157,7 @@ pub fn uniform(name: &str, v: &Value) -> Result<Uniform, String> {/// no `#import` of its own any more; WESL is a superset of WGSL, so a `wgsl` source goes in this module unchanged.pub fn module(source: &str, uniforms: &[(String, &'static str)], textures: &[String], array: Option<&str>) -> String {let group = "constants::MATERIAL_BIND_GROUP";- let mut m = String::from("import bevy_pbr::render::forward_io::VertexOutput;\nimport bevy_pbr::render::mesh_view_bindings::{view, globals};\n\n");+ let mut m = String::from("import bevy_pbr::render::forward_io::VertexOutput;\nimport bevy_pbr::render::mesh_view_bindings::{view, globals};\n@if(VISIBILITY_RANGE_DITHER) import bevy_pbr::render::pbr_functions::visibility_range_dither;\n\n");m += &format!("struct RaeaSlots {{ s: array<vec4<f32>, 16> }};\n@group({group}) @binding(0) var<uniform> raea_slots: RaeaSlots;\n");for (i, t) in textures.iter().enumerate() {m += &format!(@@ -181,7 +181,7 @@ pub fn module(source: &str, uniforms: &[(String, &'static str)], textures: &[Strm += "\n";m += source;if !source.contains("@fragment") {- m += "\n\n@fragment\nfn fragment(in: VertexOutput) -> @location(0) vec4<f32> {\n return shade(in);\n}\n";+ m += "\n\n@fragment\nfn fragment(in: VertexOutput) -> @location(0) vec4<f32> {\n @if(VISIBILITY_RANGE_DITHER) visibility_range_dither(in.position, in.visibility_range_dither);\n return shade(in);\n}\n";}m}@@ -281,7 +281,7 @@ mod tests {assert!(m.contains("fn u_tint() -> vec4<f32> { return raea_slots.s[0]; }"));assert!(m.contains("fn u_speed() -> f32 { return raea_slots.s[1].x; }"));assert!(m.contains("@binding(1) var tex_noise: texture_2d<f32>;"));- assert!(m.contains("@fragment\nfn fragment(in: VertexOutput)"));+ assert!(m.contains("@fragment\nfn fragment(in: VertexOutput)") && m.contains("visibility_range_dither(in.position, in.visibility_range_dither);"));assert!(m.contains("@binding(9) var tex_frames: texture_2d_array<f32>;"));}diff --git a/systems/shader/src/lib.rs b/systems/shader/src/lib.rsindex e3f164c2..b9a77482 100644--- a/systems/shader/src/lib.rs+++ b/systems/shader/src/lib.rs@@ -151,7 +151,8 @@ pub fn module(source: &str, uniforms: &[(String, &'static str)], textures: &[Strimport bevy_pbr::render::pbr_fragment::pbr_input_from_standard_material;\n\import bevy_pbr::render::pbr_functions::{alpha_discard, apply_pbr_lighting, main_pass_post_lighting_processing};\n\import bevy_pbr::render::forward_io::{VertexOutput, FragmentOutput};\n\- import bevy_pbr::render::mesh_view_bindings::{view, globals};\n\n",+ import bevy_pbr::render::mesh_view_bindings::{view, globals};\n\+ @if(VISIBILITY_RANGE_DITHER) import bevy_pbr::render::pbr_functions::visibility_range_dither;\n\n",);m += &format!("struct RaeaSlots {{ s: array<vec4<f32>, 16> }};\n@group({group}) @binding(100) var<uniform> raea_slots: RaeaSlots;\n");if !textures.is_empty() || array.is_some() {@@ -177,6 +178,7 @@ pub fn module(source: &str, uniforms: &[(String, &'static str)], textures: &[Strm += &format!("\n\n@fragment\n\fn fragment(in: VertexOutput, @builtin(front_facing) is_front: bool) -> FragmentOutput {{\n\+ \x20 @if(VISIBILITY_RANGE_DITHER) visibility_range_dither(in.position, in.visibility_range_dither);\n\\x20 var pbr_input = pbr_input_from_standard_material(in, is_front);\n\\x20 pbr_input.material.base_color = alpha_discard(pbr_input.material.flags, pbr_input.material.alpha_cutoff, pbr_input.material.base_color);\n\{surface}\@@ -406,6 +408,8 @@ mod tests {fn the_entry_calls_the_hooks_it_has() {let both = super::module("fn surface(in: VertexOutput, pbr: ptr<function, PbrInput>) {}\nfn lit(in: VertexOutput, pbr: PbrInput, color: vec4<f32>) -> vec4<f32> { return u_tint(); }", &[("tint".into(), "vec4<f32>")], &["pic".into()], None);assert!(both.contains("surface(in, &pbr_input);"));+ // a detail's levels cross-fade (docs/realism.feature LT21)+ assert!(both.contains("@if(VISIBILITY_RANGE_DITHER) visibility_range_dither(in.position, in.visibility_range_dither);"));assert!(both.contains("color = lit(in, pbr_input, color);"));assert!(both.contains("@binding(100) var<uniform> raea_slots"));assert!(both.contains("@binding(101) var tex_pic: texture_2d<f32>;"));