Curios slots rendering !

This commit is contained in:
2026-10-01 17:57:22 -03:00
parent ea9f3f61bb
commit f2c3cb8135
39 changed files with 1032 additions and 226 deletions
+23 -130
View File
@@ -1,115 +1,24 @@
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";
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 = [number, number, number];
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];
// 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
export 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);
// 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")!;
}
// 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
@@ -152,36 +61,20 @@ const FACES: { rect: (w: number, h: number, d: number) => number[]; corners: num
},
];
// 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;
return pose_at({ x, y, z, yaw, pitch, slim, walk: player.walk_animation(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(player: Player, skin: Texture, partial_tick: number, tint?: Vec3) {
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];
};
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);
}