All files / systems/animation SkeletonRig.ts

55.55% Statements 70/126
25% Branches 3/12
66.66% Functions 8/12
55.55% Lines 70/126

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 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806                                                                      6x           2284x 2284x 2284x   2284x                         2284x 2191x     2284x                                                   6x   81x     81x           81x           81x               81x 81x     81x           81x           81x           81x           81x           81x               81x           81x           81x           81x           81x           81x               81x 81x           81x           81x           81x               81x 81x           81x           81x           81x               81x                                                             81x                             6x                                                                                                                                                                                           6x                                                                                                                                                                           6x         3x           3x           3x                                 6x 2x 2x     2x 4x 4x     2x                           6x 2x 2x     2x 3x 3x 3x 3x     2x                         6x 29x 29x 29x                       6x 1x 28x                                               6x                                                                                                                                                                                                                                                                                                                                                     6x                                                                          
/**
 * Skeleton rig implementation for articulated body model
 * 
 * Creates and manages humanoid skeletal rig with 28 bones for realistic
 * martial arts animations. Implements bone hierarchy following Korean martial
 * arts body mechanics.
 * 
 * @module systems/animation/SkeletonRig
 * @category Animation System
 * @korean 골격시스템
 */
 
import * as THREE from "three";
import { BoneName } from "../../types/skeletal";
import type {
  Bone,
  SkeletalRig,
  JointConstraint,
  BoneChain,
} from "../../types/skeletal";
 
/**
 * Create a bone with default rest pose
 * 
 * Helper function to create a bone with position, rotation, and scale.
 * Sets up rest pose for animation blending.
 * 
 * @param name - Unique bone identifier
 * @param parent - Parent bone (null for root)
 * @param position - Local position relative to parent [x, y, z]
 * @param length - Bone length for rendering
 * @returns Newly created bone
 * 
 * @korean 뼈생성
 */
export const createBone = (
  name: string,
  parent: Bone | null,
  position: [number, number, number],
  length = 0.1
): Bone => {
  const pos = new THREE.Vector3(position[0], position[1], position[2]);
  const rot = new THREE.Euler(0, 0, 0);
  const scale = new THREE.Vector3(1, 1, 1);
 
  const bone: Bone = {
    name,
    parent,
    position: pos.clone(),
    rotation: rot.clone(),
    scale: scale.clone(),
    children: [],
    length,
    restPosition: pos.clone(),
    restRotation: rot.clone(),
  };
 
  // Add to parent's children
  if (parent) {
    parent.children.push(bone);
  }
 
  return bone;
};
 
/**
 * Create complete humanoid skeletal rig
 * 
 * Creates 28-bone humanoid skeleton following Korean martial arts anatomy:
 * - 1 root (pelvis)
 * - 3 spine bones
 * - 2 head bones (neck, head)
 * - 12 arm bones (6 per arm)
 * - 10 leg bones (5 per leg)
 * 
 * Total: 28 bones (under 30 bone limit for 60fps performance)
 * 
 * @returns Complete skeletal rig with bone hierarchy
 * 
 * @example
 * ```typescript
 * const rig = createHumanoidRig();
 * const rightHand = rig.bones.get(BoneName.HAND_R);
 * console.log(`Hand position: ${rightHand?.position}`);
 * ```
 * 
 * @korean 인간형골격생성
 */
