diff --git a/client/rendering/game_renderer.ts b/client/rendering/game_renderer.ts index 555bfc4..89e5860 100644 --- a/client/rendering/game_renderer.ts +++ b/client/rendering/game_renderer.ts @@ -45,7 +45,7 @@ export class GameRenderer { this.level_renderer.render_entities(client.level, camera, client.player, partial_tick); this.level_renderer.render_translucent(client.level, camera); end_mode_3d(); - this.hand.render(client.player.inventories); + this.hand.render(client.player); this.hud.render(client); client.screen?.on_render(); diff --git a/client/rendering/hand_renderer.ts b/client/rendering/hand_renderer.ts index 94701b2..dfa7801 100644 --- a/client/rendering/hand_renderer.ts +++ b/client/rendering/hand_renderer.ts @@ -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("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("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("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; diff --git a/client/rendering/player_renderer.ts b/client/rendering/player_renderer.ts index 2deba04..418f500 100644 --- a/client/rendering/player_renderer.ts +++ b/client/rendering/player_renderer.ts @@ -15,7 +15,7 @@ const UV_INSET = 0.01; // a part of minecraft's player model, like its ModelParts. sizes and positions are in skin pixels, the model faces // -z with its right side towards +x and its feet at 0. each part is a box from `from` to `from + size` around the // pivot it turns on, with its texture at `texture` in a 1.8 (64x64) skin and a second layer drawn a bit bigger -interface ModelPart { +export interface ModelPart { name: "head" | "body" | "right_arm" | "left_arm" | "right_leg" | "left_leg"; size: Vec3; pivot: Vec3; @@ -105,6 +105,11 @@ const SLIM_ARMS: ModelPart[] = [ ]; const SLIM_PARTS = PARTS.map((part) => SLIM_ARMS.find((arm) => arm.name === part.name) ?? part); +// the right arm of the wide or slim model, for drawing it on its own in first person +export function right_arm(slim: boolean): ModelPart { + return (slim ? SLIM_PARTS : PARTS).find((part) => part.name === "right_arm")!; +} + // a box's faces, like minecraft's ModelPart.Cube. `rect` is where the face is in the box's texture, which is laid // out as in a skin: the top and bottom above the sides (right, front, left, back). corners go counter clockwise from // outside as [x, y, z, u, v], 0 for the low end and 1 for the high one @@ -182,7 +187,8 @@ export function render_player(player: Player, skin: Texture, partial_tick: numbe } } -function push_cube( +// one layer of a part's box, its texture at uv and grown by inflate. transform takes points around the part's pivot +export function push_cube( skin: Texture, part: ModelPart, uv: [number, number],