// 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 { 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, }, ]; // 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); } } // 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 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], 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 [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 = 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); } } }