Add design persistence, world rendering, metrics overlay and logs
Some checks failed
ci / check (macos-latest) (push) Has been cancelled
ci / check (ubuntu-latest) (push) Has been cancelled
ci / check (windows-latest) (push) Has been cancelled

M3: STL/OBJ import with mesh validation, mass/COM/inertia, material database, RON scene with content-addressed blobs and v0->v1 migration. M4: deterministic heightmap terrain, trimesh colliders, wgpu renderer with instancing and depth, winit app with 1 kHz physics, fly camera, on-screen metrics overlay and per-frame CSV/dev logs. Docs: UI, logging, network and headless CLI plans.
This commit is contained in:
Loki 2026-11-16 23:34:05 +01:00
parent 79928b645b
commit 93075ab1fd
Signed by: boba
GPG key ID: 6CDE87BC9A778D97
107 changed files with 14785 additions and 46 deletions

View file

@ -28,5 +28,8 @@ fabrilis-bridge = { path = "../bridge" }
fabrilis-worldgen = { path = "../worldgen" }
fabrilis-scripting = { path = "../scripting" }
[dev-dependencies]
fabrilis-sensors = { path = "../sensors" }
[build-dependencies]
fabrilis-limits = { path = "../../tools/limits" }

View file

@ -0,0 +1,129 @@
//! M4c regression: the three dropped boxes settle upright and stay put.
#![allow(
clippy::unwrap_used,
clippy::expect_used,
reason = "test helper: a failure should abort the test"
)]
use fabrilis_core::time::SimDelta;
use fabrilis_core::units::Kilograms;
use fabrilis_core::vec::{Quat, Vec3};
use fabrilis_physics::RapierBackend;
use fabrilis_physics::backend::{BodyDesc, PhysicsBackend, ShapeDesc, TrimeshDesc};
use fabrilis_worldgen::terrain::{Heightmap, TerrainConfig};
const BOX: f64 = 0.6;
const DROPS: [(f64, f64, f64); 3] = [(-1.5, 0.4, 2.0), (1.2, -0.8, 3.6), (0.2, 1.8, 5.2)];
#[test]
fn dropped_boxes_settle_and_stop() {
let mut config = TerrainConfig {
seed: 42,
size: 65,
extent: 100.0,
height_scale: 4.0,
..TerrainConfig::default()
};
config.frequency = 2.0;
let map = Heightmap::generate(config).expect("terrain");
let mesh = map.mesh();
let mut world = RapierBackend::new();
world
.add_trimesh(TrimeshDesc {
vertices: mesh.vertices,
indices: mesh.indices,
position: Vec3::ZERO,
rotation: Quat::IDENTITY,
})
.expect("terrain collider");
let mut bodies = Vec::new();
for (x, y, drop) in DROPS {
let center = Vec3::new(x, y, map.height_at(x, y) + drop + BOX * 0.5);
bodies.push(world.add_body(BodyDesc::dynamic(
ShapeDesc::Cuboid {
half_extents: Vec3::new(BOX * 0.5, BOX * 0.5, BOX * 0.5),
},
Kilograms::new(4.0),
center,
)));
}
let dt = SimDelta::from_hz(1_000).expect("1 kHz");
let mut before = Vec::new();
for step in 0..5_000 {
world.step(dt);
if step == 3_999 {
before = bodies
.iter()
.map(|&body| world.body_transform(body).expect("body").position)
.collect();
}
}
for (index, &body) in bodies.iter().enumerate() {
let transform = world.body_transform(body).expect("body");
let ground = map.height_at(transform.position.x, transform.position.y);
println!(
"box {index}: pos {:?} ground {ground:.3}",
transform.position
);
assert!(
transform.position.z >= ground - 0.05,
"box {index} sank through the terrain"
);
let moved = (transform.position - before[index]).length();
assert!(moved < 1e-3, "box {index} still moves: {moved}");
}
}
/// Diagnostic: `cargo test -p fabrilis-modes --test box_drops -- --ignored --nocapture`.
#[test]
#[ignore = "diagnostic timing, not a pass/fail test"]
#[allow(
clippy::disallowed_methods,
reason = "diagnostic timing uses the wall clock on purpose"
)]
fn physics_step_time_spikes() {
let config = TerrainConfig {
seed: 42,
size: 65,
extent: 100.0,
height_scale: 4.0,
frequency: 2.0,
..TerrainConfig::default()
};
let map = Heightmap::generate(config).expect("terrain");
let mesh = map.mesh();
let mut world = RapierBackend::new();
world
.add_trimesh(TrimeshDesc {
vertices: mesh.vertices,
indices: mesh.indices,
position: Vec3::ZERO,
rotation: Quat::IDENTITY,
})
.expect("terrain collider");
for (x, y, drop) in DROPS {
let center = Vec3::new(x, y, map.height_at(x, y) + drop + BOX * 0.5);
world.add_body(BodyDesc::dynamic(
ShapeDesc::Cuboid {
half_extents: Vec3::new(BOX * 0.5, BOX * 0.5, BOX * 0.5),
},
Kilograms::new(4.0),
center,
));
}
let dt = SimDelta::from_hz(1_000).expect("1 kHz");
let mut times = Vec::with_capacity(5_000);
for _ in 0..5_000 {
let start = std::time::Instant::now();
world.step(dt);
times.push((start.elapsed().as_secs_f64() * 1_000.0, times.len()));
}
let total: f64 = times.iter().map(|&(ms, _)| ms).sum();
let mut sorted = times.clone();
sorted.sort_by(|a, b| b.0.total_cmp(&a.0));
println!("mean step {:.3} ms, total {:.1} ms", total / 5_000.0, total);
for &(ms, index) in sorted.iter().take(10) {
println!("slow step {index}: {ms:.3} ms");
}
}

