// the item the player is holding, drawn in the lower right in front of the camera like minecraft's // ItemInHandRenderer. empty hands show nothing import { mat4, vec3 } from "gl-matrix"; import type { ItemStack } from "$/common/inventory.ts"; import { AssetManager } from "$/client/assets.ts"; import type { ClientInventories } from "$/client/inventory.ts"; import { begin_view_model, end_mode_3d, flush_batch, set_current_texture, Texture } from "$/client/renderer/mod.ts"; import { item_block, push_item } from "./item_renderer.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(inventories: ClientInventories) { 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; } if (!item) { return; } const equip = Math.min(1, (now - this.#equipped_at) / EQUIP_MS); const swing_progress = Math.min(1, (now - this.#swung_at) / SWING_MS); const swing = Math.sin(swing_progress * Math.PI); const atlas = AssetManager.instance.get("bworld:textures"); begin_view_model(FOV); set_current_texture(atlas.tex); push_item(atlas, item, pose(item, (1 - equip) * 0.6, swing)); flush_batch(); end_mode_3d(); } } // from the item's model to in front of the camera. lower drops it out of sight, swing tips it forward and in function pose(item: ItemStack, lower: number, swing: number) { const p = item_block(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]]; }; }