168 lines
9.1 KiB
TypeScript
168 lines
9.1 KiB
TypeScript
// Rig do personagem: o rosto e o corpo são PARÂMETROS contínuos, não desenhos prontos. Uma expressão ou uma
|
||
// pose é só um conjunto de valores; dá para misturar (blend), ajustar um valor ("sorriso 0,3 a mais") e criar
|
||
// novas sem mexer no desenho. Ângulos em graus; "para fora" positivo nos dois lados (o desenho espelha).
|
||
|
||
export type FaceParams = {
|
||
/** Sobrancelha: y (px; negativo sobe) e tilt (graus; positivo = ponta de dentro sobe, como triste/preocupado). */
|
||
browL: { y: number; tilt: number };
|
||
browR: { y: number; tilt: number };
|
||
/** Abertura dos olhos (0 fechado · 1 normal · 1,35 arregalado). */
|
||
eyeL: number;
|
||
eyeR: number;
|
||
/** Tamanho da pupila (0,4 pavor · 1 normal · 1,3 encantado). */
|
||
pupil: number;
|
||
/** Direção do olhar (-1…1). */
|
||
gazeX: number;
|
||
gazeY: number;
|
||
/** Boca: abertura 0…1, sorriso -1 (triste) … 1 (feliz), largura 0,5…1,4, dentes à mostra. */
|
||
mouthOpen: number;
|
||
smile: number;
|
||
mouthWidth: number;
|
||
/** Arredondamento dos lábios (0 normal … 1 bico de "o/u"): vem da fala (lipsync.ts). */
|
||
mouthRound: number;
|
||
/** Pálpebra de cima somada à de base do personagem (−0,5 arregala … 0,6 quase fecha). */
|
||
lid: number;
|
||
teeth: boolean;
|
||
/** Extras: rubor 0…1, gota de suor, lágrimas, veia de raiva. */
|
||
blush: number;
|
||
sweat: boolean;
|
||
tears: boolean;
|
||
anger: boolean;
|
||
};
|
||
|
||
export type Limb = {
|
||
/** Abertura a partir do corpo (0 = para baixo; 90 = horizontal para fora; 180 = para cima). */
|
||
raise: number;
|
||
/** Dobra do cotovelo/joelho (positivo = antebraço/canela para dentro, na frente do corpo). */
|
||
bend: number;
|
||
};
|
||
|
||
export type BodyParams = {
|
||
/** Inclinação do tronco (graus; positivo = para a direita de quem olha). */
|
||
lean: number;
|
||
/** Cabeça: inclinação (graus), virada (-1…1, finge 3/4) e aceno (-1 olha para cima … 1 para baixo). */
|
||
headTilt: number;
|
||
headTurn: number;
|
||
headNod: number;
|
||
armL: Limb;
|
||
armR: Limb;
|
||
legL: Limb;
|
||
legR: Limb;
|
||
/** Agachar/sentar: 0 em pé … 1 sentado (quadril desce, coxa encurta em perspectiva). */
|
||
crouch: number;
|
||
};
|
||
|
||
export const NEUTRAL_FACE: FaceParams = {
|
||
browL: { y: 0, tilt: 0 }, browR: { y: 0, tilt: 0 }, eyeL: 1, eyeR: 1, pupil: 1, gazeX: 0, gazeY: 0,
|
||
mouthOpen: 0, smile: 0.1, mouthWidth: 1, mouthRound: 0, lid: 0, teeth: false, blush: 0, sweat: false, tears: false, anger: false,
|
||
};
|
||
|
||
export const NEUTRAL_BODY: BodyParams = {
|
||
lean: 0, headTilt: 0, headTurn: 0, headNod: 0,
|
||
armL: { raise: 8, bend: 6 }, armR: { raise: 8, bend: 6 }, legL: { raise: 2, bend: 0 }, legR: { raise: 2, bend: 0 }, crouch: 0,
|
||
};
|
||
|
||
type FacePatch = Partial<Omit<FaceParams, "browL" | "browR">> & { browL?: Partial<FaceParams["browL"]>; browR?: Partial<FaceParams["browR"]>; brows?: Partial<FaceParams["browL"]>; eyes?: number };
|
||
type BodyPatch = Partial<Omit<BodyParams, "armL" | "armR" | "legL" | "legR">> & { armL?: Partial<Limb>; armR?: Partial<Limb>; legL?: Partial<Limb>; legR?: Partial<Limb>; arms?: Partial<Limb>; legs?: Partial<Limb> };
|
||
|
||
/** Aplica um ajuste parcial (aceita `brows`/`eyes`/`arms`/`legs` para os dois lados de uma vez). */
|
||
export function withFace(base: FaceParams, p: FacePatch = {}): FaceParams {
|
||
const { brows, eyes, browL, browR, ...rest } = p;
|
||
return {
|
||
...base, ...rest,
|
||
browL: { ...base.browL, ...brows, ...browL }, browR: { ...base.browR, ...brows, ...browR },
|
||
eyeL: rest.eyeL ?? eyes ?? base.eyeL, eyeR: rest.eyeR ?? eyes ?? base.eyeR,
|
||
};
|
||
}
|
||
export function withBody(base: BodyParams, p: BodyPatch = {}): BodyParams {
|
||
const { arms, legs, armL, armR, legL, legR, ...rest } = p;
|
||
return {
|
||
...base, ...rest,
|
||
armL: { ...base.armL, ...arms, ...armL }, armR: { ...base.armR, ...arms, ...armR },
|
||
legL: { ...base.legL, ...legs, ...legL }, legR: { ...base.legR, ...legs, ...legR },
|
||
};
|
||
}
|
||
|
||
/** Expressões prontas = atalhos de parâmetros (dá para ajustar qualquer valor por cima com `face`). */
|
||
export const EXPRESSION_PRESETS = {
|
||
neutral: {},
|
||
smile: { smile: 0.7, eyes: 0.9 },
|
||
happy: { smile: 0.9, mouthOpen: 0.3, teeth: true, eyes: 0.85, brows: { y: -3 } },
|
||
laugh: { smile: 1, mouthOpen: 0.75, teeth: true, eyes: 0.18, brows: { y: -5 }, blush: 0.4 },
|
||
proud: { smile: 0.6, lid: 0.35, brows: { y: -2 } },
|
||
sad: { smile: -0.7, brows: { tilt: 16 }, eyes: 0.8, gazeY: 0.5 },
|
||
cry: { smile: -0.9, mouthOpen: 0.35, brows: { tilt: 20 }, eyes: 0.4, tears: true },
|
||
worried: { smile: -0.3, brows: { tilt: 14, y: -2 }, eyes: 1.05, sweat: true },
|
||
angry: { smile: -0.4, brows: { tilt: -18, y: 3 }, lid: 0.25, mouthWidth: 0.9 },
|
||
furious: { smile: -0.6, mouthOpen: 0.6, teeth: true, brows: { tilt: -24, y: 4 }, lid: 0.3, anger: true },
|
||
surprised: { mouthOpen: 0.6, mouthWidth: 0.55, smile: 0, brows: { y: -8 }, eyes: 1.25, pupil: 0.8, lid: -0.5 },
|
||
scared: { mouthOpen: 0.5, mouthWidth: 0.9, smile: -0.5, teeth: true, brows: { tilt: 18, y: -6 }, eyes: 1.3, pupil: 0.55, sweat: true, lid: -0.5 },
|
||
shocked: { mouthOpen: 0.95, mouthWidth: 0.6, smile: 0, brows: { y: -10 }, eyes: 1.35, pupil: 0.45, lid: -0.5 },
|
||
suspicious: { smile: -0.1, mouthWidth: 0.8, browL: { y: -6, tilt: 0 }, browR: { y: 4, tilt: -10 }, lid: 0.45, gazeX: 0.6 },
|
||
smirk: { smile: 0.45, mouthWidth: 0.8, browL: { y: -5 }, lid: 0.3, gazeX: -0.3 },
|
||
confused: { smile: -0.2, mouthWidth: 0.75, browL: { y: -7 }, browR: { tilt: -8 }, gazeY: -0.6, gazeX: 0.4 },
|
||
disgusted: { smile: -0.7, mouthWidth: 1.1, brows: { tilt: -10, y: 2 }, lid: 0.35 },
|
||
sleepy: { lid: 0.6, brows: { y: 3 }, mouthOpen: 0.1, smile: 0 },
|
||
determined: { smile: 0.1, mouthWidth: 0.85, brows: { tilt: -10, y: 2 }, lid: 0.2 },
|
||
embarrassed: { smile: 0.3, blush: 1, gazeY: 0.6, gazeX: -0.4, eyes: 0.8, sweat: true },
|
||
} satisfies Record<string, FacePatch>;
|
||
|
||
/** Poses prontas = atalhos de ângulos (ajustáveis por cima com `body`). */
|
||
export const POSE_PRESETS = {
|
||
stand: {},
|
||
point: { armR: { raise: 46, bend: -128 } },
|
||
wave: { armR: { raise: 140, bend: -25 } },
|
||
hold: { armR: { raise: 34, bend: 106 } },
|
||
arms_up: { arms: { raise: 158, bend: -10 } },
|
||
celebrate: { arms: { raise: 146, bend: -26 }, headNod: -0.35 },
|
||
shrug: { arms: { raise: 38, bend: 100 }, headTilt: 7 },
|
||
think: { armR: { raise: -24, bend: 138 }, headTilt: -5 },
|
||
hands_on_hips: { arms: { raise: 42, bend: 105 } },
|
||
cross_arms: { arms: { raise: 24, bend: 128 } },
|
||
facepalm: { armR: { raise: 125, bend: -103 }, headNod: 0.5, headTilt: 8 },
|
||
cover_mouth: { armR: { raise: -34, bend: 130 } },
|
||
defend: { arms: { raise: 62, bend: 118 }, lean: -4 },
|
||
reach: { armR: { raise: 68, bend: -15 }, lean: 6 },
|
||
sit: { crouch: 1, arms: { raise: 14, bend: 40 } },
|
||
kneel: { crouch: 0.6, arms: { raise: 10, bend: 20 } },
|
||
lean_in: { lean: 8, headTilt: 6 },
|
||
// Duas mãos segurando um objeto largo (livro, tábua) pelas bordas, na frente do peito.
|
||
hold_both: { arms: { raise: 42, bend: 104 } },
|
||
// Joinha: cotovelo dobrado, mão na altura do peito/ombro (a forma da mão vem do desenho: `thumbs_up`).
|
||
thumbs_up: { armR: { raise: 22, bend: 148 } },
|
||
} satisfies Record<string, BodyPatch>;
|
||
|
||
export type ExpressionName = keyof typeof EXPRESSION_PRESETS;
|
||
export type PoseName = keyof typeof POSE_PRESETS;
|
||
export const EXPRESSION_NAMES = Object.keys(EXPRESSION_PRESETS) as ExpressionName[];
|
||
export const POSE_NAMES = Object.keys(POSE_PRESETS) as PoseName[];
|
||
|
||
export const faceOf = (name: string | undefined, patch?: FacePatch): FaceParams =>
|
||
withFace(withFace(NEUTRAL_FACE, (EXPRESSION_PRESETS as Record<string, FacePatch>)[name ?? "neutral"] ?? {}), patch);
|
||
export const bodyOf = (name: string | undefined, patch?: BodyPatch): BodyParams =>
|
||
withBody(withBody(NEUTRAL_BODY, (POSE_PRESETS as Record<string, BodyPatch>)[name ?? "stand"] ?? {}), patch);
|
||
|
||
const lerp = (a: number, b: number, k: number) => a + (b - a) * k;
|
||
const limb = (a: Limb, b: Limb, k: number): Limb => ({ raise: lerp(a.raise, b.raise, k), bend: lerp(a.bend, b.bend, k) });
|
||
|
||
/** Mistura duas expressões (k = 0 → a, 1 → b). Os "liga/desliga" trocam na metade. */
|
||
export function blendFace(a: FaceParams, b: FaceParams, k: number): FaceParams {
|
||
const pick = <T,>(x: T, y: T) => (k < 0.5 ? x : y);
|
||
return {
|
||
browL: { y: lerp(a.browL.y, b.browL.y, k), tilt: lerp(a.browL.tilt, b.browL.tilt, k) },
|
||
browR: { y: lerp(a.browR.y, b.browR.y, k), tilt: lerp(a.browR.tilt, b.browR.tilt, k) },
|
||
eyeL: lerp(a.eyeL, b.eyeL, k), eyeR: lerp(a.eyeR, b.eyeR, k), pupil: lerp(a.pupil, b.pupil, k),
|
||
gazeX: lerp(a.gazeX, b.gazeX, k), gazeY: lerp(a.gazeY, b.gazeY, k),
|
||
mouthOpen: lerp(a.mouthOpen, b.mouthOpen, k), smile: lerp(a.smile, b.smile, k), mouthWidth: lerp(a.mouthWidth, b.mouthWidth, k), mouthRound: lerp(a.mouthRound, b.mouthRound, k), lid: lerp(a.lid, b.lid, k),
|
||
teeth: pick(a.teeth, b.teeth), blush: lerp(a.blush, b.blush, k), sweat: pick(a.sweat, b.sweat), tears: pick(a.tears, b.tears), anger: pick(a.anger, b.anger),
|
||
};
|
||
}
|
||
export function blendBody(a: BodyParams, b: BodyParams, k: number): BodyParams {
|
||
return {
|
||
lean: lerp(a.lean, b.lean, k), headTilt: lerp(a.headTilt, b.headTilt, k), headTurn: lerp(a.headTurn, b.headTurn, k), headNod: lerp(a.headNod, b.headNod, k),
|
||
armL: limb(a.armL, b.armL, k), armR: limb(a.armR, b.armR, k), legL: limb(a.legL, b.legL, k), legR: limb(a.legR, b.legR, k), crouch: lerp(a.crouch, b.crouch, k),
|
||
};
|
||
}
|
||
|
||
export type { FacePatch, BodyPatch };
|