CharAI - 2D 角色动画引擎 是做什么的?
CharAI 是一个 2D 角色动画引擎。你编写一个 .char 脚本(谁、做什么、何时)和一个皮肤文件(角色的外观);引擎提供骨架、运动、头发和衣服物理、口型同步和接触检测。它只绘制人物:没有场景,没有 3D,仅限侧视图。所有命令都从引擎文件夹(包含本文件的文件夹)中运行。
设置(三个命令)
node --version # v18 或更高
node tools/check.cjs examples/hello.char # 最后一行: OK: 0 errors, N warning(s). Wrote examples\hello.html
node tools/sheet.cjs examples/hello.char hello.png --n=8 # 最后一行: ... 8 frames of 8s (no page errors)
如果第二个命令打印 OK: 并且第三个命令写入 PNG,则表示成功了。Sheet、render、export 和 cast 需要 puppeteer-core 以及 Chrome 或 Edge(MP4 还需要 ffmpeg);仅当第三个命令因 puppeteer 失败时,运行一次 npm install。检查工具不需要任何依赖。
选择你的任务
| 用户说 | 你需要做 |
|---|---|
| "制作一个 10 秒的女孩挥手短片" | 编写一个影片 |
| "一个留胡子的胖厨师"、"一个拄拐杖的老人" | 设计一个角色 |
| "让他做一个侧手翻"、"她应该捂脸" | 添加一个动作(先执行 ls actions/) |
| "他们拥抱"、"递过一个面包" | 编写一个影片(使用双人动作和道具调用) |
| "把角色放到我的页面上"、"MP4"、"透明视频" | 嵌入或导出 |
编写一个影片
- 选择演员。皮肤:
tess、kai;在examples/skins/中:tot(幼儿)、baker、captain、duchess、gramps、skye、vera。道具:ball box chair door lantern loaf thorn。 - 创建
films/<name>/film.char。将你使用的任何皮肤复制到films/<name>/skins/。 - 编写头部,然后是带时间的行(如下)。从
examples/skill-demo.char开始。 - 检查:
node tools/check.cjs films/<name> --timeline。成功:最后一行OK: 0 errors。修复每个 ERROR;阅读每个 WARNING。--timeline打印+n的实际结束时间。 - 预览:
node tools/sheet.cjs films/<name> sheet.png --n=12,然后打开sheet.png。成功:每个演员都可见,没有重叠,显示结束卡片。 - 渲染:
node tools/render.cjs films/<name> out.mp4 --fps=24。成功:wrote out.mp4: N frames。
examples/skill-demo.char:
film "Fresh loaf" 14s # 标题,长度
stage width=1400 ground=600 scale=60 # 空舞台:没有场景
actor Baker = skin baker at 700 facing left
actor Tot = skin tot at 150
prop loaf at 850 # 一个道具放在 x=850 处
0.0 cam: cut(to=500, zoom=1.4) # 绝对时间
0.0 Baker: mood(happy), idle() # 两个通道,一行
0.0 Tot: face(happy)
0.5 Tot: run(to=420)
0.5 Baker: say("Hello, little one!", for=1.8) # 根据文本口型同步
0.5 caption: "Hello, little one!", by=Baker, for=1.8 # 一个对话框
+0 Baker: give(with=Tot, prop=loaf, speed=2.5) # 双人动作:一行驱动两个角色
+0 Tot: face(proud)
2.0 cam: frame(on=Tot+Baker, over=1.2) # 两个角色的特写镜头
+0.5 Tot: wave() # Tot 上一行结束后 0.5 秒
9.5 cam: home(over=1.0) # 离开特写
9.5 cam: pan(to=600, over=1.0)
11.6 card: "Fresh loaf" # 结束卡片
语法
每行一个语句;# 开始注释;名称区分大小写。
| 头部行 | 含义 |
|---|---|
film "Title" 12s | 第一行:标题,长度 |
stage width=2400 ground=600 scale=68 | 空舞台(显示默认值);scale = 每头身高的像素数 |
set hillside [dusk] | 场景;变体 day dawn dusk night |
actor N = skin S at X [facing left] [scale=1] | 一个角色。X = 像素值、锚点或名称 |
puppet N = bird at X | 动物:bird bear lamb |
prop K [as N] [at X] | 物体;不带 at 时,在首次被持有后才出现 |
时间行:<时间> <目标>: call(args), call(args)。时间是绝对秒数(4.5)或 +n = 在同一个目标的上一行结束后 n 秒。一个目标的各行时间必须递增。目标:演员或木偶名称、cam、caption、card、wind、clock。参数:位置参数(wave(both))或 key=value;文本用 "引号"。
通道:同一行的调用同时开始,且必须使用不同的通道。演员通道:body(一次只能一个;用 +0 开始下一个 body 行)、arms、face、mood、look、say;除 body 外,所有通道都可以在行走时同时进行。相机通道:pos(pan follow cut)、zoom、shake、fade、frame(frame home)。
| 分组 | 调用(必需参数在前) |
|---|---|
| 移动 | walk(to, speed=1) run(to, speed=1, end=stop|skid) sneak(to) enter(from=left) jump(to, height=1) turn() bounce(times=3) |
| 姿势 | idle() stand() kneel() crouch() sit(on=0) recoil() |
| 手势 | wave(hand=near|far|both) point(at, for=1.5) reach(at, for=1.2) cheer() bow() shrug() clap(times=4) dance() |
| 情绪 | mood(name, strength=1) neutral happy sad angry scared tired proud shy;face(mood, strength=1) 额外支持 excited surprised determined |
| 面部、语音 | look(at=front) 一个名称或 up down front back;nod();say("text", for=0, beats=0) |
| 双人 | hug(with) handshake(with) highfive(with) pat(with) shove(with) give(with, prop) throwto(with, prop);speed= 或 for= 设置时长 |
| 道具 | pickup(prop) putdown() hold(prop, how=front|up|side) drop() sit(on=chair) knock(on=door) |
| 相机 | cam: pan(to, over=1.5) follow(target) cut(to, zoom=0) zoom(to, over=1) shake(amount=8) fade(dir=out|in) frame(on, top=head, bottom=thigh, x=0.35) home() |
| 文本 | caption: "text", for=3, by=Name, at=top caption: clear card: "text", for=0, sub="..." |
| 世界 | wind: gust(amount=1, over=1.5) wind: blow(amount=0.3) |
frame 的层级:head chin chest waist hip thigh knee feet。完整表格:docs/AUTHORING.md。
设计一个角色
一个角色就是一个皮肤文件:在 mannequin 身体之上,由数字和一些 SVG 路径组成。
- 将模板复制到
films/<film>/skins/mia.js。文件名、name和 key 是同一个词。 - 根据表格设置
build和motion。此时不要绘制任何内容。 - 检查:
node tools/skincheck.cjs films/<film>/skins/mia.js。成功:OK: 0 errors, 0 warnings。 - 预览:
node tools/cast.cjs films/<film>/skins/mia.js mia.png,打开mia.png:六个姿势(站立、走路、跑步、跳跃、挥手、坐下)的完整人物。 - 按以下顺序一次添加一项,每项后重复步骤 3 和 4:调色板(
suit= 主要服装颜色,否则默认灰色会显示在颈部、肩膀、臀部)、只有在生成的头不对时才添加头、头发(hide: ['hair'],你自己的路径或链子)、衣物、配饰。 - 如果你绘制自己的头部,还要提供
swaps.mouth(至少open、closed)。胡须是位于parts.head中 y 0.32 以下的独立路径。测试:node tools/say.cjs shapes.png --skin=mia --shapes。
| 人物 | build | motion |
|---|---|---|
| 幼儿 | { heads: 2.3 } | { age: 'baby', energy: 0.8, weight: 0.2 } |
| 儿童 | { heads: 3.5 } | { age: 'child', energy: 0.8 } |
| 成人 | { heads: 7 } | { weight: 0.4, energy: 0.6 } |
| 肥胖成人 | { heads: 5.6, belly: 0.55, chest: 1.05, waist: 1.0, hips: 0.98, seat: 0.1, arm: 0.17, leg: 0.3 } | { weight: 0.9, energy: 0.35 } |
| 高瘦成人 | { heads: 8, chest: 0.72, waist: 0.52, hips: 0.78, arm: 0.1, leg: 0.16 } | { weight: 0.1, energy: 0.5 } |
| 老人 | { heads: 6.6, belly: 0.14, arm: 0.11, leg: 0.19 } | { age: 'elder', posture: 0.7, energy: 0.3 } |
范围:heads 2.2 到 8;arm 0.05 到 0.3;leg 0.1 到 0.45;chest, waist, hips 0.35 到 1.3;belly 0 到 0.7。路径单位:一个头高,面向右侧,+y 向上。用数字而非衣物修复身体问题。这个皮肤通过了 skincheck:
// mia.js: a tall woman in a teal dress with a ponytail
(function (g) {
(g.CharSkins = g.CharSkins || {}).mia = { name: 'mia', extends: 'mannequin', // 一个完整人物
build: { heads: 6.5 }, // 以头为单位的身高
motion: { weight: 0.3, energy: 0.6 }, // 0 轻..1 重;0 疲倦..1 活泼
palette: { skin: '#d9a07a', hair: '#2b1d16', shoe: '#3a2a2a', suit: '#2f6f6a', dress: '#2f6f6a' },
parts: {
torso: { cover: [0.3, 1.1], fill: 'dress' }, // 上身;到髋线为止
skirt: { skirt: true, length: 0.5, flare: 0.2, fill: 'dress' }, // 髋部以下:裙子
arm: { cover: [0, 0.3], flat: 'end', fill: 'dress' } // 手臂的前 30%
},
chains: { // 摆动
pony: { bone: 'head', at: [-0.45, 0.8], rest: 190, seg: [0.25, 0.22],
width: [0.15, 0.13, 0.04], fill: 'hair', layer: 'back', stiff: 0.35 } // width: 段数 + 1 个数字
} };
})(typeof self !== 'undefined' ? self : this);
每个 fill 是调色板名称或 '#rrggbb'。swap 仅用在 parts.head 中;服装使用 cover 或 skirt;保留 outline、springs、grip、handZ 不变。
添加一个动作
- 运行
ls actions/;将最接近的文件复制到films/<film>/actions/<name>.js(或actions/<name>.js)。 - 编写 2 到 4 个关键帧;
t是秒数且必须递增。使用标志点(['chin', 0.05, 0.05]= 向前、向上,以头高为单位),而不是原始角度,这样同一个文件可以适用于幼儿和高个子。 - 检查:
node tools/actioncheck.cjs actions/<name>.js。成功:OK: 0 errors, 0 warning(s)。 - 预览:
node tools/action.cjs <name> out.png --skin=tot,baker,vera(对于影片自己的动作,添加--film=<film>)。成功:所有三个角色都显示合理的姿势。 - 在影片中使用:
2.0 Mia: <name>()。
actions/ponder.js(通过了 actioncheck):
(function (g) { (g.CharActions = g.CharActions || {}).ponder = { // key = 文件名
channel: 'arms', // 'arms' = 在身体上的手势;'body' = 整个身体
doc: '手托下巴并缓慢点头(hold = 在下巴处停留的秒数)',
args: { hold: 1 }, // 脚本:Mia: ponder(hold=2)
face: 'determined', // 播放时的情绪表情
contact: true, // 手有意接触头部
keys: [
{ t: 0.4, handNear: 'chin', hands: 'relaxed', head: 8 }, // 手到下巴,头低垂
{ t: 0.8, handNear: ['chin', 0.05, 0.05], head: 12 },
{ t: 1.2, handNear: 'chin', head: 4, hold: true }, // hold: true = 要读取的姿势
] }; })(typeof self !== 'undefined' ? self : this);
标志点:top forehead eye mouth chin ear backhead neck chest belly hip back seat knee ground front up out down wall wallHigh wallLow。双人动作文件(pair: true,角色 a 和 b):docs/PAIRS.md。所有关键帧字段:docs/ACTIONS.md。
嵌入或导出
| 目标 | 命令或代码片段 |
|---|---|
| 网页 | node tools/build.cjs,然后 <script src="dist/char.js"></script><char-stage width="1280" height="720" skins="skins/kai.js" autoplay loop><script type="text/plain">film "x" 4s ...</script></char-stage> |
| 页面拥有的时钟 | const m = Char.mount(el, { script, skins: CharSkins, width: 1280, height: 720 }); m.seek(t);(先加载皮肤文件) |
| MP4 | node tools/render.cjs films/<name> out.mp4 --fps=24 |
| 透明 WebM | node tools/export.cjs films/<name> out.webm --format=webm --fps=30 |
| ProRes 4444 | node tools/export.cjs films/<name> out.mov --format=mov |
| PNG 序列 | node tools/export.cjs films/<name> frames --format=png |
| 动画 SVG | node tools/svgloop.cjs films/<name> out.svg --fps=10 --crop=0,360,1280,360 |
| 配音轨 | node tools/render.cjs films/<name> out.mp4 --audio=voice.wav |
透明输出需要是一个空舞台的影片(一个 set 行会绘制场景)。保持宿主的地面线与脚本的 ground= 一致。Remotion、HyperFrames 和 GSAP 的代码片段未经测试:docs/HOSTS.md。
在说完成之前检查这些
-
node tools/check.cjs打印OK: 0 errors,并且已阅读每个警告。 - 你打开了 sheet PNG(影片)或 cast PNG(皮肤)并查看过。
- 没有角色在动作期间出现在画框外。
- 执行双人动作后,没有人站在另一个人体内。
- 最后一行在影片长度之前结束(
--timeline),且长度符合要求。 - 皮肤通过
skincheck,动作通过actioncheck;编辑src/后,node --test tests/*.cjs通过。
当检查失败时
| 消息(缩略) | 修复 |
|---|---|
Tot has no action 'wallk'. Did you mean 'walk'? | 使用建议的名称;消息列出了所有有效的。 |
this starts at 7.00s but the film is 6s long | 提高 film 行中的长度或将行提前。 |
Tot's walk from line 5 runs until 6.79s, but this line starts run at 2.00s and cuts it short | 以 +0 开始该行(或在 6.8 处)。 |
unknown skin 'zed'. Available skins: ... | 将皮肤文件复制到 films/<name>/skins/ 或修正名称。 |
unknown target 'Bob'. Did you mean 'Tot'? | 声明 actor Bob = skin ... at ... 或修正名称。 |
Tot is outside the frame ... Pan the camera to Tot first | 在该行之前添加 cam: follow(Tot) 或 cam: pan(to=Tot)。 |
Kai: walk(to=5000) is off the stage; the stage is 1400 wide. Write a place from 0 to 1400, for example walk(to=1400). | 将 to= 移入舞台内(或加宽 stage width=)。 |
Kai: hug needs a partner. Write hug(with=Tess). Actors it could be: Tess. | 为每个双人动作(hug, handshake, give, ...)添加 with=<actor>。 |
say: the text must be in quotes, got hello. Write: 1 Kai: say("hello") | 将 say、caption 和 card 的文本放在 "引号" 中。 |
Tess: face: 'happyy' is not a face name. Nearest valid name is 'happy', so write face(happy). Valid: neutral, happy, ... | 使用列出的名称;mood(...) 同理。 |
colour 'teal' is not in the palette | 将 teal: '#rrggbb' 添加到皮肤的 palette。 |
'pinky' is not a landmark(actioncheck) | 使用上面列表中的标志点。 |
警告不会使检查失败,但 camera wants x=5000 but stops at x=760 表示脚本有误;planted foot slides 在 3 到 13 像素之间是已知的。
限制和已知问题
...
[翻译已截断]