Skip to content

armgit / raea

Public Imported from GitHub
0

Render Anything Engine: tell your AI what to make. Videos, motion graphics, 3D and games as React and TypeScript, rendered on your own GPU. Rust + Bevy, macOS, Linux and Windows.

The clipping check doesn't test a part in a solid whose cells are over twice its size: a glTF node spanning a whole building has cells over a metre and flagged every prop near its walls. The z-fight check measures the area two faces really share and says where (N cm2 shared, most at a point). By the Solari session

shinyobjectz346cacd71a52
diff --git a/core/zfight/src/lib.rs b/core/zfight/src/lib.rs
index 1ed92f92..24a673d9 100644
--- a/core/zfight/src/lib.rs
+++ b/core/zfight/src/lib.rs
@@ -22,7 +22,6 @@ struct Face {
n: Vec3,
d: f32,
tri: [Vec3; 3],
- area: f32,
}
fn face(t: &[Vec3; 3]) -> Option<Face> {
@@ -32,7 +31,7 @@ fn face(t: &[Vec3; 3]) -> Option<Face> {
return None;
}
let n = c / l;
- Some(Face { n, d: n.dot(t[0]), tri: *t, area: 0.5 * l })
+ Some(Face { n, d: n.dot(t[0]), tri: *t })
}
type Key = (i32, i32, i32, i64);
@@ -73,12 +72,53 @@ fn overlap(a: &Face, b: &Face) -> bool {
true
}
+/// What two coplanar faces share: its area, and its middle. One triangle cut by the other's three edges.
+fn shared(a: &Face, b: &Face) -> (f32, Vec3) {
+ let u = a.n.any_orthonormal_vector();
+ let v = a.n.cross(u);
+ let flat = |p: Vec3| Vec2::new(p.dot(u), p.dot(v));
+ let mut poly: Vec<Vec2> = a.tri.iter().map(|p| flat(*p)).collect();
+ let q = b.tri.map(flat);
+ // b's winding in this frame, so its inside is on one known side of each edge
+ let turn = (q[1] - q[0]).perp_dot(q[2] - q[0]).signum();
+ for k in 0..3 {
+ let (e0, e1) = (q[k], q[(k + 1) % 3]);
+ let side = |p: Vec2| (e1 - e0).perp_dot(p - e0) * turn;
+ let mut cut = Vec::with_capacity(poly.len() + 2);
+ for i in 0..poly.len() {
+ let (p0, p1) = (poly[i], poly[(i + 1) % poly.len()]);
+ let (s0, s1) = (side(p0), side(p1));
+ if s0 >= 0.0 {
+ cut.push(p0);
+ }
+ if (s0 > 0.0 && s1 < 0.0) || (s0 < 0.0 && s1 > 0.0) {
+ cut.push(p0 + (p1 - p0) * (s0 / (s0 - s1)));
+ }
+ }
+ poly = cut;
+ if poly.len() < 3 {
+ return (0.0, Vec3::ZERO);
+ }
+ }
+ let mut area = 0.0;
+ let mut mid = Vec2::ZERO;
+ for i in 1..poly.len() - 1 {
+ let t = 0.5 * (poly[i] - poly[0]).perp_dot(poly[i + 1] - poly[0]).abs();
+ area += t;
+ mid += (poly[0] + poly[i] + poly[i + 1]) / 3.0 * t;
+ }
+ if area <= 0.0 {
+ return (0.0, Vec3::ZERO);
+ }
+ let mid = mid / area;
+ (area, u * mid.x + v * mid.y + a.n * a.d)
+}
+
fn same_plane(a: &Face, b: &Face) -> bool {
a.n.dot(b.n) >= PARALLEL && (a.n.dot(b.tri[0]) - a.d).abs() <= PLANE
}
-/// One face over another: how many pairs, about how much area (the smaller face of each pair, summed: an upper
-/// bound), and where the first is. `b` None: within `a` itself.
+/// One face over another: how many pairs, the area they share, and the middle of the largest shared patch. `b` None: within `a` itself.
pub fn overlaps(a: &[[Vec3; 3]], b: Option<&[[Vec3; 3]]>) -> Option<(usize, f32, Vec3)> {
let o = sorted(a, b);
o.fight.or(o.twins).or(o.bases)
@@ -129,10 +169,20 @@ pub fn sorted(a: &[[Vec3; 3]], b: Option<&[[Vec3; 3]]>) -> Overlaps {
buckets.entry(key(f.n, f.d)).or_default().push(i);
}
let mut out = Overlaps::default();
- let add = |to: &mut Option<(usize, f32, Vec3)>, f: &Face, g: &Face| {
- let e = to.get_or_insert((0, 0.0, (f.tri[0] + f.tri[1] + f.tri[2]) / 3.0));
+ // the largest patch each kind has seen, for where to point
+ let mut most = [0.0f32; 3];
+ let mut add = |kind: usize, to: &mut Option<(usize, f32, Vec3)>, f: &Face, g: &Face| {
+ let (area, mid) = shared(f, g);
+ if area <= 0.0 {
+ return;
+ }
+ let e = to.get_or_insert((0, 0.0, mid));
e.0 += 1;
- e.1 += f.area.min(g.area);
+ e.1 += area;
+ if area > most[kind] {
+ most[kind] = area;
+ e.2 = mid;
+ }
};
for (i, f) in fa.iter().enumerate() {
let mut seen: Vec<usize> = Vec::new();
@@ -147,8 +197,13 @@ pub fn sorted(a: &[[Vec3; 3]], b: Option<&[[Vec3; 3]]>) -> Overlaps {
if same_plane(f, g) && overlap(f, g) {
let y = f.tri[0].y;
let base = b.is_some() && f.n.y <= -0.999 && (y - low_a).abs() <= 0.001 && (y - low_b).abs() <= 0.001;
- let to = if base { &mut out.bases } else if b.is_some() && twin(f, g) { &mut out.twins } else { &mut out.fight };
- add(to, f, g);
+ if base {
+ add(0, &mut out.bases, f, g);
+ } else if b.is_some() && twin(f, g) {
+ add(1, &mut out.twins, f, g);
+ } else {
+ add(2, &mut out.fight, f, g);
+ }
}
}
}
@@ -171,7 +226,12 @@ mod tests {
let floor = quad(0.0, -1.0, 1.0, true);
let rug = quad(0.0001, 0.0, 0.5, true);
let (n, area, _) = overlaps(&rug, Some(&floor)).expect("the rug's top fights the floor's");
- assert!(n >= 2 && area > 0.1, "{n} {area}");
+ // the rug's whole top, 0.5 m², and no more (the floor's triangles are bigger); pointed at inside the rug
+ let at = overlaps(&rug, Some(&floor)).unwrap().2;
+ assert!(n >= 2 && (area - 0.5).abs() < 1e-4 && at.x > 0.0 && at.x < 0.5 && at.z > 0.0 && at.z < 1.0, "{n} {area} {at}");
+ // a strip 2 mm wide along a metre: 20 cm²
+ let (_, strip, _) = overlaps(&quad(0.0, 0.998, 2.0, true), Some(&floor)).expect("a strip over the floor's edge");
+ assert!((strip - 0.002).abs() < 1e-5, "{strip}");
// 1 cm above it, it doesn't
assert!(overlaps(&quad(0.01, 0.0, 0.5, true), Some(&floor)).is_none());
// back to back (a box resting on another: one top up, the other's bottom down) doesn't fight
diff --git a/systems/contact/src/lib.rs b/systems/contact/src/lib.rs
index d43c16c3..1004a8f4 100644
--- a/systems/contact/src/lib.rs
+++ b/systems/contact/src/lib.rs
@@ -72,6 +72,8 @@ const ENCLOSED: f32 = 0.97;
const PIERCE: f32 = 0.15;
const PIERCE_MIN: f32 = 0.004;
const PIERCE_POINTS: usize = 4;
+/// The other part's cell at most this share of the smaller part's diagonal, or its solid can't say where the part is.
+const COARSE: f32 = 0.5;
/// A part smaller than this across (metres) is a particle: a spark, an ember, a mote.
const PARTICLE: f32 = 0.08;
/// An overlap within the bottom of the smaller part (this share of its height) is it seated in what's below.
@@ -206,6 +208,11 @@ fn volume(a: &Solid, wa: &Mat4, b: &Solid, wb: &Mat4) -> Option<Hit> {
if size < PARTICLE {
return None;
}
+ // the other's cells are coarser than the part can be placed in: a node holding every wall of one material in a
+ // mall is voxelized over a metre a cell, and each payphone near one read "47% inside" it (11-25, MALL RAT)
+ if o.cell * ko > COARSE * size {
+ return None;
+ }
// no point of the overlap is deeper than half the boxes' common part is thin: two parts side by side stop here
let (ilo, ihi) = (alo.max(blo), ahi.min(bhi));
if (ihi - ilo).min_element() < DEPTH_SHARE * size {
@@ -384,4 +391,15 @@ mod tests {
// beside it
assert!(!blocked(Vec3::new(3.0, 0.0, 0.0), Vec3::new(3.0, 0.0, 6.0), 0.05, &seen));
}
+
+ #[test]
+ fn a_part_smaller_than_the_others_cells_isnt_tested_in_it() {
+ // a stand sunk to its middle in the top of a block: found in a block whose cells place it, and one as large
+ // as the cells in a 60 m block, but not the 2 m stand in the 60 m block (its cells are over a metre)
+ let block = |x: f32| voxelize(&boxed(Vec3::new(0.0, 6.0, 0.0), Vec3::new(x, 12.0, 12.0))).unwrap();
+ let stand = |k: f32| voxelize(&boxed(Vec3::new(0.0, 12.0, 0.0), Vec3::new(0.4 * k, 2.0 * k, 0.4 * k))).unwrap();
+ let w = Mat4::IDENTITY;
+ assert!(test(&stand(1.0), &w, &block(4.0), &w).is_some() && test(&stand(4.0), &w, &block(60.0), &w).is_some());
+ assert!(test(&stand(1.0), &w, &block(60.0), &w).is_none());
+ }
}
diff --git a/systems/contact/src/lint.rs b/systems/contact/src/lint.rs
index 42c23223..38743c6c 100644
--- a/systems/contact/src/lint.rs
+++ b/systems/contact/src/lint.rs
@@ -402,18 +402,18 @@ pub fn lint(now: &[Item], next: &[Item]) -> (Vec<Value>, usize) {
errors += 1;
}
out.push(json!({"part": parts[i].label, "into": parts[j].label, "check": "zfight", "level": level, "faces": n, "area": (area * 1e6).round() / 1e6, "at": [at.x, at.y, at.z],
- "says": format!("{} and {} have faces in one plane over each other ({n} pair{}, up to {:.1} cm² at [{:.3}, {:.3}, {:.3}]): they flicker as the camera moves; lift one by a millimetre or cut the overlap", parts[i].label, parts[j].label, if n == 1 { "" } else { "s" }, area * 1e4, at.x, at.y, at.z)}));
+ "says": format!("{} and {} have faces in one plane over each other ({n} pair{}, {:.1} cm² shared, most at [{:.3}, {:.3}, {:.3}]): they flicker as the camera moves; lift one by a millimetre or cut the overlap", parts[i].label, parts[j].label, if n == 1 { "" } else { "s" }, area * 1e4, at.x, at.y, at.z)}));
}
// the same triangles in both (a slab built into two neighbouring chunks) draw alike whichever wins, unless
// the two give them different materials: said, never an error
if let Some((n, area, at)) = over.twins {
out.push(json!({"part": parts[i].label, "into": parts[j].label, "check": "doubled", "level": "warning", "faces": n, "area": (area * 1e6).round() / 1e6, "at": [at.x, at.y, at.z],
- "says": format!("{} and {} both hold the same {n} face{} ({:.1} cm², first at [{:.3}, {:.3}, {:.3}]): nothing to see where they look alike; take them out of one", parts[i].label, parts[j].label, if n == 1 { "" } else { "s" }, area * 1e4, at.x, at.y, at.z)}));
+ "says": format!("{} and {} both hold the same {n} face{} ({:.1} cm², most at [{:.3}, {:.3}, {:.3}]): nothing to see where they look alike; take them out of one", parts[i].label, parts[j].label, if n == 1 { "" } else { "s" }, area * 1e4, at.x, at.y, at.z)}));
}
}
if let Some((n, area, at)) = raea_core_zfight::overlaps(&parts[i].tris, None) {
out.push(json!({"part": parts[i].label, "check": "zfight", "level": "warning", "faces": n, "area": (area * 1e6).round() / 1e6, "at": [at.x, at.y, at.z],
- "says": format!("{} has faces doubled in one plane inside it ({n} pair{}, up to {:.1} cm²): they flicker where they show; delete the doubles (raea gen check -o)", parts[i].label, if n == 1 { "" } else { "s" }, area * 1e4)}));
+ "says": format!("{} has faces doubled in one plane inside it ({n} pair{}, {:.1} cm² shared): they flicker where they show; delete the doubles (raea gen check -o)", parts[i].label, if n == 1 { "" } else { "s" }, area * 1e4)}));
}
}
(out, errors)