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

189 lines
5.8 KiB
TypeScript

// 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 } 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, 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;
}
// 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, and where the model is
export interface PlayerPose {
x: number;
y: number;
z: number;
yaw: number;
slim: boolean;
parts: Record<PartName, Matrix>;
}
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<PartName, number> = {
head: input.pitch,
body: 0,
right_arm: -swing,
left_arm: swing,
right_leg: swing * 1.4,
left_leg: -swing * 1.4,
};
const parts = {} as Record<PartName, Matrix>;
for (const part of input.slim ? SLIM_PARTS : PARTS) {
parts[part.name] = rotation_around(part.pivot, angles[part.name], 0, 0);
}
return { x: input.x, y: input.y, z: input.z, yaw: input.yaw, slim: input.slim, parts };
}
// from the model's skin pixels to the world: 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] = [px * PIXEL, py * PIXEL, pz * 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;
}