Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 | 67x 67x | /**
* Trigram Guard Applicator
*
* Applies full trigram-specific guard poses to animation keyframes.
* Unlike the generic `withGuard()` method that only sets arm positions,
* this applicator sets the complete body position including:
* - Arms (shoulder, elbow, wrist)
* - Legs (hip, knee, ankle)
* - Torso and pelvis rotation
*
* This ensures that walk/run/idle animations maintain proper
* trigram-specific guard positions for authentic Korean martial arts movement.
*
* @module systems/animation/builders/TrigramGuardApplicator
* @korean 팔괘방어자세적용기
*/
import { TrigramStance } from "@/types/common";
import type { StanceGuardPose } from "@/types/skeletal";
import { BoneName } from "@/types/skeletal";
import type { StanceLaterality } from "../../trigram/types";
import {
GAM_WATER_GUARD_POSE,
GAN_MOUNTAIN_GUARD_POSE,
GEON_HIGH_GUARD_POSE,
getGuardPoseForStance,
GON_EARTH_GUARD_POSE,
JIN_THUNDER_GUARD_POSE,
LI_FIRE_GUARD_POSE,
SON_WIND_GUARD_POSE,
TAE_FLUID_GUARD_POSE,
} from "../catalogs/StanceGuardPoses";
import type { KeyframeConfig } from "./KeyframeConfig";
// ═══════════════════════════════════════════════════════════════════════════
// DIRECT GUARD POSE ACCESS MAP (For performance in locomotion)
// ═══════════════════════════════════════════════════════════════════════════
/**
* Direct access to guard poses by trigram stance name
* Avoids repeated function calls in animation loops
*
* @korean 방어자세직접접근맵
*/
export const TRIGRAM_GUARD_POSES: Readonly<
Record<TrigramStance, StanceGuardPose>
> = {
[TrigramStance.GEON]: GEON_HIGH_GUARD_POSE,
[TrigramStance.TAE]: TAE_FLUID_GUARD_POSE,
[TrigramStance.LI]: LI_FIRE_GUARD_POSE,
[TrigramStance.JIN]: JIN_THUNDER_GUARD_POSE,
[TrigramStance.SON]: SON_WIND_GUARD_POSE,
[TrigramStance.GAM]: GAM_WATER_GUARD_POSE,
[TrigramStance.GAN]: GAN_MOUNTAIN_GUARD_POSE,
[TrigramStance.GON]: GON_EARTH_GUARD_POSE,
} as const;
// ═══════════════════════════════════════════════════════════════════════════
// APPLICATION OPTIONS
// ═══════════════════════════════════════════════════════════════════════════
/**
* Options for applying trigram guard pose
*
* @korean 팔괘방어자세적용옵션
*/
export interface TrigramGuardOptions {
/** Apply arm positions (shoulder, elbow, wrist) */
readonly includeArms?: boolean;
/** Apply leg positions (hip, knee, ankle) */
readonly includeLegs?: boolean;
/** Apply torso rotation */
readonly includeTorso?: boolean;
/** Apply pelvis rotation */
readonly includePelvis?: boolean;
/** Blend factor 0-1 for partial application (locomotion blending) */
readonly blendFactor?: number;
/** Stance laterality ("left" or "right") */
readonly laterality?: StanceLaterality;
}
/**
* Default options for full guard pose application
*/
const DEFAULT_OPTIONS: Required<TrigramGuardOptions> = {
includeArms: true,
includeLegs: true,
includeTorso: true,
includePelvis: true,
blendFactor: 1.0,
laterality: "right",
} as const;
// ═══════════════════════════════════════════════════════════════════════════
// CORE APPLICATION FUNCTION
// ═══════════════════════════════════════════════════════════════════════════
/**
* Apply trigram-specific guard pose to a KeyframeConfig
*
* Sets the complete body position for the specified trigram stance,
* including arms, legs, torso, and pelvis. This creates authentic
* Korean martial arts posture for each of the eight trigram stances.
*
* @param config - KeyframeConfig to apply guard pose to
* @param stance - Trigram stance (e.g., TrigramStance.GEON)
* @param options - Application options
*
* @example
* ```typescript
* // Full guard pose application
* applyTrigramGuardToConfig(kf, TrigramStance.GEON);
*
* // Arms only (for locomotion that sets its own leg positions)
* applyTrigramGuardToConfig(kf, TrigramStance.GEON, {
* includeLegs: false,
* includePelvis: false
* });
*
* // Blended for walk cycle transitions
* applyTrigramGuardToConfig(kf, TrigramStance.TAE, {
* blendFactor: 0.7
* });
* ```
*
* @korean 팔괘방어자세설정적용
*/
export function applyTrigramGuardToConfig(
config: KeyframeConfig,
stance: TrigramStance,
options: TrigramGuardOptions = {}
): void {
const opts = { ...DEFAULT_OPTIONS, ...options };
// Get guard pose for stance and laterality
const guardPose =
opts.laterality === "right"
? TRIGRAM_GUARD_POSES[stance]
: getGuardPoseForStance(stance, opts.laterality);
if (!guardPose) {
console.warn(
`[TrigramGuardApplicator] No guard pose found for stance: ${stance}`
);
return;
}
const blend = opts.blendFactor;
// Apply arm positions
if (opts.includeArms) {
applyArmsToConfig(config, guardPose, blend);
}
// Apply leg positions
if (opts.includeLegs) {
applyLegsToConfig(config, guardPose, blend);
}
// Apply torso rotation
if (opts.includeTorso) {
config.rotate(
BoneName.SPINE_UPPER,
guardPose.torso.x * blend,
guardPose.torso.y * blend,
guardPose.torso.z * blend
);
}
// Apply pelvis rotation
if (opts.includePelvis) {
config.rotate(
BoneName.PELVIS,
guardPose.pelvis.x * blend,
guardPose.pelvis.y * blend,
guardPose.pelvis.z * blend
);
}
}
/**
* Apply arm positions from guard pose to config
*/
function applyArmsToConfig(
config: KeyframeConfig,
guardPose: StanceGuardPose,
blend: number
): void {
// Left arm
config.rotate(
BoneName.SHOULDER_L,
guardPose.leftArm.shoulder.x * blend,
guardPose.leftArm.shoulder.y * blend,
guardPose.leftArm.shoulder.z * blend
);
config.rotate(
BoneName.ELBOW_L,
guardPose.leftArm.elbow.x * blend,
guardPose.leftArm.elbow.y * blend,
guardPose.leftArm.elbow.z * blend
);
config.rotate(
BoneName.WRIST_L,
guardPose.leftArm.wrist.x * blend,
guardPose.leftArm.wrist.y * blend,
guardPose.leftArm.wrist.z * blend
);
// Right arm
config.rotate(
BoneName.SHOULDER_R,
guardPose.rightArm.shoulder.x * blend,
guardPose.rightArm.shoulder.y * blend,
guardPose.rightArm.shoulder.z * blend
);
config.rotate(
BoneName.ELBOW_R,
guardPose.rightArm.elbow.x * blend,
guardPose.rightArm.elbow.y * blend,
guardPose.rightArm.elbow.z * blend
);
config.rotate(
BoneName.WRIST_R,
guardPose.rightArm.wrist.x * blend,
guardPose.rightArm.wrist.y * blend,
guardPose.rightArm.wrist.z * blend
);
}
/**
* Apply leg positions from guard pose to config
*/
function applyLegsToConfig(
config: KeyframeConfig,
guardPose: StanceGuardPose,
blend: number
): void {
// Left leg
config.rotate(
BoneName.HIP_L,
guardPose.leftLeg.hip.x * blend,
guardPose.leftLeg.hip.y * blend,
guardPose.leftLeg.hip.z * blend
);
config.rotate(
BoneName.KNEE_L,
guardPose.leftLeg.knee.x * blend,
guardPose.leftLeg.knee.y * blend,
guardPose.leftLeg.knee.z * blend
);
config.rotate(
BoneName.FOOT_L,
guardPose.leftLeg.ankle.x * blend,
guardPose.leftLeg.ankle.y * blend,
guardPose.leftLeg.ankle.z * blend
);
// Right leg
config.rotate(
BoneName.HIP_R,
guardPose.rightLeg.hip.x * blend,
guardPose.rightLeg.hip.y * blend,
guardPose.rightLeg.hip.z * blend
);
config.rotate(
BoneName.KNEE_R,
guardPose.rightLeg.knee.x * blend,
guardPose.rightLeg.knee.y * blend,
guardPose.rightLeg.knee.z * blend
);
config.rotate(
BoneName.FOOT_R,
guardPose.rightLeg.ankle.x * blend,
guardPose.rightLeg.ankle.y * blend,
guardPose.rightLeg.ankle.z * blend
);
}
// ═══════════════════════════════════════════════════════════════════════════
// LOCOMOTION HELPERS (For Walk/Run Arm Swing)
// ═══════════════════════════════════════════════════════════════════════════
/**
* Get arm guard pose with swing offset for locomotion
*
* Applies a shoulder swing offset while maintaining the trigram's
* guard elbow and wrist positions. This creates authentic walking
* movement that respects each trigram's guard style.
*
* @param stance - Trigram stance
* @param swingOffset - Shoulder rotation offset from walking motion
* @param side - Which arm ("left" | "right")
* @returns Adjusted arm rotation values
*
* @korean 이동중팔자세가져오기
*/
export function getLocomotionArmPose(
stance: TrigramStance,
swingOffset: number,
side: "left" | "right"
): { shoulder: [number, number, number]; elbow: [number, number, number] } {
const guardPose = TRIGRAM_GUARD_POSES[stance];
if (!guardPose) {
// Fallback to neutral if stance not found
return {
shoulder: [swingOffset, 0, 0],
elbow: [-1.2, 0, 0],
};
}
const arm = side === "left" ? guardPose.leftArm : guardPose.rightArm;
// Add swing offset to the X rotation (forward/back)
// while preserving the guard's Y and Z rotations
return {
shoulder: [arm.shoulder.x + swingOffset, arm.shoulder.y, arm.shoulder.z],
elbow: [arm.elbow.x, arm.elbow.y, arm.elbow.z],
};
}
/**
* Get guard arm base rotations without swing for a trigram
*
* Returns the base arm position for the trigram's guard pose.
* Use this when you want to manually add swing offsets.
*
* @param stance - Trigram stance
* @returns Left and right arm base rotations
*
* @korean 팔자세베이스가져오기
*/
export function getGuardArmBase(stance: TrigramStance): {
left: {
shoulder: [number, number, number];
elbow: [number, number, number];
};
right: {
shoulder: [number, number, number];
elbow: [number, number, number];
};
} {
const guardPose = TRIGRAM_GUARD_POSES[stance];
if (!guardPose) {
// Fallback neutral guard
return {
left: { shoulder: [-0.6, 0.4, 0.3], elbow: [0, 0, -1.6] },
right: { shoulder: [-0.6, -0.4, -0.3], elbow: [0, 0, 1.6] },
};
}
return {
left: {
shoulder: [
guardPose.leftArm.shoulder.x,
guardPose.leftArm.shoulder.y,
guardPose.leftArm.shoulder.z,
],
elbow: [
guardPose.leftArm.elbow.x,
guardPose.leftArm.elbow.y,
guardPose.leftArm.elbow.z,
],
},
right: {
shoulder: [
guardPose.rightArm.shoulder.x,
guardPose.rightArm.shoulder.y,
guardPose.rightArm.shoulder.z,
],
elbow: [
guardPose.rightArm.elbow.x,
guardPose.rightArm.elbow.y,
guardPose.rightArm.elbow.z,
],
},
};
}
|