export const createHumanoidRig = (): SkeletalRig => {
  // Root (pelvis) - center of mass at hip height
  const root = createBone(BoneName.PELVIS, null, [0, 0.9, 0], 0.15);
 
  // Spine chain (3 bones)
  const spine1 = createBone(
    BoneName.SPINE_LOWER,
    root,
    [0, 0.15, 0],
    0.2
  );
  const spine2 = createBone(
    BoneName.SPINE_MIDDLE,
    spine1,
    [0, 0.2, 0],
    0.2
  );
  const spine3 = createBone(
    BoneName.SPINE_UPPER,
    spine2,
    [0, 0.2, 0],
    0.2
  );
 
  // Head chain (2 bones)
  const neck = createBone(BoneName.NECK, spine3, [0, 0.15, 0], 0.1);
  const head = createBone(BoneName.HEAD, neck, [0, 0.2, 0], 0.2);
 
  // Left arm chain (6 bones)
  const leftShoulder = createBone(
    BoneName.SHOULDER_L,
    spine3,
    [-0.15, 0.1, 0],
    0.1
  );
  const leftUpperArm = createBone(
    BoneName.UPPER_ARM_L,
    leftShoulder,
    [-0.15, 0, 0],
    0.25
  );
  const leftElbow = createBone(
    BoneName.ELBOW_L,
    leftUpperArm,
    [-0.25, 0, 0],
    0.05
  );
  const leftForearm = createBone(
    BoneName.FOREARM_L,
    leftElbow,
    [-0.25, 0, 0],
    0.25
  );
  const leftWrist = createBone(
    BoneName.WRIST_L,
    leftForearm,
    [-0.15, 0, 0],
    0.05
  );
  const leftHand = createBone(
    BoneName.HAND_L,
    leftWrist,
    [-0.08, 0, 0],
    0.08
  );
 
  // Right arm chain (6 bones - mirror of left)
  const rightShoulder = createBone(
    BoneName.SHOULDER_R,
    spine3,
    [0.15, 0.1, 0],
    0.1
  );
  const rightUpperArm = createBone(
    BoneName.UPPER_ARM_R,
    rightShoulder,
    [0.15, 0, 0],
    0.25
  );
  const rightElbow = createBone(
    BoneName.ELBOW_R,
    rightUpperArm,
    [0.25, 0, 0],
    0.05
  );
  const rightForearm = createBone(
    BoneName.FOREARM_R,
    rightElbow,
    [0.25, 0, 0],
    0.25
  );
  const rightWrist = createBone(
    BoneName.WRIST_R,
    rightForearm,
    [0.15, 0, 0],
    0.05
  );
  const rightHand = createBone(
    BoneName.HAND_R,
    rightWrist,
    [0.08, 0, 0],
    0.08
  );
 
  // Left leg chain (5 bones)
  const leftHip = createBone(BoneName.HIP_L, root, [-0.1, -0.1, 0], 0.1);
  const leftThigh = createBone(
    BoneName.THIGH_L,
    leftHip,
    [0, -0.3, 0],
    0.3
  );
  const leftKnee = createBone(
    BoneName.KNEE_L,
    leftThigh,
    [0, -0.3, 0],
    0.05
  );
  const leftShin = createBone(
    BoneName.SHIN_L,
    leftKnee,
    [0, -0.3, 0],
    0.3
  );
  const leftFoot = createBone(
    BoneName.FOOT_L,
    leftShin,
    [0, -0.1, 0.1],
    0.15
  );
 
  // Right leg chain (5 bones - mirror of left)
  const rightHip = createBone(BoneName.HIP_R, root, [0.1, -0.1, 0], 0.1);
  const rightThigh = createBone(
    BoneName.THIGH_R,
    rightHip,
    [0, -0.3, 0],
    0.3
  );
  const rightKnee = createBone(
    BoneName.KNEE_R,
    rightThigh,
    [0, -0.3, 0],
    0.05
  );
  const rightShin = createBone(
    BoneName.SHIN_R,
    rightKnee,
    [0, -0.3, 0],
    0.3
  );
  const rightFoot = createBone(
    BoneName.FOOT_R,
    rightShin,
    [0, -0.1, 0.1],
    0.15
  );
 
  // Create bone map for fast lookup
  const bones = new Map<string, Bone>([
    [BoneName.PELVIS, root],
    [BoneName.SPINE_LOWER, spine1],
    [BoneName.SPINE_MIDDLE, spine2],
    [BoneName.SPINE_UPPER, spine3],
    [BoneName.NECK, neck],
    [BoneName.HEAD, head],
    [BoneName.SHOULDER_L, leftShoulder],
    [BoneName.UPPER_ARM_L, leftUpperArm],
    [BoneName.ELBOW_L, leftElbow],
    [BoneName.FOREARM_L, leftForearm],
    [BoneName.WRIST_L, leftWrist],
    [BoneName.HAND_L, leftHand],
    [BoneName.SHOULDER_R, rightShoulder],
    [BoneName.UPPER_ARM_R, rightUpperArm],
    [BoneName.ELBOW_R, rightElbow],
    [BoneName.FOREARM_R, rightForearm],
    [BoneName.WRIST_R, rightWrist],
    [BoneName.HAND_R, rightHand],
    [BoneName.HIP_L, leftHip],
    [BoneName.THIGH_L, leftThigh],
    [BoneName.KNEE_L, leftKnee],
    [BoneName.SHIN_L, leftShin],
    [BoneName.FOOT_L, leftFoot],
    [BoneName.HIP_R, rightHip],
    [BoneName.THIGH_R, rightThigh],
    [BoneName.KNEE_R, rightKnee],
    [BoneName.SHIN_R, rightShin],
    [BoneName.FOOT_R, rightFoot],
  ]);
 
  return {
    root,
    bones,
    boneCount: bones.size,
  };
};
 