View file

@ -0,0 +1,150 @@
//! M2 closed loop: a link on a revolute joint is lifted by a PD controller that reads
//! a delayed encoder instead of the ground-truth state.
#![allow(
clippy::unwrap_used,
clippy::expect_used,
reason = "test helper: a failure should abort the test"
)]
use fabrilis_core::snapshot::hash::StateHasher;
use fabrilis_core::time::SimDelta;
use fabrilis_core::units::{Kilograms, Radians};
use fabrilis_core::vec::{Quat, Vec3};
use fabrilis_physics::backend::{BodyDesc, PhysicsBackend, ShapeDesc};
use fabrilis_physics::joints::RevoluteDesc;
use fabrilis_physics::{BodyId, JointId, RapierBackend};
use fabrilis_sensors::encoder::{Encoder, EncoderConfig};
const KP: f64 = 400.0;
const KD: f64 = 25.0;
const TARGET: f64 = 0.0;
const STEPS: usize = 3_000;
const GOLDEN: u64 = 0x014e_e966_1ea1_d0ce;
struct Rig {
world: RapierBackend,
encoder: Encoder,
link: BodyId,
joint: JointId,
dt: SimDelta,
previous_raw: f64,
continuous: f64,
}
impl Rig {
fn new(start_degrees: f64) -> Self {
let mut world = RapierBackend::new();
let base = world.add_body(BodyDesc::fixed(
ShapeDesc::Cuboid {
half_extents: Vec3::new(0.05, 0.05, 0.05),
},
Vec3::ZERO,
));
let start = Quat::from_axis_angle(Vec3::Y, Radians::from_degrees(start_degrees))
.expect("the Y axis is non-zero");
let mut link_desc = BodyDesc::dynamic(
ShapeDesc::Cuboid {
half_extents: Vec3::new(0.5, 0.05, 0.05),
},
Kilograms::new(1.0),
start.rotate(Vec3::new(0.5, 0.0, 0.0)),
);
link_desc.rotation = start;
let link = world.add_body(link_desc);
let joint = world.add_revolute(RevoluteDesc::new(
base,
link,
Vec3::Y,
Vec3::ZERO,
Vec3::new(-0.5, 0.0, 0.0),
));
let mut encoder = Encoder::new(
EncoderConfig {
resolution_bits: 16,
noise_sigma: 0.0,
bias: 0.0,
delay_steps: 1,
},
1,
)
.expect("the encoder configuration is valid");
let dt = SimDelta::from_hz(1_000).expect("1 kHz divides a second");
let first_truth = world.joint_angle(joint).expect("the joint exists");
let _primed = encoder.sample(first_truth);
let previous_raw = encoder.sample(first_truth).get();
Self {
world,
encoder,
link,
joint,
dt,
previous_raw,
continuous: wrap_to_pi(previous_raw),
}
}
fn control_step(&mut self) -> Radians {
let truth = self
.world
.joint_angle(self.joint)
.expect("the joint exists");
let raw = self.encoder.sample(truth).get();
let delta = wrap_to_pi(raw - self.previous_raw);
self.continuous += delta;
self.previous_raw = raw;
let rate = delta / self.dt.as_seconds().get();
let torque = KP * (TARGET - self.continuous) - KD * rate;
self.world.apply_torque(self.link, Vec3::Y * torque);
self.world.step(self.dt);
self.world
.joint_angle(self.joint)
.expect("the joint exists")
}
fn state(&self) -> (f64, f64) {
let transform = self
.world
.body_transform(self.link)
.expect("the link exists");
(transform.position.z, transform.rotation.y)
}
}
fn run() -> (f64, f64, u64) {
let mut rig = Rig::new(60.0);
let mut hasher = StateHasher::new();
let mut final_angle = 0.0;
let mut tail_max: f64 = 0.0;
for step in 0..STEPS {
let angle = rig.control_step();
let (z, qy) = rig.state();
hasher.write_u64(step as u64);
hasher.write_f64(angle.get());
hasher.write_f64(z);
hasher.write_f64(qy);
final_angle = angle.get();
if step > 1_000 {
tail_max = tail_max.max((angle.get() - TARGET).abs());
}
}
(final_angle, tail_max, hasher.finish())
}
#[test]
fn pd_controller_lifts_the_link_to_the_target_angle() {
let (final_angle, tail_max, _) = run();
assert!(final_angle.abs() < 0.02, "final angle = {final_angle}");
assert!(tail_max < 0.02, "tail maximum error = {tail_max}");
}
#[test]
fn the_closed_loop_is_deterministic() {
let (_, _, hash) = run();
assert_eq!(hash, GOLDEN, "new hash: {hash:#018x}");
}
fn wrap_to_pi(angle: f64) -> f64 {
let two_pi = fabrilis_core::math::TAU;
(angle + core::f64::consts::PI).rem_euclid(two_pi) - core::f64::consts::PI
}

