fabrilis/crates/render/src/overlay/mod.rs
loki5512344 93075ab1fd
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
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:34:05 +01:00

123 lines
2.6 KiB
Rust

//! Screen-space 2D overlay drawn after the 3D scene.
//!
//! The overlay is a list of rectangles, lines and text in target pixels with the
//! origin at the top-left. One triangle batch draws all of it with depth testing
//! disabled and alpha blending: solid shapes sample a white texel of the glyph
//! atlas, text samples glyph coverage.
mod atlas;
mod pass;
mod text;
#[cfg(test)]
mod tests;
pub(crate) use pass::{OverlayRenderer, Target};
/// A filled rectangle in target pixels.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct OverlayRect {
pub x: f32,
pub y: f32,
pub w: f32,
pub h: f32,
pub color: [f32; 4],
}
impl OverlayRect {
/// A rectangle at `(x, y)` with a size and a linear RGBA colour.
#[must_use]
pub fn new(x: f32, y: f32, w: f32, h: f32, color: [f32; 4]) -> Self {
Self { x, y, w, h, color }
}
}
/// A straight line of a given width in target pixels.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct OverlayLine {
pub from: [f32; 2],
pub to: [f32; 2],
pub width: f32,
pub color: [f32; 4],
}
impl OverlayLine {
/// A line from `from` to `to` with a width and a linear RGBA colour.
#[must_use]
pub fn new(from: [f32; 2], to: [f32; 2], width: f32, color: [f32; 4]) -> Self {
Self {
from,
to,
width,
color,
}
}
}
/// One line of ASCII text, anchored at its top-left corner.
#[derive(Debug, Clone, PartialEq)]
pub struct OverlayText {
pub x: f32,
pub y: f32,
pub size: f32,
pub color: [f32; 4],
pub text: String,
}
impl OverlayText {
/// Text at `(x, y)` in pixels with a font size and a linear RGBA colour.
#[must_use]
pub fn new(x: f32, y: f32, size: f32, color: [f32; 4], text: impl Into<String>) -> Self {
Self {
x,
y,
size,
color,
text: text.into(),
}
}
}
/// Everything the overlay pass draws in one frame.
#[derive(Debug, Clone, Default, PartialEq)]
pub struct Overlay {
pub rects: Vec<OverlayRect>,
pub lines: Vec<OverlayLine>,
pub texts: Vec<OverlayText>,
}
impl Overlay {
/// An empty overlay.
#[must_use]
pub fn new() -> Self {
Self::default()
}
/// Drops all shapes but keeps the allocated buffers.
pub fn clear(&mut self) {
self.rects.clear();
self.lines.clear();
self.texts.clear();
}
/// Whether the overlay would draw nothing.
#[must_use]
pub fn is_empty(&self) -> bool {
self.rects.is_empty() && self.lines.is_empty() && self.texts.is_empty()
}
/// Queues a filled rectangle.
pub fn push_rect(&mut self, rect: OverlayRect) {
self.rects.push(rect);
}
/// Queues a line.
pub fn push_line(&mut self, line: OverlayLine) {
self.lines.push(line);
}
/// Queues a text line.
pub fn push_text(&mut self, text: OverlayText) {
self.texts.push(text);
}
}