Add design persistence, world rendering, metrics overlay and logs

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 878d144b77
commit 190ef9cc4b
111 changed files with 14801 additions and 63 deletions

View file

@ -0,0 +1,162 @@
//! Fixed-step physics for the boxes shown in the window.
mod pacing;
#[cfg(test)]
mod tests;
use std::time::Duration;
use fabrilis_core::time::SimDelta;
use fabrilis_core::units::Kilograms;
use fabrilis_core::vec::{Quat, Transform, Vec3};
use fabrilis_physics::{BodyDesc, BodyId, PhysicsBackend, RapierBackend, ShapeDesc, TrimeshDesc};
use fabrilis_render::{Instance, Scene};
use fabrilis_worldgen::terrain::{Heightmap, TerrainMesh};
use crate::runtime::AppError;
use crate::runtime::metrics::Stopwatch;
use pacing::{MAX_STEPS, Pacer};
/// Physics rate; the step never changes with the frame time.
const HZ: u64 = 1_000;
/// Edge length of each dropped box, metres.
pub(crate) const BOX_SIZE: f64 = 0.6;
/// Mass of each dropped box, kilograms.
pub(crate) const BOX_MASS: f64 = 4.0;
/// Horizontal offset and drop height above the terrain for each box.
pub(crate) const DROPS: [(f64, f64, f64); 3] =
[(-1.5, 0.4, 2.0), (1.2, -0.8, 3.6), (0.2, 1.8, 5.2)];
/// One colour per box, linear RGBA.
const COLORS: [[f32; 4]; 3] = [
[0.85, 0.35, 0.15, 1.0],
[0.20, 0.55, 0.85, 1.0],
[0.85, 0.75, 0.20, 1.0],
];
struct BoxBody {
id: BodyId,
color: [f32; 4],
}
/// The Rapier world and the boxes it drives.
pub(crate) struct Simulation {
backend: RapierBackend,
boxes: Vec<BoxBody>,
step: SimDelta,
pacer: Pacer,
total_steps: u64,
slowest_step: Duration,
}
impl Simulation {
/// Builds the terrain collider and drops the boxes into the scene.
pub(crate) fn new(
terrain: &TerrainMesh,
heightmap: &Heightmap,
scene: &mut Scene,
) -> Result<Self, AppError> {
let step = SimDelta::from_hz(HZ).ok_or_else(|| {
AppError::Physics("physics step is not a whole nanosecond".to_owned())
})?;
let mut backend = RapierBackend::new();
let collider = TrimeshDesc {
vertices: terrain.vertices.clone(),
indices: terrain.indices.clone(),
position: Vec3::ZERO,
rotation: Quat::IDENTITY,
};
backend
.add_trimesh(collider)
.ok_or_else(|| AppError::Physics("terrain collider was rejected".to_owned()))?;
scene.instances.clear();
let mut boxes = Vec::with_capacity(DROPS.len());
for (offset, color) in DROPS.iter().zip(COLORS) {
let (x, y, drop) = *offset;
let center = Vec3::new(x, y, heightmap.height_at(x, y) + drop + BOX_SIZE * 0.5);
let id = backend.add_body(BodyDesc::dynamic(
ShapeDesc::Cuboid {
half_extents: Vec3::new(BOX_SIZE * 0.5, BOX_SIZE * 0.5, BOX_SIZE * 0.5),
},
Kilograms::new(BOX_MASS),
center,
));
let size = BOX_SIZE as f32;
scene.instances.push(Instance::box_at(
[center.x as f32, center.y as f32, center.z as f32],
[size, size, size],
color,
));
boxes.push(BoxBody { id, color });
}
Ok(Self {
backend,
boxes,
step,
pacer: Pacer::new(),
total_steps: 0,
slowest_step: Duration::ZERO,
})
}
/// Runs the fixed steps owed for this frame and copies transforms to the scene.
///
/// Diagnostics time each step individually, but only observe it: the number
/// of steps, their order and the Rapier calls stay exactly the same, so the
/// resulting state hash does not depend on logging being enabled.
pub(crate) fn advance(&mut self, scene: &mut Scene) -> pacing::Frame {
let frame = self.pacer.advance(self.step.nanos(), MAX_STEPS);
self.slowest_step = Duration::ZERO;
self.total_steps = self.total_steps.saturating_add(u64::from(frame.steps));
for _ in 0..frame.steps {
let step_start = Stopwatch::start();
self.backend.step(self.step);
self.slowest_step = self.slowest_step.max(step_start.elapsed());
}
self.sync(scene);
frame
}
/// Simulated seconds represented by the steps that have actually run.
#[must_use]
pub(crate) fn sim_seconds(&self) -> f64 {
self.total_steps as f64 * self.step.nanos() as f64 / 1_000_000_000.0
}
/// The slowest single physics step of the latest frame.
#[must_use]
pub(crate) fn slowest_step(&self) -> Duration {
self.slowest_step
}
/// Copies every body transform into its rendered instance.
fn sync(&self, scene: &mut Scene) {
for (instance, body) in scene.instances.iter_mut().zip(&self.boxes) {
let Some(transform) = self.backend.body_transform(body.id) else {
continue;
};
instance.model = model(transform, BOX_SIZE as f32);
instance.color = body.color;
}
}
}
/// A column-major model matrix for a rotated, translated box.
fn model(transform: Transform, size: f32) -> [[f32; 4]; 4] {
let [x, y, z] = transform.rotation.to_mat3().cols();
let [px, py, pz] = [
transform.position.x as f32,
transform.position.y as f32,
transform.position.z as f32,
];
[
[x.x as f32 * size, x.y as f32 * size, x.z as f32 * size, 0.0],
[y.x as f32 * size, y.y as f32 * size, y.z as f32 * size, 0.0],
[z.x as f32 * size, z.y as f32 * size, z.z as f32 * size, 0.0],
[px, py, pz, 1.0],
]
}