import type { Document, Skin } from '@gltf-transform/core'; import type { CanonicalSkeleton } from './conform'; import { type SkinModel } from './armBindSkin'; /** * BIND-POSE NORMALISATION — re-bind a generated character's arms (and, when it hangs, its head) onto the platform * A-pose, so the shared clips move it like Base Female instead of through its own body. * * ── WHY ──────────────────────────────────────────────────────────────────────────────────────────────────── * `--fit source` (the default) keeps the source mesh exactly and embeds the canonical skeleton at its joints, with * each limb's rest aimed along the SOURCE segment (conform.ts). Two consumers then disagree about such a rig: * - the engine copies a clip's local rotations verbatim, so a bone lands where Base Female's lands whatever its bind; * - the Marketplace preview (helixgame.com lib/dreamer/avatar-animations.ts) copies the clip only onto a bone whose * rest is within 0.12 rad of the clip's own rest, and otherwise carries the clip's WORLD-space delta onto the * avatar's bind. The canonical walk adducts the upper arm ~24 deg from a 36 deg A-pose, so an arm bound at 14 deg * (Meshy/Tripo routinely bind 13-20 deg) walks at 0-10 deg — inside its own body. Cream Hoodie Anime Girl 1.0.6: * 16-18% of the forearm surface 4-10 cm inside the hoodie on average over the walk (Base Female 0). * Rotating the arm mesh onto the canonical direction at bind, with the bones' rests set to the canonical rest, makes * both consumers apply the clip identically: the Marketplace takes the clip's absolute pose, like Base Female. * * ── ARMS ─────────────────────────────────────────────────────────────────────────────────────────────────── * Per side, rotation only: the upper arm about the shoulder onto the canonical upper-arm direction, then the forearm * about the (moved) elbow onto the canonical forearm direction; the hand, fingers and twist helpers follow the * forearm. Both directions are first turned outward (about the body's forward axis) by a SLEEVE ALLOWANCE: a thick * sleeve over a wide hem needs the arm further out than Base Female's bare arm to clear it. The allowance is derived * from the arm's measured thickness against Base Female's (upper-arm and forearm radius about the bone axis, scaled * by body height): `atan(2 · (excess − 4 mm) / shoulder-to-wrist)` — the sleeve's extra thickness on the arm and, * assumed equal, on the torso it swings past. The Cream Hoodie measures 2.5 cm of sleeve and gets 6 deg, the value a * blind round picked by eye (6 deg beat 0 deg, 7 vs 5.5). Bare arms get 0. * * The mesh is linear-blend-skinned into the new pose and BAKED into POSITION / NORMAL / TANGENT (and morph deltas), * the moved joints are rebound (node TRS + inverse bind matrices), and the upper arm, forearm and hand rests become * the canonical rest (so the 0.12 rad test passes); every other joint under them keeps its frame relative to the mesh. * The allowance therefore lives in the joint offsets — the elbow and wrist sit `allowance` further out in the parent * bone's frame — and is kept by every consumer. Every skin (LOD) is processed with identical angles. * * ── FUSED SLEEVE / TORSO CLOTH ───────────────────────────────────────────────────────────────────────────── * Generated meshes fuse the inner sleeve to the torso cloth below the armpit (one welded surface). Cutting that * band tore the 1.0.1 hoodie open (35.9 cm in the walk); a hard weight split tore it too (10-23 cm). Instead, the arm * weight is FEATHERED across the band before the bake: every torso vertex within `fusedBandRadiusM` of surface * distance from an arm-owned vertex below the armpit takes a share of that side's arm weight that fades smoothly to 0 * at the radius, so the rotation (and later the walk) stretches the band over several rings of triangles instead of * one. Vertices above the armpit, on the back/front of the chest away from the band, and the arm side keep theirs. * * ── HEAD ─────────────────────────────────────────────────────────────────────────────────────────────────── * `headPitchDeg` (positive = chin up) rotates the head mesh about the head joint, mesh only — the head bone keeps the * canonical rest, so every consumer carries the correction (re-aiming the bone instead only works where the * Marketplace falls back to its delta rule, and never in the engine). */ export type BindNormalizeMode = 'repair' | 'report' | 'off'; export interface BindNormalizeOptions { mode?: BindNormalizeMode; /** Re-bind the arms (default true). */ arms?: boolean; /** Extra outward angle over the canonical A-pose (deg), or 'auto' (default) from the measured sleeve thickness. */ sleeveAllowanceDeg?: number | 'auto'; /** Cap on the automatic allowance (default 10 deg). */ maxSleeveAllowanceDeg?: number; /** 'raise' (default): only arms bound LOWER than the target are re-bound — the defect; a wider bind (Daft Punk 48 deg, * a T-pose) is left as authored. 'both' also lowers them onto the target. */ armDirection?: 'raise' | 'both'; /** A side whose upper arm and forearm are both within this of the target is left alone (default 4 deg). */ armToleranceDeg?: number; /** Feather arm weight across a fused sleeve/torso band before the bake (default true). */ fusedBand?: boolean; /** Surface radius of the feathering (default 0.08 m at a 1.70 m body). */ fusedBandRadiusM?: number; /** Head pitch correction (deg, positive = chin up), or 'auto' (default) from the measured face pitch; 0 = off. */ headPitchDeg?: number | 'auto'; /** |auto head pitch| below this is left alone (default 5 deg). */ headToleranceDeg?: number; /** Cap on the automatic head pitch (default 15 deg). */ maxHeadPitchDeg?: number; /** Largest stretch the re-bind may put on an arm<->leg (fused hand-to-thigh) edge (default 0.03 m at 1.70 m). */ maxBridgeStretchM?: number; /** Target rest (default the embedded helix-humanoid@1 skeleton, the walk clip's own rest). */ canonical?: CanonicalSkeleton; /** * Sides whose hand-to-thigh bridge limb-bridge repair already judged under the full re-bind and found to stretch * little there, and less than its cut would (`bridgeVerdict`): the guard does not hold them back. */ unguardedSides?: Array<'l' | 'r'>; } export interface ArmSideReport { side: 'l' | 'r'; /** Upper arm from straight down (deg), before -> after. */ upperarmFromVerticalDeg: [number, number]; /** Upper arm and forearm direction error vs the target (deg), before. */ upperarmErrorDeg: number; forearmErrorDeg: number; corrected: boolean; /** Share of the correction applied (< 1 when a fused hand-to-thigh bridge limited it). */ fraction: number; /** Arm<->leg (fused) edges on this side, LOD0. */ bridgeEdges: number; } export interface BindNormalizeReport { mode: BindNormalizeMode; skipped?: string; skins: number; sleeve: { upperRadiusCm: number; forearmRadiusCm: number; referenceUpperCm: number; referenceForearmCm: number; excessCm: number; allowanceDeg: number; source: 'auto' | 'explicit'; } | null; arms: ArmSideReport[]; head: { measuredPitchDeg: number | null; appliedDeg: number; source: 'auto' | 'explicit' | 'off'; }; fusedBand: { vertices: number; bandVertices: number; } | null; /** Vertices moved by the bake, per skin (LOD). */ verticesMoved: number[]; /** Worst rest-edge length change the bake made (cm), and edges changed by more than 2 cm, on the reference LOD. */ bakeEdgeGrowMaxCm: number; bakeEdgesOver2cm: number; } /** Base Female 1.3.1 arm radii about the bone axis (median over 20-80% of the segment), as a share of body height. */ export declare const REFERENCE_ARM_RADIUS: { upper: number; forearm: number; }; /** The skin with the most vertices — LOD0. */ export declare function referenceModel(models: readonly SkinModel[]): SkinModel; export interface SleeveMeasure { upper: number; forearm: number; height: number; reach: number; } /** The reference LOD's arm thickness (mean of both sides; m) and shoulder-to-wrist reach. */ export declare function measureSleeve(model: SkinModel, worlds: Map): SleeveMeasure | null; /** Allowance (deg) for a measured sleeve: atan(2 · max(0, excess − deadband) / reach), capped. */ export declare function sleeveAllowanceDeg(m: SleeveMeasure, maxDeg?: number): { excess: number; deg: number; }; /** Calibration: the face-profile slope Cream Hoodie 1.0.8 (the head Jack accepted after +12 deg) and the good stylised * avatars measure — Pastel Cyberpunk 22.1, Kemonomimi 18.9, Cream Hoodie 1.0.8 21.2 (raw import 27.8). */ export declare const FACE_PROFILE_TARGET_DEG = 21; /** * Face pitch of the reference LOD: the slope (deg from vertical, + = forehead ahead of the chin, i.e. looking down) of * the front profile of the face's central strip — head-weighted, forward-facing vertices within 20% of the head's * width of the midline, the frontmost point per height bin over the lower 60% of the face (bangs and the brow ridge * above are left out). Returns the measured slope and the chin-up rotation about the head joint that brings it to * FACE_PROFILE_TARGET_DEG (0 when already at or above it), or null when there is no face to read. */ export declare function measureFacePitch(model: SkinModel, worlds: Map): { slopeDeg: number; liftDeg: number; readable: boolean; } | null; /** * The arm re-bind `normalizeBindPose` would apply, without applying it, for the sides its hand-to-thigh bridge guard * would hold back: per skin, the world-space transform it bakes into each joint's vertices (by joint index; null = * unchanged), at the FULL correction. Limb-bridge repair (limbBridge.ts) runs before the re-bind and judges its cut * under this pose — where the re-bind pulls the hand away from the thigh — instead of in the low source pose, where the * bridge does not stretch and the cut never looked worth it. Null when no side is held back (arms within tolerance, * re-bind off, no bridge in the way) or the rig cannot be read. */ export declare function previewArmRebind(document: Document, options?: BindNormalizeOptions): { sides: Array<'l' | 'r'>; pose: (skin: Skin) => Array | null; } | null; /** * Normalise the bind pose of every skin in `document` (in place). Report mode measures and returns what it would do. */ export declare function normalizeBindPose(document: Document, options?: BindNormalizeOptions): BindNormalizeReport;