Files

66 lines
3.2 KiB
TypeScript

// SPDX-FileCopyrightText: 2026 Paula Wisnievski
// SPDX-License-Identifier: AGPL-3.0-or-later
// 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 { ItemStack } from "@/common/inventory.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 { bone_matrices, IDENTITY, type Vec3 } from "./model_math.ts";
import { push_model } from "./model_renderer.ts";
import type { VertexSink } from "./player_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) {
push_worn(player.worn, pose, tint);
}
// what's worn on a player in that pose, its vertices to emit, the batch by default, like the player in the inventory
export function push_worn(worn_items: ItemStack[], pose: PlayerPose, tint?: Vec3, emit?: VertexSink) {
if (worn_items.length === 0) return;
const atlas = AssetManager.instance.get<Texture>("cubi:textures");
const to_world = pose_to_world(pose);
for (const stack of worn_items) {
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,
bone_matrices(model, worn_bone_pose(pose)),
atlas,
get_sprite_region(look.texture),
to_world,
tint,
undefined,
emit,
);
}
}
// 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>("cubi: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");
const bones = bone_matrices(model, () => IDENTITY);
push_model(model, bones, atlas, get_sprite_region(look.texture), to_arm, tint, only);
}
}