forked from 1b-agent/fieldcraft-upstream
render: translucent faces in both renderers; datum planes get a light fill (#302)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UhhZMBTviDQxZvwnyP89fo
This commit is contained in:
1 parent
89dfc62c2f
commit
bbf2dba092
4 files changed
+115
-33
No files matched your search
+14
-14
@@ -51,7 +51,7 @@ use std::collections::{BTreeMap, BTreeSet, HashSet};
|
||||
use fieldcraft_model::{BoundaryCondition, ContactSurfaces, ElementFace, Load, Model, Procedure, ResolvedSet, Target};
|
||||
|
||||
use crate::display::{centre, polygon_normal};
|
||||
use crate::{Aabb3, Rgb, Scene, SceneLabel, SceneLine, ScenePoint, Vec3};
|
||||
use crate::{Aabb3, Rgb, Scene, SceneFill, SceneLabel, SceneLine, ScenePoint, SceneTri, Vec3};
|
||||
|
||||
/// The symbol unit drawn when the caller gives none: a fraction of the model's bounding box
|
||||
/// diagonal.
|
||||
@@ -894,8 +894,8 @@ impl Glyphs {
|
||||
const DATUM_MARGIN: f64 = 0.05;
|
||||
/// A datum point's cross: each arm's length, as a fraction of the bounding box diagonal.
|
||||
const DATUM_POINT: f64 = 0.02;
|
||||
/// Hatch lines across a datum plane's rectangle (its light fill).
|
||||
const DATUM_HATCH: usize = 4;
|
||||
/// A datum plane's fill: how opaque its translucent rectangle is.
|
||||
pub const DATUM_FILL_ALPHA: f64 = 0.12;
|
||||
|
||||
/// Corner `k` (0..8, one bit per axis) of box `b`.
|
||||
fn box_corner(b: &Aabb3, k: usize) -> Vec3 {
|
||||
@@ -958,7 +958,7 @@ fn axis_ends(bounds: &Aabb3, p: Vec3, a: Vec3) -> (Vec3, Vec3) {
|
||||
(p + a * (t0 - margin), p + a * (t1 + margin))
|
||||
}
|
||||
|
||||
/// Draw the model's datum planes (an outlined, lightly hatched rectangle: the model's extent
|
||||
/// Draw the model's datum planes (an outlined rectangle with a light translucent fill: the model's extent
|
||||
/// within the plane, `bounds`, plus a margin), axes (a line along the model's extent) and points
|
||||
/// (a small cross), each with its name; returns the box around them. Only the datums `shown`
|
||||
/// takes are drawn, and `highlight` (the one selected in the browser) whether shown or not, in
|
||||
@@ -976,7 +976,7 @@ pub fn datums(
|
||||
for (name, datum) in model.datums().iter().filter(|(n, _)| shown(n) || highlight == Some(n.as_str())) {
|
||||
let picked = highlight == Some(name.as_str());
|
||||
let color = if picked { colors.highlight } else { colors.datum };
|
||||
let (outline, thin) = if picked { (HIGHLIGHT_WIDTH, OUTLINE_WIDTH) } else { (OUTLINE_WIDTH, COUPLING_WIDTH) };
|
||||
let outline = if picked { HIGHLIGHT_WIDTH } else { OUTLINE_WIDTH };
|
||||
// The one selected in the browser shows through the part.
|
||||
(d.color, d.on_top) = (color, picked);
|
||||
match *datum {
|
||||
@@ -991,11 +991,9 @@ pub fn datums(
|
||||
let b = ring.get((k + 1) % 4).copied().unwrap_or(*a);
|
||||
d.line(*a, b);
|
||||
}
|
||||
d.width = thin;
|
||||
for k in 1..DATUM_HATCH {
|
||||
let t = -1.0 + 2.0 * k as f64 / DATUM_HATCH as f64;
|
||||
d.line(corner(t, -1.0), corner(t, 1.0));
|
||||
d.line(corner(-1.0, t), corner(1.0, t));
|
||||
let [a, b, c2, e] = ring;
|
||||
for p in [[a, b, c2], [a, c2, e]] {
|
||||
d.scene.fills.push(SceneFill { tri: SceneTri { p, normal: n, color }, alpha: DATUM_FILL_ALPHA });
|
||||
}
|
||||
d.scene.labels.push(SceneLabel { p: corner(1.0, 1.0), text: name.clone(), color });
|
||||
}
|
||||
@@ -1712,9 +1710,11 @@ mod tests {
|
||||
let bounds = cube(0.0, 10.0);
|
||||
let mut scene = Scene::default();
|
||||
let drawn = datums(&m, &bounds, &|_| true, None, &COLORS, &mut scene);
|
||||
// A rectangle with its hatch (4 + 2 × 3 lines), the axis line and the point's three arms,
|
||||
// each crossing at the point.
|
||||
assert_eq!(scene.lines.len(), 14);
|
||||
// A rectangle (4 lines, filled by two translucent triangles), the axis line and the
|
||||
// point's three arms, each crossing at the point.
|
||||
assert_eq!(scene.lines.len(), 8);
|
||||
assert_eq!(scene.fills.len(), 2);
|
||||
assert!(scene.fills.iter().all(|f| f.alpha == DATUM_FILL_ALPHA && f.tri.normal == Vec3::X));
|
||||
let p = Vec3::new(1.0, 2.0, 3.0);
|
||||
assert!(scene.lines.iter().rev().take(3).all(|l| ((l.a + l.b) * 0.5).dist(p) < 1e-12));
|
||||
let mut names: Vec<&str> = scene.labels.iter().map(|l| l.text.as_str()).collect();
|
||||
@@ -1734,7 +1734,7 @@ mod tests {
|
||||
assert_eq!((only.lines.len(), only.labels.len()), (1, 1));
|
||||
let mut picked = Scene::default();
|
||||
datums(&m, &bounds, &|_| false, Some("Mid"), &COLORS, &mut picked);
|
||||
assert_eq!((picked.lines.len(), picked.on_top.len()), (0, 10));
|
||||
assert_eq!((picked.lines.len(), picked.on_top.len(), picked.fills.len()), (0, 4, 2));
|
||||
assert!(picked.on_top.iter().all(|l| l.color == COLORS.highlight));
|
||||
}
|
||||
|
||||
|
||||
@@ -18,7 +18,7 @@ mod vec;
|
||||
pub use camera::{Camera, FIT_MARGIN, FULL_VIEW, Mat4, Orientation, StandardView};
|
||||
pub use display::{DisplayEdge, DisplayLine, DisplayMesh, DisplayNode, DisplayTri, SurfaceCurve, SurfaceTri};
|
||||
pub use raster::{
|
||||
Canvas, LABEL_OFFSET, MAX_DIM, MAX_PIXELS, MAX_PRIMITIVES, RenderError, Rgb, Scene, SceneLabel, SceneLine, ScenePoint, SceneTri, fit_size, png,
|
||||
render, render_layers, render_layers_png, render_png, shade,
|
||||
Canvas, LABEL_OFFSET, MAX_DIM, MAX_PIXELS, MAX_PRIMITIVES, RenderError, Rgb, Scene, SceneFill, SceneLabel, SceneLine, ScenePoint, SceneTri,
|
||||
fit_size, png, render, render_layers, render_layers_png, render_png, shade,
|
||||
};
|
||||
pub use vec::{Aabb3, Vec3};
|
||||
@@ -26,6 +26,14 @@ pub struct SceneTri {
|
||||
pub color: Rgb,
|
||||
}
|
||||
|
||||
/// A translucent triangle (a datum plane's fill): drawn after the opaque faces, depth-tested
|
||||
/// against them without hiding what is behind, blended at `alpha` (0–1).
|
||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||
pub struct SceneFill {
|
||||
pub tri: SceneTri,
|
||||
pub alpha: f64,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||
pub struct SceneLine {
|
||||
pub a: Vec3,
|
||||
@@ -55,6 +63,8 @@ pub struct SceneLabel {
|
||||
#[derive(Clone, Debug, Default, PartialEq)]
|
||||
pub struct Scene {
|
||||
pub tris: Vec<SceneTri>,
|
||||
/// Translucent triangles ([`SceneFill`]).
|
||||
pub fills: Vec<SceneFill>,
|
||||
pub lines: Vec<SceneLine>,
|
||||
pub points: Vec<ScenePoint>,
|
||||
pub labels: Vec<SceneLabel>,
|
||||
@@ -71,6 +81,7 @@ impl Scene {
|
||||
/// Add `other`'s primitives (not its background or radius) to this scene.
|
||||
pub fn append(&mut self, other: Scene) {
|
||||
self.tris.extend(other.tris);
|
||||
self.fills.extend(other.fills);
|
||||
self.lines.extend(other.lines);
|
||||
self.points.extend(other.points);
|
||||
self.labels.extend(other.labels);
|
||||
@@ -221,7 +232,7 @@ const DEPTH_BIAS: f64 = 2e-4;
|
||||
|
||||
/// [`render`] with overlays drawn over the scene (the setup's symbols, a selection highlight):
|
||||
/// their faces over the scene's faces they coincide with (a highlight repeats the faces it
|
||||
/// marks), then every layer's lines and points depth-tested, then every layer's
|
||||
/// marks), then every layer's translucent faces blended over them, then every layer's lines and points depth-tested, then every layer's
|
||||
/// [`Scene::on_top`] lines and labels over everything. The overlays' background and radius are
|
||||
/// not used.
|
||||
pub fn render_layers(scene: &Scene, overlays: &[&Scene], cam: &Camera, w: usize, h: usize) -> Result<Canvas, RenderError> {
|
||||
@@ -229,7 +240,13 @@ pub fn render_layers(scene: &Scene, overlays: &[&Scene], cam: &Camera, w: usize,
|
||||
return Err(RenderError::Size { w, h });
|
||||
}
|
||||
let count = |s: &Scene| {
|
||||
s.tris.len().saturating_add(s.lines.len()).saturating_add(s.points.len()).saturating_add(s.labels.len()).saturating_add(s.on_top.len())
|
||||
s.tris
|
||||
.len()
|
||||
.saturating_add(s.fills.len())
|
||||
.saturating_add(s.lines.len())
|
||||
.saturating_add(s.points.len())
|
||||
.saturating_add(s.labels.len())
|
||||
.saturating_add(s.on_top.len())
|
||||
};
|
||||
let primitives = overlays.iter().fold(count(scene), |n, o| n.saturating_add(count(o)));
|
||||
if primitives > MAX_PRIMITIVES {
|
||||
@@ -277,6 +294,18 @@ pub fn render_layers(scene: &Scene, overlays: &[&Scene], cam: &Camera, w: usize,
|
||||
});
|
||||
}
|
||||
let layers = || std::iter::once(scene).chain(overlays.iter().copied());
|
||||
// Translucent faces over the opaque ones, without writing depth: what is behind shows.
|
||||
for f in layers().flat_map(|s| &s.fills) {
|
||||
let t = &f.tri;
|
||||
let (Some(a), Some(b), Some(cc)) = (project(t.p[0]), project(t.p[1]), project(t.p[2])) else { continue };
|
||||
let alpha = if f.alpha.is_finite() { f.alpha.clamp(0.0, 1.0) } else { 0.0 };
|
||||
let col = shade(t.normal, view_dir, t.color.f());
|
||||
raster_tri(a, b, cc, sw, sh, |x, y, z| {
|
||||
if c.depth.get(y * sw + x).is_some_and(|d| z < *d) {
|
||||
c.put(x, y, col, alpha);
|
||||
}
|
||||
});
|
||||
}
|
||||
let line = |c: &mut Canvas, l: &SceneLine, depth_test: bool| {
|
||||
let (Some(a), Some(b)) = (biased(l.a, 1.0), biased(l.b, 1.0)) else { return };
|
||||
let len = ((b[0] - a[0]).powi(2) + (b[1] - a[1]).powi(2)).sqrt();
|
||||
@@ -486,6 +515,28 @@ mod tests {
|
||||
assert!(p[2] > 200 && p[0] < 60, "{p:?}");
|
||||
}
|
||||
|
||||
/// A translucent face tints what is behind it and hides nothing; one behind an opaque face
|
||||
/// does not show (#302).
|
||||
#[test]
|
||||
fn translucent_faces_blend_over_what_is_in_front_only() {
|
||||
let mut cam = Camera { distance: 50.0, ..Default::default() };
|
||||
cam.set_view(StandardView::Front);
|
||||
let px = |c: &Canvas| c.rgba[(32 * c.w + 32) * 4..(32 * c.w + 32) * 4 + 3].to_vec();
|
||||
let red = red_triangle();
|
||||
let plain = px(&render(&red, &cam, 64, 64).unwrap());
|
||||
let fill = |y: f64| SceneFill {
|
||||
tri: SceneTri { p: red.tris[0].p.map(|p| Vec3::new(p.x, y, p.z)), color: Rgb(0, 0, 255), ..red.tris[0] },
|
||||
alpha: 0.25,
|
||||
};
|
||||
let mut front = red.clone();
|
||||
front.fills.push(fill(-1.0));
|
||||
let tinted = px(&render(&front, &cam, 64, 64).unwrap());
|
||||
assert!(tinted[2] > plain[2] + 20 && tinted[0] > 120, "red still shows, tinted blue: {tinted:?} over {plain:?}");
|
||||
let mut back = red.clone();
|
||||
back.fills.push(fill(1.0));
|
||||
assert_eq!(px(&render(&back, &cam, 64, 64).unwrap()), plain);
|
||||
}
|
||||
|
||||
/// In perspective, close to the part (the near plane at its smallest), a line a little
|
||||
/// behind a face stays hidden and one on it shows (#282).
|
||||
#[test]
|
||||
|
||||
Reference in new issue
Block a user