// SPDX-FileCopyrightText: 2026 Paula Wisnievski // SPDX-License-Identifier: AGPL-3.0-or-later // entities mods define (entities/.json), drawn with their entity model and a texture from the atlas. the model // turns to the entity's yaw, and its bones are posed by its type's animator from a mod (see entity_animators.ts). without // one, a bone named "head" turns to where it looks, and bones named like minecraft's humanoid ones (right_arm, left_arm, // right_leg, left_leg) swing as it walks, the right arm when it attacks too. entities without a model are their texture // facing the camera, like minecraft's fishing hook import { AssetManager } from "@/client/assets.ts"; import type { Camera } from "@/client/camera.ts"; import type { RemoteEntity } from "@/client/entity/remote_entity.ts"; import { push_vertex, type Texture } from "@/client/renderer/mod.ts"; 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 } from "./player_renderer.ts"; import { bone_matrices, 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 const PIXEL = 1 / 16; // the atlas has to be the current texture export function render_mod_entity(entity: RemoteEntity, camera: Camera, partial_tick: number) { const json = EverythingRegistry.get("entities", entity.type); if (!json) return; const atlas = AssetManager.instance.get("cubi:textures"); const region = get_sprite_region(json.texture); const hurt = entity.hurt_time > 0 || entity.dead ? HURT_TINT : undefined; const tint = entity_tint(entity, partial_tick, hurt, entity.data.on_fire === true); if (json.model === undefined) { render_sprite(entity, camera, atlas, region, partial_tick, tint); return; } const model = EverythingRegistry.get("entity_models", json.model); if (!model) return; 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 offsets = entity.animation?.pose(entity.age + partial_tick); const pose = offsets ? () => IDENTITY : default_pose(entity, partial_tick); push_model(model, bone_matrices(model, pose, offsets), atlas, region, to_world, tint); } // the head looking and the humanoid swing, for an entity without an animator function default_pose(entity: RemoteEntity, partial_tick: number) { const limbs = limb_angles(entity, partial_tick); return (bone: EntityBoneJson) => { let matrix = IDENTITY; if (bone.name === "head") { 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); return matrix; }; } // how far each humanoid limb turns forward (radians), like minecraft's HumanoidModel.setupAnim: arms and legs swing // opposite each other as it walks, and the right arm comes down in an attack like undergarden's rotwalker's function limb_angles(entity: RemoteEntity, partial_tick: number): Record { const walk = entity.walk_animation(partial_tick); const swing = Math.cos(walk.position * 0.6662) * 1.4 * walk.speed; const attack = entity.attack_animation(partial_tick); // ease out quart, so it comes down fast const eased = 1 - (1 - attack) ** 4; return { right_arm: -swing + Math.sin(eased * Math.PI) * 1.2, left_arm: swing, right_leg: swing, left_leg: -swing, }; } // the whole texture, 16 pixels to a block, facing the camera around the middle of the entity's box function render_sprite( entity: RemoteEntity, camera: Camera, atlas: Texture, region: SpriteRegion, partial_tick: number, [r, g, b]: Vec3, ) { const { x, y, z } = entity.render_position(partial_tick); const cy = y + entity.height / 2; // the camera's right and up, halved to the sprite's size const half_width = region.width * PIXEL / 2; const half_height = region.height * PIXEL / 2; const [yaw_sin, yaw_cos] = [Math.sin(camera.yaw), Math.cos(camera.yaw)]; const [pitch_sin, pitch_cos] = [Math.sin(camera.pitch), Math.cos(camera.pitch)]; const right: Vec3 = [yaw_cos * half_width, 0, -yaw_sin * half_width]; const up: Vec3 = [yaw_sin * pitch_sin * half_height, pitch_cos * half_height, yaw_cos * pitch_sin * half_height]; const u0 = region.x / atlas.width; const v0 = region.y / atlas.height; const u1 = (region.x + region.width) / atlas.width; const v1 = (region.y + region.height) / atlas.height; // bottom left, bottom right, top right, top left: counter clockwise seen from the camera const corners = [[-1, -1], [1, -1], [1, 1], [-1, 1]]; for (const i of [0, 1, 2, 0, 2, 3]) { const [sx, sy] = corners[i]; push_vertex( x + right[0] * sx + up[0] * sy, cy + right[1] * sx + up[1] * sy, z + right[2] * sx + up[2] * sy, sx < 0 ? u0 : u1, sy < 0 ? v1 : v0, r, g, b, 1, ); } }