Communitygithub.com

animeant

使用 Animeant 建立 2D 和 3D 骨骼動畫,這是一個以節點為基礎、優先使用程式碼的 TypeScript 函式庫。

animeant 是什麼?

Animeant 是一個專為 2D 和 3D 骨骼動畫設計的 TypeScript 函式庫,採用節點樹結構與局部變換,支援動作片段(clip)、反向運動學(IK)、彈簧鏈(spring chain)與布偶物理(ragdoll)等功能。其核心套件 `@animeant/core` 無任何外部依賴,並提供 `@animeant/canvas` 與 `@animeant/three` 作為渲染選擇,分別支援 Canvas 2D 與 three.js 3D 場景。開發者可以透過 JSON 定義骨架(rig)與動作片段,並結合動畫控制器(Animator)進行圖層混合、淡入淡出與播放控制。此外,Animeant 也提供 MCP(Model Context Protocol)工具,讓 AI agent(如 Claude Code 或 Cursor)能直接透過工具呼叫來建立角色、調整姿勢、解決 IK 限制,以及匯出動作片段。這使得 AI 輔助的程式碼生成與視覺除錯流程更加流暢。

相容平台~Claude Code~Codex CLI~Cursor
npx skills add https://github.com/HarshitKumar9030/animeant/tree/HEAD/skills/animeant

在你喜歡的 AI 中提問

開啟一個已預先載入此 Agent Skill 的新對話。

說明文件

animeant 是做什麼的?

一個角色(figure)是一棵由節點組成的樹。每個節點都有一個相對於其父節點的局部變換。動作片段、IK、彈簧和布偶物理最後都會寫入相同的東西:關節旋轉(以及根節點的位置),因此它們可以自由組合,且骨骼永遠不會拉伸。

套件用途
@animeant/core一切:節點、角色、IK、補間、動作片段、動畫控制器、物理、2D 與 3D。零依賴,ESM。
@animeant/canvas在 <canvas> 上繪製 2D Figure
@animeant/three在 three.js 場景中為 3D Figure3D 提供 FigureView(three 為 peer 依賴)
@animeant/mcpMCP 伺服器;請參閱「使用 MCP 工具」

慣例(先搞懂這些)

2D(Figure, Node)3D(Figure3D, Node3D)
單位像素公尺
向上方向-y(y 向下,螢幕空間)+y
向前方向無+z;角色的左邊是 +x
旋轉rotation(弧度,螢幕上正值為順時針)rx, ry, rz(弧度,XYZ 尤拉角)或透過 setQuaternion 使用四元數
通道x y rotation scaleX scaleYx 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?];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 / 根節點 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 vs Chain3D、CanvasRenderer vs FigureView。Animator 兩者都支援。

使用 MCP 工具

如果 animeant MCP 工具已連接(create_figure、solve_ik 等),請使用它們來檢查工作,而不是憑空猜測:

  1. 使用 create_figure 搭配 preset: "humanoid" / "humanoid3d" 或您的骨架 JSON。閱讀它返回的節點列表。
  2. 使用 set_pose(度數)或 solve_ik(世界目標)擺姿勢。clampedToLimits 會告訴您何時關節達到了限制。
  3. 查看它: render_figure 返回 SVG;對於 3D,傳入 view: "front" | "side" | "top" | "three-quarter" 和 markers 來顯示目標點。
  4. 使用 build_clip(時間點姿勢 → 片段 JSON)創作動作。它會根據角色進行驗證,因此請修正任何錯誤或限制警告,然後使用 sample_clip 在幾個時間點預覽影格並渲染每個影格。
  5. export_rig 將當前姿勢烘焙為靜止姿勢,用於儲存新的靜止姿勢或自訂骨架。

將 build_clip 產生的片段 JSON 直接放入程式碼中:animator.play(clipJson)。

相關技能