/**
 * Joint constraints for anatomically correct movement
 * 
 * Defines rotation limits for each joint to prevent unrealistic poses.
 * Based on human anatomy and Korean martial arts biomechanics.
 * 
 * @korean 관절제약조건들
 */
export const JOINT_CONSTRAINTS: JointConstraint[] = [
  // Elbow joints - can only bend one direction
  {
    boneName: BoneName.ELBOW_L,
    minRotation: new THREE.Vector3(0, 0, -2.4), // ~-137 degrees
    maxRotation: new THREE.Vector3(0, 0, 0),
    canTwist: false,
  },
  {
    boneName: BoneName.ELBOW_R,
    minRotation: new THREE.Vector3(0, 0, 0),
    maxRotation: new THREE.Vector3(0, 0, 2.4), // ~137 degrees
    canTwist: false,
  },
 
  // Knee joints - can only bend backward
  {
    boneName: BoneName.KNEE_L,
    minRotation: new THREE.Vector3(-2.4, 0, 0), // ~-137 degrees
    maxRotation: new THREE.Vector3(0, 0, 0),
    canTwist: false,
  },
  {
    boneName: BoneName.KNEE_R,
    minRotation: new THREE.Vector3(-2.4, 0, 0), // ~-137 degrees
    maxRotation: new THREE.Vector3(0, 0, 0),
    canTwist: false,
  },
 
  // Shoulder joints - full range of motion
  {
    boneName: BoneName.SHOULDER_L,
    minRotation: new THREE.Vector3(-1.5, -1.5, -3.0),
    maxRotation: new THREE.Vector3(1.5, 1.5, 3.0),
    canTwist: true,
  },
  {
    boneName: BoneName.SHOULDER_R,
    minRotation: new THREE.Vector3(-1.5, -1.5, -3.0),
    maxRotation: new THREE.Vector3(1.5, 1.5, 3.0),
    canTwist: true,
  },
 
  // Hip joints - large range for kicks
  {
    boneName: BoneName.HIP_L,
    minRotation: new THREE.Vector3(-1.8, -0.8, -1.5),
    maxRotation: new THREE.Vector3(1.8, 0.8, 1.5),
    canTwist: true,
  },
  {
    boneName: BoneName.HIP_R,
    minRotation: new THREE.Vector3(-1.8, -0.8, -1.5),
    maxRotation: new THREE.Vector3(1.8, 0.8, 1.5),
    canTwist: true,
  },
 
  // Neck - moderate range
  {
    boneName: BoneName.NECK,
    minRotation: new THREE.Vector3(-0.5, -0.8, -0.5),
    maxRotation: new THREE.Vector3(0.5, 0.8, 0.5),
    canTwist: true,
  },
 
  // Spine bones - limited rotation for realism
  {
    boneName: BoneName.SPINE_LOWER,
    minRotation: new THREE.Vector3(-0.3, -0.5, -0.3),
    maxRotation: new THREE.Vector3(0.3, 0.5, 0.3),
    canTwist: true,
  },
  {
    boneName: BoneName.SPINE_MIDDLE,
    minRotation: new THREE.Vector3(-0.3, -0.5, -0.3),
    maxRotation: new THREE.Vector3(0.3, 0.5, 0.3),
    canTwist: true,
  },
  {
    boneName: BoneName.SPINE_UPPER,
    minRotation: new THREE.Vector3(-0.3, -0.5, -0.3),
    maxRotation: new THREE.Vector3(0.3, 0.5, 0.3),
    canTwist: true,
  },
];
 
/**
 * Bone chains for IK (Inverse Kinematics)
 * 
 * Defines logical bone chains for limbs, used for IK solving
 * and animation retargeting.
 * 
 * @korean 뼈체인들
 */
