Skip to content

Turn one illustration into an animated 2D mesh avatar with a coding agent and a local editor

Switch overlay layers on and off in the Studio preview

shinshin86cbe84a5211fd
diff --git a/README.ja.md b/README.ja.md
index 3ecde88..995a93d 100644
--- a/README.ja.md
+++ b/README.ja.md
@@ -78,7 +78,7 @@ npm run create -- --random --prompt "銀髪のショートヘア、制服"
### 2. プロジェクトを開いて確認する
-エディタを起動し(`npm run dev`)、「プロジェクトを開く」の一覧から作ったプロジェクトを選びます。一覧の鉛筆ボタンを押すと、プロジェクトの名前を `projects/` のフォルダ名ごと変えられます。「ポーズ確認」で顔の向きや傾き、目の開閉を確かめ、「口の動き」では「ゆったり」「キビキビ」などを選んで口パクを試します。「細かく調整する」で口の大きさや閉じ方を調整し、音声ファイルでも確認できます。
+エディタを起動し(`npm run dev`)、「プロジェクトを開く」の一覧から作ったプロジェクトを選びます。一覧の鉛筆ボタンを押すと、プロジェクトの名前を `projects/` のフォルダ名ごと変えられます。眼鏡などの重ねるレイヤー(`built/overlays/`)があるアバターでは、プレビューの「重ねるレイヤー」で表示を切り替えられます。「ポーズ確認」で顔の向きや傾き、目の開閉を確かめ、「口の動き」では「ゆったり」「キビキビ」などを選んで口パクを試します。「細かく調整する」で口の大きさや閉じ方を調整し、音声ファイルでも確認できます。
![口の動きの確認](docs/images/ja/03-lip-sync.png)
diff --git a/README.md b/README.md
index 7e9169c..3e4b559 100644
--- a/README.md
+++ b/README.md
@@ -104,6 +104,8 @@ npm run create -- --random --prompt "silver short hair, school uniform"
Start the editor (`npm run dev`), choose **Open project** and pick your project from the list.
The pencil next to a project renames it, together with its folder under `projects/`.
+If the avatar has overlay layers such as glasses (`built/overlays/`), the preview lists them
+under **Overlays** so you can switch them on and off.
In the preview, use **Pose test** to turn and tilt the head and close the eyes. In **Lip sync**,
choose a mouth style and try it with kana text. Open **Fine-tune** to adjust movement, size
and closing, or preview a local audio file.
diff --git a/README.zh.md b/README.zh.md
index 7632df0..f6821c7 100644
--- a/README.zh.md
+++ b/README.zh.md
@@ -77,7 +77,7 @@ npm run create -- --random --prompt "银色短发,校服"
### 2. 打开项目并检查
-启动编辑器(`npm run dev`),选择**打开项目**,从列表中挑选你的项目。点击项目旁的铅笔按钮可以重命名项目,`projects/` 下的文件夹也会一起改名。在预览里使用**姿态测试**转头、倾斜头部、闭眼。在**嘴型测试**里选择口型样式,输入假名文本试试效果。打开**高级设置**调整动作幅度、大小和闭合度,或预览本地音频文件。
+启动编辑器(`npm run dev`),选择**打开项目**,从列表中挑选你的项目。点击项目旁的铅笔按钮可以重命名项目,`projects/` 下的文件夹也会一起改名。如果虚拟形象带有眼镜等叠加层(`built/overlays/`),可以在预览的**叠加层**中切换显示。在预览里使用**姿态测试**转头、倾斜头部、闭眼。在**嘴型测试**里选择口型样式,输入假名文本试试效果。打开**高级设置**调整动作幅度、大小和闭合度,或预览本地音频文件。
![嘴型检查](docs/images/en/03-lip-sync.png)
diff --git a/e2e/projects-lipsync.spec.ts b/e2e/projects-lipsync.spec.ts
index 56ec879..6869eca 100644
--- a/e2e/projects-lipsync.spec.ts
+++ b/e2e/projects-lipsync.spec.ts
@@ -1,7 +1,8 @@
import { expect, test, type Page } from '@playwright/test';
-import { cp, mkdir, mkdtemp, readFile, rm } from 'node:fs/promises';
+import { cp, mkdir, mkdtemp, readFile, rm, writeFile } from 'node:fs/promises';
import { resolve, basename, join } from 'node:path';
import { createHash } from 'node:crypto';
+import { PNG } from 'pngjs';
import { request as httpRequest } from 'node:http';
import fixture from '../samples/miko-qipao/rig.json' with { type: 'json' };
import { dismissGuide, samplePresent, sampleSkipReason } from './sample';
@@ -192,6 +193,37 @@ test('the edit button renames a project in place, keeps it open and follows it i
await expect(page.getByTestId('project-location')).toContainText(renamed);
});
+test('overlay layers are listed in the preview and switch on and off', async ({ page }) => {
+ // a full-size transparent image with one opaque block over the face
+ const { width, height } = fixture.image, overlay = new PNG({ width, height });
+ const [x0, y0] = [Math.round(fixture.face.nose.cx) - 60, Math.round(fixture.face.nose.cy) - 20];
+ for (let y = y0; y < y0 + 40; y++) for (let x = x0; x < x0 + 120; x++) overlay.data.set([20, 20, 20, 255], (y * width + x) * 4);
+ await mkdir(join(directory, 'built/overlays'), { recursive: true });
+ await writeFile(join(directory, 'built/overlays/glasses.png'), PNG.sync.write(overlay));
+ await writeFile(join(directory, 'built/overlays/overlays.json'), JSON.stringify({ version: 1, overlays: [{ name: 'glasses', file: 'glasses.png' }] }));
+ await open(page, name);
+ const tools = page.getByTestId('overlay-tools'), toggle = tools.getByRole('button', { name: 'glasses', exact: true });
+ await expect(tools).toContainText('Overlays');
+ await expect(toggle).toHaveAttribute('aria-pressed', 'false');
+ const hash = async () => createHash('sha256').update(await page.getByTestId('preview').screenshot()).digest('hex');
+ await page.getByTestId('idle-toggle').first().click();
+ // Switching redraws the paused preview, so the reference is a redrawn frame with the overlay hidden.
+ await toggle.click(); await toggle.click();
+ await expect(toggle).toHaveAttribute('aria-pressed', 'false');
+ let plain = '';
+ await expect.poll(async () => { const first = await hash(); await page.waitForTimeout(500); plain = await hash(); return first === plain; }, { timeout: 15_000 }).toBe(true);
+ await toggle.click();
+ await expect(toggle).toHaveAttribute('aria-pressed', 'true');
+ await expect.poll(hash).not.toBe(plain);
+ await toggle.click();
+ await expect(toggle).toHaveAttribute('aria-pressed', 'false');
+ await expect.poll(hash).toBe(plain);
+ // projects without overlays show no overlay controls
+ await page.locator('.open-menu > summary').click(); await page.getByTestId('project-sample-miko-qipao').click();
+ await expect(page.getByTestId('preview-status')).toHaveAttribute('data-state', 'ready');
+ await expect(page.getByTestId('overlay-tools')).toHaveCount(0);
+});
+
test('a ?project= address opens that project instead of the recent one and is then removed', async ({ page }) => {
await open(page, 'sample-miko-qipao');
await page.goto(`/?project=${name}`);
diff --git a/src/editor/Preview.tsx b/src/editor/Preview.tsx
index e5d930c..0732e84 100644
--- a/src/editor/Preview.tsx
+++ b/src/editor/Preview.tsx
@@ -38,6 +38,20 @@ export function Preview({ projectKey, rig, assets, lipSync, onLipSyncChange, has
const [liveMouth, setLiveMouth] = useState(0);
const [tab, setTab] = useState<'pose' | 'lip'>('pose');
const [lightingOpen, setLightingOpen] = useState(false);
+ // Overlay layers (built/overlays, such as glasses) are listed from the avatar and start hidden.
+ const [overlayNames, setOverlayNames] = useState<string[]>([]);
+ const [shownOverlays, setShownOverlays] = useState<string[]>([]);
+ const shownRef = useRef(shownOverlays); shownRef.current = shownOverlays;
+ useEffect(() => {
+ let cancelled = false;
+ setOverlayNames([]); setShownOverlays([]);
+ const url = assets?.['overlays/overlays.json'];
+ if (url) fetch(url).then(response => response.json()).then(sheet => {
+ if (!cancelled && Array.isArray(sheet?.overlays)) setOverlayNames(sheet.overlays.map((item: { name?: unknown }) => item?.name).filter((name: unknown): name is string => typeof name === 'string'));
+ }).catch(() => undefined);
+ return () => { cancelled = true; };
+ }, [assets]);
+ useEffect(() => { avatar.current?.setOverlays(shownOverlays); avatar.current?.advance(0); setRevision(current => current + 1); }, [shownOverlays]);
const skipped = skippedKanaCharacters(text).join(' ');
const controls = useRef({ idle, stress, parameters });
controls.current = { idle, stress, parameters };
@@ -52,6 +66,7 @@ export function Preview({ projectKey, rig, assets, lipSync, onLipSyncChange, has
instance = value;
avatar.current = value;
value.setLighting(lightRef.current);
+ value.setOverlays(shownRef.current);
const control = controls.current;
value.setAutoIdle(control.idle && !control.stress);
value.setAutoMotion(control.idle && !control.stress);
@@ -120,6 +135,11 @@ export function Preview({ projectKey, rig, assets, lipSync, onLipSyncChange, has
<button className="icon-button" aria-label={idle ? t.pause : t.play} title={idle ? t.pause : t.play}
data-testid={tab === 'pose' ? 'idle-toggle' : undefined} aria-pressed={idle} onClick={() => setIdle(current => !current)}><Icon name={idle ? 'pause' : 'play'} /></button><span>{t.idle}</span>
</div>
+ {overlayNames.length > 0 && <div className="overlay-tools" role="group" aria-label={t.overlays} data-testid="overlay-tools">
+ <span>{t.overlays}</span>
+ {overlayNames.map(name => <button key={name} aria-pressed={shownOverlays.includes(name)} title={`${t.overlayToggle}: ${name}`}
+ onClick={() => setShownOverlays(current => current.includes(name) ? current.filter(item => item !== name) : [...current, name])}>{name}</button>)}
+ </div>}
<div className="preview-tabs" role="tablist" aria-label={t.preview}>
<button id="pose-tab" role="tab" aria-selected={tab === 'pose'} aria-controls="pose-panel" onClick={() => setTab('pose')}><Icon name="pose" />{t.pose}</button>
<button id="lip-tab" role="tab" aria-selected={tab === 'lip'} aria-controls="lip-panel" onClick={() => setTab('lip')}><Icon name="mouth" />{t.lipSync}</button>
diff --git a/src/editor/i18n-zh.ts b/src/editor/i18n-zh.ts
index eada4ff..e003647 100644
--- a/src/editor/i18n-zh.ts
+++ b/src/editor/i18n-zh.ts
@@ -24,6 +24,7 @@ export const uiZh = {
lineHint: '拖动控制点 · 双击线段添加点 · Alt + 点击控制点删除',
panHint: '双指捏合缩放 · 滚动或拖动空白处平移', noDots: '在选中部件的面板中调整数值',
preview: '实时预览', idle: '待机动作', play: '播放待机动作', pause: '暂停待机动作',
+ overlays: '叠加层', overlayToggle: '显示或隐藏叠加层',
pose: '姿态测试', reset: '重置', sweep: '连续测试角度', stopSweep: '停止角度测试',
lipSync: '嘴型测试', release: '松开嘴型', lipText: '日语假名文本', lipPlay: '播放', lipStop: '停止',
lipSpeed: '每秒音拍数', lipLoop: '循环', skippedKana: '已跳过的字符:', lipHelp: '支持日语假名和空格(あ=a、い=i、う=u、え=e、お=o)。仅预览嘴型,不播放声音。',
diff --git a/src/editor/i18n.tsx b/src/editor/i18n.tsx
index 239f362..99fd34d 100644
--- a/src/editor/i18n.tsx
+++ b/src/editor/i18n.tsx
@@ -32,6 +32,7 @@ const en = {
lineHint: 'Drag dots · double-click a line to add a dot · Alt-click a dot to remove it',
panHint: 'Pinch to zoom · scroll or drag empty space to pan', noDots: 'Edit the values in the selected part card',
preview: 'Live preview', idle: 'Idle motion', play: 'Play idle motion', pause: 'Pause idle motion',
+ overlays: 'Overlays', overlayToggle: 'Show or hide the overlay',
pose: 'Pose test', reset: 'Reset', sweep: 'Sweep angles', stopSweep: 'Stop sweep',
lipSync: 'Lip sync', release: 'Release', lipText: 'Kana text', lipPlay: 'Play', lipStop: 'Stop',
lipSpeed: 'Morae per second', lipLoop: 'Loop', skippedKana: 'Skipped characters:', lipHelp: 'Hiragana, katakana and spaces. Preview only; no audio.',
@@ -79,6 +80,7 @@ const ja: typeof en = {
lineHint: '点をドラッグ · 線をダブルクリックで点を追加 · ⌥+点をクリックで削除',
panHint: 'ピンチで拡大・縮小 · スクロールや余白のドラッグで移動', noDots: '右の選択パーツ欄で数値を調整',
preview: '動きのプレビュー', idle: '待機中の動き', play: '待機中の動きを再生', pause: '待機中の動きを一時停止',
+ overlays: '重ねるレイヤー', overlayToggle: 'レイヤーの表示を切り替え',
pose: 'ポーズ確認', reset: 'リセット', sweep: '角度を連続確認', stopSweep: '連続確認を停止',
lipSync: '口の動き', release: '解除', lipText: 'かなの文章', lipPlay: '再生', lipStop: '停止',
lipSpeed: '1秒あたりの拍数', lipLoop: '繰り返し', skippedKana: '読み飛ばす文字:', lipHelp: 'ひらがな・カタカナ・空白に対応。音声は再生しません。',
diff --git a/src/editor/style.css b/src/editor/style.css
index ef6da6b..3a3dd96 100644
--- a/src/editor/style.css
+++ b/src/editor/style.css
@@ -289,6 +289,7 @@ main:has(.stale) .editor-panel, main:has(.stale) .parts-panel { height: calc(100
.preview-panel:has(#lip-tab[aria-selected="true"]) > .panel-title,
.preview-panel:has(#lip-tab[aria-selected="true"]) > .lighting-preview,
.preview-panel:has(#lip-tab[aria-selected="true"]) > .preview-tools,
+ .preview-panel:has(#lip-tab[aria-selected="true"]) > .overlay-tools,
.preview-panel:has(#lip-tab[aria-selected="true"]) > .preview-tabs { flex-shrink: 0; }
.preview-panel:has(#lip-tab[aria-selected="true"]) > .lip-sync,
.preview-panel:has(#lip-tab[aria-selected="true"]) > .lighting-section { min-height: 0; overflow-y: auto; scrollbar-width: thin; }
@@ -297,6 +298,10 @@ main:has(.stale) .editor-panel, main:has(.stale) .parts-panel { height: calc(100
}
.preview-tools[hidden] { display: none; }
+.overlay-tools { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; padding: 6px 16px; border-top: 1px solid var(--line); font-size: 12px; color: var(--muted); }
+.overlay-tools button { padding: 3px 10px; border-radius: 999px; font-size: 12px; color: var(--ink); }
+.overlay-tools button[aria-pressed="true"] { color: white; background: var(--accent); border-color: var(--accent); }
+.overlay-tools button[aria-pressed="true"]:hover:not(:disabled) { color: white; background: #1d4ed8; }
.preview-panel .lip-idle-toggle { width: 20px; height: 20px; padding: 2px; border: 0; color: var(--accent); }
#lip-panel .lip-live { margin: 0; }
.audio-file-picker { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; }