Player model

This commit is contained in:
2026-09-27 11:50:08 -03:00
parent eada67de13
commit edf4b72dc4
6 changed files with 212 additions and 42 deletions
Binary file not shown.

After

Width:  |  Height:  |  Size: 662 B

+20
View File
@@ -5,8 +5,28 @@ import { Entity } from "./entity.ts";
export abstract class Player extends Entity {
name: string;
// how far the limbs have swung and how hard they swing (0 to 1), like minecraft's WalkAnimationState
walk_position = 0;
walk_speed = 0;
prev_walk_speed = 0;
constructor(level: ClientLevel, id: string, name: string) {
super(level, id, PLAYER_WIDTH, PLAYER_HEIGHT, PLAYER_EYE_HEIGHT);
this.name = name;
}
// after moving each tick. full speed is 5 blocks per second, a bit faster than walking
update_walk_animation() {
const moved = Math.hypot(this.x - this.prev_x, this.z - this.prev_z);
this.prev_walk_speed = this.walk_speed;
this.walk_speed += (Math.min(moved * 4, 1) - this.walk_speed) * 0.4;
this.walk_position += this.walk_speed;
}
walk_animation(partial_tick: number) {
return {
position: this.walk_position - this.walk_speed * (1 - partial_tick),
speed: this.prev_walk_speed + (this.walk_speed - this.prev_walk_speed) * partial_tick,
};
}
}
+1 -27
View File
@@ -7,8 +7,6 @@ const SMOOTHING = 12;
// another player on the server, it moves where the server says instead of simulating anything
export class RemotePlayer extends Player {
color: [number, number, number];
// where the server last said it is, the drawn position eases towards it so movement isn't choppy
target_x: number;
target_y: number;
@@ -22,7 +20,6 @@ export class RemotePlayer extends Player {
this.target_z = info.z;
this.yaw = info.yaw;
this.pitch = info.pitch;
this.color = color_from_name(info.name);
}
lerp_to(x: number, y: number, z: number, yaw: number, pitch: number) {
@@ -38,29 +35,6 @@ export class RemotePlayer extends Player {
this.x += (this.target_x - this.x) * t;
this.y += (this.target_y - this.y) * t;
this.z += (this.target_z - this.z) * t;
this.update_walk_animation();
}
}
function color_from_name(name: string): [number, number, number] {
let hash = 0;
for (const ch of name) {
hash = (hash * 31 + ch.charCodeAt(0)) | 0;
}
const hue = ((hash % 360) + 360) % 360;
// hsl with s=0.6 l=0.6 to rgb
const c = 0.48;
const x = c * (1 - Math.abs(((hue / 60) % 2) - 1));
const m = 0.36;
const [r, g, b] = hue < 60
? [c, x, 0]
: hue < 120
? [x, c, 0]
: hue < 180
? [0, c, x]
: hue < 240
? [0, x, c]
: hue < 300
? [x, 0, c]
: [c, 0, x];
return [r + m, g + m, b + m];
}
+1
View File
@@ -174,6 +174,7 @@ canvas.addEventListener("contextmenu", (event) => event.preventDefault());
AssetManager.instance.load("bworld:assets_text", "/assets/ASSETS.md");
AssetManager.instance.load("bworld:player", "/assets/sprites/player.png");
AssetManager.instance.load("bworld:skin", "/assets/sprites/skin_template.png");
AssetManager.instance.load("bworld:roguelike", "/assets/sprites/roguelike.png");
AssetManager.instance.load("bworld:ui", "/assets/sprites/ui.png");
+4 -15
View File
@@ -8,12 +8,12 @@ import type { Entity } from "$/client/entity/entity.ts";
import { RemotePlayer } from "$/client/entity/remote_player.ts";
import { ItemEntity } from "$/client/entity/item_entity.ts";
import { render_item_entity } from "./item_renderer.ts";
import { render_player } from "./player_renderer.ts";
import {
draw_terrain,
flush_batch,
push_back_face,
push_bottom_face,
push_box,
push_front_face,
push_left_face,
push_right_face,
@@ -21,7 +21,6 @@ import {
set_current_texture,
Texture,
view_projection,
white_tex,
} from "$/client/renderer/mod.ts";
import type { RenderLayer } from "$/common/everything_registry.ts";
import { FACE_GROUPS, UNALIGNED_GROUP } from "$/client/workers/chunk_messages.ts";
@@ -95,11 +94,12 @@ export class LevelRenderer {
// every entity but the one the camera is in
render_entities(level: ClientLevel, camera_entity: Entity, partial_tick: number) {
flush_batch();
set_current_texture(white_tex!);
const skin = AssetManager.instance.get<Texture>("bworld:skin");
set_current_texture(skin.tex);
for (const entity of level.entities.values()) {
if (entity !== camera_entity && entity instanceof RemotePlayer) {
render_player(entity, partial_tick);
render_player(entity, skin, partial_tick);
}
}
flush_batch();
@@ -180,14 +180,3 @@ export function group_faces_camera(
const position = axis === 0 ? camera.x : axis === 1 ? camera.y : camera.z;
return FACE_NORMALS[group][axis] > 0 ? position > min : position < max;
}
// a box body and head in the player's color
function render_player(player: RemotePlayer, partial_tick: number) {
const [r, g, b] = player.color;
const { x, y, z } = player.render_position(partial_tick);
// body
push_box(x - 0.3, y, z - 0.15, 0.6, 1.3, 0.3, r, g, b);
// head
push_box(x - 0.25, y + 1.3, z - 0.25, 0.5, 0.5, 0.5, 0.95, 0.8, 0.65);
}
+186
View File
@@ -0,0 +1,186 @@
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";
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,
},
];
// 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 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,
[u, v]: [number, number],
inflate: number,
transform: (x: number, y: number, z: number) => Vec3,
) {
const [w, h, d] = part.size;
const low = part.from.map((n) => n - inflate);
const high = part.from.map((n, i) => n + part.size[i] + inflate);
for (const face of FACES) {
const [rx, ry, rw, rh] = face.rect(w, h, d);
const u0 = (u + rx + UV_INSET) / skin.width;
const v0 = (v + ry + UV_INSET) / skin.height;
const u1 = (u + rx + rw - UV_INSET) / skin.width;
const v1 = (v + ry + rh - UV_INSET) / skin.height;
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, face.shade, face.shade, face.shade, 1);
}
}
}