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: edge: true measures a part's distances to its nearest edge, not its middle, for big parts a camera stands in or beside (a city's tile, a terrain chunk). Bevy measures a range to one point, so each frame every mesh's range is moved out by the gap between that point and the part's nearest edge for the shot's camera, which the shadow passes measure from too; a start at 0 and an endless end stay put. On 200 m slabs whose near edge is 58 m from the camera and middle 153 m: far 60 with edge drawn, far 60 without it not, far 40 with edge not.

shinyobjectz33b3d3d4a6e7
diff --git a/sdk/raea/conformance/detail.test.tsx b/sdk/raea/conformance/detail.test.tsx
index 53ad84e8..66c4811d 100644
--- a/sdk/raea/conformance/detail.test.tsx
+++ b/sdk/raea/conformance/detail.test.tsx
@@ -11,13 +11,13 @@ describe("LT21: detail", () => {
test("a Mesh3d's detail is a Detail component, every field given, and the mesh keeps its shape", () => {
const out = html(<Mesh3d detail={{ far: 120, minPx: 4 }}><Cuboid /><StandardMaterial /></Mesh3d>);
expect(out).toContain('data-shape="box(1, 1, 1)"');
- expect(props(out, "BevyComponent")).toEqual([{ type: "raea_sys_lod::Detail", value: { near: 0, far: 120, min_px: 4, max_px: 0, fade: 0.1, shadow: true, size: 0 } }]);
+ expect(props(out, "BevyComponent")).toEqual([{ type: "raea_sys_lod::Detail", value: { near: 0, far: 120, min_px: 4, max_px: 0, fade: 0.1, shadow: true, size: 0, edge: false } }]);
});
test("a Model's detail is a component inside it, and not a prop of its row", () => {
- const out = html(<Model src="a.glb" detail={{ near: 120, shadow: false }} />);
+ const out = html(<Model src="a.glb" detail={{ near: 120, shadow: false, edge: true }} />);
expect(props(out, "Model")[0]).not.toHaveProperty("detail");
- expect(props(out, "BevyComponent")[0].value).toMatchObject({ near: 120, shadow: false });
+ expect(props(out, "BevyComponent")[0].value).toMatchObject({ near: 120, shadow: false, edge: true });
});
test("a shape inside a fragment is the mesh's shape", () => {
diff --git a/sdk/render/src/3d/detail.tsx b/sdk/render/src/3d/detail.tsx
index ec81cb4f..f349f2a6 100644
--- a/sdk/render/src/3d/detail.tsx
+++ b/sdk/render/src/3d/detail.tsx
@@ -20,8 +20,11 @@ export interface Detail {
fade?: number;
/** false: this level casts no shadow, so a coarser level drawn for the shadows alone can (true by default) */
shadow?: boolean;
- /** the part's size across, metres, for the sizes on screen (by default its meshes' bounds) */
+ /** the part's size across, metres, for the sizes on screen (by default its meshes' bounds' diagonal) */
size?: number;
+ /** true: the distances are to its nearest edge, not its middle: a big part a camera stands in or beside (a city's
+ * tile, a terrain chunk), whose middle can be hundreds of metres from where the camera is (false by default) */
+ edge?: boolean;
}
const NUMS = ["near", "far", "minPx", "maxPx", "fade", "size"] as const;
@@ -33,7 +36,7 @@ export function detailNode(who: string, d: Detail | undefined) {
for (const k of NUMS) if (d[k] !== undefined && !(Number.isFinite(d[k]) && (d[k] as number) >= 0)) throw new RangeError(`${who}: detail.${k} is 0 or more, got ${d[k]}`);
if (d.fade !== undefined && d.fade > 1) throw new RangeError(`${who}: detail.fade is a share of the distance, 0 to 1, got ${d.fade}`);
if (d.far !== undefined && d.near !== undefined && d.far > 0 && d.far <= d.near) throw new RangeError(`${who}: detail.far (${d.far}) must be past detail.near (${d.near})`);
- if (d.shadow !== undefined && typeof d.shadow !== "boolean") throw new TypeError(`${who}: detail.shadow is true or false, got ${JSON.stringify(d.shadow)}`);
- const value = { near: d.near ?? 0, far: d.far ?? 0, min_px: d.minPx ?? 0, max_px: d.maxPx ?? 0, fade: d.fade ?? 0.1, shadow: d.shadow ?? true, size: d.size ?? 0 };
+ for (const k of ["shadow", "edge"] as const) if (d[k] !== undefined && typeof d[k] !== "boolean") throw new TypeError(`${who}: detail.${k} is true or false, got ${JSON.stringify(d[k])}`);
+ const value = { near: d.near ?? 0, far: d.far ?? 0, min_px: d.minPx ?? 0, max_px: d.maxPx ?? 0, fade: d.fade ?? 0.1, shadow: d.shadow ?? true, size: d.size ?? 0, edge: d.edge ?? false };
return <BevyComponent key="raea-detail" type="raea_sys_lod::Detail" value={value} />;
}
diff --git a/systems/lod/src/lib.rs b/systems/lod/src/lib.rs
index 7f537e6e..b246ddba 100644
--- a/systems/lod/src/lib.rs
+++ b/systems/lod/src/lib.rs
@@ -19,7 +19,11 @@ use bevy::{
/// Where a part is drawn. Distances are metres from the camera, 0 for none; sizes on screen are the part's size across
/// in pixels, 0 for none. Drawn from the farther of `near` and the distance where it shrinks to `max_px`, to the nearer
/// of `far` and the distance where it shrinks to `min_px`. Each edge cross-fades over `fade` of its distance (0.1: 5%
-/// either side). `size` is the part's size across in metres, 0 to take it from its meshes' bounds.
+/// either side). `size` is the part's size across in metres, 0 to take it from its meshes' bounds. `edge` measures the
+/// distances to the part's nearest edge rather than its middle: a big part (a city's tile, a terrain chunk) whose
+/// middle is far from a camera standing in it. Bevy measures to one point, so each mesh's range is shifted every frame
+/// by the gap between that point and the part's nearest edge, exact for the shot's camera (and its shadow passes,
+/// which measure from it).
#[derive(Component, Reflect, Clone, Debug, PartialEq)]
#[reflect(Component, Default)]
pub struct Detail {
@@ -30,11 +34,12 @@ pub struct Detail {
pub fade: f32,
pub shadow: bool,
pub size: f32,
+ pub edge: bool,
}
impl Default for Detail {
fn default() -> Self {
- Self { near: 0.0, far: 0.0, min_px: 0.0, max_px: 0.0, fade: 0.1, shadow: true, size: 0.0 }
+ Self { near: 0.0, far: 0.0, min_px: 0.0, max_px: 0.0, fade: 0.1, shadow: true, size: 0.0, edge: false }
}
}
@@ -79,29 +84,57 @@ pub fn range(start: f32, end: f32, fade: f32, use_aabb: bool) -> VisibilityRange
VisibilityRange { 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.
+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;
+ }
+ if r.end_margin.end < f32::MAX {
+ r.end_margin = r.end_margin.start + by..r.end_margin.end + by;
+ }
+ 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)
+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)
+}
+
type MeshRow<'a> = (&'a GlobalTransform, Option<&'a Aabb>, Option<&'a VisibilityRange>, Has<NotShadowCaster>, Has<ShadowLeft>);
-fn apply(cams: Query<(&Camera, &Projection, Has<bevy::camera::ShadowLodOrigin>), With<Camera3d>>, parts: Query<(Entity, &Detail)>, children: Query<&Children>, meshes: Query<MeshRow, With<Mesh3d>>, mut commands: Commands) {
+fn apply(cams: Query<(&Camera, &Projection, &GlobalTransform, Has<bevy::camera::ShadowLodOrigin>), With<Camera3d>>, parts: Query<(Entity, &Detail)>, children: Query<&Children>, meshes: Query<MeshRow, With<Mesh3d>>, mut commands: Commands) {
// 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 are measured for one camera
+ // so sizes on screen and nearest edges are measured for one camera
let shot = cams.iter().filter(|(c, ..)| c.is_active).max_by_key(|(.., origin)| *origin);
- let focal = shot.and_then(|(c, p, _)| match p {
+ let focal = shot.and_then(|(c, p, ..)| match p {
Projection::Perspective(p) => c.physical_target_size().map(|s| s.y as f32 / (2.0 * (p.fov * 0.5).tan())),
_ => None,
});
+ let eye = shot.map(|(_, _, at, _)| at.translation());
for (part, d) in &parts {
let found: Vec<Entity> = std::iter::once(part).chain(children.iter_descendants(part)).filter(|e| meshes.contains(*e)).collect();
if found.is_empty() {
continue;
}
- let size = if d.size > 0.0 { d.size } else { across(&found, &meshes) };
+ let bounds = bounds(&found, &meshes);
+ let size = if d.size > 0.0 { d.size } else { bounds.map_or(0.0, |(lo, hi)| (hi - lo).length()) };
let (start, end) = span(d, size, focal);
for e in found {
- let Ok((_, aabb, now, no_shadow, ours)) = meshes.get(e) else { continue };
+ 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 want = range(start, end, d.fade, use_aabb);
+ let point = match aabb.filter(|_| use_aabb) {
+ Some(a) => gt.affine().transform_point3(Vec3::from(a.center)),
+ None => gt.translation(),
+ };
+ let gap = match (d.edge, eye, bounds) {
+ (true, Some(eye), Some((lo, hi))) => edge_gap(eye, point, lo, hi),
+ _ => 0.0,
+ };
+ let want = shifted(range(start, end, d.fade, use_aabb), gap);
if now != Some(&want) {
commands.entity(e).insert(want);
}
@@ -118,8 +151,8 @@ fn apply(cams: Query<(&Camera, &Projection, Has<bevy::camera::ShadowLodOrigin>),
}
}
-/// the meshes' bounds together, in the world, as a size across (their box's diagonal); 0 before any has bounds
-fn across(found: &[Entity], meshes: &Query<MeshRow, With<Mesh3d>>) -> f32 {
+/// the meshes' bounds together, in the world; None before any has bounds
+fn bounds(found: &[Entity], meshes: &Query<MeshRow, With<Mesh3d>>) -> Option<(Vec3, Vec3)> {
let (mut lo, mut hi) = (Vec3::splat(f32::MAX), Vec3::splat(f32::MIN));
for (gt, aabb, ..) in found.iter().filter_map(|e| meshes.get(*e).ok()) {
let Some(a) = aabb else { continue };
@@ -129,7 +162,7 @@ fn across(found: &[Entity], meshes: &Query<MeshRow, With<Mesh3d>>) -> f32 {
lo = lo.min(c - h);
hi = hi.max(c + h);
}
- if lo.x > hi.x { 0.0 } else { (hi - lo).length() }
+ (lo.x <= hi.x).then_some((lo, hi))
}
#[cfg(test)]
@@ -166,4 +199,18 @@ mod tests {
assert!((a.end_margin.start - 114.0).abs() < 1e-3 && (a.end_margin.end - 126.0).abs() < 1e-3);
assert_eq!(range(0.0, f32::INFINITY, 0.1, false).end_margin, f32::MAX..f32::MAX);
}
+
+ #[test]
+ fn edge_distances_move_the_range_by_the_gap_to_the_nearest_edge() {
+ // a 10 m box from 10 m to 20 m out, measured from its middle at 15 m: 5 m behind its nearest edge
+ let gap = edge_gap(Vec3::ZERO, Vec3::new(15.0, 0.0, 0.0), Vec3::new(10.0, -1.0, -1.0), Vec3::new(20.0, 1.0, 1.0));
+ assert!((gap - 5.0).abs() < 1e-4);
+ // drawn to 12 m from the edge: to 17 m from the middle, so drawn here (middle at 15 m, edge at 10 m)
+ let r = shifted(range(0.0, 12.0, 0.0, true), gap);
+ assert_eq!(r.start_margin, 0.0..0.0);
+ assert!(r.is_visible_at_all(15.0) && !r.is_visible_at_all(17.5));
+ // standing inside it, 3 m from its middle: 0 m from it, so the whole 3 m is the gap; an endless end stays endless
+ assert_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);
+ }
}