Merge pull request #165 from rdbtCVS/t3code/fix-inventory-banner-rendering
Fix inventory banner face depth ordering
This commit is contained in:
@@ -10,6 +10,8 @@ use crate::hud::{HudBatch, Rect};
|
||||
use glam::{EulerRot, Mat4, Vec2, Vec3};
|
||||
use std::{collections::HashMap, f32::consts::PI};
|
||||
|
||||
mod depth;
|
||||
|
||||
/// `Lighting.DIFFUSE_LIGHT_0` and `DIFFUSE_LIGHT_1`.
|
||||
fn diffuse_lights() -> (Vec3, Vec3) {
|
||||
(
|
||||
@@ -177,35 +179,19 @@ fn draw_quads(
|
||||
rect: [f32; 4],
|
||||
time: f32,
|
||||
) {
|
||||
// Back-face culling: front faces wind counter-clockwise on screen with y up.
|
||||
let mut visible: Vec<(f32, usize)> = quads
|
||||
.iter()
|
||||
.enumerate()
|
||||
.filter(|(_, q)| {
|
||||
let p = q.pos.map(|v| Vec2::new(v.x, -v.y));
|
||||
let area: f32 = (0..4)
|
||||
.map(|i| {
|
||||
let (a, b) = (p[i], p[(i + 1) % 4]);
|
||||
a.x * b.y - b.x * a.y
|
||||
})
|
||||
.sum();
|
||||
area > 0.0
|
||||
})
|
||||
.map(|(i, q)| (q.pos.iter().map(|v| v.z).sum::<f32>() / 4.0, i))
|
||||
.collect();
|
||||
// Far to near; the depth buffer of the item atlas, approximated per quad.
|
||||
visible.sort_by(|a, b| a.0.total_cmp(&b.0));
|
||||
for (_, i) in visible {
|
||||
let q = &quads[i];
|
||||
let points: [Point; 4] = std::array::from_fn(|k| Point {
|
||||
p: Vec2::new(q.pos[k].x, q.pos[k].y),
|
||||
uv: q.uv[k],
|
||||
});
|
||||
for face in depth::sort(quads) {
|
||||
let q = &quads[face.quad];
|
||||
let points: Vec<_> = face
|
||||
.corners
|
||||
.iter()
|
||||
.map(|v| Point {
|
||||
p: v.pos.truncate(),
|
||||
uv: v.uv,
|
||||
})
|
||||
.collect();
|
||||
let color = lit_color(q, lights);
|
||||
for tri in [
|
||||
[points[0], points[1], points[2]],
|
||||
[points[0], points[2], points[3]],
|
||||
] {
|
||||
for i in 1..points.len().saturating_sub(1) {
|
||||
let tri = [points[0], points[i], points[i + 1]];
|
||||
let fan = clip_triangle(tri, rect);
|
||||
if fan.len() < 3 {
|
||||
continue;
|
||||
@@ -275,6 +261,108 @@ pub fn draw_banner_result(
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn face(y0: f32, y1: f32, z: impl Fn(f32) -> f32, color: u32) -> ItemQuad {
|
||||
ItemQuad {
|
||||
pos: [(0.0, y0), (0.0, y1), (8.0, y1), (8.0, y0)].map(|(x, y)| Vec3::new(x, y, z(y))),
|
||||
uv: [[0.0, 0.0], [0.0, 1.0], [1.0, 1.0], [1.0, 0.0]],
|
||||
normal: Vec3::Z,
|
||||
color,
|
||||
foil: Foil::None,
|
||||
light_emission: 0,
|
||||
}
|
||||
}
|
||||
|
||||
// Sample the emitted triangles in draw order, using opaque test faces.
|
||||
fn color_at(batch: &HudBatch, p: Vec2) -> Option<u32> {
|
||||
batch
|
||||
.vertices
|
||||
.as_chunks::<3>()
|
||||
.0
|
||||
.iter()
|
||||
.rev()
|
||||
.find_map(|tri| {
|
||||
let [a, b, c] = std::array::from_fn(|i| Vec2::from(tri[i].pos));
|
||||
let area = (b - a).perp_dot(c - a);
|
||||
if area.abs() < 1e-6 {
|
||||
return None;
|
||||
}
|
||||
let u = (b - p).perp_dot(c - p) / area;
|
||||
let v = (c - p).perp_dot(a - p) / area;
|
||||
let w = 1.0 - u - v;
|
||||
(u >= 0.0 && v >= 0.0 && w >= 0.0).then_some(tri[0].color)
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn banner_cloth_occludes_the_support_at_the_top_edge() {
|
||||
// The cloth is nearer where it overlaps the bar, although its
|
||||
// average depth is farther away because it slopes along its height.
|
||||
let bar = face(0.0, 1.0, |_| 0.0, 0xFF80_4000);
|
||||
let cloth = face(0.0, 8.0, |y| 2.0 - y, 0xFFFF_FFFF);
|
||||
let mut batch = HudBatch::new([320, 240]);
|
||||
draw_quads(
|
||||
&[bar, cloth],
|
||||
(Vec3::Z, Vec3::Z),
|
||||
&mut batch,
|
||||
[0.0, 0.0, 16.0, 16.0],
|
||||
0.0,
|
||||
);
|
||||
assert_eq!(color_at(&batch, Vec2::new(4.0, 0.5)), Some(u32::MAX));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn intersecting_faces_change_draw_order_at_their_intersection() {
|
||||
let flat = face(0.0, 8.0, |_| 0.0, 0xFFFF_0000);
|
||||
let tilted = face(0.0, 8.0, |y| y - 4.0, 0xFF00_00FF);
|
||||
for quads in [[flat, tilted], [tilted, flat]] {
|
||||
let mut batch = HudBatch::new([320, 240]);
|
||||
draw_quads(
|
||||
&quads,
|
||||
(Vec3::Z, Vec3::Z),
|
||||
&mut batch,
|
||||
[0.0, 0.0, 16.0, 16.0],
|
||||
0.0,
|
||||
);
|
||||
assert_eq!(
|
||||
color_at(&batch, Vec2::new(4.0, 2.0)),
|
||||
Some(lit_color(&flat, (Vec3::Z, Vec3::Z)))
|
||||
);
|
||||
assert_eq!(
|
||||
color_at(&batch, Vec2::new(4.0, 6.0)),
|
||||
Some(lit_color(&tilted, (Vec3::Z, Vec3::Z)))
|
||||
);
|
||||
// The cut must interpolate the texture along with the position.
|
||||
for v in &batch.vertices {
|
||||
assert!((v.uv[0] - v.pos[0] / 8.0).abs() < 1e-6);
|
||||
assert!((v.uv[1] - v.pos[1] / 8.0).abs() < 1e-6);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn coplanar_pattern_layers_keep_submission_order() {
|
||||
let base = face(0.0, 8.0, |y| 2.0 - y, 0xFFFF_FFFF);
|
||||
let pattern = ItemQuad {
|
||||
color: 0xFF00_00FF,
|
||||
..base
|
||||
};
|
||||
let quads = [face(0.0, 1.0, |_| 0.0, 0xFF80_4000), base, pattern];
|
||||
let mut batch = HudBatch::new([320, 240]);
|
||||
draw_quads(
|
||||
&quads,
|
||||
(Vec3::Z, Vec3::Z),
|
||||
&mut batch,
|
||||
[0.0, 0.0, 16.0, 16.0],
|
||||
0.0,
|
||||
);
|
||||
for y in [0.5, 6.0] {
|
||||
assert_eq!(
|
||||
color_at(&batch, Vec2::new(4.0, y)),
|
||||
Some(lit_color(&quads[2], (Vec3::Z, Vec3::Z)))
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn flat_lights_match_joml() {
|
||||
// new Matrix4f().rotationY(-PI/8).rotateX(3PI/4).transformDirection(L0).
|
||||
|
||||
@@ -0,0 +1,119 @@
|
||||
//! Back-to-front ordering for the orthographic GUI item view (near is +Z).
|
||||
//! Whole-face average depths fail when a long, tilted face overlaps a short
|
||||
//! one, such as a banner's cloth and support. Partition faces by their planes
|
||||
//! instead, splitting intersecting faces with their UVs. Coplanar faces keep
|
||||
//! submission order so banner/shield pattern layers still paint bottom first.
|
||||
use super::super::ItemQuad;
|
||||
use glam::{Vec3, Vec4};
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
pub(super) struct Corner {
|
||||
pub pos: Vec3,
|
||||
pub uv: [f32; 2],
|
||||
}
|
||||
|
||||
pub(super) struct Face {
|
||||
pub quad: usize,
|
||||
pub corners: Vec<Corner>,
|
||||
}
|
||||
|
||||
const EPSILON: f32 = 1e-4;
|
||||
|
||||
fn split(face: Face, distances: &[f32]) -> (Face, Face) {
|
||||
let mut back = Vec::new();
|
||||
let mut front = Vec::new();
|
||||
for i in 0..face.corners.len() {
|
||||
let a = face.corners[i];
|
||||
let b = face.corners[(i + 1) % face.corners.len()];
|
||||
let da = distances[i];
|
||||
let db = distances[(i + 1) % face.corners.len()];
|
||||
if da <= EPSILON {
|
||||
back.push(a);
|
||||
}
|
||||
if da >= -EPSILON {
|
||||
front.push(a);
|
||||
}
|
||||
if (da < -EPSILON && db > EPSILON) || (da > EPSILON && db < -EPSILON) {
|
||||
let t = da / (da - db);
|
||||
let corner = Corner {
|
||||
pos: a.pos.lerp(b.pos, t),
|
||||
uv: std::array::from_fn(|k| a.uv[k] + (b.uv[k] - a.uv[k]) * t),
|
||||
};
|
||||
back.push(corner);
|
||||
front.push(corner);
|
||||
}
|
||||
}
|
||||
(
|
||||
Face {
|
||||
quad: face.quad,
|
||||
corners: back,
|
||||
},
|
||||
Face {
|
||||
quad: face.quad,
|
||||
corners: front,
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
pub(super) fn sort(quads: &[ItemQuad]) -> Vec<Face> {
|
||||
let mut planes = vec![Vec4::ZERO; quads.len()];
|
||||
let faces: Vec<_> = quads
|
||||
.iter()
|
||||
.enumerate()
|
||||
.filter_map(|(i, q)| {
|
||||
let normal = (q.pos[2] - q.pos[0]).cross(q.pos[3] - q.pos[1]);
|
||||
// Front faces wind counter-clockwise on screen with Y up.
|
||||
if normal.z >= 0.0 {
|
||||
return None;
|
||||
}
|
||||
let normal = -normal.normalize();
|
||||
planes[i] = normal.extend(-normal.dot(q.pos[0]));
|
||||
Some(Face {
|
||||
quad: i,
|
||||
corners: std::array::from_fn::<_, 4, _>(|k| Corner {
|
||||
pos: q.pos[k],
|
||||
uv: q.uv[k],
|
||||
})
|
||||
.to_vec(),
|
||||
})
|
||||
})
|
||||
.collect();
|
||||
let mut out = Vec::new();
|
||||
// An explicit stack avoids recursion for models with many parallel faces.
|
||||
let mut pending = vec![(faces, false)];
|
||||
while let Some((faces, emit)) = pending.pop() {
|
||||
if emit {
|
||||
out.extend(faces);
|
||||
continue;
|
||||
}
|
||||
let mut faces = faces.into_iter();
|
||||
let Some(first) = faces.next() else { continue };
|
||||
let plane = planes[first.quad];
|
||||
let mut coplanar = vec![first];
|
||||
let mut back = Vec::new();
|
||||
let mut front = Vec::new();
|
||||
for face in faces {
|
||||
let distances: Vec<_> = face
|
||||
.corners
|
||||
.iter()
|
||||
.map(|c| plane.dot(c.pos.extend(1.0)))
|
||||
.collect();
|
||||
let behind = distances.iter().any(|d| *d < -EPSILON);
|
||||
let ahead = distances.iter().any(|d| *d > EPSILON);
|
||||
match (behind, ahead) {
|
||||
(false, false) => coplanar.push(face),
|
||||
(true, false) => back.push(face),
|
||||
(false, true) => front.push(face),
|
||||
(true, true) => {
|
||||
let (b, f) = split(face, &distances);
|
||||
back.push(b);
|
||||
front.push(f);
|
||||
}
|
||||
}
|
||||
}
|
||||
pending.push((front, false));
|
||||
pending.push((coplanar, true));
|
||||
pending.push((back, false));
|
||||
}
|
||||
out
|
||||
}
|
||||
Reference in New Issue
Block a user