From be024f225a77ca6980afe94ffef826c03694e96a Mon Sep 17 00:00:00 2001 From: DoodleBears Date: Thu, 8 Oct 2026 14:37:14 +0900 Subject: [PATCH] feat(avatar): add optional compatible speech mouth layers Issue: #6 Consumer: BotHarness/DeepSeekBot#1143 Agent-Task: codex/local/01a11734-bb09-7f01-9d45-adc0f5df6269 --- CHANGELOG.md | 1 + README.md | 2 + README.zh.md | 4 ++ packages/avatar/package.json | 2 +- packages/avatar/src/figure.ts | 58 ++++++++++++++++++++--------- packages/avatar/src/index.ts | 8 +++- packages/avatar/src/svg.ts | 5 ++- packages/avatar/test/speech.test.ts | 44 ++++++++++++++++++++++ 8 files changed, 103 insertions(+), 21 deletions(-) create mode 100644 packages/avatar/test/speech.test.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index 63d52da..fc95838 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Unreleased +- `@botharness/pixel-avatar` 0.2.0: opt-in `mouthLayers` adds saved/closed/half-open/open speech features to every head turn. Existing recipes, static snapshots and default golden output remain unchanged; text pacing stays with the consumer. - `@botharness/pixel-morph`: one morph that throws no longer stops the shared animation loop for the others, and a morph started from inside a frame no longer starts a second frame chain. ## 0.2.0 diff --git a/README.md b/README.md index ceda75b..68e7dce 100644 --- a/README.md +++ b/README.md @@ -51,6 +51,8 @@ The engine does not know what it is drawing. Here it morphs one generated face s ## Custom avatars +Pass `{ mouthLayers: true }` to `pixelAvatarSvg` or `pixelFigure` to opt into speech features. Each head pose contains four `[data-avatar-mouth]` groups: `saved`, `closed`, `half-open`, and `open`. Show one state in every pose at a time; return to `saved` when speech ends or is cancelled. Layers include shared brows, nose and cheeks so their paint order stays exact. Eyes, blink, glasses and head turns remain independent. Text pacing and reduced-motion policy belong to the consuming app. Without the option, every existing output stays byte-for-byte unchanged; recipes and saved snapshots do not change. + A recipe is plain JSON (`head`, `hair`, `eyes`, `outfit`, `accessory`, colours…). `AVATAR_PARTS`, `AVATAR_SWATCHES` and `AVATAR_PRESETS` list every option, so an app can build its own avatar editor on top; `isPixelAvatarRecipe` validates what users save. Hosts add their own state symbols with `symbolArtCells(rows, color)`, which draws 24×24 character art (`A` body, `H` highlight) in the built-in style. diff --git a/README.zh.md b/README.zh.md index e623344..605bbdb 100644 --- a/README.zh.md +++ b/README.zh.md @@ -12,4 +12,8 @@ 用法、原理和贡献方式见 [README.md](README.md)。 +## 说话嘴型 + +给 `pixelAvatarSvg` 或 `pixelFigure` 传入 `{ mouthLayers: true }`,即可按需生成嘴型图层。每个转头角度都包含四组 `[data-avatar-mouth]`:`saved`(原本的表情)、`closed`(闭合)、`half-open`(半开)、`open`(张开)。每个角度同时只显示一个状态;说话结束或取消时恢复 `saved`。图层保留共享的眉毛、鼻子和脸颊,以维持原有像素绘制顺序;眼神、眨眼、眼镜和转头保持独立。文本节奏与减少动效策略由使用方负责。不启用该选项时,现有输出逐字节保持不变,配方与已保存的快照不变。 + 已保存的头像不能变:`packages/avatar/test/fixtures/botharness-golden.json` 锁住了每个选项和种子的输出。 diff --git a/packages/avatar/package.json b/packages/avatar/package.json index 1f50321..6ab435a 100644 --- a/packages/avatar/package.json +++ b/packages/avatar/package.json @@ -1,6 +1,6 @@ { "name": "@botharness/pixel-avatar", - "version": "0.1.0", + "version": "0.2.0", "description": "Seeded, editable 32×32 chibi pixel avatars with state symbols that morph in and out", "keywords": [ "agent-ui", diff --git a/packages/avatar/src/figure.ts b/packages/avatar/src/figure.ts index cc0b09b..a0d3d1a 100644 --- a/packages/avatar/src/figure.ts +++ b/packages/avatar/src/figure.ts @@ -17,6 +17,10 @@ const TONGUE = '#ef6f84'; const GOLD = '#efb93f'; export type PixelGrid = Grid; +export type PixelMouthState = 'saved' | 'closed' | 'half-open' | 'open'; +export interface PixelFigureOptions { + mouthLayers?: boolean; +} const blank = (): Grid => Array.from({ length: N }, () => Array(N).fill(undefined)); const ellipse = (cx: number, cy: number, rx: number, ry: number): Mask => @@ -685,6 +689,7 @@ export function pixelTileColor(hair: string): string { export function pixelFigure( recipe: Recipe, yawDeg: number, + options: PixelFigureOptions = {}, ): { tile: string; body: string; head: string; cells: PixelCell[] } { const yaw = (yawDeg * Math.PI) / 180; const sinY = Math.sin(yaw); @@ -848,22 +853,41 @@ export function pixelFigure( if (recipe.nose === 'dot') sprite(features, ['N'], cx, 18 + h, { N: shade(skin, 0.78) }); if (recipe.nose === 'button') sprite(features, ['N'], cx, 18 + h, { N: shade(skin, 0.88) }); if (recipe.nose === 'line') sprite(features, ['N', 'N'], cx, 17 + h, { N: shade(skin, 0.82) }); - const mouth = MOUTHS[recipe.mouth]; const mx = C - 0.5 + fs * 0.6; - sprite(features, mouth, Math.round(mx - (mouth[0]!.length - 1) / 2), 20 + h, { - K: MOUTH, - M: MOUTH_INSIDE, - T: TONGUE, - W: WHITE, - }); - if (recipe.cheeks === 'blush') { - sprite(features, ['PP'], left.x - 1, 19 + h, { P: mix(BLUSH, skin, 0.25) }); - sprite(features, ['PP'], right.x + right.w - 1, 19 + h, { P: mix(BLUSH, skin, 0.25) }); - } - if (recipe.cheeks === 'freckles') { - sprite(features, ['F.F'], left.x, 19 + h, { F: shade(skin, 0.7) }); - sprite(features, ['F.F'], right.x + 1, 19 + h, { F: shade(skin, 0.7) }); - } + const withMouth = (mouth: readonly string[]) => { + const layer = features.map((row) => [...row]); + sprite(layer, mouth, Math.round(mx - (mouth[0]!.length - 1) / 2), 20 + h, { + K: MOUTH, + M: MOUTH_INSIDE, + T: TONGUE, + W: WHITE, + }); + if (recipe.cheeks === 'blush') { + sprite(layer, ['PP'], left.x - 1, 19 + h, { P: mix(BLUSH, skin, 0.25) }); + sprite(layer, ['PP'], right.x + right.w - 1, 19 + h, { P: mix(BLUSH, skin, 0.25) }); + } + if (recipe.cheeks === 'freckles') { + sprite(layer, ['F.F'], left.x, 19 + h, { F: shade(skin, 0.7) }); + sprite(layer, ['F.F'], right.x + 1, 19 + h, { F: shade(skin, 0.7) }); + } + return layer; + }; + const savedFeatures = withMouth(MOUTHS[recipe.mouth]); + const featureMarkup = options.mouthLayers + ? ( + [ + ['saved', MOUTHS[recipe.mouth]], + ['closed', ['KKK']], + ['half-open', ['KKK', 'MMM']], + ['open', ['.K.', 'KMK', '.K.']], + ] satisfies readonly [PixelMouthState, readonly string[]][] + ) + .map( + ([state, rows]) => + `${rects(withMouth(rows))}`, + ) + .join('') + : rects(savedFeatures); const frame = (fx: number, w: number, round: boolean) => paint( glasses, @@ -908,7 +932,7 @@ export function pixelFigure( const cells: PixelCell[] = []; for (let y = 0; y < N; y++) for (let x = 0; x < N; x++) { - const c = glasses[y]![x] ?? eyes[y]![x] ?? features[y]![x] ?? full[y]![x]; + const c = glasses[y]![x] ?? eyes[y]![x] ?? savedFeatures[y]![x] ?? full[y]![x]; if (c) cells.push({ x, y, c }); } return { @@ -917,7 +941,7 @@ export function pixelFigure( body: `${rects(bodyCells)}`, head: [ `${rects(headCells)}`, - `${rects(features)}`, + `${featureMarkup}`, `${rects(eyes)}`, `${rects(closed)}`, `${rects(glasses)}`, diff --git a/packages/avatar/src/index.ts b/packages/avatar/src/index.ts index aabef65..f717635 100644 --- a/packages/avatar/src/index.ts +++ b/packages/avatar/src/index.ts @@ -1,5 +1,11 @@ export type { PixelCell } from '@botharness/pixel-morph'; -export { pixelFigure, pixelTileColor, type PixelGrid } from './figure.js'; +export { + pixelFigure, + pixelTileColor, + type PixelGrid, + type PixelFigureOptions, + type PixelMouthState, +} from './figure.js'; export { AVATAR_COLORS, AVATAR_HAIR_PARTS, diff --git a/packages/avatar/src/svg.ts b/packages/avatar/src/svg.ts index a666214..30511b0 100644 --- a/packages/avatar/src/svg.ts +++ b/packages/avatar/src/svg.ts @@ -8,6 +8,7 @@ const YAW = { front: 0, left: -25, right: 25 } as const; export const AVATAR_TURNS = [-14, -7, 7, 14] as const; export interface PixelAvatarSvgOptions { + mouthLayers?: boolean; /** Extra yaw offsets to pre-render as hidden `[data-avatar-turn]` layers. */ turns?: readonly number[]; /** Class prefix for the rig layers (`-body`, `-head`, `-face`, `-gaze`, `-blink`). */ @@ -25,10 +26,10 @@ export function pixelAvatarSvg( options: PixelAvatarSvgOptions = {}, ): string { if (!isPixelAvatarRecipe(recipe)) throw new Error('invalid Avatar recipe'); - const base = pixelFigure(recipe, YAW[recipe.pose]); + const base = pixelFigure(recipe, YAW[recipe.pose], options); const turns = (options.turns ?? []) .map((delta) => { - const figure = pixelFigure(recipe, YAW[recipe.pose] + delta); + const figure = pixelFigure(recipe, YAW[recipe.pose] + delta, options); const layers = `${figure.body}${figure.head}`.replaceAll( 'class="bh-illustrated-', 'data-turn-part="', diff --git a/packages/avatar/test/speech.test.ts b/packages/avatar/test/speech.test.ts new file mode 100644 index 0000000..dd97c5a --- /dev/null +++ b/packages/avatar/test/speech.test.ts @@ -0,0 +1,44 @@ +import { describe, expect, it } from 'vitest'; +import { + AVATAR_PARTS, + AVATAR_PRESETS, + AVATAR_TURNS, + DEFAULT_RECIPE, + detailedRecipe, + pixelAvatarSvg, + type PixelAvatarRecipe, +} from '../src/index.js'; + +describe('optional speech mouth rig', () => { + it('provides saved, closed, half-open and open feature layers at every head turn', () => { + const svg = pixelAvatarSvg(DEFAULT_RECIPE, { turns: AVATAR_TURNS, mouthLayers: true }); + for (const state of ['saved', 'closed', 'half-open', 'open']) { + expect(svg.match(new RegExp(`data-avatar-mouth="${state}"`, 'gu'))).toHaveLength(5); + } + expect(svg).toContain('data-avatar-mouth="saved" opacity="1"'); + expect(svg).toContain('data-avatar-mouth="open" opacity="0"'); + expect(pixelAvatarSvg(DEFAULT_RECIPE)).not.toContain('data-avatar-mouth'); + }); + + it('restores exact saved markup across catalog faces, poses and detailed geometry', () => { + const recipes: PixelAvatarRecipe[] = [ + ...AVATAR_PRESETS, + ...AVATAR_PARTS.mouth.flatMap((mouth) => + AVATAR_PARTS.pose.flatMap((pose) => [ + { ...DEFAULT_RECIPE, mouth, pose }, + { ...detailedRecipe(DEFAULT_RECIPE), mouth, pose, height: 1, spacing: -1 }, + ]), + ), + ]; + for (const recipe of recipes) { + const snapshot = JSON.stringify(recipe); + const normal = pixelAvatarSvg(recipe, { turns: AVATAR_TURNS }); + const speaking = pixelAvatarSvg(recipe, { turns: AVATAR_TURNS, mouthLayers: true }); + const restored = speaking + .replace(/.*?<\/g>/gu, '') + .replace(/(.*?)<\/g>/gu, '$1'); + expect(restored).toBe(normal); + expect(JSON.stringify(recipe)).toBe(snapshot); + } + }); +});