Files
horizon-airdef/crates/horizon-client/src/breaking.rs
T
Ludovic Marie 0944280fb3 Merge PR #52 (entity directional lighting) into review-52
Conflicts with #51: entity.wgsl keeps the crumbling sprite UVs and
samples with the per-face colour; gfx.rs keeps crumbling_sprite and
outline_params and puts the Nether flag in time.y; game.rs keeps #51's
breaking and cloud geometry. #51's new vertices (breaking decals,
cloud faces, the batching test) get the new zero normal, so they stay
unshaded.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-13 23:18:40 +02:00

110 lines
4.1 KiB
Rust

//! BlockModelFeatureRenderer breaking quads and SheetedDecalTextureGenerator UVs.
use crate::{camera::joml, mesher::Vertex, models::catalog::BakedModel};
use horizon_assets::model::Direction;
use std::f32::consts::{FRAC_PI_2, PI};
/// Project in model space: the inverse decal pose cancels the block translation/offset.
pub fn decal_uv([x, y, z]: [f32; 3], direction: Direction) -> [f32; 2] {
let sy = joml::sin(PI);
let cy = joml::cos_from_sin(sy, PI);
let sx = joml::sin(-FRAC_PI_2);
let cx = joml::cos_from_sin(sx, -FRAC_PI_2);
let p = [cy * x + sy * z, y, -sy * x + cy * z];
let p = [p[0], cx * p[1] - sx * p[2], sx * p[1] + cx * p[2]];
let (angle_x, angle_z) = match direction {
Direction::Down => (PI, 0.0),
Direction::Up => (0.0, 0.0),
Direction::North => (FRAC_PI_2, PI),
Direction::South => (FRAC_PI_2, 0.0),
Direction::West => (FRAC_PI_2, FRAC_PI_2),
Direction::East => (FRAC_PI_2, -FRAC_PI_2),
};
let p = joml::Quat::rotation_yxz(0.0, angle_x, angle_z).transform(p);
[-p[0], -p[1]]
}
pub fn geometry(model: &BakedModel, origin: glam::Vec3, translucent: bool, out: &mut Vec<Vertex>) {
// Vanilla visits all six cull groups and then the unculled faces, even beside solid neighbours.
for cull in Direction::ALL.map(Some).into_iter().chain([None]) {
for face in model.faces.iter().filter(|face| face.quad.cull == cull) {
for pos in face.quad.positions {
out.push(Vertex {
pos: (origin + glam::Vec3::from(pos)).to_array(),
uv: decal_uv(pos, face.quad.facing),
layer: if translucent { 12 } else { 11 },
color: 0xffff_ffff,
light: 240 | (240 << 8),
normal: [0; 4],
});
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::models::catalog::BakedFace;
use horizon_assets::{bake::ModelQuad, model::Material};
fn slab_top() -> BakedModel {
BakedModel {
ambient_occlusion: true,
cube: None,
faces: vec![BakedFace {
layer: 0,
opaque: true,
translucent: false,
quad: ModelQuad {
positions: [
[0.0, 0.5, 0.0],
[0.0, 0.5, 1.0],
[1.0, 0.5, 1.0],
[1.0, 0.5, 0.0],
],
uv: [[0.0; 2]; 4],
facing: Direction::Up,
cull: None,
material: Material {
sprite: "block/stone".into(),
force_translucent: false,
},
tint_index: -1,
shade_direction_override: None,
light_emission: 0,
},
}],
}
}
#[test]
fn slab_overlay_follows_baked_top_not_unit_cube() {
let model = slab_top();
let mut vertices = Vec::new();
geometry(&model, glam::Vec3::new(2.0, 3.0, 4.0), false, &mut vertices);
assert_eq!(vertices.len(), 4);
for (v, p) in vertices.iter().zip(model.faces[0].quad.positions) {
assert_eq!(v.pos, [p[0] + 2.0, p[1] + 3.0, p[2] + 4.0]);
assert_eq!(v.color, 0xffff_ffff);
}
}
#[test]
fn small_faces_use_projected_not_stretched_uvs() {
let a = decal_uv([0.0, 0.0, 0.0], Direction::North);
let b = decal_uv([0.0, 0.5, 0.0], Direction::North);
assert!(((b[1] - a[1]).abs() - 0.5).abs() < 1e-6);
}
#[test]
fn translucent_overlay_has_its_own_pass() {
let mut vertices = Vec::new();
geometry(&slab_top(), glam::Vec3::ZERO, true, &mut vertices);
assert!(vertices.iter().all(|v| v.layer == 12));
}
#[test]
fn empty_model_has_no_cube_overlay() {
let mut model = slab_top();
model.faces.clear();
let mut vertices = Vec::new();
geometry(&model, glam::Vec3::ZERO, false, &mut vertices);
assert!(vertices.is_empty());
}
}