Preserve geometric normals for dynamic block models

Co-Authored-By: Kimi Code <noreply@moonshot.ai>
This commit is contained in:
2026-11-04 17:51:24 +03:00
parent 57f8b2853c
commit 45b43f00df
+35 -2
View File
@@ -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(&degenerate, Direction::North),
glam::Vec3::NEG_Z
);
}
#[test]
fn display_offset_matches_vanilla_for_each_cart_kind() {
assert_eq!(display_offset(CartKind::Hopper), 1.0);