Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

docs(animation): frame sizes, offsets, and clips built from separate images

esengine13e601ca3704
diff --git a/desktop b/desktop
index 893d8f5ac..db040f8b3 160000
--- a/desktop
+++ b/desktop
@@ -1 +1 @@
-Subproject commit 893d8f5ac78c0b00215102c547c0175ae8e2cc54
+Subproject commit db040f8b36f8e448cbdcff80dae406156c463219
diff --git a/docs/astro/src/content/docs/animation/overview.mdx b/docs/astro/src/content/docs/animation/overview.mdx
index 95dc5425c..7e0e94e3a 100644
--- a/docs/astro/src/content/docs/animation/overview.mdx
+++ b/docs/astro/src/content/docs/animation/overview.mdx
@@ -94,13 +94,17 @@ const listen = defineSystem([Res(SpriteAnimation)], (anim) => {
Clips are `.esanim` assets you author visually — no JSON by hand:
-1. **Right-click a texture** in the Content Browser and choose **Create Sprite
- Animation**. The Flipbook editor opens with the sheet under a slicing grid
- (the cell size is guessed from the image; adjust cell width/height, margin,
- and spacing in the toolbar).
-2. **Click or drag across cells** to append frames. The frame strip below shows
- each frame's thumbnail with an editable duration (ms; empty = `1000 / FPS`),
- drag to reorder, and a live looping preview.
+1. Either **right-click a texture** and choose **Create Sprite Animation** —
+ the Flipbook editor opens with the sheet under a slicing grid (the cell size
+ is guessed from the image; adjust cell width/height, margin, and spacing in
+ the toolbar) — or **select several images** and choose **Create Sprite
+ Animation from N Images**, which lines them up in file-name order (counting
+ numerically, so `run_2` precedes `run_10`).
+2. **Click or drag across cells** to append frames, or **drop images onto the
+ timeline** to insert them. A block on the timeline is as wide as its frame is
+ long: drag its right edge to retime, drag the block itself to reorder. The
+ viewport draws the current frame at its true size against the clip's anchor,
+ with the neighbouring frames ghosted in their own places.
3. **Save**, then **right-click the `.esanim` → Create Animated Sprite** (or
drag it into the viewport). You get a `Sprite` + `SpriteAnimator` entity
posed at frame 0 — it plays in Play mode with zero code.
@@ -113,6 +117,44 @@ Re-slicing the grid moves every frame consistently — frames reference grid
cells, not pixel rectangles. Frames that fall outside the current grid are
flagged red in the strip.
+The **frame under the playhead** has its own section in the Details inspector —
+duration, size, offset, anchor — alongside the clip's fps, loop mode, and the
+two switches below.
+
+### Per-frame sizes and offsets
+
+A sheet's cells are all the same size, so one `Sprite.size` fits every frame of
+it. Separate images are not: a 96×64 attack frame and a 48×96 idle frame drawn
+at one size are both wrong. Tick **Frame Sizes** and the clip owns `Sprite.size`
+during playback — each frame draws at its own natural size (its texture's
+pixels, or its sheet cell), or at a `size` you give it.
+
+That also unlocks **offsets**. An offset shifts a frame's artwork, in the same
+units as the size, and is folded into the frame's anchor when the clip loads —
+so nothing downstream has to know about it. Authoring the shift in units rather
+than as a raw anchor is the point: the same offset is the same visual shift on
+frames of any size, which a normalized anchor cannot be. Drag the frame in the
+viewport to set one, or type it in the inspector.
+
+```jsonc
+// step.esanim — three images of different sizes, one of them nudged right
+{
+ "version": "1.5",
+ "fps": 6,
+ "frameSizing": "frame",
+ "pivot": { "x": 0.5, "y": 0 },
+ "frames": [
+ { "texture": "@uuid:…" },
+ { "texture": "@uuid:…", "offset": { "x": 20, "y": 0 } },
+ { "texture": "@uuid:…", "duration": 0.35 }
+ ]
+}
+```
+
+A clip that declares neither leaves `Sprite.size` exactly as the entity set it,
+so clips written before this existed play unchanged. Unticking the switch drops
+the mode and every per-frame size and offset, which is the way back to that.
+
### Per-frame anchors
An anchor is the point a sprite sits, rotates and scales about — `Sprite.pivot`,
@@ -179,11 +221,14 @@ const registerRun = defineSystem([Res(Assets), Res(SpriteAnimation)], async (ass
| Helper | Description |
|---|---|
| `createAnimClip(texture, cellWidth, cellHeight, pageWidth, pageHeight)` | A fresh sheet-sliced `AnimClipAssetData` (fps `12`, `loop: true`, margin/spacing `0`, no frames yet). |
-| `parseAnimClipData(name, data, textureHandles)` | Resolve asset data into a runtime `SpriteAnimClip` — each cell frame gets its texture handle and UV window. |
+| `createAnimClipFromTextures(textures)` | A fresh clip over a sequence of per-frame textures — no sheet, and owning size from the start. |
+| `parseAnimClipData(name, data, textureHandles, textureSizes?)` | Resolve asset data into a runtime `SpriteAnimClip` — each frame gets its texture handle, UV window, and (for a clip that owns size) the size it draws at. Pass `textureSizes` so a per-texture frame has a natural size to fall back on. |
| `parseAnimClipAsset(json)` / `serializeAnimClip(data)` | Parse / write the `.esanim` JSON document (the pair round-trips). |
| `animClipCellRect(sheet, cell)` / `animClipCellUv(sheet, cell)` | Pixel rect / UV window of a grid cell — what a `Sprite` shows for that frame. |
-| `animClipFramePivot(data, frame)` | The anchor a frame renders with — its own, else the clip's, else centered. `null` when the clip authors none (leave `Sprite.pivot` alone). |
+| `animClipFramePivot(data, frame, size?)` | The anchor a frame renders with — its own, else the clip's, else centered, with the frame's offset folded in when a `size` is given. `null` when the clip authors none (leave `Sprite.pivot` alone). |
| `animClipDrivesPivot(data)` | Whether the clip authors anchors at all. |
+| `animClipFrameSize(data, frame, natural?)` | The size a frame draws at — its own, else the natural one you pass. `null` when the clip does not own size (leave `Sprite.size` alone). |
+| `animClipDrivesSize(data)` | Whether the clip owns `Sprite.size` — set outright, or implied by any frame's size or offset. |
### Driving clips from a state machine — no code
diff --git a/docs/astro/src/content/docs/zh-cn/animation/overview.mdx b/docs/astro/src/content/docs/zh-cn/animation/overview.mdx
index d6f5282d5..18af005ad 100644
--- a/docs/astro/src/content/docs/zh-cn/animation/overview.mdx
+++ b/docs/astro/src/content/docs/zh-cn/animation/overview.mdx
@@ -90,10 +90,13 @@ const listen = defineSystem([Res(SpriteAnimation)], (anim) => {
剪辑是 `.esanim` 资产,全程可视化创作——不用手写 JSON:
-1. 在内容浏览器中**右键纹理**,选择**创建精灵动画**。Flipbook 编辑器打开,
- 精灵表上覆盖切片网格(格子尺寸按图片猜测;在工具栏调整格宽/格高/边距/间距)。
-2. **点击或拖选格子**追加帧。下方帧条显示每帧缩略图与可编辑时长
- (毫秒;留空 = `1000 / 帧率`),可拖拽重排,并带实时循环预览。
+1. 两条路:**右键一张纹理**选**创建精灵动画**——Flipbook 编辑器打开,精灵表上
+ 覆盖切片网格(格子尺寸按图片猜测;在工具栏调整格宽/格高/边距/间距);或者
+ **选中多张图片**,选**用 N 张图片创建精灵动画**,按文件名顺序排好
+ (按数字比较,所以 `run_2` 排在 `run_10` 前面)。
+2. **点击或拖选格子**追加帧,或**把图片拖到时间轴上**插入。时间轴上一个块有多宽
+ 就代表这帧有多长:拖它的右边缘改时长,拖块本身重排。视口把当前帧按真实尺寸
+ 画在剪辑锚点上,相邻帧则各就各位地叠印在后面。
3. **保存**,然后**右键 `.esanim` → 创建动画精灵**(或直接拖进视口)。
得到一个定格在第 0 帧的 `Sprite` + `SpriteAnimator` 实体——进 Play 零代码播放。
@@ -103,6 +106,40 @@ const listen = defineSystem([Res(SpriteAnimation)], (anim) => {
重新切片时所有帧一致跟随——帧引用的是网格格子,不是像素矩形。
超出当前网格的帧会在帧条中标红。
+**播放头所在的那一帧**在检视器里有自己的一节——时长、尺寸、偏移、锚点,
+和剪辑的帧率、循环模式以及下面这两个开关并排。
+
+### 逐帧尺寸与偏移
+
+精灵表的格子都一样大,所以整段剪辑共用一个 `Sprite.size` 是对的。多张独立
+图片不是:96×64 的攻击帧和 48×96 的待机帧按同一个尺寸画,两个都是错的。
+勾上**逐帧尺寸**,播放期间 `Sprite.size` 就归剪辑管——每帧按自己的自然尺寸
+(纹理像素,或所在格子)绘制,也可以给某帧写一个 `size` 覆盖。
+
+这同时解锁了**偏移**。偏移把某帧的画面挪开,单位与尺寸相同,加载时被折进
+该帧的锚点——所以下游没有任何一层需要认识这个字段。用单位而不是直接改锚点
+来表达位移,正是关键:同一个偏移在任意尺寸的帧上都是同样的视觉位移,
+归一化的锚点做不到这点。在视口里拖动这一帧即可设定,也可以在检视器里输入。
+
+```jsonc
+// step.esanim —— 三张尺寸不同的图片,其中一张往右挪
+{
+ "version": "1.5",
+ "fps": 6,
+ "frameSizing": "frame",
+ "pivot": { "x": 0.5, "y": 0 },
+ "frames": [
+ { "texture": "@uuid:…" },
+ { "texture": "@uuid:…", "offset": { "x": 20, "y": 0 } },
+ { "texture": "@uuid:…", "duration": 0.35 }
+ ]
+}
+```
+
+两者都没声明的剪辑会原封不动地保留实体自己设的 `Sprite.size`,所以在这个能力
+出现之前写的剪辑播放起来完全一样。取消勾选会清除该模式以及所有逐帧尺寸与偏移,
+回到这种「不插手」的行为。
+
### 逐帧锚点
锚点就是精灵的落点、旋转与缩放中心——也就是 `Sprite.pivot`,在帧内归一化,
@@ -165,11 +202,14 @@ const registerRun = defineSystem([Res(Assets), Res(SpriteAnimation)], async (ass
| 辅助函数 | 说明 |
|---|---|
| `createAnimClip(texture, cellWidth, cellHeight, pageWidth, pageHeight)` | 一个全新的按网格切片的 `AnimClipAssetData`(fps `12`、`loop: true`、边距/间距 `0`、暂无帧)。 |
-| `parseAnimClipData(name, data, textureHandles)` | 把资产数据解析成运行时 `SpriteAnimClip`——每个格子帧得到纹理句柄与 UV 窗口。 |
+| `createAnimClipFromTextures(textures)` | 一个全新的逐帧贴图剪辑——没有精灵表,且一开始就接管尺寸。 |
+| `parseAnimClipData(name, data, textureHandles, textureSizes?)` | 把资产数据解析成运行时 `SpriteAnimClip`——每帧得到纹理句柄、UV 窗口,以及(剪辑接管尺寸时)它的绘制尺寸。传 `textureSizes` 好让逐帧贴图有自然尺寸可回落。 |
| `parseAnimClipAsset(json)` / `serializeAnimClip(data)` | 解析 / 写出 `.esanim` JSON 文档(两者可往返)。 |
| `animClipCellRect(sheet, cell)` / `animClipCellUv(sheet, cell)` | 网格格子的像素矩形 / UV 窗口——即该帧 `Sprite` 显示的内容。 |
-| `animClipFramePivot(data, frame)` | 该帧实际渲染用的锚点——帧自己的 → 剪辑的 → 居中。剪辑没有声明锚点时返回 `null`(表示不要动 `Sprite.pivot`)。 |
+| `animClipFramePivot(data, frame, size?)` | 该帧实际渲染用的锚点——帧自己的 → 剪辑的 → 居中;给了 `size` 时把该帧的偏移折进去。剪辑没有声明锚点时返回 `null`(表示不要动 `Sprite.pivot`)。 |
| `animClipDrivesPivot(data)` | 该剪辑是否声明了锚点。 |
+| `animClipFrameSize(data, frame, natural?)` | 该帧的绘制尺寸——帧自己的 → 你传入的自然尺寸。剪辑不接管尺寸时返回 `null`(表示不要动 `Sprite.size`)。 |
+| `animClipDrivesSize(data)` | 该剪辑是否接管 `Sprite.size`——直接设定,或由任一帧的尺寸/偏移隐含。 |
### 用状态机驱动剪辑——零代码