export const BONE_CHAINS: BoneChain[] = [
  // Left arm chain
  {
    name: "left_arm",
    startBone: BoneName.SHOULDER_L,
    endBone: BoneName.HAND_L,
    bones: [
      BoneName.SHOULDER_L,
      BoneName.UPPER_ARM_L,
      BoneName.ELBOW_L,
      BoneName.FOREARM_L,
      BoneName.WRIST_L,
      BoneName.HAND_L,
    ],
  },
 
  // Right arm chain
  {
    name: "right_arm",
    startBone: BoneName.SHOULDER_R,
    endBone: BoneName.HAND_R,
    bones: [
      BoneName.SHOULDER_R,
      BoneName.UPPER_ARM_R,
      BoneName.ELBOW_R,
      BoneName.FOREARM_R,
      BoneName.WRIST_R,
      BoneName.HAND_R,
    ],
  },
 
  // Left leg chain
  {
    name: "left_leg",
    startBone: BoneName.HIP_L,
    endBone: BoneName.FOOT_L,
    bones: [
      BoneName.HIP_L,
      BoneName.THIGH_L,
      BoneName.KNEE_L,
      BoneName.SHIN_L,
      BoneName.FOOT_L,
    ],
  },
 
  // Right leg chain
  {
    name: "right_leg",
    startBone: BoneName.HIP_R,
    endBone: BoneName.FOOT_R,
    bones: [
      BoneName.HIP_R,
      BoneName.THIGH_R,
      BoneName.KNEE_R,
      BoneName.SHIN_R,
      BoneName.FOOT_R,
    ],
  },
 
  // Spine chain
  {
    name: "spine",
    startBone: BoneName.PELVIS,
    endBone: BoneName.HEAD,
    bones: [
      BoneName.PELVIS,
      BoneName.SPINE_LOWER,
      BoneName.SPINE_MIDDLE,
      BoneName.SPINE_UPPER,
      BoneName.NECK,
      BoneName.HEAD,
    ],
  },
];
 
/**
 * Apply joint constraints to bone rotation
 * 
 * Clamps bone rotation to anatomically correct ranges.
 * Prevents unrealistic poses like backward-bending elbows.
 * 
 * @param bone - Bone to constrain
 * @param constraints - Joint constraint to apply
 * 
 * @korean 관절제약적용
 */
export const applyJointConstraint = (
  bone: Bone,
  constraint: JointConstraint
): void => {
  // Clamp X rotation
  bone.rotation.x = Math.max(
    constraint.minRotation.x,
    Math.min(constraint.maxRotation.x, bone.rotation.x)
  );
 
  // Clamp Y rotation
  bone.rotation.y = Math.max(
    constraint.minRotation.y,
    Math.min(constraint.maxRotation.y, bone.rotation.y)
  );
 
  // Clamp Z rotation
  bone.rotation.z = Math.max(
    constraint.minRotation.z,
    Math.min(constraint.maxRotation.z, bone.rotation.z)
  );
};
 
/**
 * Get world position of bone
 * 
 * Calculates absolute world position by traversing parent chain.
 * Accounts for parent rotations and positions.
 * 
 * @param bone - Bone to get world position for
 * @returns World position as Vector3
 * 
 * @korean 뼈의세계위치구하기
 */
export const getBoneWorldPosition = (bone: Bone): THREE.Vector3 => {
  const worldPos = new THREE.Vector3();
  let currentBone: Bone | null = bone;
 
  // Traverse up parent chain
  while (currentBone) {
    worldPos.add(currentBone.position);
    currentBone = currentBone.parent;
  }
 
  return worldPos;
};
 
/**
 * Get world rotation of bone
 * 
 * Calculates absolute world rotation by traversing parent chain.
 * Combines all parent rotations.
 * 
 * @param bone - Bone to get world rotation for
 * @returns World rotation as Euler
 * 
 * @korean 뼈의세계회전구하기
 */
export const getBoneWorldRotation = (bone: Bone): THREE.Euler => {
  const worldRot = new THREE.Euler();
  let currentBone: Bone | null = bone;
 
  // Traverse up parent chain
  while (currentBone) {
    worldRot.x += currentBone.rotation.x;
    worldRot.y += currentBone.rotation.y;
    worldRot.z += currentBone.rotation.z;
    currentBone = currentBone.parent;
  }
 
  return worldRot;
};
 
/**
 * Reset bone to rest pose
 * 
 * Resets bone position and rotation to default rest pose.
 * Useful for animation blending and initialization.
 * 
 * @param bone - Bone to reset
 * 
 * @korean 뼈를기본자세로초기화
 */
export const resetBoneToRestPose = (bone: Bone): void => {
  bone.position.copy(bone.restPosition);
  bone.rotation.copy(bone.restRotation);
  bone.scale.set(1, 1, 1);
};
 
