Player model
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user