Animeant 是做什么的?
一个角色是一棵 节点 树。每个节点具有相对于其父节点的局部变换。剪辑、IK、弹簧和布娃娃都写回相同的内容,关节旋转(以及根节点的位置),因此它们可以自由组合,骨骼永远不会拉伸。
| 包 | 用途 |
|---|---|
@animeant/core | 一切:节点、角色、IK、补间、剪辑、动画器、物理、2D 和 3D。零依赖,ESM。 |
@animeant/canvas | 在 <canvas> 上绘制 2D Figure |
@animeant/three | 用于 three.js 场景中 3D Figure3D 的 FigureView(three 是对等依赖) |
@animeant/mcp | MCP 服务器;见下文“使用 MCP 工具” |
约定(先弄对这些)
2D (Figure, Node) | 3D (Figure3D, Node3D) | |
|---|---|---|
| 单位 | 像素 | 米 |
| 向上 | -y(y 向下,屏幕空间) | +y |
| 向前 | 不适用 | +z;角色的左侧是 +x |
| 旋转 | rotation(弧度,屏幕上顺时针为正) | rx、ry、rz(弧度,XYZ 欧拉)或通过 setQuaternion 使用的四元数 |
| 通道 | x y rotation scaleX scaleY | x y z rx ry rz scaleX scaleY scaleZ |
| 限制 | limits: [min, max] | limits: { x?: [min, max], y?, z? };min === max 的轴被锁定 |
- 代码使用 弧度;剪辑可能指定
units: "deg";MCP 工具始终使用 度。 - 节点的
x/y(/z)是其相对于父关节的偏移量,因此骨骼的长度是子节点的偏移量。 - 关节限制由
turn、constrain、IK 和物理强制执行。剪辑不受限制。
配方
加载骨骼并摆姿势(FK)
import { Figure, humanoid, degToRad } from "@animeant/core";
const fig = Figure.fromRig(humanoid); // 节点:hips spine neck head shoulderL/R elbowL/R handL/R hipL/R kneeL/R footL/R hair1-4
fig.node("shoulderR").rotation = degToRad(-40); // 其下的所有内容都跟随
fig.node("elbowR").turn(degToRad(20)); // 相对变换,感知限制
const p = fig.node("handR").worldPosition; // {x, y},惰性重新计算
IK
fig.chain("handR").solve({ x: 240, y: 120 }); // shoulder→elbow→hand,FABRIK
fig.chain("footL", 2).solve(target, { solver: "ccd" }); // CCD 通常在限制严格时更好
fig.chain(["spine", "neck", "head"]).solve(lookAt); // 显式指定,基础项在前
// → { reached, error, iterations }
3D 版本在 Figure3D 上使用相同的 API。其默认求解器 "auto" 在链具有铰链(肘部、膝盖)时使用 CCD,否则使用 FABRIK。铰链保持为铰链。
剪辑和动画器
import { Animator, humanoidClips, type Clip } from "@animeant/core";
const wave: Clip = {
name: "wave", duration: 1.2, loop: true, units: "deg",
tracks: { elbowR: { rotation: [[0, -20, "inOutSine"], [0.6, -70, "inOutSine"], [1.2, -20]] } },
};
const animator = new Animator(fig);
animator.play(humanoidClips.idle); // 层级 0
animator.play(wave, { layer: 1, fade: 0.3 }); // 混合在层级 0 之上
// 每步:animator.update(dt)
- 关键帧 =
[time, value, ease?];缓动函数塑造了 到下一个关键帧 的片段。 - 缓动函数:
linear inQuad outQuad inOutQuad inCubic outCubic inOutCubic outQuint inSine outSine inOutSine outExpo outCirc outBack inOutBack outElastic step,或在代码中使用cubicBezier(x1,y1,x2,y2)。 - 同一层级:通过加权平均交叉淡入淡出。较高层级:按其权重混合在较低层级之上。
mode: "additive"会添加剪辑第一个关键帧的变化。 - 根节点的
x/y(/z)始终是叠加的(根运动)。如果宿主本身移动了根节点,请在update之前调用animator.rebase(fig.root)。 stop(layer?, fade?)淡出并返回基础姿势;clear()会丢弃剪辑 而不 改变姿势(在将角色交给布娃娃之前使用)。- 播放控制:
pb.pause() resume() seek(t),pb.speed,pb.weight,await pb.finished。
补间
const tweens = new Tweens();
tweens.to(fig.node("head"), { rotation: 0.3 }, { duration: 0.5, ease: "outBack" });
tweens.to(node3d, { rx: 0.4 }, { duration: 0.4, angles: ["rx"] }); // 3D:指定角度键
// 每步:tweens.update(dt)
循环
const ticker = new Ticker(); // 固定 1/120 秒步长
ticker.onFixed((dt) => { animator.update(dt); tweens.update(dt); hair.step(dt); });
ticker.onFrame(() => renderer.draw(fig));
ticker.start(); // 或 ticker.advance(dt) 用于测试/离线
建议的步骤顺序:定位根节点 → animator.update → IK → 补间 → 弹簧/物理。
物理(2D)
const hair = new SpringChain(fig.chain(["hair1", "hair2", "hair3", "hair4"]), { stiffness: 0.05, damping: 0.9 });
const ragdoll = new Ragdoll(fig, { bounds: { floor: 400 } });
animator.clear();
ragdoll.start({ x: 120, y: 0 }); // 可选的初始速度,单位/秒
ragdoll.grab(fig.node("handR"), pointer); ragdoll.drag(pointer); ragdoll.release();
// 每步:ragdoll.step(dt)
完全对称的姿势可能会保持平衡;ragdoll.impulse({ x: 40, y: 0 }, fig.node("head")) 会使其倾斜。
渲染
// 2D
const renderer = new CanvasRenderer(canvas); // 处理 devicePixelRatio
renderer.draw(fig, { focus: { node, amount: 1 }, floor: 400 });
// 3D
import { Figure3D, humanoid3d, humanoid3dClips } from "@animeant/core";
import { FigureView } from "@animeant/three";
const fig3 = Figure3D.fromRig(humanoid3d);
const view = new FigureView(fig3); // 球体 + 胶囊 + 头部
scene.add(view.object);
// 每帧,在角色改变之后:view.update();
// 拾取:view.pick(raycaster, ["handL", "handR"]) → 节点名称 | null
身体(来自骨骼的 3D 模型)
import { BodyView } from "@animeant/three";
const body = new BodyView(fig3); // 在静止时构建;读取 meta.body / meta.blobs / root meta.bodyColor
scene.add(body.object);
// 每帧:body.update(); 淡入淡出:body.opacity = 0.5(抖动,无需排序)
- 节点上的
meta.body: { r: [r0, r1], squash?: [side, front], color? }为其父节点到该节点的骨骼提供一个锥形胶囊。 meta.blobs: [{ r: [rx, ry, rz], at?: [x, y, z], rot?, color? }]将椭球体(头骨、躯干、耳朵、眼睛)附加到节点。- 带身体的预设:
dog3d(+dog3dClips.idle | trot | sit)和hominids.ape | australopithecus | habilis | erectus | sapiens,或使用hominid(params)创建自定义的。 hominidGait(rig, { stride, crouch, lean, armSwing, quadruped })→{ clip, height, speed }。在播放前设置fig.root.y = height;以speed米/秒移动角色,以保持脚掌着地。
骨骼 JSON
// 2D。一个节点没有父节点。偏移量为像素,旋转和限制为弧度。
{ "name": "arm", "nodes": [
{ "name": "shoulder", "x": 100, "y": 100 },
{ "name": "elbow", "parent": "shoulder", "x": 50, "limits": [-2.6, 0] },
{ "name": "hand", "parent": "elbow", "x": 45 },
{ "name": "tail1", "parent": "shoulder", "x": -10, "meta": { "boneWidth": 2 } }
]}
// 3D。米,Y 轴向上,面向 +Z。
{ "name": "leg", "dimensions": 3, "nodes": [
{ "name": "hip", "y": 0.9 },
{ "name": "knee", "parent": "hip", "y": -0.42, "limits": { "x": [0, 2.6], "y": [0, 0], "z": [0, 0] } },
{ "name": "foot", "parent": "knee", "y": -0.42 }
]}
meta 是自由格式的,供渲染器和工具使用;核心从不读取它。渲染器遵循的约定:boneWidth(0 隐藏骨骼,任何数字标记为装饰链)、hidden。
humanoid3d 符号,用于编写 3D 剪辑
对于悬垂的肢体,负 rx 会使其向前摆动。+rz 会将 左 肢体向侧面抬起,-rz 抬起 右 肢体。肘部以 负 rx 弯曲,膝盖以 正 rx 弯曲。脊柱、颈部和头部以 正 rx 向前弯曲。预设:humanoid3dClips.idle | walk | wave。
陷阱
- 2D y 轴符号错误。 向上是负值。头部在颈部上方时
y: -26。 - 在弧度字段中使用度。 骨骼 JSON 和节点属性是弧度;只有带有
units: "deg"的剪辑和 MCP 工具才使用度。 - 剪辑与 IK 冲突。 剪辑每一步都会重写它动画的通道。在
animator.update之后 运行 IK,或者将 IK 混合到剪辑之上。 - 在剪辑播放中间拖动关节无效。 原因相同。停止剪辑,或者在动画器之后使用 IK 驱动该关节。
- 3D IK 父节点上的非均匀缩放。 世界旋转忽略缩放;保持任何链通过的节点的缩放均匀。
- 期望剪辑尊重限制。 它们不会;
validate_clip(MCP)会警告关键帧超出限制。 Figure与Figure3D。 它们不能混用:Chain与Chain3D,CanvasRenderer与FigureView。Animator两者都支持。
使用 MCP 工具
如果连接了 animeant MCP 工具(create_figure、solve_ik……),请使用它们来检查工作,而不是猜测:
- 使用
preset: "humanoid"/"humanoid3d"或您的骨骼 JSON 调用create_figure。读取它返回的节点列表。 - 使用
set_pose(度)或solve_ik(世界目标)摆姿势。clampedToLimits会告诉您关节何时达到其限制。 - 查看它:
render_figure返回 SVG;对于 3D,传递view: "front" | "side" | "top" | "three-quarter"和markers来显示目标。 - 使用
build_clip(定时姿势 → 剪辑 JSON)创作运动。它会根据角色进行验证,因此修复任何错误或限制警告,然后使用sample_clip在几个时间点预览帧并渲染每个帧。 export_rig将当前姿势烘焙为静止姿势,用于保存新的静止姿势或自定义骨骼。
将 build_clip 中的剪辑 JSON 直接放入代码:animator.play(clipJson)。