// SPDX-FileCopyrightText: 2026 Paula Wisnievski // SPDX-License-Identifier: AGPL-3.0-or-later // the item the player is holding, drawn in the lower right in front of the camera like minecraft's // ItemInHandRenderer. with empty hands it's their right arm, in their skin, like minecraft's // FirstPersonHandsAndItemsRenderer.renderPlayerArm import { mat4, vec3 } from "gl-matrix"; import type { ItemStack } from "@/common/inventory.ts"; import { AssetManager } from "@/client/assets.ts"; import type { LocalPlayer } from "@/client/entity/local_player.ts"; import { begin_view_model, end_mode_3d, flush_batch, set_current_texture, Texture, use_texture, } from "@/client/renderer/mod.ts"; import { display_transform, drawn_as_model, item_model_of, push_item } from "./item_renderer.ts"; import { push_cube, type Vec3 } from "./player_renderer.ts"; import { right_arm } from "./player_model.ts"; import { render_worn_arm } from "./worn_renderer.ts"; import { entity_tint } from "./entity_light.ts"; // wider than the world's field of view, like minecraft's fixed 70 degrees for the hand const FOV = 70 * Math.PI / 180; // where the item sits in front of the camera (x right, y up, looking down -z) and how it's turned, in radians const ITEM_POSE = { x: 0.72, y: -0.46, z: -1.15, yaw: -1.25, roll: 0.42, pitch: 0, scale: 0.58 }; const BLOCK_POSE = { x: 0.72, y: -0.52, z: -1.15, yaw: Math.PI / 4, roll: 0, pitch: 0.45, scale: 0.3 }; // a new item rises into place, like minecraft's equip animation const EQUIP_MS = 180; // clicking swings it down and back const SWING_MS = 300; const SWING_ANGLE = 0.9; export class HandRenderer { // what was held last frame, as slot and item id, and when it changed #held = ""; #equipped_at = -Infinity; #swung_at = -Infinity; swing() { const now = performance.now(); // clicking again mid swing doesn't restart it, like minecraft if (now - this.#swung_at > SWING_MS / 2) { this.#swung_at = now; } } render(player: LocalPlayer, partial_tick: number) { const inventories = player.inventories; const item = inventories.inventory.get_item(inventories.hotbar_selected); const held = item ? `${inventories.hotbar_selected} ${item.type_id}` : ""; const now = performance.now(); if (held !== this.#held) { this.#held = held; this.#equipped_at = now; } const equip = Math.min(1, (now - this.#equipped_at) / EQUIP_MS); const swing_progress = Math.min(1, (now - this.#swung_at) / SWING_MS); // the light where the player's eyes are, like every other entity const tint = entity_tint(player, partial_tick); begin_view_model(FOV); if (item) { const atlas = AssetManager.instance.get("bworld:textures"); set_current_texture(atlas.tex); const display = item_model_of(item)?.display.firstperson_righthand; const transform = display ? display_transform(held_pose(1 - equip, swing_progress), display) : pose(item, (1 - equip) * 0.6, Math.sin(swing_progress * Math.PI)); push_item(atlas, item, transform, tint); } else { const skin = player.skin ?? AssetManager.instance.get("bworld:skin"); const arm = right_arm(player.slim); const transform = arm_pose(arm.pivot, 1 - equip, swing_progress); set_current_texture(skin.tex); push_cube(skin, arm, arm.texture, 0, transform, tint); push_cube(skin, arm, arm.overlay, arm.inflate, transform, tint); // what's worn on it goes over it, from the atlas if (player.worn.length > 0) { use_texture(AssetManager.instance.get("bworld:textures").tex); render_worn_arm(player, transform, tint); } } flush_batch(); end_mode_3d(); } } // minecraft's renderPlayerArm for the right arm, from the arm's own points (around its pivot, in skin pixels) to in // front of the camera. lower is how far it's dropped out of sight (its inverseArmHeight), attack how far through the // swing it is, both 0 to 1. the numbers are minecraft's, in its model's axes: y down and x mirrored from ours export function arm_pose(pivot: Vec3, lower: number, attack: number) { const degrees = Math.PI / 180; const sqrt_attack = Math.sqrt(attack); const matrix = mat4.create(); mat4.translate(matrix, matrix, [ -0.3 * Math.sin(sqrt_attack * Math.PI) + 0.64, 0.4 * Math.sin(sqrt_attack * Math.PI * 2) - 0.6 - lower * 0.6, -0.4 * Math.sin(attack * Math.PI) - 0.72, ]); mat4.rotateY(matrix, matrix, 45 * degrees); mat4.rotateY(matrix, matrix, Math.sin(sqrt_attack * Math.PI) * 70 * degrees); mat4.rotateZ(matrix, matrix, Math.sin(attack * attack * Math.PI) * -20 * degrees); mat4.translate(matrix, matrix, [-1, 3.6, 3.5]); mat4.rotateZ(matrix, matrix, 120 * degrees); mat4.rotateX(matrix, matrix, 200 * degrees); mat4.rotateY(matrix, matrix, -135 * degrees); mat4.translate(matrix, matrix, [5.6, 0, 0]); // the arm's ModelPart: to its pivot, tipped out a little like the player renderer's renderHand does, in pixels const [px, py, pz] = pivot; mat4.translate(matrix, matrix, [-px / 16, (24 - py) / 16, pz / 16]); mat4.rotateZ(matrix, matrix, 0.1); mat4.scale(matrix, matrix, [1 / 16, 1 / 16, 1 / 16]); const point = vec3.create(); return (x: number, y: number, z: number): Vec3 => { // from our model's axes to minecraft's vec3.transformMat4(point, [-x, -y, z], matrix); return [point[0], point[1], point[2]]; }; } // where a held model's firstperson_righthand transform starts from: low on the right in front of the camera, dropped // out of sight by lower and swung by attack like arm_pose. it's where the display format's transforms are made from, so // ones made for it elsewhere look the same here export function held_pose(lower: number, attack: number) { const degrees = Math.PI / 180; const sqrt_attack = Math.sqrt(attack); const matrix = mat4.create(); mat4.translate(matrix, matrix, [ -0.4 * Math.sin(sqrt_attack * Math.PI) + 0.56, 0.2 * Math.sin(sqrt_attack * Math.PI * 2) - 0.52 - lower * 0.6, -0.2 * Math.sin(attack * Math.PI) - 0.72, ]); const swing = Math.sin(sqrt_attack * Math.PI); mat4.rotateY(matrix, matrix, (45 - Math.sin(attack * attack * Math.PI) * 20) * degrees); mat4.rotateZ(matrix, matrix, swing * -20 * degrees); mat4.rotateX(matrix, matrix, swing * -80 * degrees); mat4.rotateY(matrix, matrix, -45 * degrees); return matrix; } // from the item's model to in front of the camera, for one without a display transform for it. lower drops it out of // sight, swing tips it forward and in function pose(item: ItemStack, lower: number, swing: number) { const p = drawn_as_model(item) ? BLOCK_POSE : ITEM_POSE; const matrix = mat4.create(); mat4.translate(matrix, matrix, [p.x - swing * 0.25, p.y - lower + swing * 0.1, p.z - swing * 0.2]); mat4.rotateX(matrix, matrix, p.pitch - swing * SWING_ANGLE); mat4.rotateY(matrix, matrix, p.yaw); mat4.rotateZ(matrix, matrix, p.roll); mat4.scale(matrix, matrix, [p.scale, p.scale, p.scale]); // turn around the model's middle, not its bottom mat4.translate(matrix, matrix, [0, -0.5, 0]); const point = vec3.create(); return (x: number, y: number, z: number): [number, number, number] => { vec3.transformMat4(point, [x, y, z], matrix); return [point[0], point[1], point[2]]; }; }