From 45b43f00dffc3e0ae391e48095ddcd61bcf4c612 Mon Sep 17 00:00:00 2001 From: chexit Date: Wed, 4 Nov 2026 17:51:24 +0300 Subject: [PATCH] Preserve geometric normals for dynamic block models Co-Authored-By: Kimi Code --- crates/horizon-client/src/game/minecarts.rs | 37 +++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/crates/horizon-client/src/game/minecarts.rs b/crates/horizon-client/src/game/minecarts.rs index 6b01ae76a..8a9e9d3a8 100644 --- a/crates/horizon-client/src/game/minecarts.rs +++ b/crates/horizon-client/src/game/minecarts.rs @@ -1251,8 +1251,9 @@ impl Game<'_> { 0xFF00_0000 | shade << 16 | shade << 8 | shade }; let normal = if diffuse { - use horizon_client::item_render::{direction_vector, transform_normal}; - let n = transform_normal(m, direction_vector(face.quad.facing)); + use horizon_client::item_render::transform_normal; + let n = block_model_normal(&face.quad.positions, face.quad.facing); + let n = transform_normal(m, n); let n = n.to_array().map(|v| (v.clamp(-1.0, 1.0) * 127.0) as i8); [n[0], n[1], n[2], 127] } else { @@ -1552,10 +1553,42 @@ impl Minecart { } } +fn block_model_normal( + positions: &[[f32; 3]; 4], + facing: horizon_assets::model::Direction, +) -> glam::Vec3 { + let p = positions.map(glam::Vec3::from_array); + (p[1] - p[0]) + .cross(p[2] - p[0]) + .try_normalize() + .unwrap_or_else(|| horizon_client::item_render::direction_vector(facing)) +} + #[cfg(test)] mod tests { use super::*; + #[test] + fn block_model_normals_preserve_rotation_and_handle_degenerates() { + use horizon_assets::model::Direction; + for slope in [-2.0, -1.0, -0.25, 0.0, 0.25, 1.0, 2.0] { + let positions = [ + [0.0, 0.0, 0.0], + [1.0, 0.0, 0.0], + [1.0, 1.0, slope], + [0.0, 1.0, slope], + ]; + let normal = block_model_normal(&positions, Direction::Up); + let expected = glam::Vec3::new(0.0, -slope, 1.0).normalize(); + assert!((normal - expected).length() < 1.0e-6, "slope={slope}"); + } + let degenerate = [[0.0; 3]; 4]; + assert_eq!( + block_model_normal(°enerate, Direction::North), + glam::Vec3::NEG_Z + ); + } + #[test] fn display_offset_matches_vanilla_for_each_cart_kind() { assert_eq!(display_offset(CartKind::Hopper), 1.0);