Skip to content

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

Keep the mouth shape of live expressions

wtc-toshiaki1382c220235a
diff --git a/.changeset/expression-mouth-form.md b/.changeset/expression-mouth-form.md
new file mode 100644
index 0000000..d6d56c6
--- /dev/null
+++ b/.changeset/expression-mouth-form.md
@@ -0,0 +1,5 @@
+---
+'mesh-avatar': patch
+---
+
+Live expressions now carry their mouth shape as well as their mouth opening. While the expression's mouth is open wider than the voice or tracking, the surprise expression shows its "o" mouth instead of falling into the "a" region, at rest and in the quiet gaps of speech. Louder syllables keep the lip-sync vowel, and expressions without an open mouth leave the mouth shape unchanged.
diff --git a/packages/runtime/src/engine/expression-overlay.js b/packages/runtime/src/engine/expression-overlay.js
index a123f6d..3379caf 100644
--- a/packages/runtime/src/engine/expression-overlay.js
+++ b/packages/runtime/src/engine/expression-overlay.js
@@ -10,13 +10,13 @@ export const OVERLAY_EXPRESSIONS = {
// Brows are often hidden under bangs, so some faces also lean on gaze and head pitch to read.
const POSE = { sad: { pitch: -6, gy: -0.45 } };
const clamp = (v, a, b) => Math.max(a, Math.min(b, v));
-const NEUTRAL = Object.freeze({ eyeOpen: 1, eyeOpenL: 1, eyeSmile: 0, eyeSmileL: 0, blush: 0, browY: 0, browAngle: 0, mouthOpen: 0, gx: 0, gy: 0, pitch: 0 });
+const NEUTRAL = Object.freeze({ eyeOpen: 1, eyeOpenL: 1, eyeSmile: 0, eyeSmileL: 0, blush: 0, browY: 0, browAngle: 0, mouthOpen: 0, mouthForm: 0, gx: 0, gy: 0, pitch: 0 });
export function overlayTarget(name) {
const E = EXPRESSIONS[OVERLAY_EXPRESSIONS[name]]?.p ?? {}, pose = POSE[name] ?? {};
return { eyeOpen: E.eyeOpen ?? 1, eyeOpenL: E.eyeOpenL ?? E.eyeOpen ?? 1, eyeSmile: E.eyeSmile ?? 0,
eyeSmileL: E.eyeSmileL ?? E.eyeSmile ?? 0, blush: E.blush ?? 0, browY: E.browY ?? 0, browAngle: E.browAngle ?? 0,
- mouthOpen: E.mouthOpen ?? 0, gx: E.glance?.[0] ?? 0, gy: pose.gy ?? E.glance?.[1] ?? 0, pitch: pose.pitch ?? 0 };
+ mouthOpen: E.mouthOpen ?? 0, mouthForm: E.mouthForm ?? 0, gx: E.glance?.[0] ?? 0, gy: pose.gy ?? E.glance?.[1] ?? 0, pitch: pose.pitch ?? 0 };
}
/** Eyes scale with the expression so blinks still show; smiles, blush and brows add on top. */
@@ -30,6 +30,9 @@ export function applyOverlay(P, m) {
out.browY = clamp((P.browY ?? 0) + m.browY, -1, 1);
out.browAngle = clamp((P.browAngle ?? 0) + m.browAngle, -1, 1);
out.mouthOpen = Math.max(P.mouthOpen ?? 0, m.mouthOpen);
+ // While the expression's own mouth is open wider than the voice or tracking, show its mouth
+ // shape too (surprise is an "o"); louder syllables keep the lip-sync vowel.
+ if (m.mouthOpen > (P.mouthOpen ?? 0)) out.mouthForm = m.mouthForm;
out.gazeX = clamp((P.gazeX ?? 0) + m.gx, -1, 1);
out.gazeY = clamp((P.gazeY ?? 0) + m.gy, -1, 1);
out.angleY = clamp((P.angleY ?? 0) + m.pitch, -30, 30);
diff --git a/tests/expression-overlay.test.ts b/tests/expression-overlay.test.ts
index c38f952..7f18dcd 100644
--- a/tests/expression-overlay.test.ts
+++ b/tests/expression-overlay.test.ts
@@ -1,5 +1,6 @@
import { expect, test } from 'vitest';
import { ExpressionOverlay, OVERLAY_EXPRESSIONS, applyOverlay, overlayTarget } from '../packages/runtime/src/engine/expression-overlay.js';
+import { createSpriteModule } from '../packages/runtime/src/engine/sprites.js';
const tracked = { angleY: 0, eyeLOpen: 1, eyeROpen: 1, eyeSmile: 0, eyeSmileL: 0, mouthOpen: 0.7, mouthForm: 0.4, browY: 0.1, browAngle: 0, blush: 0, gazeX: 0.2, gazeY: 0 };
@@ -44,3 +45,43 @@ test('unknown names, including inherited object keys, fall back to neutral', ()
const overlay = new ExpressionOverlay();
for (const name of ['unknown', 'toString', '__proto__', 'constructor']) { overlay.set(name); expect(overlay.name).toBe('neutral'); }
});
+
+// Only the mouth drawing choice is used here, so the engine and rig are minimal stand-ins.
+const { mouthShape } = createSpriteModule({ baseWeights: () => [], deformBase: () => undefined, MOUTH: {} } as never, { mouth: { area: {} } } as never);
+
+test('surprise keeps its "o" mouth while its mouth is open wider than the voice', () => {
+ const surprise = overlayTarget('surprise');
+ expect(surprise.mouthForm).toBeGreaterThan(0.35);
+ // At rest and in the quiet gaps of speech the expression's mouth is wider, so its shape is shown.
+ for (const voice of [{ mouthOpen: 0, mouthForm: 0 }, { mouthOpen: 0.05, mouthForm: -0.8 }, { mouthOpen: 0.3, mouthForm: 0.1 }]) {
+ const out = applyOverlay({ ...tracked, ...voice }, surprise);
+ expect(out.mouthOpen).toBe(surprise.mouthOpen);
+ expect(out.mouthForm).toBe(surprise.mouthForm);
+ expect(mouthShape(out.mouthOpen, out.mouthForm)).toBe('o');
+ }
+ // Louder syllables keep the lip-sync vowel.
+ for (const form of [-0.8, -0.4, 0, 0.9]) {
+ const loud = applyOverlay({ ...tracked, mouthOpen: 0.85, mouthForm: form }, surprise);
+ expect(loud.mouthOpen).toBe(0.85); expect(loud.mouthForm).toBe(form);
+ }
+});
+
+test('expressions without an open mouth leave the mouth shape to the voice or tracking', () => {
+ for (const name of Object.keys(OVERLAY_EXPRESSIONS)) {
+ if (overlayTarget(name).mouthOpen > 0) continue;
+ for (const voice of [{ mouthOpen: 0, mouthForm: -0.6 }, { mouthOpen: 0.7, mouthForm: 0.4 }]) {
+ expect(applyOverlay({ ...tracked, ...voice }, overlayTarget(name)).mouthForm).toBe(voice.mouthForm);
+ }
+ }
+});
+
+test('the mouth shape fades with the expression and is released after switching back', () => {
+ const overlay = new ExpressionOverlay(), idle = { ...tracked, mouthOpen: 0, mouthForm: -0.3 };
+ overlay.set('surprise');
+ for (let i = 0; i < 120; i++) overlay.apply(idle, 1 / 60);
+ expect(overlay.apply(idle, 1 / 60).mouthForm).toBeCloseTo(overlayTarget('surprise').mouthForm, 3);
+ overlay.set('neutral');
+ for (let i = 0; i < 240 && overlay.active; i++) overlay.apply(idle, 1 / 60);
+ expect(overlay.active).toBe(false);
+ expect(overlay.apply(idle, 1 / 60).mouthForm).toBe(-0.3);
+});