View file

@ -0,0 +1,129 @@
//! M4a: terrain generated from a seed carries a body without falling through.
#![allow(
clippy::unwrap_used,
clippy::expect_used,
reason = "test helper: a failure should abort the test"
)]
use fabrilis_core::snapshot::hash::StateHasher;
use fabrilis_core::time::SimDelta;
use fabrilis_core::units::Kilograms;
use fabrilis_core::vec::{Quat, Vec3};
use fabrilis_physics::RapierBackend;
use fabrilis_physics::backend::{BodyDesc, PhysicsBackend, ShapeDesc, TrimeshDesc};
use fabrilis_worldgen::terrain::{Heightmap, TerrainConfig};
const STEPS: usize = 3_000;
fn terrain(seed: u64, height_scale: f64, size: usize) -> (RapierBackend, Heightmap) {
let config = TerrainConfig {
seed,
size,
extent: 100.0,
height_scale,
frequency: 2.0,
octaves: 4,
..TerrainConfig::default()
};
let map = Heightmap::generate(config).expect("the terrain configuration is valid");
let mesh = map.mesh();
let mut world = RapierBackend::new();
let terrain = world
.add_trimesh(TrimeshDesc {
vertices: mesh.vertices,
indices: mesh.indices,
position: Vec3::ZERO,
rotation: Quat::IDENTITY,
})
.expect("the terrain mesh is valid");
assert!(world.body_transform(terrain).is_some());
(world, map)
}
#[test]
fn a_ball_rests_on_flat_terrain() {
let (mut world, _map) = terrain(1, 0.0, 33);
let radius = 0.25;
let ball = world.add_body(BodyDesc::dynamic(
ShapeDesc::Ball { radius },
Kilograms::new(1.0),
Vec3::new(0.0, 0.0, 3.0),
));
let dt = SimDelta::from_hz(1_000).expect("1 kHz divides a second");
for _ in 0..STEPS {
world.step(dt);
}
let z = world
.body_transform(ball)
.expect("the ball exists")
.position
.z;
assert!((z - radius).abs() < 0.02, "ball z = {z}");
}
#[test]
fn a_box_stays_above_a_seeded_relief() {
let (world, map) = terrain(42, 4.0, 65);
let mut world = world;
let start = map.height_at(0.0, 0.0) + 3.0;
let body = world.add_body(BodyDesc::dynamic(
ShapeDesc::Cuboid {
half_extents: Vec3::new(0.25, 0.25, 0.1),
},
Kilograms::new(5.0),
Vec3::new(0.0, 0.0, start),
));
let dt = SimDelta::from_hz(1_000).expect("1 kHz divides a second");
for _ in 0..STEPS {
world.step(dt);
}
let position = world.body_transform(body).expect("the box exists").position;
assert!(position.is_finite());
let ground = map.height_at(position.x, position.y);
assert!(
position.z >= ground - 0.05,
"box at {position:?} below terrain {ground}"
);
}
#[test]
fn the_terrain_scene_is_deterministic() {
let (first, second) = (run_scene(), run_scene());
assert_eq!(first.0.x.to_bits(), second.0.x.to_bits());
assert_eq!(first.0.y.to_bits(), second.0.y.to_bits());
assert_eq!(first.0.z.to_bits(), second.0.z.to_bits());
assert_eq!(first.1, second.1);
}
#[test]
fn the_terrain_trajectory_matches_golden() {
let (_, hash) = run_scene();
assert_eq!(hash, GOLDEN, "new hash: {hash:#018x}");
}
const GOLDEN: u64 = 0xbb09_e470_43d3_1f2e;
fn run_scene() -> (Vec3, u64) {
let (mut world, map) = terrain(42, 4.0, 65);
let start = map.height_at(0.0, 0.0) + 3.0;
let body = world.add_body(BodyDesc::dynamic(
ShapeDesc::Cuboid {
half_extents: Vec3::new(0.25, 0.25, 0.1),
},
Kilograms::new(5.0),
Vec3::new(0.0, 0.0, start),
));
let dt = SimDelta::from_hz(1_000).expect("1 kHz divides a second");
let mut hasher = StateHasher::new();
let mut last = Vec3::ZERO;
for step in 0..1_000 {
world.step(dt);
last = world.body_transform(body).expect("the box exists").position;
hasher.write_u64(step as u64);
hasher.write_f64(last.x);
hasher.write_f64(last.y);
hasher.write_f64(last.z);
}
(last, hasher.finish())
}