fabrilis/crates/render/src/scene.rs
loki5512344 190ef9cc4b 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.
2026-11-16 23:40:39 +01:00

80 lines
1.9 KiB
Rust

//! Scene data handed to the renderer: one triangle mesh plus instanced boxes.
type Mat4 = [[f32; 4]; 4];
/// A triangle mesh with positions in metres and `u32` triangle indices.
#[derive(Debug, Clone, Default, PartialEq)]
pub struct MeshData {
/// Vertex positions in world space.
pub vertices: Vec<[f32; 3]>,
/// Triples of vertex indices.
pub indices: Vec<u32>,
}
impl MeshData {
/// The number of complete triangles in the mesh.
#[must_use]
pub fn triangle_count(&self) -> usize {
self.indices.len() / 3
}
}
/// One drawable box: a model matrix and a flat colour.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Instance {
/// Column-major model matrix.
pub model: Mat4,
/// Linear RGBA colour.
pub color: [f32; 4],
}
impl Instance {
/// The identity model matrix.
pub const IDENTITY: Mat4 = [
[1.0, 0.0, 0.0, 0.0],
[0.0, 1.0, 0.0, 0.0],
[0.0, 0.0, 1.0, 0.0],
[0.0, 0.0, 0.0, 1.0],
];
/// Builds an instance from a model matrix and a colour.
#[must_use]
pub fn new(model: Mat4, color: [f32; 4]) -> Self {
Self { model, color }
}
/// A box centred at `center` with the given size along each axis.
#[must_use]
pub fn box_at(center: [f32; 3], size: [f32; 3], color: [f32; 4]) -> Self {
let model = [
[size[0], 0.0, 0.0, 0.0],
[0.0, size[1], 0.0, 0.0],
[0.0, 0.0, size[2], 0.0],
[center[0], center[1], center[2], 1.0],
];
Self::new(model, color)
}
}
/// Everything the renderer draws in one frame.
#[derive(Debug, Clone, Default, PartialEq)]
pub struct Scene {
/// The terrain surface.
pub mesh: MeshData,
/// Colour multiplied into the terrain vertices.
pub mesh_color: [f32; 4],
/// Boxes drawn with instancing.
pub instances: Vec<Instance>,
}
impl Scene {
/// A scene with one terrain mesh and the default terrain colour.
#[must_use]
pub fn new(mesh: MeshData) -> Self {
Self {
mesh,
mesh_color: [0.34, 0.46, 0.29, 1.0],
instances: Vec::new(),
}
}
}