Files
bworld/client/rendering/player_renderer.ts
T
2026-10-01 17:57:22 -03:00

126 lines
4.4 KiB
TypeScript

import type { Player } from "$/client/entity/player.ts";
import { push_vertex, type Texture } from "$/client/renderer/mod.ts";
import type { Vec3 } from "./model_math.ts";
import {
type ModelPart,
part_point,
PARTS,
type PlayerPose,
pose_at,
pose_to_world,
SLIM_PARTS,
} from "./player_model.ts";
export type { Vec3 };
// how much of each color is left while something flashes red after being hurt, like minecraft's
export const HURT_TINT: Vec3 = [1, 0.6, 0.6];
// keeps nearest sampling from picking the texel next to a face's edge
const UV_INSET = 0.01;
// 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
const FACES: { rect: (w: number, h: number, d: number) => number[]; corners: number[][]; shade: number }[] = [
// top
{
rect: (w, _h, d) => [d, 0, w, d],
corners: [[1, 1, 0, 0, 1], [0, 1, 0, 1, 1], [0, 1, 1, 1, 0], [1, 1, 1, 0, 0]],
shade: 1.0,
},
// bottom, the same way around as the top so it's mirrored from below like in minecraft
{
rect: (w, _h, d) => [d + w, 0, w, d],
corners: [[1, 0, 0, 0, 1], [1, 0, 1, 0, 0], [0, 0, 1, 1, 0], [0, 0, 0, 1, 1]],
shade: 0.5,
},
// right
{
rect: (_w, h, d) => [0, d, d, h],
corners: [[1, 0, 1, 0, 1], [1, 0, 0, 1, 1], [1, 1, 0, 1, 0], [1, 1, 1, 0, 0]],
shade: 0.6,
},
// front
{
rect: (w, h, d) => [d, d, w, h],
corners: [[1, 0, 0, 0, 1], [0, 0, 0, 1, 1], [0, 1, 0, 1, 0], [1, 1, 0, 0, 0]],
shade: 0.8,
},
// left
{
rect: (w, h, d) => [d + w, d, d, h],
corners: [[0, 0, 0, 0, 1], [0, 0, 1, 1, 1], [0, 1, 1, 1, 0], [0, 1, 0, 0, 0]],
shade: 0.6,
},
// back
{
rect: (w, h, d) => [2 * d + w, d, w, h],
corners: [[0, 0, 1, 0, 1], [1, 0, 1, 1, 1], [1, 1, 1, 1, 0], [0, 1, 1, 0, 0]],
shade: 0.8,
},
];
// how a player is posed this frame, for drawing them and what they wear
export function player_pose(player: Player, partial_tick: number): PlayerPose {
const { x, y, z } = player.render_position(partial_tick);
const { yaw, pitch, slim } = player;
return pose_at({ x, y, z, yaw, pitch, slim, walk: player.walk_animation(partial_tick) });
}
// a player with a minecraft skin, like minecraft's PlayerRenderer. the skin has to be the current texture. tint
// multiplies its colors, for flashing red when hurt
export function render_player(skin: Texture, pose: PlayerPose, tint?: Vec3) {
const to_world = pose_to_world(pose);
for (const part of pose.slim ? SLIM_PARTS : PARTS) {
// from points around the part's pivot to where they are in the world
const transform = (x: number, y: number, z: number) => to_world(...part_point(pose, part, x, y, z));
push_cube(skin, part, part.texture, 0, transform, tint);
push_cube(skin, part, part.overlay, part.inflate, transform, tint);
}
}
// 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],
inflate: number,
transform: (x: number, y: number, z: number) => Vec3,
tint?: Vec3,
) {
const low = part.from.map((n) => n - inflate) as Vec3;
const high = part.from.map((n, i) => n + part.size[i] + inflate) as Vec3;
push_box(part.size, low, high, uv, (u, v) => [u / skin.width, v / skin.height], transform, tint);
}
// a box of a model, from low to high, textured like minecraft's box uv from [u, v] for a box of size (in texture
// pixels). to_uv turns texture pixels into the current texture's coordinates, tint multiplies its colors
export function push_box(
size: Vec3,
low: Vec3,
high: Vec3,
[u, v]: [number, number],
to_uv: (u: number, v: number) => [number, number],
transform: (x: number, y: number, z: number) => Vec3,
tint: Vec3 = [1, 1, 1],
) {
const [w, h, d] = size;
for (const face of FACES) {
const [rx, ry, rw, rh] = face.rect(w, h, d);
const [u0, v0] = to_uv(u + rx + UV_INSET, v + ry + UV_INSET);
const [u1, v1] = to_uv(u + rx + rw - UV_INSET, v + ry + rh - UV_INSET);
const [r, g, b] = tint.map((c) => c * face.shade);
for (const i of [0, 1, 2, 0, 2, 3]) {
const [cx, cy, cz, cu, cv] = face.corners[i];
const [px, py, pz] = transform(
cx ? high[0] : low[0],
cy ? high[1] : low[1],
cz ? high[2] : low[2],
);
push_vertex(px, py, pz, cu ? u1 : u0, cv ? v1 : v0, r, g, b, 1);
}
}
}