From 8ef418e223eed07d4ca2215e6f0d3016890bb76c Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Fri, 23 Oct 2026 18:34:27 +0200 Subject: [PATCH] Maps in one hand: the off hand, or the main hand beside an item, draws the map FirstPersonHandsAndItemsRenderer.submitArmWithItem draws a stack with a map_id two-handed only from the main hand with an empty off hand; any other map is renderOneHandedMap in its own hand. Horizon only had the two-handed case and drew every other held map as its item model (#1459). MapHold picks the case from the hand renderer's visible items, and one_handed_map_pose is renderOneHandedMap's transform, checked against vanilla's PoseStack. renderMap (background, map, decorations) is shared by both cases. The arms under held maps are still not drawn, as before. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/first_person.rs | 120 ++++++++++++++++++++++ crates/horizon-client/src/game.rs | 31 ++++-- crates/horizon-client/src/game/maps.rs | 25 ++--- 3 files changed, 156 insertions(+), 20 deletions(-) diff --git a/crates/horizon-client/src/first_person.rs b/crates/horizon-client/src/first_person.rs index 02132d942..0735ce2ba 100644 --- a/crates/horizon-client/src/first_person.rs +++ b/crates/horizon-client/src/first_person.rs @@ -137,6 +137,48 @@ pub fn item_pose(left: bool, inverse_height: f32, attack: f32, pose: ItemPose) - m } +/// How `submitArmWithItem` holds a stack with a `minecraft:map_id`: in both +/// hands when it is the main hand's and the off hand is empty +/// (`renderTwoHandedMap`), else in its own hand (`renderOneHandedMap`). +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum MapHold { + TwoHanded, + OneHanded, +} +impl MapHold { + pub fn of(hand: Hand, off_hand_empty: bool) -> Self { + if hand == Hand::Main && off_hand_empty { + Self::TwoHanded + } else { + Self::OneHanded + } + } +} + +/// `renderOneHandedMap` up to `renderMap`: the pose of a map in the hand on +/// the `left` or right arm (the arm under it is not drawn). +pub fn one_handed_map_pose(left: bool, inverse_height: f32, attack: f32) -> glam::Mat4 { + use glam::{Mat4, Vec3}; + use horizon_core::geometry::trig::Mth; + let invert = if left { -1.0 } else { 1.0 }; + let pi = std::f32::consts::PI; + let root = attack.sqrt(); + let x_swing = (root * pi).msin(); + Mat4::from_translation(Vec3::new(invert * 0.125, -0.125, 0.0)) + * Mat4::from_translation(Vec3::new( + invert * 0.51, + -0.08 + inverse_height * -1.2, + -0.75, + )) + * Mat4::from_translation(Vec3::new( + invert * -0.5 * x_swing, + 0.4 * (root * std::f32::consts::TAU).msin() - 0.3 * x_swing, + -0.3 * (attack * pi).msin(), + )) + * Mat4::from_rotation_x((x_swing * -45.0).to_radians()) + * Mat4::from_rotation_y((invert * x_swing * -30.0).to_radians()) +} + /// `FirstPersonHandsAndItems.evaluateWhichHandsToRender`: whether the main /// and the off hand show, from what each holds (item and whether it is a /// charged crossbow) and the item in use with its hand. A bow or crossbow @@ -819,6 +861,84 @@ mod tests { mod item_pose_tests { use super::*; #[test] + fn a_map_is_two_handed_only_in_the_main_hand_with_an_empty_off_hand() { + assert_eq!(MapHold::of(Hand::Main, true), MapHold::TwoHanded); + assert_eq!(MapHold::of(Hand::Main, false), MapHold::OneHanded); + assert_eq!(MapHold::of(Hand::Off, true), MapHold::OneHanded); + assert_eq!(MapHold::of(Hand::Off, false), MapHold::OneHanded); + } + #[test] + fn one_handed_maps_follow_render_one_handed_map() { + // PoseStack.last().pose() from vanilla's renderOneHandedMap + // transforms (column-major), for (left, inverse height, attack). + let cases: [(bool, f32, f32, [f32; 16]); 3] = [ + ( + false, + 0.0, + 0.0, + [ + 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, -0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.635, -0.205, + -0.75, 1.0, + ], + ), + ( + true, + 0.25, + 0.3, + [ + 0.86894435, + -0.34686038, + -0.35302067, + 0.0, + 0.0, + 0.713303, + -0.70085573, + 0.0, + 0.4949098, + 0.6090046, + 0.61982065, + 0.0, + -0.1406033, + -0.9197687, + -0.9926984, + 1.0, + ], + ), + ( + false, + 0.6, + 0.85, + [ + 0.9919303, + 0.024030307, + 0.12448644, + 0.0, + 0.0, + 0.9818737, + -0.18953651, + 0.0, + -0.12678458, + 0.18800701, + 0.97395027, + 0.0, + 0.5136029, + -1.1862278, + -0.88621765, + 1.0, + ], + ), + ]; + for (left, inverse_height, attack, want) in cases { + let got = one_handed_map_pose(left, inverse_height, attack).to_cols_array(); + for (g, w) in got.iter().zip(want) { + assert!( + (g - w).abs() < 1e-5, + "{left} {inverse_height} {attack}: {got:?}" + ); + } + } + } + #[test] fn ranged_draw_uses_native_charge_curves_and_caps() { for (ticks, stretch) in [ (0.0, 1.0), diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 03854e9f8..14fdc8192 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -8798,20 +8798,15 @@ impl Game<'_> { ); let packed = (u32::from(light >> 4) * 16) | ((u32::from(light & 15) * 16) << 8); let deg = |d: f32| d.to_radians(); - if self.held_map_geometry(to_world, attacks[0], inverse_heights[0], packed, &mut out) { - return out; - } let shown = self.hands_to_render(); let used_hand = self.used_hand(); let spinning = self .player .as_ref() .is_some_and(|p| p.auto_spin_attack_ticks > 0); - for (i, (hand, item)) in Hand::BOTH - .into_iter() - .zip(self.visible_hand_items()) - .enumerate() - { + let items = self.visible_hand_items(); + let off_hand_empty = items[1].is_none(); + for (i, (hand, item)) in Hand::BOTH.into_iter().zip(items).enumerate() { if !shown[i] { continue; } @@ -8874,6 +8869,26 @@ impl Game<'_> { ); continue; }; + if let Some(id) = maps::map_id(&stack) { + use horizon_client::first_person::{MapHold, one_handed_map_pose}; + match MapHold::of(hand, off_hand_empty) { + MapHold::TwoHanded => self.two_handed_map_geometry( + to_world, + attack, + inverse_height, + id, + packed, + &mut out, + ), + MapHold::OneHanded => self.held_map( + to_world * one_handed_map_pose(left, inverse_height, attack), + id, + packed, + &mut out, + ), + } + continue; + } let mut owner = self.player_owner(Some(&stack)); // The other hand may hold an equal stack: only the used // hand's item is in use, only the main hand's selected. diff --git a/crates/horizon-client/src/game/maps.rs b/crates/horizon-client/src/game/maps.rs index bc2534659..5809cf3b6 100644 --- a/crates/horizon-client/src/game/maps.rs +++ b/crates/horizon-client/src/game/maps.rs @@ -918,21 +918,17 @@ impl Game<'_> { ); } } - /// `renderTwoHandedMap`: the held map (main hand, empty off hand) in view space. - pub(super) fn held_map_geometry( + /// `renderTwoHandedMap`: map `id` held in both hands, in view space (the + /// arms under it are not drawn). + pub(super) fn two_handed_map_geometry( &mut self, to_world: glam::Mat4, attack: f32, inverse_height: f32, + id: i32, light: u32, out: &mut Vec, - ) -> bool { - let Some(id) = self.inventory[self.selected].as_ref().and_then(map_id) else { - return false; - }; - if self.offhand.is_some() { - return false; - } + ) { let deg = |d: f32| d.to_radians(); let sqrt_attack = attack.sqrt(); let y_swing = -0.2 * (attack * std::f32::consts::PI).sin(); @@ -949,10 +945,16 @@ impl Game<'_> { )) * glam::Mat4::from_rotation_x(deg(tilt * -85.0)); let swing = (sqrt_attack * std::f32::consts::PI).sin(); - // renderMap: background (with 7-pixel border) then the map. let m = base * glam::Mat4::from_rotation_x(deg(swing * 20.0)) - * glam::Mat4::from_scale(glam::Vec3::splat(2.0)) + * glam::Mat4::from_scale(glam::Vec3::splat(2.0)); + self.held_map(m, id, light, out); + } + /// `renderMap`: map `id` at pose `m`, its background (with a 7-pixel + /// border) then the map. + pub(super) fn held_map(&mut self, m: glam::Mat4, id: i32, light: u32, out: &mut Vec) { + let deg = |d: f32| d.to_radians(); + let m = m * glam::Mat4::from_rotation_y(deg(180.0)) * glam::Mat4::from_rotation_z(deg(180.0)) * glam::Mat4::from_scale(glam::Vec3::splat(0.38)) @@ -984,7 +986,6 @@ impl Game<'_> { if has_data { self.map_vertices(id, m, light, out); } - true } }