Animators and coilhead animations

This commit is contained in:
2026-11-21 21:54:02 -03:00
parent 69ad4b80d0
commit 0c711b8163
21 changed files with 960 additions and 34 deletions
+73 -2
View File
@@ -327,8 +327,8 @@ where a face that's left out isn't drawn and a negative size flips it. Faces are
`entity_alphatest` material, so a flat cube needs only one textured face. A bone's parent has to come before it. The
model faces -z, 16 pixels to a block with its feet at 0, and shows the way Blockbench shows it.
There are no animation files yet. The whole model turns with the entity, and bones with Minecraft's humanoid names move
on their own:
The whole model turns with the entity. An entity type with an [animator](#animations) has its bones posed by that.
Without one, bones with Minecraft's humanoid names move on their own:
- `head` turns to where the entity looks.
- `right_arm`, `left_arm`, `right_leg` and `left_leg` swing as it walks, each arm with the opposite leg.
@@ -336,3 +336,74 @@ on their own:
Without a model, the whole texture is drawn facing the camera, 16 pixels to a block, centered on the middle of the
entity's box.
## Animations
Animations are keyframes made in Blockbench, played by code in the mod's client script. This is how NeoForge does it
too: JSON keyframes, applied by the model's own `setupAnim`. Bedrock's Molang expressions and animation controllers
aren't used, since the animator does both in TypeScript.
### Clips
`animations/<name>.animation.json` are animation files as Blockbench exports them for Bedrock. Each animation in one is
a clip, named after the file and the last part of the animation's name: `animation.coilhead.walk` in
`animations/coilhead.animation.json` is `<mod id>:coilhead/walk`. From each animation, bworld uses:
- `animation_length`, in seconds, or the last keyframe's time without it,
- `loop`: `true` loops. `false`, no `loop` and `"hold_on_last_frame"` all stay on the last frame after the clip's end,
- each bone's `rotation` (degrees, added to the bone's own), `position` (pixels) and `scale`, as one value for the whole
clip or keyframes by time. A keyframe is a number for all three axes, `[x, y, z]`, `{ "pre": ..., "post": ... }`, and
can be `"lerp_mode": "catmullrom"` for a smooth curve.
A value that's a Molang expression, like `"math.cos(query.life_time * 180) * 2.5"`, is an error: do it in the animator
with `pose.rotate` or `pose.move`. `deno task check-mods` warns about what the animator does instead, like
`sound_effects` and `anim_time_update`.
### Animators
The client phase registers an animator for an entity type. Each entity of that type the client has gets its own, and
before each frame it's drawn, the animator plays clips and moves bones. A coilhead that bobs as it walks, looks around,
and plays `stop` once when it freezes:
```ts
import { AnimationState, type ClientContext } from "cubi/client";
export function client(ctx: ClientContext) {
const walk = ctx.render.clip("my_mod:coilhead/walk");
const stop = ctx.render.clip("my_mod:coilhead/stop");
ctx.render.register_entity_animator("my_mod:coilhead", (coilhead) => {
const frozen = new AnimationState();
return {
on_data(field, time) {
if (field === "frozen") frozen.animate_when(coilhead.get_client_data("frozen") === true, time);
},
pose(pose, time) {
if (frozen.started && !frozen.finished(stop, time)) pose.play_state(stop, frozen, time);
else pose.play_walk(walk, coilhead, time);
pose.look("head");
},
};
});
}
```
- `time` is the entity's age on this client in ticks, with the part of the next one.
- `pose.play(clip, seconds, weight)` plays a clip at some point in it. Clips played together add up, and `weight` scales
one, so blending from one to another is turning one down while the other goes up.
- `pose.play_state(clip, state, time)` plays it from when an `AnimationState` started. An animator keeps one for each
clip it starts on its own, and starts and stops it with `start`, `stop` and `animate_when`.
- `pose.play_walk(clip, entity, time)` plays it as far through as the entity has walked, as strongly as it's walking.
- `pose.rotate`, `pose.move` and `pose.scale` add to a bone on top of the clips, in the clips' units.
- `pose.look(bone)` turns a bone to where the entity looks.
An animator that throws is removed, with the error in the console, and its entity goes back to the humanoid pose.
What the animator knows of the mob's state comes from its client data, which the server side sets:
```ts
mob.set_client_data("frozen", true);
mob.set_client_data("frozen", undefined); // removes it
```
Client data is saved with the mob and sent to players when it changes. The animator's `on_data` hears about each field
as it changes, and about the ones already set when the entity first shows up.