Player hand rendering

This commit is contained in:
2026-09-30 16:09:44 -03:00
parent 5e2b415f5c
commit 93382d3c97
3 changed files with 61 additions and 13 deletions
+52 -10
View File
@@ -1,11 +1,13 @@
// the item the player is holding, drawn in the lower right in front of the camera like minecraft's
// ItemInHandRenderer. empty hands show nothing
// 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 { ClientInventories } from "$/client/inventory.ts";
import type { LocalPlayer } from "$/client/entity/local_player.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";
import { push_cube, right_arm, type Vec3 } from "./player_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;
@@ -32,7 +34,8 @@ export class HandRenderer {
}
}
render(inventories: ClientInventories) {
render(player: LocalPlayer) {
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();
@@ -40,23 +43,62 @@ export class HandRenderer {
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<Texture>("bworld:textures");
begin_view_model(FOV);
set_current_texture(atlas.tex);
push_item(atlas, item, pose(item, (1 - equip) * 0.6, swing));
if (item) {
const atlas = AssetManager.instance.get<Texture>("bworld:textures");
set_current_texture(atlas.tex);
push_item(atlas, item, pose(item, (1 - equip) * 0.6, Math.sin(swing_progress * Math.PI)));
} else {
const skin = player.skin ?? AssetManager.instance.get<Texture>("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);
push_cube(skin, arm, arm.overlay, arm.inflate, transform);
}
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]];
};
}
// 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;