Files
bworld/client/rendering/player_renderer.ts
T
2026-11-21 19:24:09 -03:00

173 lines
6.2 KiB
TypeScript

// SPDX-FileCopyrightText: 2026 Paula Wisnievski
// SPDX-License-Identifier: AGPL-3.0-or-later
import type { Player } from "@/client/entity/player.ts";
import { push_vertex, type Texture } from "@/client/renderer/mod.ts";
import type { FaceUvs } from "@/common/mod_data.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. `name` is the face's in a bedrock model's per
// face uvs, which go where its box uv would put it except the bottom, which is upside down
const FACES: {
name: keyof FaceUvs;
rect: (w: number, h: number, d: number) => number[];
corners: number[][];
shade: number;
}[] = [
// top
{
name: "up",
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
{
name: "down",
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
{
name: "east",
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
{
name: "north",
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
{
name: "west",
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
{
name: "south",
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,
},
];
// a face's corners as two triangles
const QUAD_CORNERS = [0, 1, 2, 0, 2, 3];
// 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;
const walk = player.walk_animation(partial_tick);
const swim = player.render_swim(partial_tick);
return pose_at({ x, y, z, yaw, pitch, slim, walk, swim, time: player.level.time + 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);
}
}
// where a face is in the texture as [u, v, width, height] in pixels, undefined when per face uvs leave it out
function face_rect(face: typeof FACES[number], [w, h, d]: Vec3, uv: [number, number] | FaceUvs) {
if (Array.isArray(uv)) {
const [rx, ry, rw, rh] = face.rect(w, h, d);
return [uv[0] + rx, uv[1] + ry, rw, rh];
}
const own = uv[face.name];
if (!own) return undefined;
const [[u, v], [rw, rh]] = [own.uv, own.uv_size];
return face.name === "down" ? [u, v + rh, rw, -rh] : [u, v, rw, rh];
}
// what a box's vertices go to: the batch, or something collecting them, like the player in the inventory
export type VertexSink = typeof push_vertex;
// 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,
emit: VertexSink = push_vertex,
) {
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, emit);
}
// a box of a model, from low to high, textured from uv: minecraft's box uv from [u, v] for a box of size (in texture
// pixels), or a bedrock model's per face uvs. 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,
uv: [number, number] | FaceUvs,
to_uv: (u: number, v: number) => [number, number],
transform: (x: number, y: number, z: number) => Vec3,
tint: Vec3 = [1, 1, 1],
emit: VertexSink = push_vertex,
) {
const [w, h, d] = size;
// the box's 8 corners, transformed once each rather than once per vertex. indexed by x * 4 + y * 2 + z, 0 for
// the low end and 1 for the high one
const points: Vec3[] = [];
for (let i = 0; i < 8; i++) {
points.push(transform(i & 4 ? high[0] : low[0], i & 2 ? high[1] : low[1], i & 1 ? high[2] : low[2]));
}
for (const face of FACES) {
const rect = face_rect(face, size, uv);
if (!rect) continue;
const [rx, ry, rw, rh] = rect;
// inward, which is backwards on a flipped face
const inset_u = Math.sign(rw) * UV_INSET;
const inset_v = Math.sign(rh) * UV_INSET;
const [u0, v0] = to_uv(rx + inset_u, ry + inset_v);
const [u1, v1] = to_uv(rx + rw - inset_u, ry + rh - inset_v);
const r = tint[0] * face.shade;
const g = tint[1] * face.shade;
const b = tint[2] * face.shade;
for (const i of QUAD_CORNERS) {
const corner = face.corners[i];
const point = points[corner[0] * 4 + corner[1] * 2 + corner[2]];
emit(point[0], point[1], point[2], corner[3] ? u1 : u0, corner[4] ? v1 : v0, r, g, b, 1);
}
}
}