Animators and coilhead animations
This commit is contained in:
@@ -10,6 +10,11 @@ import type { BoneOffsets, Vec3 } from "./model_math.ts";
|
||||
// a keyframe this close to the time is the one it's on, in seconds
|
||||
const ON_KEYFRAME = 1 / 1200;
|
||||
|
||||
// a bone nothing has moved
|
||||
export function rest_offsets(): BoneOffsets {
|
||||
return { rotation: [0, 0, 0], position: [0, 0, 0], scale: [1, 1, 1] };
|
||||
}
|
||||
|
||||
// adds what clip does seconds into it to each bone's offsets, scaled by weight: rotation and position times weight,
|
||||
// scale weight of the way from 1. a clip that loops wraps around its length, one that doesn't stays on its last frame
|
||||
export function sample_clip(clip: ClipJson, seconds: number, offsets: Map<string, BoneOffsets>, weight = 1) {
|
||||
@@ -17,7 +22,7 @@ export function sample_clip(clip: ClipJson, seconds: number, offsets: Map<string
|
||||
for (const [bone, channels] of Object.entries(clip.bones)) {
|
||||
let offset = offsets.get(bone);
|
||||
if (!offset) {
|
||||
offset = { rotation: [0, 0, 0], position: [0, 0, 0], scale: [1, 1, 1] };
|
||||
offset = rest_offsets();
|
||||
offsets.set(bone, offset);
|
||||
}
|
||||
for (const channel of ["rotation", "position"] as const) {
|
||||
|
||||
@@ -0,0 +1,172 @@
|
||||
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
// mods' entity animators, see ClientRender.register_entity_animator and ANIMATION.md. each entity of a type with one
|
||||
// gets its own, made with the entity, which says what its model's bones do every frame it's drawn
|
||||
import type { AnimatedEntity, Clip, EntityAnimator, EntityPose } from "@/common/mod_api/client.ts";
|
||||
import type { ClipJson } from "@/common/mod_data.ts";
|
||||
import type { EntityData } from "@/common/protocol.ts";
|
||||
import { EverythingRegistry } from "@/common/everything_registry.ts";
|
||||
import { wrap_angle } from "@/common/utils.ts";
|
||||
import { rest_offsets, sample_clip } from "./animation.ts";
|
||||
import type { BoneOffsets } from "./model_math.ts";
|
||||
|
||||
export interface AnimatorRegistration {
|
||||
mod: string;
|
||||
create: (entity: AnimatedEntity) => EntityAnimator;
|
||||
}
|
||||
|
||||
// by entity type
|
||||
export const entity_animators = new Map<string, AnimatorRegistration>();
|
||||
|
||||
// what an animator reads of its entity, a RemoteEntity's
|
||||
export interface AnimatorSource {
|
||||
readonly id: string;
|
||||
readonly type: string;
|
||||
readonly data: EntityData;
|
||||
// ticks since this client got it
|
||||
readonly age: number;
|
||||
readonly yaw: number;
|
||||
readonly head_yaw: number;
|
||||
readonly pitch: number;
|
||||
walk_animation(partial_tick: number): { position: number; speed: number };
|
||||
attack_animation(partial_tick: number): number;
|
||||
}
|
||||
|
||||
const DEGREES = 180 / Math.PI;
|
||||
|
||||
// one entity's animator, undefined for a type without one
|
||||
export function animation_for(source: AnimatorSource): EntityAnimation | undefined {
|
||||
const registration = entity_animators.get(source.type);
|
||||
return registration && new EntityAnimation(source, registration);
|
||||
}
|
||||
|
||||
export class EntityAnimation {
|
||||
#source: AnimatorSource;
|
||||
#mod: string;
|
||||
#entity: AnimatedEntity;
|
||||
#animator: EntityAnimator | undefined;
|
||||
|
||||
constructor(source: AnimatorSource, { mod, create }: AnimatorRegistration) {
|
||||
this.#source = source;
|
||||
this.#mod = mod;
|
||||
this.#entity = animated_entity(source);
|
||||
this.#run("create", () => this.#animator = create(this.#entity));
|
||||
for (const field of Object.keys(client_data(source.data))) this.#on_data(field);
|
||||
}
|
||||
|
||||
// its client data was before and is now the source's
|
||||
data_changed(before: EntityData) {
|
||||
const old = client_data(before);
|
||||
const now = client_data(this.#source.data);
|
||||
for (const field of new Set([...Object.keys(old), ...Object.keys(now)])) {
|
||||
if (JSON.stringify(old[field]) !== JSON.stringify(now[field])) this.#on_data(field);
|
||||
}
|
||||
}
|
||||
|
||||
// what the animator does to the bones at time, undefined when there's no animator (anymore)
|
||||
pose(time: number): Map<string, BoneOffsets> | undefined {
|
||||
const animator = this.#animator;
|
||||
if (!animator) return undefined;
|
||||
const offsets = new Map<string, BoneOffsets>();
|
||||
const posed = this.#run("pose", () => animator.pose(entity_pose(offsets, this.#entity), time));
|
||||
return posed ? offsets : undefined;
|
||||
}
|
||||
|
||||
#on_data(field: string) {
|
||||
const animator = this.#animator;
|
||||
if (animator?.on_data) this.#run("on_data", () => animator.on_data!(field, this.#source.age));
|
||||
}
|
||||
|
||||
// false when it threw, which is logged with the mod and the entity, and the animator is gone
|
||||
#run(what: string, fn: () => void) {
|
||||
try {
|
||||
fn();
|
||||
return true;
|
||||
} catch (e) {
|
||||
const { type, id } = this.#source;
|
||||
console.error(`[${this.#mod}] the animator of ${type} ${id} threw in ${what}, it's removed:`, e);
|
||||
this.#animator = undefined;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function client_data(data: EntityData): Record<string, unknown> {
|
||||
return (data.client_data ?? {}) as Record<string, unknown>;
|
||||
}
|
||||
|
||||
function animated_entity(source: AnimatorSource): AnimatedEntity {
|
||||
// time is in ticks with the part of the next one, the source's walk and attack take that part
|
||||
const partial = (time: number) => Math.min(Math.max(time - source.age, 0), 1);
|
||||
return {
|
||||
get id() {
|
||||
return source.id;
|
||||
},
|
||||
get type() {
|
||||
return source.type;
|
||||
},
|
||||
get_client_data: (field) => structuredClone(client_data(source.data)[field]) as never,
|
||||
walk_position: (time) => source.walk_animation(partial(time)).position,
|
||||
walk_speed: (time) => source.walk_animation(partial(time)).speed,
|
||||
attack_progress: (time) => source.attack_animation(partial(time)),
|
||||
// mirrored like a model's rotations, see model_math's own_matrix
|
||||
get head_pitch() {
|
||||
return -source.pitch * DEGREES;
|
||||
},
|
||||
get head_yaw() {
|
||||
return -wrap_angle(source.head_yaw - source.yaw) * DEGREES;
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
// a clip by what ClientRender.clip gave
|
||||
function clip_json(clip: Clip): ClipJson {
|
||||
const json = EverythingRegistry.get<ClipJson>("animations", clip?.id);
|
||||
if (!json) throw new Error(`${clip?.id} isn't a clip, see ctx.render.clip`);
|
||||
return json;
|
||||
}
|
||||
|
||||
function entity_pose(offsets: Map<string, BoneOffsets>, entity: AnimatedEntity): EntityPose {
|
||||
const offset = (bone: string) => {
|
||||
let found = offsets.get(bone);
|
||||
if (!found) {
|
||||
found = rest_offsets();
|
||||
offsets.set(bone, found);
|
||||
}
|
||||
return found;
|
||||
};
|
||||
const pose: EntityPose = {
|
||||
play(clip, seconds, weight = 1) {
|
||||
sample_clip(clip_json(clip), seconds, offsets, weight);
|
||||
},
|
||||
play_state(clip, state, time, weight) {
|
||||
if (state.started) pose.play(clip, state.seconds(time), weight);
|
||||
},
|
||||
play_walk(clip, walker, time, speed_scale = 1) {
|
||||
pose.play(clip, walker.walk_position(time) / 20, Math.min(walker.walk_speed(time) * speed_scale, 1));
|
||||
},
|
||||
rotate(bone, x, y, z) {
|
||||
const rotation = offset(bone).rotation;
|
||||
rotation[0] += x;
|
||||
rotation[1] += y;
|
||||
rotation[2] += z;
|
||||
},
|
||||
move(bone, x, y, z) {
|
||||
const position = offset(bone).position;
|
||||
position[0] += x;
|
||||
position[1] += y;
|
||||
position[2] += z;
|
||||
},
|
||||
scale(bone, x, y, z) {
|
||||
const scale = offset(bone).scale;
|
||||
scale[0] *= x;
|
||||
scale[1] *= y;
|
||||
scale[2] *= z;
|
||||
},
|
||||
look(bone) {
|
||||
pose.rotate(bone, entity.head_pitch, entity.head_yaw, 0);
|
||||
},
|
||||
};
|
||||
return pose;
|
||||
}
|
||||
@@ -1,10 +1,11 @@
|
||||
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
// entities mods define (entities/<name>.json), drawn with their entity model and a texture from the atlas. there are no
|
||||
// animation files yet: the model turns to the entity's yaw, a bone named "head" 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
|
||||
// entities mods define (entities/<name>.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";
|
||||
@@ -14,7 +15,7 @@ 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 { IDENTITY, model_pivot, multiply, rotation_around, type Vec3 } from "./model_math.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";
|
||||
|
||||
@@ -44,8 +45,15 @@ export function render_mod_entity(entity: RemoteEntity, camera: Camera, partial_
|
||||
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);
|
||||
const pose = (bone: EntityBoneJson) => {
|
||||
return (bone: EntityBoneJson) => {
|
||||
let matrix = IDENTITY;
|
||||
if (bone.name === "head") {
|
||||
matrix = rotation_around(model_pivot(bone), entity.pitch, entity.head_yaw - entity.yaw, 0);
|
||||
@@ -54,7 +62,6 @@ export function render_mod_entity(entity: RemoteEntity, camera: Camera, partial_
|
||||
if (limb) matrix = multiply(rotation_around(model_pivot(bone), limb, 0, 0), matrix);
|
||||
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
|
||||
|
||||
@@ -5,15 +5,15 @@
|
||||
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, multiply, own_matrix, type Vec3 } from "./model_math.ts";
|
||||
import { apply, type Matrix, multiply, own_matrix, type Vec3 } from "./model_math.ts";
|
||||
import { push_box, type VertexSink } 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. emit gets the vertices, the batch by default
|
||||
// bones is each bone's matrix, see bone_matrices. 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. emit gets the vertices, the batch by default
|
||||
export function push_model(
|
||||
model: EntityModelJson,
|
||||
pose: (bone: EntityBoneJson) => Matrix,
|
||||
bones: Map<string, Matrix>,
|
||||
atlas: Texture,
|
||||
region: SpriteRegion,
|
||||
to_world: (x: number, y: number, z: number) => Vec3,
|
||||
@@ -25,11 +25,10 @@ export function push_model(
|
||||
(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;
|
||||
for (const cube of bone.cubes) {
|
||||
const matrix = multiply(matrices.get(bone.name)!, own_matrix(cube));
|
||||
const matrix = multiply(bones.get(bone.name)!, own_matrix(cube));
|
||||
const transform = (px: number, py: number, pz: number) => to_world(...apply(matrix, px, py, pz));
|
||||
const inflate = cube.inflate ?? 0;
|
||||
const [ox, oy, oz] = cube.origin;
|
||||
|
||||
@@ -11,7 +11,7 @@ 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 { 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";
|
||||
@@ -34,7 +34,7 @@ export function push_worn(worn_items: ItemStack[], pose: PlayerPose, tint?: Vec3
|
||||
if (!model) continue;
|
||||
push_model(
|
||||
model,
|
||||
worn_bone_pose(pose),
|
||||
bone_matrices(model, worn_bone_pose(pose)),
|
||||
atlas,
|
||||
get_sprite_region(look.texture),
|
||||
to_world,
|
||||
@@ -59,6 +59,7 @@ export function render_worn_arm(player: Player, transform: (x: number, y: number
|
||||
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);
|
||||
const bones = bone_matrices(model, () => IDENTITY);
|
||||
push_model(model, bones, atlas, get_sprite_region(look.texture), to_arm, tint, only);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user