131 lines
5.7 KiB
TypeScript
131 lines
5.7 KiB
TypeScript
// 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 } from "$/client/renderer/mod.ts";
|
|
import { item_block, 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<Texture>("bworld:textures");
|
|
set_current_texture(atlas.tex);
|
|
push_item(atlas, item, pose(item, (1 - equip) * 0.6, Math.sin(swing_progress * Math.PI)), tint);
|
|
} 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, 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) {
|
|
flush_batch();
|
|
set_current_texture(AssetManager.instance.get<Texture>("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]];
|
|
};
|
|
}
|
|
|
|
// 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]];
|
|
};
|
|
}
|