/**
 * Reset entire rig to rest pose
 * 
 * Resets all bones in rig to rest pose.
 * 
 * @param rig - Skeletal rig to reset
 * 
 * @korean 골격을기본자세로초기화
 */
export const resetRigToRestPose = (rig: SkeletalRig): void => {
  rig.bones.forEach((bone) => {
    resetBoneToRestPose(bone);
  });
};
 
/**
 * Create hand bones with 5 fingers
 * 
 * Creates detailed hand structure with 5 fingers (thumb, index, middle, ring, pinky),
 * attached to an existing hand/palm bone.
 * Each finger has 3-4 bones for realistic animation.
 * 
 * Finger bone structure:
 * - Thumb: 3 bones (metacarpal, proximal, distal) - no intermediate
 * - Other fingers: 4 bones (metacarpal, proximal, intermediate, distal)
 * 
 * Total created by this function: 19 finger bones per hand (3 thumb + 4*4 other fingers),
 * all attached to the provided hand bone.
 * 
 * @param handBone - Parent hand bone (acts as the palm/root for finger bones)
 * @param side - Hand side ("left" or "right")
 * @returns Map of finger bones added to the rig
 * 
 * @korean 손뼈생성
 */
export const createHandBones = (
  handBone: Bone,
  side: "left" | "right"
): Map<string, Bone> => {
  const fingerBones = new Map<string, Bone>();
  
  // Hand orientation: left hand extends to -X, right hand extends to +X
  const sideMultiplier = side === "left" ? -1 : 1;
  
  // Thumb (3 bones) - offset slightly toward palm center and forward
  const thumbMeta = createBone(
    `${BoneName[`THUMB_META_${side.toUpperCase()[0]}` as keyof typeof BoneName]}`,
    handBone,
    [0.015 * sideMultiplier, 0.02, 0.01],
    0.025
  );
  const thumbProx = createBone(
    `${BoneName[`THUMB_PROX_${side.toUpperCase()[0]}` as keyof typeof BoneName]}`,
    thumbMeta,
    [0.015 * sideMultiplier, 0.015, 0.01],
    0.02
  );
  const thumbDist = createBone(
    `${BoneName[`THUMB_DIST_${side.toUpperCase()[0]}` as keyof typeof BoneName]}`,
    thumbProx,
    [0.01 * sideMultiplier, 0.01, 0],
    0.015
  );
  
  fingerBones.set(thumbMeta.name, thumbMeta);
  fingerBones.set(thumbProx.name, thumbProx);
  fingerBones.set(thumbDist.name, thumbDist);
  
  // Index finger (4 bones)
  const indexMeta = createBone(
    `${BoneName[`INDEX_META_${side.toUpperCase()[0]}` as keyof typeof BoneName]}`,
    handBone,
    [0.015 * sideMultiplier, 0.06, 0],
    0.03
  );
  const indexProx = createBone(
    `${BoneName[`INDEX_PROX_${side.toUpperCase()[0]}` as keyof typeof BoneName]}`,
    indexMeta,
    [0, 0.025, 0],
    0.025
  );
  const indexInter = createBone(
    `${BoneName[`INDEX_INTER_${side.toUpperCase()[0]}` as keyof typeof BoneName]}`,
    indexProx,
    [0, 0.02, 0],
    0.02
  );
  const indexDist = createBone(
    `${BoneName[`INDEX_DIST_${side.toUpperCase()[0]}` as keyof typeof BoneName]}`,
    indexInter,
    [0, 0.015, 0],
    0.015
  );
  
  fingerBones.set(indexMeta.name, indexMeta);
  fingerBones.set(indexProx.name, indexProx);
  fingerBones.set(indexInter.name, indexInter);
  fingerBones.set(indexDist.name, indexDist);
  
  // Middle finger (4 bones) - longest finger
  const middleMeta = createBone(
    `${BoneName[`MIDDLE_META_${side.toUpperCase()[0]}` as keyof typeof BoneName]}`,
    handBone,
    [0.005 * sideMultiplier, 0.065, 0],
    0.035
  );
  const middleProx = createBone(
    `${BoneName[`MIDDLE_PROX_${side.toUpperCase()[0]}` as keyof typeof BoneName]}`,
    middleMeta,
    [0, 0.03, 0],
    0.03
  );
  const middleInter = createBone(
    `${BoneName[`MIDDLE_INTER_${side.toUpperCase()[0]}` as keyof typeof BoneName]}`,
    middleProx,
    [0, 0.025, 0],
    0.025
  );
  const middleDist = createBone(
    `${BoneName[`MIDDLE_DIST_${side.toUpperCase()[0]}` as keyof typeof BoneName]}`,
    middleInter,
    [0, 0.02, 0],
    0.02
  );
  
  fingerBones.set(middleMeta.name, middleMeta);
  fingerBones.set(middleProx.name, middleProx);
  fingerBones.set(middleInter.name, middleInter);
  fingerBones.set(middleDist.name, middleDist);
  
  // Ring finger (4 bones)
  const ringMeta = createBone(
    `${BoneName[`RING_META_${side.toUpperCase()[0]}` as keyof typeof BoneName]}`,
    handBone,
    [-0.005 * sideMultiplier, 0.06, 0],
    0.03
  );
  const ringProx = createBone(
    `${BoneName[`RING_PROX_${side.toUpperCase()[0]}` as keyof typeof BoneName]}`,
    ringMeta,
    [0, 0.025, 0],
    0.025
  );
  const ringInter = createBone(
    `${BoneName[`RING_INTER_${side.toUpperCase()[0]}` as keyof typeof BoneName]}`,
    ringProx,
    [0, 0.02, 0],
    0.02
  );
  const ringDist = createBone(
    `${BoneName[`RING_DIST_${side.toUpperCase()[0]}` as keyof typeof BoneName]}`,
    ringInter,
    [0, 0.015, 0],
    0.015
  );
  
  fingerBones.set(ringMeta.name, ringMeta);
  fingerBones.set(ringProx.name, ringProx);
  fingerBones.set(ringInter.name, ringInter);
  fingerBones.set(ringDist.name, ringDist);
  
  // Pinky finger (4 bones) - shortest finger
  const pinkyMeta = createBone(
    `${BoneName[`PINKY_META_${side.toUpperCase()[0]}` as keyof typeof BoneName]}`,
    handBone,
    [-0.015 * sideMultiplier, 0.05, 0],
    0.025
  );
  const pinkyProx = createBone(
    `${BoneName[`PINKY_PROX_${side.toUpperCase()[0]}` as keyof typeof BoneName]}`,
    pinkyMeta,
    [0, 0.02, 0],
    0.02
  );
  const pinkyInter = createBone(
    `${BoneName[`PINKY_INTER_${side.toUpperCase()[0]}` as keyof typeof BoneName]}`,
    pinkyProx,
    [0, 0.015, 0],
    0.015
  );
  const pinkyDist = createBone(
    `${BoneName[`PINKY_DIST_${side.toUpperCase()[0]}` as keyof typeof BoneName]}`,
    pinkyInter,
    [0, 0.01, 0],
    0.01
  );
  
  fingerBones.set(pinkyMeta.name, pinkyMeta);
  fingerBones.set(pinkyProx.name, pinkyProx);
  fingerBones.set(pinkyInter.name, pinkyInter);
  fingerBones.set(pinkyDist.name, pinkyDist);
  
  return fingerBones;
};
 
