Render a hand

This commit is contained in:
2026-09-27 00:18:16 -03:00
parent a2cf590315
commit 535bd36d91
5 changed files with 140 additions and 61 deletions
+5
View File
@@ -235,6 +235,11 @@ export class Client {
return;
}
// the held item swings on every click, and keeps swinging while breaking, like minecraft
if (InputManager.is_mouse_pressed(0) || InputManager.is_mouse_pressed(2) || (attacking && hit)) {
this.game_renderer.hand.swing();
}
if (hit) {
if (InputManager.is_mouse_pressed(0)) {
this.game_mode.start_destroy_block(hit);
+15
View File
@@ -333,6 +333,21 @@ export function begin_mode_3d(new_camera: Camera) {
write_mvp();
}
// 3d drawing in front of the camera instead of in the world, for the item in the player's hand: x right, y up, looking
// down -z. depth is cleared first, so it's never hidden inside a wall, like minecraft's first person hand
export function begin_view_model(fov: number) {
flush_batch();
if (pass) {
pass.end();
pass = undefined;
}
pending_depth_clear = true;
mode3d = true;
mat4.perspectiveZO(mvp, fov, canvas.width / canvas.height, 0.01, 10);
write_mvp();
}
export function end_mode_3d() {
if (!mode3d) {
return;
+3
View File
@@ -4,6 +4,7 @@ import { daylight } from "$/common/time.ts";
import { Hud } from "$/client/gui/hud.ts";
import { DebugOverlay } from "$/client/gui/debug_overlay.ts";
import { LevelRenderer } from "./level_renderer.ts";
import { HandRenderer } from "./hand_renderer.ts";
// the sky at noon and in the middle of the night
const DAY_SKY = [0.69, 0.8, 1];
@@ -16,6 +17,7 @@ export class GameRenderer {
level_renderer = new LevelRenderer();
hud = new Hud();
debug_overlay = new DebugOverlay();
hand = new HandRenderer();
// what the lightmap was last made for, so it's only rebuilt when the light changes
#lightmap_daylight = -1;
@@ -43,6 +45,7 @@ export class GameRenderer {
this.level_renderer.render_entities(client.level, client.player, partial_tick);
this.level_renderer.render_translucent(client.level, camera);
end_mode_3d();
this.hand.render(client.player.inventories);
this.hud.render(client);
client.screen?.on_render();
+77
View File
@@ -0,0 +1,77 @@
// 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<Texture>("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]];
};
}
+40 -61
View File
@@ -3,6 +3,7 @@ import { BlockRegistry, EverythingRegistry, ItemRegistry } from "$/common/everyt
import { AssetManager } from "$/client/assets.ts";
import { get_sprite_region } from "$/client/sprites.ts";
import type { ItemEntity } from "$/client/entity/item_entity.ts";
import type { ItemStack } from "$/common/inventory.ts";
import { push_vertex, Texture } from "$/client/renderer/mod.ts";
import { block_item_texture, cube_face_texture } from "$/common/block_models.ts";
import { item_model } from "./item_model.ts";
@@ -38,85 +39,63 @@ function copies(amount: number) {
return 1;
}
// a point of an item's model to where it's drawn. models are 1 across, from y 0 to 1, centered on x and z
export type ItemTransform = (x: number, y: number, z: number) => [number, number, number];
// the atlas has to be the current texture
export function render_item_entity(entity: ItemEntity, partial_tick: number) {
const atlas = AssetManager.instance.get<Texture>("bworld:textures");
const item_info = EverythingRegistry.get<ItemRegistry>("items", entity.item.type_id);
// blocks that aren't cubes, like plants, are drawn as their item's sprite
let block_info = item_info?.block_id
? EverythingRegistry.get<BlockRegistry>("blocks", item_info.block_id)
: undefined;
if (block_info && block_item_texture(block_info) !== undefined) block_info = undefined;
// spinning and bobbing, like minecraft's ItemEntityRenderer
const time = entity.age + partial_tick;
const angle = time / 20 + entity.bob_offset;
const bob = Math.sin(time / 10 + entity.bob_offset) * 0.1 + 0.1;
const { x, y, z } = entity.render_position(partial_tick);
const sin = Math.sin(angle);
const cos = Math.cos(angle);
const scale = block_info ? BLOCK_SCALE : ITEM_SCALE;
const scale = item_block(entity.item) ? BLOCK_SCALE : ITEM_SCALE;
for (let i = 0; i < copies(entity.item.amount); i++) {
const [ox, oy, oz] = COPY_OFFSETS[i];
const base = { x: x + ox * scale, y: y + bob + oy * scale * 0.5, z: z + oz * scale };
if (block_info) {
push_block(atlas, block_info, base, scale, angle);
} else {
push_sprite(atlas, item_texture(entity, item_info), base, scale, angle);
const bx = x + ox * scale;
const by = y + bob + oy * scale * 0.5;
const bz = z + oz * scale;
push_item(atlas, entity.item, (lx, ly, lz) => [
bx + (lx * cos - lz * sin) * scale,
by + ly * scale,
bz + (lx * sin + lz * cos) * scale,
]);
}
}
// the full block an item places, drawn as a cube. blocks that aren't cubes, like plants, are drawn as their sprite
export function item_block(item: ItemStack): BlockRegistry | undefined {
const item_info = EverythingRegistry.get<ItemRegistry>("items", item.type_id);
const block = item_info?.block_id ? EverythingRegistry.get<BlockRegistry>("blocks", item_info.block_id) : undefined;
return block && block_item_texture(block) === undefined ? block : undefined;
}
// an item's model: a cube for blocks, its sprite made 3d for everything else (see item_model.ts)
export function push_item(atlas: Texture, item: ItemStack, transform: ItemTransform) {
const block = item_block(item);
if (block) {
for (const face of CUBE_FACES) {
const region = get_sprite_region(cube_face_texture(block, face.texture));
const corners = face.corners.map(([cx, cy, cz]) => transform(cx - 0.5, cy, cz - 0.5));
push_textured_quad(atlas, region, corners, face.shade);
}
return;
}
}
// a small cube turned around its middle
function push_block(
atlas: Texture,
block: BlockRegistry,
base: { x: number; y: number; z: number },
size: number,
angle: number,
) {
const sin = Math.sin(angle);
const cos = Math.cos(angle);
const corner = (cx: number, cy: number, cz: number) => {
const lx = (cx - 0.5) * size;
const lz = (cz - 0.5) * size;
return [base.x + lx * cos - lz * sin, base.y + cy * size, base.z + lx * sin + lz * cos];
};
for (const face of CUBE_FACES) {
const region = get_sprite_region(cube_face_texture(block, face.texture));
push_textured_quad(atlas, region, face.corners.map(([cx, cy, cz]) => corner(cx, cy, cz)), face.shade);
}
}
// a flat item as its 3d model (see item_model.ts), standing up and turning around its middle
function push_sprite(
atlas: Texture,
texture_id: string,
base: { x: number; y: number; z: number },
size: number,
angle: number,
) {
const texture_id = item_texture(item);
const region = get_sprite_region(texture_id);
const sin = Math.sin(angle);
const cos = Math.cos(angle);
const u = (pixels: number) => (region.x * TEXTURE_SIZE + pixels) / atlas.width;
const v = (pixels: number) => (region.y * TEXTURE_SIZE + pixels) / atlas.height;
for (const quad of item_model(texture_id)) {
for (const i of [0, 1, 2, 0, 2, 3]) {
const [lx, ly, lz] = quad.corners[i];
const [px, py, pz] = transform(...quad.corners[i]);
const [pu, pv] = quad.uvs[i];
push_vertex(
base.x + (lx * cos - lz * sin) * size,
base.y + ly * size,
base.z + (lx * sin + lz * cos) * size,
u(pu),
v(pv),
quad.shade,
quad.shade,
quad.shade,
1,
);
push_vertex(px, py, pz, u(pu), v(pv), quad.shade, quad.shade, quad.shade, 1);
}
}
}
@@ -134,10 +113,10 @@ function push_textured_quad(atlas: Texture, region: { x: number; y: number }, co
}
}
function item_texture(entity: ItemEntity, item_info: ItemRegistry | undefined) {
const texture_id = item_info?.texture_id;
function item_texture(item: ItemStack) {
const texture_id = EverythingRegistry.get<ItemRegistry>("items", item.type_id)?.texture_id;
if (typeof texture_id === "function") {
return texture_id(entity.item);
return texture_id(item);
}
return texture_id ?? "engine:missing";
}