Draw worn armor on the player and shift-click armor on

HumanoidArmorLayer for the player's model (in third person and the inventory), the armour parts posed like the body. Shift-clicking armour or an off-hand item in the inventory puts it in its empty slot (InventoryMenu.quickMoveStack), and shift-clicking an armour slot takes it back. The GUI clip handles rectangles with corners in any order.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Ludovic Marie
2026-10-09 08:44:16 +02:00
parent 9c16c364d6
commit 3c9d804ebb
5 changed files with 137 additions and 29 deletions
+33
View File
@@ -3475,7 +3475,40 @@ impl Game<'_> {
}
return;
}
if shift && (500..504).contains(&slot) {
// InventoryMenu.quickMoveStack: armour back to the main
// inventory, then the hotbar.
if let Some(stack) = self.armor[slot - 500].take() {
if let Some(i) = (9..36).chain(0..9).find(|&i| self.inventory[i].is_none()) {
self.inventory[i] = Some(stack);
} else {
self.armor[slot - 500] = Some(stack);
}
self.apply_equipment();
}
return;
}
if shift && slot < 36 {
// InventoryMenu.quickMoveStack: armour into its empty slot, an
// off-hand item into an empty off hand.
let equip = self.inventory[slot]
.as_ref()
.and_then(|s| self.res.items.get(&s.item))
.and_then(|i| i.equip_slot.clone());
let armor = ["head", "chest", "legs", "feet"]
.iter()
.position(|s| equip.as_deref() == Some(*s));
if let Some(a) = armor
&& self.armor[a].is_none()
{
self.armor[a] = self.inventory[slot].take();
self.apply_equipment();
return;
}
if equip.as_deref() == Some("offhand") && self.offhand.is_none() {
self.offhand = self.inventory[slot].take();
return;
}
// Quick move between hotbar and main inventory.
if let Some(stack) = self.inventory[slot].take() {
let range = if slot < 9 { 9..36 } else { 0..9 };
@@ -21,6 +21,8 @@ pub(super) struct ArmorStandAssets {
pub small: Option<Part>,
/// (slot model suffix, small) -> model.
pub layers: HashMap<(&'static str, bool), Part>,
/// The player's armour models by slot (helmet, chestplate, leggings, boots).
pub player: HashMap<&'static str, Part>,
/// asset id -> layer kind (humanoid / humanoid_leggings) -> (texture, undyed colour).
pub equipment: HashMap<String, HashMap<String, (String, Option<u32>)>>,
}
@@ -28,7 +30,11 @@ impl ArmorStandAssets {
pub fn load(root: &std::path::Path) -> Self {
let load = |key: &str| horizon_client::mobs::load_model(root, key);
let mut layers = HashMap::new();
let mut player = HashMap::new();
for slot in ["helmet", "chestplate", "leggings", "boots"] {
if let Some(part) = load(&format!("minecraft:player#{slot}")) {
player.insert(slot, part);
}
for (small, model) in [(false, "armor_stand"), (true, "armor_stand_small")] {
if let Some(part) = load(&format!("minecraft:{model}#{slot}")) {
layers.insert((slot, small), part);
@@ -71,11 +77,54 @@ impl ArmorStandAssets {
main: load("minecraft:armor_stand#main"),
small: load("minecraft:armor_stand_small#main"),
layers,
player,
equipment,
}
}
}
impl Game<'_> {
/// `HumanoidArmorLayer` for the player: (model, texture rect, colour) of
/// each worn piece, head to feet.
pub(super) fn player_armor_layers(&self) -> Vec<(&Part, [f32; 4], u32)> {
let assets = &self.armor_stand_assets;
let mut out = Vec::new();
for (i, layer) in ["helmet", "chestplate", "leggings", "boots"]
.into_iter()
.enumerate()
{
let Some(stack) = &self.armor[i] else {
continue;
};
let Some(asset) = self
.res
.items
.get(&stack.item)
.and_then(|i| i.equip_asset.clone())
else {
continue;
};
let kind = if layer == "leggings" {
"humanoid_leggings"
} else {
"humanoid"
};
let Some((texture, undyed)) = assets.equipment.get(&asset).and_then(|k| k.get(kind))
else {
continue;
};
let (Some(r), Some(part)) = (
self.res.atlas.sprites.get(texture.as_str()),
assets.player.get(layer),
) else {
continue;
};
let color = undyed.map_or(0xFFFF_FFFF, |c| {
0xFF00_0000 | (c & 0xFF) << 16 | (c & 0xFF00) | (c >> 16) & 0xFF
});
out.push((part, [r.u0, r.v0, r.u1, r.v1], color));
}
out
}
/// `ArmorStandItem.useOn`.
pub(super) fn place_armor_stand(&mut self, hit: &super::Hit, stack: &ItemStack) -> bool {
if &*stack.item != "minecraft:armor_stand" || hit.face == [0, -1, 0] {
@@ -232,13 +232,14 @@ impl Game<'_> {
let packed = (u32::from(light >> 4) * 16) | ((u32::from(light & 15) * 16) << 8);
let atlas = &self.res.atlas;
let lookup = |name: &str| atlas.sprites.get(name).map(|r| [r.u0, r.v0, r.u1, r.v1]);
horizon_client::mobs::render::render(
horizon_client::mobs::render::render_with(
m,
alpha as f32,
camera,
&lookup,
packed,
&self.res.mob_data,
&self.player_armor_layers(),
out,
);
}
@@ -284,13 +285,14 @@ impl Game<'_> {
let atlas = &self.res.atlas;
let lookup = |name: &str| atlas.sprites.get(name).map(|r| [r.u0, r.v0, r.u1, r.v1]);
let mut verts = Vec::new();
horizon_client::mobs::render::render(
horizon_client::mobs::render::render_with(
&m,
1.0,
camera,
&lookup,
240 | 240 << 8,
&self.res.mob_data,
&self.player_armor_layers(),
&mut verts,
);
let height = if m.extra.crouching { 1.5 } else { 1.8 };
+33 -27
View File
@@ -282,43 +282,49 @@ impl HudBatch {
let quads: Vec<[HudVertex; 6]> = self.vertices[start..].as_chunks::<6>().0.to_vec();
self.vertices.truncate(start);
for q in quads {
let (a, b, c, d) = (q[0], q[1], q[2], q[5]);
let aligned = a.pos[1] == b.pos[1]
&& a.pos[0] == d.pos[0]
&& c.pos[0] == b.pos[0]
&& c.pos[1] == d.pos[1];
if !aligned {
let cx = (a.pos[0] + c.pos[0]) / 2.0;
let cy = (a.pos[1] + c.pos[1]) / 2.0;
let corners = [q[0], q[1], q[2], q[5]];
let (mut x_lo, mut x_hi) = (f32::INFINITY, f32::NEG_INFINITY);
let (mut y_lo, mut y_hi) = (f32::INFINITY, f32::NEG_INFINITY);
for v in &corners {
x_lo = x_lo.min(v.pos[0]);
x_hi = x_hi.max(v.pos[0]);
y_lo = y_lo.min(v.pos[1]);
y_hi = y_hi.max(v.pos[1]);
}
// An axis-aligned rectangle, its corners in any order.
let find = |x: f32, y: f32| corners.iter().find(|v| v.pos == [x, y]).copied();
let rect = (
find(x_lo, y_lo),
find(x_hi, y_lo),
find(x_hi, y_hi),
find(x_lo, y_hi),
);
let (Some(tl), Some(tr), Some(br), Some(bl)) = rect else {
let cx = corners.iter().map(|v| v.pos[0]).sum::<f32>() / 4.0;
let cy = corners.iter().map(|v| v.pos[1]).sum::<f32>() / 4.0;
if cx >= x0 && cx < x1 && cy >= y0 && cy < y1 {
self.vertices.extend_from_slice(&q);
}
continue;
}
let (px0, py0, px1, py1) = (a.pos[0], a.pos[1], c.pos[0], c.pos[1]);
let (cx0, cy0) = (px0.max(x0), py0.max(y0));
let (cx1, cy1) = (px1.min(x1), py1.min(y1));
};
let (cx0, cy0) = (x_lo.max(x0), y_lo.max(y0));
let (cx1, cy1) = (x_hi.min(x1), y_hi.min(y1));
if cx0 >= cx1 || cy0 >= cy1 {
continue;
}
let lerp = |t0: f32, t1: f32, p: f32, lo: f32, hi: f32| {
if hi == lo {
t0
} else {
t0 + (t1 - t0) * (p - lo) / (hi - lo)
}
let t = |p: f32, lo: f32, hi: f32| if hi == lo { 0.0 } else { (p - lo) / (hi - lo) };
let uv = |x: f32, y: f32| {
let (s, r) = (t(x, x_lo, x_hi), t(y, y_lo, y_hi));
std::array::from_fn(|i| {
let top = tl.uv[i] + (tr.uv[i] - tl.uv[i]) * s;
let bottom = bl.uv[i] + (br.uv[i] - bl.uv[i]) * s;
top + (bottom - top) * r
})
};
let u = |x: f32| lerp(a.uv[0], c.uv[0], x, px0, px1);
let v = |y: f32| lerp(a.uv[1], c.uv[1], y, py0, py1);
self.quad(
[[cx0, cy0], [cx1, cy0], [cx1, cy1], [cx0, cy1]],
[
[u(cx0), v(cy0)],
[u(cx1), v(cy0)],
[u(cx1), v(cy1)],
[u(cx0), v(cy1)],
],
a.color,
[uv(cx0, cy0), uv(cx1, cy0), uv(cx1, cy1), uv(cx0, cy1)],
tl.color,
);
}
}
+18
View File
@@ -647,6 +647,21 @@ pub fn render(
light: u32,
data: &MobData,
out: &mut Vec<Vertex>,
) {
render_with(m, alpha, camera, atlas, light, data, &[], out);
}
/// `render` plus extra layers posed like the body (`HumanoidArmorLayer`:
/// the armour model's parts copy the body's): (model, texture rect, colour).
#[allow(clippy::too_many_arguments)]
pub fn render_with(
m: &Mob,
alpha: f32,
camera: glam::DVec3,
atlas: Atlas,
light: u32,
data: &MobData,
extra: &[(&Part, [f32; 4], u32)],
out: &mut Vec<Vertex>,
) {
let base = base_transform(m, alpha, camera);
let overlay = u32::from(m.hurt_time > 0 || m.death_time > 0);
@@ -671,6 +686,9 @@ pub fn render(
out,
);
}
for (part, uv, color) in extra {
draw(part, base, &poses, *uv, *color, light, overlay, out);
}
held_items(m, &model, base, &poses, atlas, light, out);
for layer in layers(m) {
let Some(part) = m.kind.layer(&layer.model).cloned() else {