import { PLAYER_HEIGHT } from "$/common/constants.ts"; import type { Player } from "$/client/entity/player.ts"; import { push_vertex, type Texture } from "$/client/renderer/mod.ts"; export type Vec3 = [number, number, number]; // minecraft's player model is 32 skin pixels tall, it's scaled to the player's height const PIXEL = PLAYER_HEIGHT / 32; // keeps nearest sampling from picking the texel next to a face's edge 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 { name: "head" | "body" | "right_arm" | "left_arm" | "right_leg" | "left_leg"; size: Vec3; pivot: Vec3; from: Vec3; texture: [number, number]; overlay: [number, number]; inflate: number; } const PARTS: ModelPart[] = [ { name: "head", size: [8, 8, 8], pivot: [0, 24, 0], from: [-4, 0, -4], texture: [0, 0], overlay: [32, 0], inflate: 0.5, }, { name: "body", size: [8, 12, 4], pivot: [0, 12, 0], from: [-4, 0, -2], texture: [16, 16], overlay: [16, 32], inflate: 0.25, }, { name: "right_arm", size: [4, 12, 4], pivot: [5, 22, 0], from: [-1, -10, -2], texture: [40, 16], overlay: [40, 32], inflate: 0.25, }, { name: "left_arm", size: [4, 12, 4], pivot: [-5, 22, 0], from: [-3, -10, -2], texture: [32, 48], overlay: [48, 48], inflate: 0.25, }, { name: "right_leg", size: [4, 12, 4], pivot: [2, 12, 0], from: [-2, -12, -2], texture: [0, 16], overlay: [0, 32], inflate: 0.25, }, { name: "left_leg", size: [4, 12, 4], pivot: [-2, 12, 0], from: [-2, -12, -2], texture: [16, 48], overlay: [0, 48], inflate: 0.25, }, ]; // slim (alex) skins have 3 pixel wide arms, half a pixel lower, like minecraft's PlayerModel with slim set const SLIM_ARMS: ModelPart[] = [ { name: "right_arm", size: [3, 12, 4], pivot: [5, 21.5, 0], from: [-1, -10, -2], texture: [40, 16], overlay: [40, 32], inflate: 0.25, }, { name: "left_arm", size: [3, 12, 4], pivot: [-5, 21.5, 0], from: [-2, -10, -2], texture: [32, 48], overlay: [48, 48], inflate: 0.25, }, ]; const SLIM_PARTS = PARTS.map((part) => SLIM_ARMS.find((arm) => arm.name === part.name) ?? part); // 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 player with a minecraft skin, like minecraft's PlayerRenderer. the skin has to be the current texture export function render_player(player: Player, skin: Texture, partial_tick: number) { const { x, y, z } = player.render_position(partial_tick); const yaw_sin = Math.sin(player.yaw); const yaw_cos = Math.cos(player.yaw); // limbs swing opposite to each other, like minecraft's HumanoidModel.setupAnim const walk = player.walk_animation(partial_tick); const swing = Math.cos(walk.position * 0.6662) * walk.speed; const angles = { head: player.pitch, body: 0, right_arm: -swing, left_arm: swing, right_leg: swing * 1.4, left_leg: -swing * 1.4, }; for (const part of player.slim ? SLIM_PARTS : PARTS) { const sin = Math.sin(angles[part.name]); const cos = Math.cos(angles[part.name]); const [pivot_x, pivot_y, pivot_z] = part.pivot; // turns around the pivot's x axis, then the whole model turns to the player's yaw const transform = (px: number, py: number, pz: number): Vec3 => { const mx = (px + pivot_x) * PIXEL; const my = (py * cos - pz * sin + pivot_y) * PIXEL; const mz = (py * sin + pz * cos + pivot_z) * PIXEL; return [x + mx * yaw_cos + mz * yaw_sin, y + my, z - mx * yaw_sin + mz * yaw_cos]; }; push_cube(skin, part, part.texture, 0, transform); push_cube(skin, part, part.overlay, part.inflate, transform); } } function push_cube( skin: Texture, part: ModelPart, uv: [number, number], inflate: number, transform: (x: number, y: number, z: number) => 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); } // 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); } } }