7c1dd49810
Meshing sorts each section's and distant tile's solid quads into seven buckets: one per axis direction (all four corners in one axis plane, both triangles wound the same way) and one for everything else, and keeps each axis bucket's extreme plane. A quad facing +X at x = p is a back face from an eye at x <= p, which the solid pipeline culls anyway, so the frame draws only the buckets that can face the eye, merged into runs of adjacent buckets: one to three indirect draws per section on the existing page-run, occlusion-culling and flat paths. The eye comes from the frame's view-projection, so view bobbing is exact. The picture does not change; `horizon-ctl section-sides on|off` switches it for A/B checks. Written from scratch from the general back-face technique; no other renderer's code or Horizon's removed code read. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
347 lines
12 KiB
Rust
347 lines
12 KiB
Rust
//! A mesh's solid quads sorted by the side they face, so that the renderer
|
|
//! can leave out the quads turned away from the eye before the GPU shades
|
|
//! their vertices.
|
|
//!
|
|
//! A quad in the plane `x = p` whose corners go counter-clockwise seen from
|
|
//! +X faces +X. From an eye at `x < p` it is seen from behind, and at
|
|
//! `x = p` edge on; the solid pipeline culls back faces, so it draws nothing
|
|
//! from there. A bucket of such quads draws nothing at all while the eye is
|
|
//! at or below the lowest of their planes, and likewise for the other five
|
|
//! axis directions. The seventh bucket holds every other quad (sloped,
|
|
//! rotated, degenerate, or whose two triangles wind opposite ways): it is
|
|
//! drawn from everywhere.
|
|
//!
|
|
//! The buckets lie one after another in the mesh, in an order chosen so that
|
|
//! the buckets an eye can see are usually one or two runs of quads: one draw
|
|
//! each. Inside a bucket quads keep their model order, so coplanar quads
|
|
//! facing one way (an overlay on its base) still draw in that order.
|
|
use super::TerrainVertex;
|
|
|
|
/// The buckets in mesh order: +X, +Z, other, +Y, -Z, -X, -Y. Seen from
|
|
/// above (+Y, the commonest case), each pair of horizontal sides with a
|
|
/// common corner is then one run, or two.
|
|
const BUCKETS: [Bucket; 7] = [
|
|
Bucket::Side(0, true),
|
|
Bucket::Side(2, true),
|
|
Bucket::Other,
|
|
Bucket::Side(1, true),
|
|
Bucket::Side(2, false),
|
|
Bucket::Side(0, false),
|
|
Bucket::Side(1, false),
|
|
];
|
|
const OTHER: usize = 2;
|
|
/// How far, in blocks, the eye must be behind a bucket's planes before the
|
|
/// bucket is left out. The renderer computes both the eye and the section
|
|
/// origins in `f32`, with errors far below this.
|
|
const MARGIN: f32 = 1.0 / 16.0;
|
|
|
|
#[derive(Clone, Copy)]
|
|
enum Bucket {
|
|
/// Quads in a plane across `axis` facing its positive (`true`) or
|
|
/// negative direction.
|
|
Side(usize, bool),
|
|
Other,
|
|
}
|
|
|
|
/// Where each bucket of a mesh's solid quads ends, and the planes that tell
|
|
/// whether it can face the eye.
|
|
#[derive(Clone, Copy, Debug, Default, PartialEq)]
|
|
pub struct Sides {
|
|
/// The solid quads up to the end of each bucket, in `BUCKETS` order.
|
|
ends: [u32; 7],
|
|
/// Per bucket facing a positive direction, its lowest plane; facing a
|
|
/// negative one, its highest (unused for the other bucket).
|
|
planes: [f32; 7],
|
|
}
|
|
|
|
impl Sides {
|
|
/// `quads` solid quads in one bucket drawn from everywhere: for meshes
|
|
/// that were not sorted.
|
|
pub fn unsorted(quads: u32) -> Self {
|
|
let mut ends = [0; 7];
|
|
ends[OTHER..].fill(quads);
|
|
Self {
|
|
ends,
|
|
planes: [0.0; 7],
|
|
}
|
|
}
|
|
|
|
/// The number of solid quads the buckets hold.
|
|
pub fn quads(&self) -> u32 {
|
|
self.ends[6]
|
|
}
|
|
|
|
/// Calls `draw(first, count)` for each run of quads an eye at `eye`
|
|
/// (relative to the mesh's origin) can see the front of, nearest the
|
|
/// start first. Empty buckets do not break a run.
|
|
pub fn visible(&self, eye: [f32; 3], mut draw: impl FnMut(u32, u32)) {
|
|
let mut start = 0;
|
|
let mut run: Option<u32> = None;
|
|
for (i, bucket) in BUCKETS.iter().enumerate() {
|
|
let end = self.ends[i];
|
|
if end == start {
|
|
continue;
|
|
}
|
|
let seen = match *bucket {
|
|
Bucket::Side(axis, true) => eye[axis] > self.planes[i] - MARGIN,
|
|
Bucket::Side(axis, false) => eye[axis] < self.planes[i] + MARGIN,
|
|
Bucket::Other => true,
|
|
};
|
|
match (seen, run) {
|
|
(true, None) => run = Some(start),
|
|
(false, Some(first)) => {
|
|
draw(first, start - first);
|
|
run = None;
|
|
}
|
|
_ => {}
|
|
}
|
|
start = end;
|
|
}
|
|
if let Some(first) = run {
|
|
draw(first, start - first);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// The bucket of a quad: the axis plane it lies in and the side its
|
|
/// triangles (corners 0, 1, 2 and 0, 2, 3, as the renderer draws them) face.
|
|
fn bucket(quad: &[TerrainVertex; 4]) -> usize {
|
|
let p = quad.map(|v| v.pos);
|
|
for axis in 0..3 {
|
|
if p.iter().any(|v| v[axis] != p[0][axis]) {
|
|
continue;
|
|
}
|
|
// The normal's component along `axis`, twice each triangle's area.
|
|
let (u, w) = ((axis + 1) % 3, (axis + 2) % 3);
|
|
let area = |a: usize, b: usize, c: usize| {
|
|
(p[b][u] - p[a][u]) * (p[c][w] - p[a][w]) - (p[b][w] - p[a][w]) * (p[c][u] - p[a][u])
|
|
};
|
|
let (first, second) = (area(0, 1, 2), area(0, 2, 3));
|
|
let positive = first >= 0.0 && second >= 0.0;
|
|
let negative = first <= 0.0 && second <= 0.0;
|
|
if positive == negative {
|
|
// Opposite windings, or no area at all.
|
|
return OTHER;
|
|
}
|
|
return BUCKETS
|
|
.iter()
|
|
.position(|b| matches!(*b, Bucket::Side(a, s) if a == axis && s == positive))
|
|
.expect("every side has a bucket");
|
|
}
|
|
OTHER
|
|
}
|
|
|
|
/// Reorders `solid`'s quads by bucket, keeping their order inside each.
|
|
pub fn sort(solid: &mut Vec<TerrainVertex>) -> Sides {
|
|
let (quads, rest) = solid.as_chunks::<4>();
|
|
debug_assert!(rest.is_empty(), "solid vertices come in quads");
|
|
let buckets: Vec<u8> = quads.iter().map(|q| bucket(q) as u8).collect();
|
|
let mut counts = [0u32; 7];
|
|
let mut planes = [0f32; 7];
|
|
for (quad, &b) in quads.iter().zip(&buckets) {
|
|
let b = usize::from(b);
|
|
if let Bucket::Side(axis, positive) = BUCKETS[b] {
|
|
let p = quad[0].pos[axis];
|
|
planes[b] = match (counts[b], positive) {
|
|
(0, _) => p,
|
|
(_, true) => planes[b].min(p),
|
|
(_, false) => planes[b].max(p),
|
|
};
|
|
}
|
|
counts[b] += 1;
|
|
}
|
|
let mut ends = [0u32; 7];
|
|
let mut total = 0;
|
|
for (end, count) in ends.iter_mut().zip(counts) {
|
|
total += count;
|
|
*end = total;
|
|
}
|
|
if counts.iter().filter(|&&c| c > 0).count() > 1 {
|
|
let mut next: [u32; 7] = std::array::from_fn(|b| ends[b] - counts[b]);
|
|
let mut sorted = vec![quads[0]; quads.len()];
|
|
for (quad, &b) in quads.iter().zip(&buckets) {
|
|
let slot = &mut next[usize::from(b)];
|
|
sorted[*slot as usize] = *quad;
|
|
*slot += 1;
|
|
}
|
|
*solid = sorted.into_flattened();
|
|
}
|
|
Sides { ends, planes }
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
fn quad(corners: [[f32; 3]; 4], layer: u16) -> [TerrainVertex; 4] {
|
|
corners.map(|p| TerrainVertex::new(p, [0.0; 2], layer, 0, 0))
|
|
}
|
|
|
|
/// A unit block's six faces at `origin`, wound counter-clockwise seen
|
|
/// from outside, as block models are.
|
|
fn cube(origin: [f32; 3], layer: u16) -> Vec<TerrainVertex> {
|
|
let [x, y, z] = origin;
|
|
let (a, b, c) = (x + 1.0, y + 1.0, z + 1.0);
|
|
[
|
|
[[a, y, z], [a, b, z], [a, b, c], [a, y, c]], // +X
|
|
[[x, y, z], [x, y, c], [x, b, c], [x, b, z]], // -X
|
|
[[x, b, z], [x, b, c], [a, b, c], [a, b, z]], // +Y
|
|
[[x, y, z], [a, y, z], [a, y, c], [x, y, c]], // -Y
|
|
[[x, y, c], [a, y, c], [a, b, c], [x, b, c]], // +Z
|
|
[[x, y, z], [x, b, z], [a, b, z], [a, y, z]], // -Z
|
|
]
|
|
.into_iter()
|
|
.flat_map(|q| quad(q, layer))
|
|
.collect()
|
|
}
|
|
|
|
fn runs(sides: &Sides, eye: [f32; 3]) -> Vec<(u32, u32)> {
|
|
let mut out = Vec::new();
|
|
sides.visible(eye, |first, count| out.push((first, count)));
|
|
out
|
|
}
|
|
|
|
/// The quads of `solid` whose triangles face `eye`, by their layer.
|
|
fn front_facing(solid: &[TerrainVertex], eye: [f32; 3]) -> Vec<u16> {
|
|
let sub = |a: [f32; 3], b: [f32; 3]| [a[0] - b[0], a[1] - b[1], a[2] - b[2]];
|
|
solid
|
|
.as_chunks::<4>()
|
|
.0
|
|
.iter()
|
|
.filter(|q| {
|
|
[[0, 1, 2], [0, 2, 3]].iter().any(|t| {
|
|
let (e1, e2) = (sub(q[t[1]].pos, q[t[0]].pos), sub(q[t[2]].pos, q[t[0]].pos));
|
|
let n = [
|
|
e1[1] * e2[2] - e1[2] * e2[1],
|
|
e1[2] * e2[0] - e1[0] * e2[2],
|
|
e1[0] * e2[1] - e1[1] * e2[0],
|
|
];
|
|
let to_eye = sub(eye, q[t[0]].pos);
|
|
n[0] * to_eye[0] + n[1] * to_eye[1] + n[2] * to_eye[2] > 0.0
|
|
})
|
|
})
|
|
.map(|q| q[0].layer)
|
|
.collect()
|
|
}
|
|
|
|
#[test]
|
|
fn cube_faces_go_to_their_buckets_in_order() {
|
|
let mut solid = cube([3.0, 4.0, 5.0], 0);
|
|
for (i, v) in solid.iter_mut().enumerate() {
|
|
v.layer = (i / 4) as u16;
|
|
}
|
|
let sides = sort(&mut solid);
|
|
assert_eq!(sides.ends, [1, 2, 2, 3, 4, 5, 6]);
|
|
let layers: Vec<u16> = solid.iter().step_by(4).map(|v| v.layer).collect();
|
|
// +X, +Z, +Y, -Z, -X, -Y.
|
|
assert_eq!(layers, [0, 4, 2, 5, 1, 3]);
|
|
assert_eq!(sides.planes[0], 4.0); // +X at x = 4
|
|
assert_eq!(sides.planes[5], 3.0); // -X at x = 3
|
|
assert_eq!(sides.planes[3], 5.0); // +Y at y = 5
|
|
}
|
|
|
|
#[test]
|
|
fn the_drawn_runs_hold_every_quad_that_faces_the_eye() {
|
|
// Blocks across a section, with a sloped quad and a bowtie.
|
|
let mut solid = Vec::new();
|
|
let mut layer = 0;
|
|
for origin in [
|
|
[0.0, 0.0, 0.0],
|
|
[7.0, 3.0, 12.0],
|
|
[15.0, 15.0, 15.0],
|
|
[2.0, 9.0, 4.0],
|
|
] {
|
|
for q in cube(origin, 0).chunks(4) {
|
|
solid.extend(q.iter().map(|v| TerrainVertex { layer, ..*v }));
|
|
layer += 1;
|
|
}
|
|
}
|
|
let slope = [
|
|
[1.0, 1.0, 1.0],
|
|
[2.0, 1.0, 1.0],
|
|
[2.0, 2.0, 2.0],
|
|
[1.0, 2.0, 2.0],
|
|
];
|
|
let bowtie = [
|
|
[5.0, 1.0, 1.0],
|
|
[5.0, 2.0, 2.0],
|
|
[5.0, 1.0, 2.0],
|
|
[5.0, 2.0, 1.0],
|
|
];
|
|
for corners in [slope, bowtie] {
|
|
solid.extend(quad(corners, layer));
|
|
layer += 1;
|
|
}
|
|
let sides = sort(&mut solid);
|
|
assert_eq!(sides.quads(), u32::from(layer));
|
|
let eyes = [-20.0f32, -0.5, 0.0, 3.5, 8.0, 15.99, 16.0, 30.0];
|
|
for &x in &eyes {
|
|
for &y in &eyes {
|
|
for &z in &eyes {
|
|
let eye = [x, y, z];
|
|
let mut drawn = Vec::new();
|
|
for (first, count) in runs(&sides, eye) {
|
|
drawn.extend(
|
|
solid[first as usize * 4..(first + count) as usize * 4]
|
|
.iter()
|
|
.step_by(4)
|
|
.map(|v| v.layer),
|
|
);
|
|
}
|
|
for layer in front_facing(&solid, eye) {
|
|
assert!(drawn.contains(&layer), "{eye:?} misses quad {layer}");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn runs_merge_across_hidden_and_empty_buckets() {
|
|
let mut solid = cube([0.0; 3], 0);
|
|
let sides = sort(&mut solid);
|
|
// From above, +X and +Z: +X, +Z, (no other), +Y is one run.
|
|
assert_eq!(runs(&sides, [8.0, 8.0, 8.0]), [(0, 3)]);
|
|
// From below, -X and -Z: -Z, -X, -Y.
|
|
assert_eq!(runs(&sides, [-8.0, -8.0, -8.0]), [(3, 3)]);
|
|
// From above, -X and +Z: +Z, +Y, then -X.
|
|
assert_eq!(runs(&sides, [-8.0, 8.0, 8.0]), [(1, 2), (4, 1)]);
|
|
// Inside the block every face is seen from behind.
|
|
assert!(runs(&sides, [0.5; 3]).is_empty());
|
|
// Level with its top, the eye may see it: the margin keeps it.
|
|
assert_eq!(runs(&sides, [0.5, 1.0, 0.5]), [(2, 1)]);
|
|
}
|
|
|
|
#[test]
|
|
fn unsorted_meshes_draw_everything() {
|
|
let sides = Sides::unsorted(9);
|
|
assert_eq!(sides.quads(), 9);
|
|
assert_eq!(runs(&sides, [-100.0; 3]), [(0, 9)]);
|
|
assert!(runs(&Sides::unsorted(0), [0.0; 3]).is_empty());
|
|
}
|
|
|
|
#[test]
|
|
fn one_bucket_keeps_the_mesh_as_it_is() {
|
|
let mut solid: Vec<_> = (0..3)
|
|
.flat_map(|i| {
|
|
quad(
|
|
[
|
|
[1.0, 0.0, 0.0],
|
|
[1.0, 1.0, 0.0],
|
|
[1.0, 1.0, 1.0],
|
|
[1.0, 0.0, 1.0],
|
|
],
|
|
i,
|
|
)
|
|
})
|
|
.collect();
|
|
let before = solid.clone();
|
|
let sides = sort(&mut solid);
|
|
assert_eq!(sides.ends, [3; 7]);
|
|
assert_eq!(
|
|
solid.iter().map(|v| v.layer).collect::<Vec<_>>(),
|
|
before.iter().map(|v| v.layer).collect::<Vec<_>>()
|
|
);
|
|
}
|
|
}
|