Cartoon: estudo de handoff do personagem (.studies) e AGENTS.md
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
215faa331c
commit
b923132a69
@@ -0,0 +1,87 @@
|
||||
// Boca acompanhando a fala SEM perder a expressão. O formato vem das letras da palavra que está sendo dita
|
||||
// (tempos de cada palavra da transcrição): "a" abre, "e/i" esticam, "o/u" arredondam, "m/b/p" fecham, "f/v"
|
||||
// mostram os dentes; vogal dura mais que consoante e as letras vizinhas se misturam (coarticulação). Se houver
|
||||
// o volume do áudio quadro a quadro (`env`), ele fecha a boca nas pausas e abre mais nas sílabas fortes.
|
||||
// A expressão continua por cima: os cantos (sorriso/tristeza), as sobrancelhas e os olhos são dela.
|
||||
import type { FaceParams } from "./rig";
|
||||
|
||||
export type Viseme = { open: number; wide: number; round: number; teeth: boolean };
|
||||
export type SpokenWord = { text: string; startS: number; endS: number };
|
||||
|
||||
const CLOSED: Viseme = { open: 0, wide: 0, round: 0, teeth: false };
|
||||
const V: Record<string, Viseme> = {
|
||||
a: { open: 0.85, wide: 0.3, round: 0, teeth: false },
|
||||
e: { open: 0.55, wide: 0.65, round: 0, teeth: true },
|
||||
i: { open: 0.3, wide: 0.95, round: 0, teeth: true },
|
||||
o: { open: 0.65, wide: 0, round: 0.75, teeth: false },
|
||||
u: { open: 0.35, wide: 0, round: 1, teeth: false },
|
||||
mbp: { open: 0, wide: 0.1, round: 0, teeth: false },
|
||||
fv: { open: 0.12, wide: 0.2, round: 0, teeth: true },
|
||||
cons: { open: 0.3, wide: 0.3, round: 0, teeth: true },
|
||||
};
|
||||
|
||||
function visemeOf(ch: string): Viseme {
|
||||
if ("aáàâã".includes(ch)) return V.a;
|
||||
if ("eéê".includes(ch)) return V.e;
|
||||
if ("iíy".includes(ch)) return V.i;
|
||||
if ("oóôõ".includes(ch)) return V.o;
|
||||
if ("uúü".includes(ch)) return V.u;
|
||||
if ("mbp".includes(ch)) return V.mbp;
|
||||
if ("fv".includes(ch)) return V.fv;
|
||||
return V.cons;
|
||||
}
|
||||
const isVowel = (ch: string) => /[aáàâãeéêiíyoóôõuúü]/.test(ch);
|
||||
|
||||
function mix(a: Viseme, b: Viseme, k: number): Viseme {
|
||||
return { open: a.open + (b.open - a.open) * k, wide: a.wide + (b.wide - a.wide) * k, round: a.round + (b.round - a.round) * k, teeth: k < 0.5 ? a.teeth : b.teeth };
|
||||
}
|
||||
|
||||
/** Formato da boca no instante `t` (segundos do trecho) para as palavras dadas; fechada fora das palavras. */
|
||||
export function visemeAt(words: SpokenWord[], t: number, env?: { values: number[]; fps: number }): Viseme {
|
||||
const w = words.find((x) => t >= x.startS && t < x.endS);
|
||||
if (!w) {
|
||||
// Fim de palavra: fecha em ~60 ms em vez de bater a boca.
|
||||
const prev = [...words].reverse().find((x) => x.endS <= t);
|
||||
return prev && t - prev.endS < 0.06 ? mix(V.cons, CLOSED, (t - prev.endS) / 0.06) : CLOSED;
|
||||
}
|
||||
const letters = [...w.text.toLowerCase().replace(/(ch|lh|nh|qu)/g, "c").replace(/[^a-záàâãéêíóôõúüçy]/g, "")];
|
||||
if (!letters.length) return CLOSED;
|
||||
// Vogal pesa o dobro da consoante no tempo da palavra.
|
||||
const weights = letters.map((ch) => (isVowel(ch) ? 1.7 : 0.8));
|
||||
const total = weights.reduce((a, b) => a + b, 0);
|
||||
let pos = ((t - w.startS) / Math.max(0.04, w.endS - w.startS)) * total;
|
||||
let i = 0;
|
||||
while (i < letters.length - 1 && pos > weights[i]) { pos -= weights[i]; i++; }
|
||||
const frac = Math.min(1, pos / weights[i]);
|
||||
const cur = visemeOf(letters[i]);
|
||||
const next = i + 1 < letters.length ? visemeOf(letters[i + 1]) : CLOSED;
|
||||
// Coarticulação: a 2ª metade da letra já caminha para a próxima.
|
||||
let v = frac > 0.5 ? mix(cur, next, (frac - 0.5) * 0.9) : cur;
|
||||
if (env?.values.length) {
|
||||
const a = env.values[Math.min(env.values.length - 1, Math.max(0, Math.floor(t * env.fps)))] ?? 0;
|
||||
const k = Math.max(0, Math.min(1.15, (a - 0.03) / 0.3));
|
||||
v = { ...v, open: v.open * (0.25 + 0.75 * Math.min(1, k)) * (k < 0.05 ? 0.2 : 1) };
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
/** Ênfase (0–1) no instante: o volume perto do pico levanta um pouco as sobrancelhas. */
|
||||
export function emphasisAt(t: number, env?: { values: number[]; fps: number }): number {
|
||||
if (!env?.values.length) return 0;
|
||||
const a = env.values[Math.min(env.values.length - 1, Math.max(0, Math.floor(t * env.fps)))] ?? 0;
|
||||
return Math.max(0, Math.min(1, (a - 0.45) / 0.4));
|
||||
}
|
||||
|
||||
/** Põe a fala na boca mantendo a expressão (cantos, sobrancelhas, olhos). `k` = quanto da fala entra (0–1). */
|
||||
export function speak(face: FaceParams, v: Viseme, k = 1, emphasis = 0): FaceParams {
|
||||
const open = Math.max(face.mouthOpen * 0.35, v.open * 0.9);
|
||||
return {
|
||||
...face,
|
||||
mouthOpen: face.mouthOpen + (open - face.mouthOpen) * k,
|
||||
mouthWidth: face.mouthWidth * (1 + (v.wide * 0.22 - v.round * 0.45) * k),
|
||||
mouthRound: face.mouthRound + (v.round - face.mouthRound) * k,
|
||||
teeth: k > 0.5 ? v.teeth && v.open > 0.1 : face.teeth,
|
||||
browL: { ...face.browL, y: face.browL.y - emphasis * 3 * k },
|
||||
browR: { ...face.browR, y: face.browR.y - emphasis * 3 * k },
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user