/**
 * Create humanoid rig with optional hand bones
 * 
 * Creates complete skeletal rig with optional detailed hand bones.
 * Hand bones can be excluded for performance (LOD system).
 * 
 * @param includeHandBones - Whether to include detailed hand bones (default: false)
 * @returns Complete skeletal rig with or without hand bones
 * 
 * @korean 손뼈포함골격생성
 */
export const createHumanoidRigWithHands = (
  includeHandBones: boolean = false
): SkeletalRig => {
  // Create base rig
  const baseRig = createHumanoidRig();
  
  // If hand bones not requested, return base rig
  if (!includeHandBones) {
    return baseRig;
  }
  
  // Get hand bones from base rig
  const leftHand = baseRig.bones.get(BoneName.HAND_L);
  const rightHand = baseRig.bones.get(BoneName.HAND_R);
  
  if (!leftHand || !rightHand) {
    console.warn("Hand bones not found in base rig");
    return baseRig;
  }
  
  // Create finger bones
  const leftFingerBones = createHandBones(leftHand, "left");
  const rightFingerBones = createHandBones(rightHand, "right");
  
  // Add finger bones to rig bone map
  leftFingerBones.forEach((bone, name) => {
    baseRig.bones.set(name, bone);
  });
  rightFingerBones.forEach((bone, name) => {
    baseRig.bones.set(name, bone);
  });
  
  return {
    ...baseRig,
    boneCount: baseRig.bones.size,
  };
};