/** * Eye System Module * Handles eye blinking, gaze direction, and eye movement for character rigs */ import type { EyeData, EyeDirection, EyeSockets, EyelidState, EyeMovementConfig, EyeState, Position, RigData } from '../types.js'; /** Options for creating an EyeSystem with socket-derived movement ranges */ export interface EyeSystemOptions { eyeData: EyeData; eyeSockets?: EyeSockets; /** Head IMAGE width (from dimensionValues.head) — used to convert 0-100% socket coords */ headWidth?: number; /** Head IMAGE height (from dimensionValues.head) */ headHeight?: number; /** Joint offset of the head connection (jointOffset['torso_head']). * Needed to map socket percentages into the iris coordinate space. */ headJointOffset?: { x: number; y: number; }; } /** * Compute auto-movement ranges from eye socket polygons. * * Mirrors the logic in Body.js `computeIrisScaleFactorFromSockets` so that manual * eye-position keyframes and automatic wandering use the same socket-derived scale. * * For each axis: * socketHalfPx = full socket bounding-box width/height in pixels ÷ 2 * irisHalfPx = ratio-scaled rendered iris dimension ÷ 2 * strictRange = socketHalfPx − irisHalfPx (keeps the iris edge inside socket) * * When strictRange > 1 px → use strictRange (hard containment) * When iris ≥ socket size → use socketHalfPx (iris center moves to socket wall; * best-effort containment — same fallback as Body.js) * * Socket coordinates are stored as 0-100 percentages of the head image dimensions. * headWidth / headHeight must be the HEAD IMAGE size (dimensionValues.head), not the * full character body, because the socket is drawn on top of the head sprite in * EyeSocketModal. */ /** Per-eye movement ranges returned by computeMovementRangesFromSockets. */ export interface PerEyeMovementRanges { left: Record; right: Record; leftRaw: DirectionalRanges; rightRaw: DirectionalRanges; } export interface DirectionalRanges { negX: number; posX: number; negY: number; posY: number; } /** * EyeSystem class - Manages eye animation, blinking, and gaze for characters */ export declare class EyeSystem { private state; private characterId; /** * Base iris coordinates - immutable snapshot from initial eyeData * Used as reference point for applying manual offsets */ private readonly baseIrisCoords; /** * Per-eye movement ranges per direction (derived from eye sockets or defaults). * Each eye gets its own range so asymmetric sockets are handled correctly. */ private readonly perEyeRanges; /** * Max pixel range for manual positions (-2 to 2 scale) - used in setManualIrisPositions */ private _leftManualScale; private _rightManualScale; /** * Creates a new EyeSystem instance * @param characterId - Unique identifier for the character * @param eyeDataOrOptions - Eye configuration data, or options object with eyeData + optional eyeSockets/headWidth/headHeight */ constructor(characterId: string, eyeDataOrOptions: EyeData | EyeSystemOptions); /** * Get current eye state */ getState(): EyeState; /** * Reset timing (call when timeline resets or on first frame) */ resetTiming(currentTime: number): void; /** * Detect if timeline has reset (time went backwards) */ private detectReset; /** * Update blinking animation * @param currentTime - Current time in seconds */ updateBlinking(currentTime: number): void; /** * Update eye gaze direction * @param currentTime - Current time in seconds * @param config - Movement configuration (direction or target position) */ updateGazeDirection(currentTime: number, config: EyeMovementConfig): void; /** * Set eye direction using predefined movement ranges */ setEyeDirection(direction: EyeDirection): void; /** * Calculate eye direction to look at a target position * @param targetPos - Target position in world space * @param characterPos - Character position (optional, defaults to {x: 0, y: 0}) */ calculateDirectionToPosition(targetPos: Position, characterPos?: Position): EyeDirection; /** * Random eye movement (simulates natural eye wandering) */ randomEyeMovement(): void; /** * Update eye system (handles both blinking and movement timing) * @param currentTime - Current time in seconds * @param isPlaying - Whether animation is playing (controls automatic movements) * @param speakingCharacterPos - Position of speaking character (if any) * @param characterPos - This character's position */ update(currentTime: number, isPlaying?: boolean, speakingCharacterPos?: Position, characterPos?: Position): void; /** * Get current eyelid image key based on state */ getEyelidImageKey(side: 'left' | 'right'): string; /** * Get current iris positions */ getIrisPositions(): { left: Position; right: Position; }; /** * Get current eyelid state */ getEyelidState(): EyelidState; /** * Set eye update interval (time between automatic eye movements) */ setEyeUpdateInterval(interval: number): void; /** * Set blink interval (time between blinks) */ setBlinkInterval(interval: number): void; /** * Force immediate blink */ forceBlink(): void; /** * Force blink at a specific timeline time (for timeline-driven "eye blink" actions). * Use this when playback is driven by timeline currentTime so the blink animation * progresses correctly in sync with the timeline. */ forceBlinkAtTime(timelineTime: number): void; /** * Set manual iris positions from timeline eye_movement actions * This method handles manual eye positioning by applying user-specified offsets * to the base iris coordinates. * * @param eyePositions - Eye position offsets in -2 to 2 range * @param currentTime - Current time in seconds * * The eyePositions parameter should have the structure: * { * left: { x: number, y: number }, // -2 to 2 range * right: { x: number, y: number } // -2 to 2 range * } * * These values are normalized offsets where: * - -2 represents maximum left/down * - 0 represents center * - +2 represents maximum right/up */ setManualIrisPositions(eyePositions: { left: { x: number; y: number; }; right: { x: number; y: number; }; }, currentTime: number): void; /** * Raw per-direction ranges for each eye. * Used by external preview code (e.g. calculatePreviewPosition) to normalize * mouse offsets to the -2..+2 scale using the same asymmetric limits. */ getDirectionalRanges(): { left: DirectionalRanges; right: DirectionalRanges; }; /** * Symmetric manual scale factor (min of left/right) for backward compatibility. * Body.js reads this property to detect and override broken scales. */ get manualScaleFactor(): number; set manualScaleFactor(value: number); /** * Get debug info */ getDebugInfo(): string; } /** * Factory function to create eye system from rig data * @param characterId - Unique identifier for the character * @param eyeDataOrRigData - Eye configuration data, or full RigData object (will extract eyes and eyeSockets) * @param options - Optional: { eyeSockets, headWidth, headHeight } - when provided, movement ranges are computed from eye socket polygons */ export declare function createEyeSystem(characterId: string, eyeDataOrRigData: EyeData | RigData, options?: { eyeSockets?: EyeSockets; headWidth?: number; headHeight?: number; headJointOffset?: { x: number; y: number; }; }): EyeSystem; //# sourceMappingURL=eyeSystem.d.ts.map