Curios slots rendering !
This commit is contained in:
@@ -10,7 +10,9 @@ import { get_sprite_region } from "$/client/sprites.ts";
|
||||
import type { SpriteRegion } from "$/common/constants.ts";
|
||||
import { EverythingRegistry } from "$/common/everything_registry.ts";
|
||||
import type { EntityBoneJson, EntityJson, EntityModelJson } from "$/common/mod_data.ts";
|
||||
import { HURT_TINT, push_box, type Vec3 } from "./player_renderer.ts";
|
||||
import { HURT_TINT } from "./player_renderer.ts";
|
||||
import { IDENTITY, model_pivot, multiply, rotation_around, type Vec3 } from "./model_math.ts";
|
||||
import { push_model } from "./model_renderer.ts";
|
||||
import { entity_tint } from "./entity_light.ts";
|
||||
|
||||
// models are in texture pixels, 16 to a block
|
||||
@@ -31,42 +33,25 @@ export function render_mod_entity(entity: RemoteEntity, camera: Camera, partial_
|
||||
const model = EverythingRegistry.get<EntityModelJson>("entity_models", json.model);
|
||||
if (!model) return;
|
||||
|
||||
const to_uv = (u: number, v: number): [number, number] => [
|
||||
(region.x + u * region.width / model.texture_width) / atlas.width,
|
||||
(region.y + v * region.height / model.texture_height) / atlas.height,
|
||||
];
|
||||
|
||||
const { x, y, z } = entity.render_position(partial_tick);
|
||||
const yaw_sin = Math.sin(entity.yaw);
|
||||
const yaw_cos = Math.cos(entity.yaw);
|
||||
const to_world = (px: number, py: number, pz: number): Vec3 => {
|
||||
const [mx, my, mz] = [px * PIXEL, py * PIXEL, pz * PIXEL];
|
||||
return [x + mx * yaw_cos + mz * yaw_sin, y + my, z - mx * yaw_sin + mz * yaw_cos];
|
||||
};
|
||||
|
||||
const limbs = limb_angles(entity, partial_tick);
|
||||
const bones = new Map<string, Matrix>();
|
||||
for (const bone of model.bones) {
|
||||
let matrix = bone_matrix(bone);
|
||||
const pose = (bone: EntityBoneJson) => {
|
||||
let matrix = IDENTITY;
|
||||
if (bone.name === "head") {
|
||||
const look = rotation_around(model_pivot(bone), entity.pitch, entity.head_yaw - entity.yaw, 0);
|
||||
matrix = multiply(look, matrix);
|
||||
matrix = rotation_around(model_pivot(bone), entity.pitch, entity.head_yaw - entity.yaw, 0);
|
||||
}
|
||||
const limb = limbs[bone.name];
|
||||
if (limb) matrix = multiply(rotation_around(model_pivot(bone), limb, 0, 0), matrix);
|
||||
if (bone.parent) matrix = multiply(bones.get(bone.parent) ?? IDENTITY, matrix);
|
||||
bones.set(bone.name, matrix);
|
||||
|
||||
const transform = (px: number, py: number, pz: number): Vec3 => {
|
||||
const [mx, my, mz] = apply(matrix, px, py, pz).map((n) => n * PIXEL);
|
||||
return [x + mx * yaw_cos + mz * yaw_sin, y + my, z - mx * yaw_sin + mz * yaw_cos];
|
||||
};
|
||||
for (const cube of bone.cubes) {
|
||||
const inflate = cube.inflate ?? 0;
|
||||
const [ox, oy, oz] = cube.origin;
|
||||
const [w, h, d] = cube.size;
|
||||
// bedrock models are mirrored on x from the engine's, like blockbench shows them: +x is the model's right
|
||||
const low: Vec3 = [-(ox + w) - inflate, oy - inflate, oz - inflate];
|
||||
const high: Vec3 = [-ox + inflate, oy + h + inflate, oz + d + inflate];
|
||||
push_box(cube.size, low, high, cube.uv, to_uv, transform, tint);
|
||||
}
|
||||
}
|
||||
return matrix;
|
||||
};
|
||||
push_model(model, pose, atlas, region, to_world, tint);
|
||||
}
|
||||
|
||||
// how far each humanoid limb turns forward (radians), like minecraft's HumanoidModel.setupAnim: arms and legs swing
|
||||
@@ -125,57 +110,3 @@ function render_sprite(
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// 3 rows of 4, the last column moving: a turn and a move, from a bone's pixels to the model's
|
||||
type Matrix = number[];
|
||||
|
||||
const IDENTITY: Matrix = [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0];
|
||||
|
||||
function model_pivot(bone: EntityBoneJson): Vec3 {
|
||||
return [-bone.pivot[0], bone.pivot[1], bone.pivot[2]];
|
||||
}
|
||||
|
||||
// its own rotation around its pivot. mirroring x turns x and y rotations the other way
|
||||
function bone_matrix(bone: EntityBoneJson): Matrix {
|
||||
if (!bone.rotation) return IDENTITY;
|
||||
const [rx, ry, rz] = bone.rotation.map((degrees) => degrees * Math.PI / 180);
|
||||
return rotation_around(model_pivot(bone), -rx, -ry, rz);
|
||||
}
|
||||
|
||||
// turns around pivot by x, then y, then z (radians). y turns like the whole model does (see render_mod_entity) and x
|
||||
// like the player's head pitch
|
||||
function rotation_around(pivot: Vec3, rx: number, ry: number, rz: number): Matrix {
|
||||
const [sx, cx] = [Math.sin(rx), Math.cos(rx)];
|
||||
const [sy, cy] = [Math.sin(ry), Math.cos(ry)];
|
||||
const [sz, cz] = [Math.sin(rz), Math.cos(rz)];
|
||||
const rot_x = [1, 0, 0, 0, 0, cx, -sx, 0, 0, sx, cx, 0];
|
||||
const rot_y = [cy, 0, sy, 0, 0, 1, 0, 0, -sy, 0, cy, 0];
|
||||
const rot_z = [cz, -sz, 0, 0, sz, cz, 0, 0, 0, 0, 1, 0];
|
||||
const [px, py, pz] = pivot;
|
||||
const rotation = multiply(rot_z, multiply(rot_y, rot_x));
|
||||
return multiply(
|
||||
[1, 0, 0, px, 0, 1, 0, py, 0, 0, 1, pz],
|
||||
multiply(rotation, [1, 0, 0, -px, 0, 1, 0, -py, 0, 0, 1, -pz]),
|
||||
);
|
||||
}
|
||||
|
||||
// a after b
|
||||
function multiply(a: Matrix, b: Matrix): Matrix {
|
||||
const out: Matrix = [];
|
||||
for (let row = 0; row < 3; row++) {
|
||||
for (let col = 0; col < 4; col++) {
|
||||
let sum = col === 3 ? a[row * 4 + 3] : 0;
|
||||
for (let k = 0; k < 3; k++) sum += a[row * 4 + k] * b[k * 4 + col];
|
||||
out.push(sum);
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
function apply(m: Matrix, x: number, y: number, z: number): Vec3 {
|
||||
return [
|
||||
m[0] * x + m[1] * y + m[2] * z + m[3],
|
||||
m[4] * x + m[5] * y + m[6] * z + m[7],
|
||||
m[8] * x + m[9] * y + m[10] * z + m[11],
|
||||
];
|
||||
}
|
||||
|
||||
@@ -7,7 +7,9 @@ import { AssetManager } from "$/client/assets.ts";
|
||||
import type { LocalPlayer } from "$/client/entity/local_player.ts";
|
||||
import { begin_view_model, end_mode_3d, flush_batch, set_current_texture, Texture } from "$/client/renderer/mod.ts";
|
||||
import { item_block, push_item } from "./item_renderer.ts";
|
||||
import { push_cube, right_arm, type Vec3 } from "./player_renderer.ts";
|
||||
import { push_cube, type Vec3 } from "./player_renderer.ts";
|
||||
import { right_arm } from "./player_model.ts";
|
||||
import { render_worn_arm } from "./worn_renderer.ts";
|
||||
import { entity_tint } from "./entity_light.ts";
|
||||
|
||||
// wider than the world's field of view, like minecraft's fixed 70 degrees for the hand
|
||||
@@ -62,6 +64,12 @@ export class HandRenderer {
|
||||
set_current_texture(skin.tex);
|
||||
push_cube(skin, arm, arm.texture, 0, transform, tint);
|
||||
push_cube(skin, arm, arm.overlay, arm.inflate, transform, tint);
|
||||
// what's worn on it goes over it, from the atlas
|
||||
if (player.worn.length > 0) {
|
||||
flush_batch();
|
||||
set_current_texture(AssetManager.instance.get<Texture>("bworld:textures").tex);
|
||||
render_worn_arm(player, transform, tint);
|
||||
}
|
||||
}
|
||||
flush_batch();
|
||||
end_mode_3d();
|
||||
|
||||
@@ -7,7 +7,10 @@ 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_block_items, render_item_entity } from "./item_renderer.ts";
|
||||
import { HURT_TINT, render_player } from "./player_renderer.ts";
|
||||
import { HURT_TINT, player_pose, render_player } from "./player_renderer.ts";
|
||||
import { render_worn } from "./worn_renderer.ts";
|
||||
import type { PlayerPose } from "./player_model.ts";
|
||||
import type { Vec3 } from "./model_math.ts";
|
||||
import { render_mod_entity } from "./entity_renderer.ts";
|
||||
import { entity_tint } from "./entity_light.ts";
|
||||
import { RemoteEntity } from "$/client/entity/remote_entity.ts";
|
||||
@@ -98,6 +101,8 @@ export class LevelRenderer {
|
||||
render_entities(level: ClientLevel, camera: Camera, camera_entity: Entity, partial_tick: number) {
|
||||
flush_batch();
|
||||
const default_skin = AssetManager.instance.get<Texture>("bworld:skin");
|
||||
// what they wear is drawn with the atlas, after every skin
|
||||
const posed: { player: RemotePlayer; pose: PlayerPose; tint: Vec3 }[] = [];
|
||||
for (const entity of level.entities.values()) {
|
||||
if (entity !== camera_entity && entity instanceof RemotePlayer) {
|
||||
const skin = entity.skin ?? default_skin;
|
||||
@@ -106,12 +111,15 @@ export class LevelRenderer {
|
||||
set_current_texture(skin.tex);
|
||||
}
|
||||
const tint = entity_tint(entity, partial_tick, entity.hurt_time > 0 ? HURT_TINT : undefined);
|
||||
render_player(entity, skin, partial_tick, tint);
|
||||
const pose = player_pose(entity, partial_tick);
|
||||
render_player(skin, pose, tint);
|
||||
posed.push({ player: entity, pose, tint });
|
||||
}
|
||||
}
|
||||
flush_batch();
|
||||
|
||||
set_current_texture(level.image.tex);
|
||||
for (const { player, pose, tint } of posed) render_worn(player, pose, tint);
|
||||
for (const entity of level.entities.values()) {
|
||||
if (entity instanceof ItemEntity) {
|
||||
render_item_entity(entity, partial_tick);
|
||||
|
||||
@@ -0,0 +1,72 @@
|
||||
// turning and moving the parts of models: entity models' bones, the player's parts and what's worn on them. no
|
||||
// drawing here, so tests can use it
|
||||
import type { EntityBoneJson, EntityModelJson } from "$/common/mod_data.ts";
|
||||
|
||||
export type Vec3 = [number, number, number];
|
||||
|
||||
// 3 rows of 4, the last column moving: a turn and a move, from a bone's pixels to the model's
|
||||
export type Matrix = number[];
|
||||
|
||||
export const IDENTITY: Matrix = [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0];
|
||||
|
||||
// bedrock models are mirrored on x from the engine's, like blockbench shows them: +x is the model's right
|
||||
export function model_pivot(bone: EntityBoneJson): Vec3 {
|
||||
return [-bone.pivot[0], bone.pivot[1], bone.pivot[2]];
|
||||
}
|
||||
|
||||
// its own rotation around its pivot. mirroring x turns x and y rotations the other way
|
||||
export function bone_matrix(bone: EntityBoneJson): Matrix {
|
||||
if (!bone.rotation) return IDENTITY;
|
||||
const [rx, ry, rz] = bone.rotation.map((degrees) => degrees * Math.PI / 180);
|
||||
return rotation_around(model_pivot(bone), -rx, -ry, rz);
|
||||
}
|
||||
|
||||
// every bone's matrix: its own rotation, then pose's for it (like a head looking or a limb swinging), then its
|
||||
// parent's. parents come before their children in the model
|
||||
export function bone_matrices(model: EntityModelJson, pose: (bone: EntityBoneJson) => Matrix): Map<string, Matrix> {
|
||||
const bones = new Map<string, Matrix>();
|
||||
for (const bone of model.bones) {
|
||||
let matrix = multiply(pose(bone), bone_matrix(bone));
|
||||
if (bone.parent) matrix = multiply(bones.get(bone.parent) ?? IDENTITY, matrix);
|
||||
bones.set(bone.name, matrix);
|
||||
}
|
||||
return bones;
|
||||
}
|
||||
|
||||
// turns around pivot by x, then y, then z (radians). y turns like the whole model does and x like the player's head
|
||||
// pitch
|
||||
export function rotation_around(pivot: Vec3, rx: number, ry: number, rz: number): Matrix {
|
||||
const [sx, cx] = [Math.sin(rx), Math.cos(rx)];
|
||||
const [sy, cy] = [Math.sin(ry), Math.cos(ry)];
|
||||
const [sz, cz] = [Math.sin(rz), Math.cos(rz)];
|
||||
const rot_x = [1, 0, 0, 0, 0, cx, -sx, 0, 0, sx, cx, 0];
|
||||
const rot_y = [cy, 0, sy, 0, 0, 1, 0, 0, -sy, 0, cy, 0];
|
||||
const rot_z = [cz, -sz, 0, 0, sz, cz, 0, 0, 0, 0, 1, 0];
|
||||
const [px, py, pz] = pivot;
|
||||
const rotation = multiply(rot_z, multiply(rot_y, rot_x));
|
||||
return multiply(
|
||||
[1, 0, 0, px, 0, 1, 0, py, 0, 0, 1, pz],
|
||||
multiply(rotation, [1, 0, 0, -px, 0, 1, 0, -py, 0, 0, 1, -pz]),
|
||||
);
|
||||
}
|
||||
|
||||
// a after b
|
||||
export function multiply(a: Matrix, b: Matrix): Matrix {
|
||||
const out: Matrix = [];
|
||||
for (let row = 0; row < 3; row++) {
|
||||
for (let col = 0; col < 4; col++) {
|
||||
let sum = col === 3 ? a[row * 4 + 3] : 0;
|
||||
for (let k = 0; k < 3; k++) sum += a[row * 4 + k] * b[k * 4 + col];
|
||||
out.push(sum);
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
export function apply(m: Matrix, x: number, y: number, z: number): Vec3 {
|
||||
return [
|
||||
m[0] * x + m[1] * y + m[2] * z + m[3],
|
||||
m[4] * x + m[5] * y + m[6] * z + m[7],
|
||||
m[8] * x + m[9] * y + m[10] * z + m[11],
|
||||
];
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
// entity models' boxes, for mod entities and for what players wear. the atlas has to be the current texture
|
||||
import type { Texture } from "$/client/renderer/mod.ts";
|
||||
import type { SpriteRegion } from "$/common/constants.ts";
|
||||
import type { EntityBoneJson, EntityModelJson } from "$/common/mod_data.ts";
|
||||
import { apply, bone_matrices, type Matrix, type Vec3 } from "./model_math.ts";
|
||||
import { push_box } from "./player_renderer.ts";
|
||||
|
||||
// pose is each bone's turn before its parent's, like a head looking or a limb swinging. to_world takes the model's
|
||||
// pixels, after the bones are turned, to the world. the texture is region of the atlas, stretched to the model's
|
||||
// texture size. only picks the bones drawn, all of them when not given
|
||||
export function push_model(
|
||||
model: EntityModelJson,
|
||||
pose: (bone: EntityBoneJson) => Matrix,
|
||||
atlas: Texture,
|
||||
region: SpriteRegion,
|
||||
to_world: (x: number, y: number, z: number) => Vec3,
|
||||
tint?: Vec3,
|
||||
only?: (bone: EntityBoneJson) => boolean,
|
||||
) {
|
||||
const to_uv = (u: number, v: number): [number, number] => [
|
||||
(region.x + u * region.width / model.texture_width) / atlas.width,
|
||||
(region.y + v * region.height / model.texture_height) / atlas.height,
|
||||
];
|
||||
const matrices = bone_matrices(model, pose);
|
||||
for (const bone of model.bones) {
|
||||
if (only && !only(bone)) continue;
|
||||
const matrix = matrices.get(bone.name)!;
|
||||
const transform = (px: number, py: number, pz: number) => to_world(...apply(matrix, px, py, pz));
|
||||
for (const cube of bone.cubes) {
|
||||
const inflate = cube.inflate ?? 0;
|
||||
const [ox, oy, oz] = cube.origin;
|
||||
const [w, h, d] = cube.size;
|
||||
// bedrock models are mirrored on x from the engine's, like blockbench shows them: +x is the model's right
|
||||
const low: Vec3 = [-(ox + w) - inflate, oy - inflate, oz - inflate];
|
||||
const high: Vec3 = [-ox + inflate, oy + h + inflate, oz + d + inflate];
|
||||
push_box(cube.size, low, high, cube.uv, to_uv, transform, tint);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,188 @@
|
||||
// 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;
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
// what players wear, like curios' render layer: each worn item's model (see WornProperties) on the player, its bones
|
||||
// named like the player's parts turned the way those parts are, so it moves with them like minecraft's
|
||||
// TransformCopyingHumanoidModel. see docs/modding/items/index.md
|
||||
import { AssetManager } from "$/client/assets.ts";
|
||||
import type { Player } from "$/client/entity/player.ts";
|
||||
import type { Texture } from "$/client/renderer/mod.ts";
|
||||
import { get_sprite_region } from "$/client/sprites.ts";
|
||||
import { EverythingRegistry, type ItemRegistry } from "$/common/everything_registry.ts";
|
||||
import type { EntityBoneJson, EntityModelJson } from "$/common/mod_data.ts";
|
||||
import { IDENTITY, type Vec3 } from "./model_math.ts";
|
||||
import { push_model } from "./model_renderer.ts";
|
||||
import { on_part, type PlayerPose, pose_to_world, right_arm, worn_bone_pose, worn_look } from "./player_model.ts";
|
||||
|
||||
// the atlas has to be the current texture. tint is the player's, for light and flashing red
|
||||
export function render_worn(player: Player, pose: PlayerPose, tint?: Vec3) {
|
||||
if (player.worn.length === 0) return;
|
||||
const atlas = AssetManager.instance.get<Texture>("bworld:textures");
|
||||
const to_world = pose_to_world(pose);
|
||||
for (const stack of player.worn) {
|
||||
const worn = EverythingRegistry.get<ItemRegistry>("items", stack.type_id)?.worn;
|
||||
if (!worn) continue;
|
||||
const look = worn_look(worn, pose.slim);
|
||||
const model = EverythingRegistry.get<EntityModelJson>("entity_models", look.model);
|
||||
if (!model) continue;
|
||||
push_model(model, worn_bone_pose(pose), atlas, get_sprite_region(look.texture), to_world, tint);
|
||||
}
|
||||
}
|
||||
|
||||
// what's worn on a player's right arm, in first person, like artifacts' renderFirstPersonArm. transform is the arm's,
|
||||
// from points around its pivot (see hand_renderer's arm_pose). the atlas has to be the current texture
|
||||
export function render_worn_arm(player: Player, transform: (x: number, y: number, z: number) => Vec3, tint?: Vec3) {
|
||||
const atlas = AssetManager.instance.get<Texture>("bworld:textures");
|
||||
const [px, py, pz] = right_arm(player.slim).pivot;
|
||||
// the model's pixels, standing still, to around the arm's pivot
|
||||
const to_arm = (x: number, y: number, z: number) => transform(x - px, y - py, z - pz);
|
||||
for (const stack of player.worn) {
|
||||
const worn = EverythingRegistry.get<ItemRegistry>("items", stack.type_id)?.worn;
|
||||
if (!worn) continue;
|
||||
const look = worn_look(worn, player.slim);
|
||||
const model = EverythingRegistry.get<EntityModelJson>("entity_models", look.model);
|
||||
if (!model) continue;
|
||||
const only = (bone: EntityBoneJson) => on_part(model, bone, "right_arm");
|
||||
push_model(model, () => IDENTITY, atlas, get_sprite_region(look.texture), to_arm, tint, only);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user