// minecraft's player model and how it's posed each frame, for drawing players and what they wear. no drawing here, so // tests can use it import { PLAYER_HEIGHT, PLAYER_SWIM_HEIGHT } from "$/common/constants.ts"; import { PLAYER_PARTS, type WornProperties } from "$/common/everything_registry.ts"; import type { EntityBoneJson, EntityModelJson } from "$/common/mod_data.ts"; import { apply, IDENTITY, type Matrix, multiply, rotation_around, type Vec3 } from "./model_math.ts"; export type PartName = typeof PLAYER_PARTS[number]; // minecraft's player model is 32 skin pixels tall, it's scaled to the player's height export const PIXEL = PLAYER_HEIGHT / 32; // 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 export interface ModelPart { name: PartName; size: Vec3; pivot: Vec3; from: Vec3; texture: [number, number]; overlay: [number, number]; inflate: number; } export 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, }, ]; export const SLIM_PARTS = PARTS.map((part) => SLIM_ARMS.find((arm) => arm.name === part.name) ?? part); // the right arm of the wide or slim model, for drawing it on its own in first person export function right_arm(slim: boolean): ModelPart { return (slim ? SLIM_PARTS : PARTS).find((part) => part.name === "right_arm")!; } // what a player's pose comes from, see Player and Entity.walk_animation export interface PoseInput { x: number; y: number; z: number; yaw: number; pitch: number; walk: { position: number; speed: number }; slim: boolean; // how far over they've tipped and which way the body lies, see Player.update_swim_animation. none is standing swim?: { amount: number; pitch: number }; // ticks, for the arms drifting while they swim in place time?: number; } // where a player is drawn this frame: each part's matrix, which turns its points (in skin pixels, around the model's // feet, like a bone's) the way the part is turned, then the root's, which turns the whole model (lying flat to swim), // and where the model is export interface PlayerPose { x: number; y: number; z: number; yaw: number; slim: boolean; root: Matrix; parts: Record; } // the model lies flat around the middle of its length, moved down to the middle of the swimming box const SWIM_PIVOT: Vec3 = [0, 16, 0]; const SWIM_DROP = PLAYER_SWIM_HEIGHT / 2 / PIXEL - SWIM_PIVOT[1]; // the crawl: arms go around this much per walk step (once a second swimming at full speed), legs flutter this fast and this far const STROKE_RATE = 0.3; const FLUTTER_RATE = 1.2; const FLUTTER = 0.3; // arms out a little so they go around beside the head, not through it const STROKE_SPREAD = 0.15; // swimming in place, the arms and legs drift slowly, about every 2 seconds const DRIFT_RATE = 0.15; const DRIFT = 0.25; export function pose_at(input: PoseInput): PlayerPose { // limbs swing opposite to each other, like minecraft's HumanoidModel.setupAnim const swing = Math.cos(input.walk.position * 0.6662) * input.walk.speed; const angles: Record = { head: input.pitch, body: 0, right_arm: -swing, left_arm: swing, right_leg: swing * 1.4, left_leg: -swing * 1.4, }; const spread: Record = { head: 0, body: 0, right_arm: 0, left_arm: 0, right_leg: 0, left_leg: 0 }; let root = IDENTITY; const swim = input.swim?.amount ?? 0; if (swim > 0) { // forward onto its front, then further for going down or less for going up const tilt = swim * (Math.PI / 2 - input.swim!.pitch); root = multiply([1, 0, 0, 0, 0, 1, 0, swim * SWIM_DROP, 0, 0, 1, 0], rotation_around(SWIM_PIVOT, -tilt, 0, 0)); // the head turns back up as much, so it faces where they look angles.head = Math.max(-Math.PI / 2, Math.min(Math.PI / 2, input.pitch + tilt)); const speed = input.walk.speed; const drift = Math.sin((input.time ?? 0) * DRIFT_RATE) * DRIFT; // arms go around half a turn apart, forward over the head and back under, like walking's -swing const stroke = -input.walk.position * STROKE_RATE; const arm = (offset: number) => drift * (1 - speed) + wrap(stroke + offset - drift) * speed; const flutter = Math.cos(input.walk.position * FLUTTER_RATE) * FLUTTER * speed + drift * (1 - speed); const swimming: Partial> = { right_arm: arm(0), left_arm: arm(Math.PI), right_leg: flutter, left_leg: -flutter, }; for (const [name, angle] of Object.entries(swimming) as [PartName, number][]) { angles[name] += (angle - angles[name]) * swim; } spread.right_arm = STROKE_SPREAD * swim; spread.left_arm = -STROKE_SPREAD * swim; } const parts = {} as Record; for (const part of input.slim ? SLIM_PARTS : PARTS) { parts[part.name] = rotation_around(part.pivot, angles[part.name], 0, spread[part.name]); } return { x: input.x, y: input.y, z: input.z, yaw: input.yaw, slim: input.slim, root, parts }; } // an angle the same way around, between -pi and pi function wrap(angle: number): number { return angle - 2 * Math.PI * Math.round(angle / (2 * Math.PI)); } // from the model's skin pixels to the world: turned by the root, scaled to the player's height, then turned to their // yaw export function pose_to_world(pose: PlayerPose): (x: number, y: number, z: number) => Vec3 { const [sin, cos] = [Math.sin(pose.yaw), Math.cos(pose.yaw)]; return (px, py, pz) => { const [mx, my, mz] = apply(pose.root, px, py, pz).map((n) => n * PIXEL); return [pose.x + mx * cos + mz * sin, pose.y + my, pose.z - mx * sin + mz * cos]; }; } // a point of a part, around its pivot like ModelPart.from, where it is in the pose, in the model's pixels export function part_point(pose: PlayerPose, part: ModelPart, x: number, y: number, z: number): Vec3 { const [px, py, pz] = part.pivot; return apply(pose.parts[part.name], x + px, y + py, z + pz); } // the model and texture an item is drawn with on a player with or without slim arms export function worn_look(worn: WornProperties, slim: boolean): { model: string; texture: string } { if (slim && worn.slim_model) return { model: worn.slim_model, texture: worn.slim_texture ?? worn.texture }; return { model: worn.model, texture: (slim && worn.slim_texture) || worn.texture }; } export function is_part(name: string): name is PartName { return (PLAYER_PARTS as readonly string[]).includes(name); } // whether a bone is that part or under it, so it goes where the part goes export function on_part(model: EntityModelJson, bone: EntityBoneJson, part: PartName): boolean { for (let at: EntityBoneJson | undefined = bone; at; at = model.bones.find((b) => b.name === at!.parent)) { if (at.name === part) return true; } return false; } // a worn model's bones: one named like a player part turns with it, around the part's pivot. the rest follow their // parent, or stay with the body export function worn_bone_pose(pose: PlayerPose): (bone: EntityBoneJson) => Matrix { return (bone) => is_part(bone.name) ? pose.parts[bone.name] : IDENTITY; }