@asiyst/sdk 0.1.34 → 0.1.35

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -85,7 +85,7 @@ var ALL_ACTION_KINDS = [
85
85
  // src/core/constants.ts
86
86
  function resolveSdkVersion() {
87
87
  {
88
- return "0.1.34";
88
+ return "0.1.35";
89
89
  }
90
90
  }
91
91
  var SDK_VERSION = resolveSdkVersion();
@@ -612,9 +612,30 @@ var HttpTransport = class {
612
612
  if (req.body !== void 0) {
613
613
  defaultHeaders["Content-Type"] = "application/json";
614
614
  }
615
+ const mergedHeaders = new Headers();
616
+ for (const [key, value] of Object.entries(defaultHeaders)) {
617
+ mergedHeaders.set(key, value);
618
+ }
619
+ if (req.headers) {
620
+ if (typeof req.headers.forEach === "function" && !Array.isArray(req.headers)) {
621
+ req.headers.forEach((value, key) => {
622
+ mergedHeaders.set(key, value);
623
+ });
624
+ } else if (Array.isArray(req.headers)) {
625
+ for (const [key, value] of req.headers) {
626
+ mergedHeaders.set(key, value);
627
+ }
628
+ } else if (typeof req.headers === "object") {
629
+ for (const [key, value] of Object.entries(req.headers)) {
630
+ if (value !== void 0 && value !== null) {
631
+ mergedHeaders.set(key, String(value));
632
+ }
633
+ }
634
+ }
635
+ }
615
636
  const response = await fetch(url, {
616
637
  method: req.method,
617
- headers: req.headers ?? defaultHeaders,
638
+ headers: mergedHeaders,
618
639
  body: req.body === void 0 ? void 0 : JSON.stringify(req.body),
619
640
  signal: controller.signal
620
641
  });
@@ -3303,27 +3324,57 @@ var RigDetector = class {
3303
3324
  return rawName.trim().replace(/^.*[:|/]/, "").replace(/^(mixamorig\d*|bip01|armature|root)[:_ ]?/i, "").replace(/[^a-zA-Z0-9._-]/g, "").toLowerCase();
3304
3325
  }
3305
3326
  /**
3306
- * Analyzes an imported 3D model root, identifies all bones, maps them to canonical AUBR,
3307
- * validates hierarchy, and reports capability profile.
3327
+ * Analyzes an imported 3D model root, identifies all bones and skinned meshes,
3328
+ * maps them to canonical AUBR, validates hierarchy, and reports capability profile.
3308
3329
  */
3309
- static detect(modelRoot) {
3330
+ static detect(modelRoot, options) {
3310
3331
  const allBones = [];
3332
+ const seenBones = /* @__PURE__ */ new Set();
3333
+ const skinnedMeshes = [];
3311
3334
  modelRoot.traverse((node) => {
3312
- if (node.isBone) {
3335
+ if (node.isSkinnedMesh) {
3336
+ const sm = node;
3337
+ skinnedMeshes.push(sm);
3338
+ if (sm.skeleton?.bones) {
3339
+ for (const b of sm.skeleton.bones) {
3340
+ if (b && !seenBones.has(b)) {
3341
+ seenBones.add(b);
3342
+ allBones.push(b);
3343
+ }
3344
+ }
3345
+ }
3346
+ }
3347
+ if (node.isBone && !seenBones.has(node)) {
3348
+ seenBones.add(node);
3313
3349
  allBones.push(node);
3314
3350
  }
3315
3351
  });
3316
3352
  if (allBones.length === 0) {
3317
3353
  modelRoot.traverse((node) => {
3354
+ if (node.isMesh) return;
3318
3355
  if (node.name && (node.type === "Bone" || /bone|joint|limb|hips|spine|arm|leg|head/i.test(node.name))) {
3319
- if (!allBones.includes(node)) {
3356
+ if (!seenBones.has(node)) {
3357
+ seenBones.add(node);
3320
3358
  allBones.push(node);
3321
3359
  }
3322
3360
  }
3323
3361
  });
3324
3362
  }
3363
+ const embeddedAnimationCount = typeof options?.embeddedAnimationCount === "number" && Number.isFinite(options.embeddedAnimationCount) ? Math.max(0, Math.floor(options.embeddedAnimationCount)) : Array.isArray(modelRoot.animations) ? modelRoot.animations.length : 0;
3364
+ const hasEmbeddedAnimations = embeddedAnimationCount > 0;
3365
+ const skinnedMeshCount = skinnedMeshes.length;
3366
+ const hasSkinnedMesh = skinnedMeshCount > 0;
3367
+ const boneCount = allBones.length;
3368
+ const hasSkeleton = boneCount > 0;
3325
3369
  if (allBones.length === 0) {
3326
- const caps = this.createUnsupportedCapabilities("unsupported", ["hips", "spine", "head"]);
3370
+ const caps = this.createUnsupportedCapabilities("unsupported", ["hips", "spine", "head"], {
3371
+ hasSkeleton,
3372
+ hasSkinnedMesh,
3373
+ boneCount,
3374
+ skinnedMeshCount,
3375
+ embeddedAnimationCount,
3376
+ hasEmbeddedAnimations
3377
+ });
3327
3378
  return {
3328
3379
  rigType: "unsupported",
3329
3380
  capabilities: caps,
@@ -3364,26 +3415,43 @@ var RigDetector = class {
3364
3415
  const hasArms = matchedBones.has("leftUpperArm") && matchedBones.has("leftLowerArm") && matchedBones.has("rightUpperArm") && matchedBones.has("rightLowerArm");
3365
3416
  const isHumanoid = hasHipsAndSpine && (hasLegs || hasArms);
3366
3417
  let finalRigType = rigFlavor;
3418
+ const detectedRigFlavor = isHumanoid ? rigFlavor : "unsupported";
3367
3419
  if (!isHumanoid) {
3368
3420
  finalRigType = "unsupported";
3369
3421
  } else if (missingRequired.length > 0) {
3370
3422
  finalRigType = "partial_humanoid";
3371
3423
  }
3424
+ const proceduralIdle = isHumanoid && hasHipsAndSpine && hasHead;
3425
+ const proceduralLocomotion = isHumanoid && hasLegs;
3426
+ const proceduralRunning = isHumanoid && hasLegs;
3427
+ const upperBodyGestures = isHumanoid && hasArms && hasHipsAndSpine;
3428
+ const proceduralAnimationAvailable = proceduralIdle || proceduralLocomotion || upperBodyGestures;
3429
+ const clipPlayback = hasSkeleton && allBones.length > 5 || hasEmbeddedAnimations;
3430
+ const animationSourceMode = proceduralAnimationAvailable && hasEmbeddedAnimations ? "embedded_and_procedural" : proceduralAnimationAvailable ? "procedural_only" : hasEmbeddedAnimations ? "embedded_only" : "static";
3372
3431
  const capabilities = {
3373
3432
  rigType: finalRigType,
3433
+ detectedRigFlavor,
3374
3434
  isHumanoid,
3435
+ hasSkeleton,
3436
+ hasSkinnedMesh,
3437
+ boneCount,
3438
+ skinnedMeshCount,
3375
3439
  confidence: Math.round(confidence * 100) / 100,
3376
- proceduralIdle: hasHipsAndSpine && hasHead,
3377
- proceduralLocomotion: isHumanoid && hasLegs,
3378
- proceduralRunning: isHumanoid && hasLegs,
3440
+ proceduralAnimationAvailable,
3441
+ proceduralIdle,
3442
+ proceduralLocomotion,
3443
+ proceduralRunning,
3379
3444
  turning: isHumanoid,
3380
- legIK: hasLegs,
3381
- armIK: hasArms,
3382
- footPlacement: hasLegs,
3383
- headLookAt: hasHead,
3384
- upperBodyGestures: hasArms && hasHipsAndSpine,
3445
+ legIK: isHumanoid && hasLegs,
3446
+ armIK: isHumanoid && hasArms,
3447
+ footPlacement: isHumanoid && hasLegs,
3448
+ headLookAt: isHumanoid && hasHead,
3449
+ upperBodyGestures,
3385
3450
  facialAnimation: this.detectFacialCapabilities(modelRoot),
3386
- clipPlayback: allBones.length > 5,
3451
+ hasEmbeddedAnimations,
3452
+ embeddedAnimationCount,
3453
+ clipPlayback,
3454
+ animationSourceMode,
3387
3455
  missingRequiredBones: missingRequired,
3388
3456
  missingOptionalBones: missingOptional
3389
3457
  };
@@ -3398,15 +3466,15 @@ var RigDetector = class {
3398
3466
  /**
3399
3467
  * Alias for detect()
3400
3468
  */
3401
- static detectRig(modelRoot) {
3402
- return this.detect(modelRoot);
3469
+ static detectRig(modelRoot, options) {
3470
+ return this.detect(modelRoot, options);
3403
3471
  }
3404
3472
  /**
3405
3473
  * Assesses avatar rig capabilities from detection result or model.
3406
3474
  */
3407
- static assessCapabilities(detection, modelRoot) {
3475
+ static assessCapabilities(detection, modelRoot, options) {
3408
3476
  if (modelRoot) {
3409
- return this.detect(modelRoot).capabilities;
3477
+ return this.detect(modelRoot, options).capabilities;
3410
3478
  }
3411
3479
  return detection.capabilities;
3412
3480
  }
@@ -3471,6 +3539,24 @@ var RigDetector = class {
3471
3539
  if (rightLow && rightFoot && !this.isDescendantOf(rightFoot, rightLow)) {
3472
3540
  matched.delete("rightFoot");
3473
3541
  }
3542
+ const leftUpArm = matched.get("leftUpperArm");
3543
+ const leftLowArm = matched.get("leftLowerArm");
3544
+ const leftHand = matched.get("leftHand");
3545
+ if (leftUpArm && leftLowArm && !this.isDescendantOf(leftLowArm, leftUpArm)) {
3546
+ matched.delete("leftLowerArm");
3547
+ }
3548
+ if (leftLowArm && leftHand && !this.isDescendantOf(leftHand, leftLowArm)) {
3549
+ matched.delete("leftHand");
3550
+ }
3551
+ const rightUpArm = matched.get("rightUpperArm");
3552
+ const rightLowArm = matched.get("rightLowerArm");
3553
+ const rightHand = matched.get("rightHand");
3554
+ if (rightUpArm && rightLowArm && !this.isDescendantOf(rightLowArm, rightUpArm)) {
3555
+ matched.delete("rightLowerArm");
3556
+ }
3557
+ if (rightLowArm && rightHand && !this.isDescendantOf(rightHand, rightLowArm)) {
3558
+ matched.delete("rightHand");
3559
+ }
3474
3560
  const hips = matched.get("hips");
3475
3561
  const spine = matched.get("spine");
3476
3562
  const head = matched.get("head");
@@ -3499,11 +3585,19 @@ var RigDetector = class {
3499
3585
  });
3500
3586
  return hasMorphTargets;
3501
3587
  }
3502
- static createUnsupportedCapabilities(rigType, missingReq) {
3588
+ static createUnsupportedCapabilities(rigType, missingReq, meta) {
3589
+ const hasEmbeddedAnimations = meta?.hasEmbeddedAnimations ?? false;
3590
+ const embeddedAnimationCount = meta?.embeddedAnimationCount ?? 0;
3503
3591
  return {
3504
3592
  rigType,
3593
+ detectedRigFlavor: rigType,
3505
3594
  isHumanoid: false,
3595
+ hasSkeleton: meta?.hasSkeleton ?? false,
3596
+ hasSkinnedMesh: meta?.hasSkinnedMesh ?? false,
3597
+ boneCount: meta?.boneCount ?? 0,
3598
+ skinnedMeshCount: meta?.skinnedMeshCount ?? 0,
3506
3599
  confidence: 0,
3600
+ proceduralAnimationAvailable: false,
3507
3601
  proceduralIdle: false,
3508
3602
  proceduralLocomotion: false,
3509
3603
  proceduralRunning: false,
@@ -3514,7 +3608,10 @@ var RigDetector = class {
3514
3608
  headLookAt: false,
3515
3609
  upperBodyGestures: false,
3516
3610
  facialAnimation: false,
3517
- clipPlayback: false,
3611
+ hasEmbeddedAnimations,
3612
+ embeddedAnimationCount,
3613
+ clipPlayback: hasEmbeddedAnimations,
3614
+ animationSourceMode: hasEmbeddedAnimations ? "embedded_only" : "static",
3518
3615
  missingRequiredBones: missingReq,
3519
3616
  missingOptionalBones: [...OPTIONAL_CANONICAL_BONES]
3520
3617
  };
@@ -3575,12 +3672,16 @@ ScratchMath.v4 = new THREE.Vector3();
3575
3672
  ScratchMath.v5 = new THREE.Vector3();
3576
3673
  ScratchMath.v6 = new THREE.Vector3();
3577
3674
  ScratchMath.v7 = new THREE.Vector3();
3675
+ ScratchMath.v8 = new THREE.Vector3();
3676
+ ScratchMath.v9 = new THREE.Vector3();
3578
3677
  // Quaternion scratch pool
3579
3678
  ScratchMath.q0 = new THREE.Quaternion();
3580
3679
  ScratchMath.q1 = new THREE.Quaternion();
3581
3680
  ScratchMath.q2 = new THREE.Quaternion();
3582
3681
  ScratchMath.q3 = new THREE.Quaternion();
3583
3682
  ScratchMath.q4 = new THREE.Quaternion();
3683
+ ScratchMath.q5 = new THREE.Quaternion();
3684
+ ScratchMath.q6 = new THREE.Quaternion();
3584
3685
  // Matrix4 scratch pool
3585
3686
  ScratchMath.m0 = new THREE.Matrix4();
3586
3687
  ScratchMath.m1 = new THREE.Matrix4();
@@ -3597,16 +3698,29 @@ ScratchMath.LEFT = new THREE.Vector3(-1, 0, 0);
3597
3698
 
3598
3699
  // src/avatar/procedural/AUBRSkeleton.ts
3599
3700
  var AUBRSkeleton = class {
3600
- constructor(modelRoot) {
3701
+ constructor(modelRoot, options) {
3601
3702
  this.joints = /* @__PURE__ */ new Map();
3703
+ /** Rest foot positions in modelRoot local space */
3704
+ this.restLeftFootModelPos = new THREE.Vector3();
3705
+ this.restRightFootModelPos = new THREE.Vector3();
3602
3706
  this.boneToCanonical = /* @__PURE__ */ new Map();
3707
+ this.restModelQuat = /* @__PURE__ */ new Map();
3708
+ this.restModelQuatInv = /* @__PURE__ */ new Map();
3603
3709
  this.modelRoot = modelRoot;
3604
- const detection = RigDetector.detect(modelRoot);
3710
+ this.modelRoot.updateMatrixWorld(true);
3711
+ const detection = RigDetector.detect(modelRoot, options);
3605
3712
  this.capabilities = detection.capabilities;
3713
+ const rootWorldQuatInv = new THREE.Quaternion();
3714
+ this.modelRoot.getWorldQuaternion(rootWorldQuatInv).invert();
3606
3715
  for (const [canonicalName, bone] of detection.matchedBones.entries()) {
3607
3716
  const restPos = bone.position.clone();
3608
3717
  const restQuat = bone.quaternion.clone();
3609
3718
  const restScale = bone.scale.clone();
3719
+ const boneWorldQuat = new THREE.Quaternion();
3720
+ bone.getWorldQuaternion(boneWorldQuat);
3721
+ const boneModelQuat = rootWorldQuatInv.clone().multiply(boneWorldQuat).normalize();
3722
+ this.restModelQuat.set(canonicalName, boneModelQuat);
3723
+ this.restModelQuatInv.set(canonicalName, boneModelQuat.clone().invert());
3610
3724
  let restDir = new THREE.Vector3(0, 1, 0);
3611
3725
  let boneLen = 0.2;
3612
3726
  if (bone.children.length > 0) {
@@ -3635,6 +3749,79 @@ var AUBRSkeleton = class {
3635
3749
  this.boneToCanonical.set(bone, canonicalName);
3636
3750
  }
3637
3751
  this.proportions = this.calculateProportions(detection.matchedBones);
3752
+ this.worldUnitsPerMeter = this.proportions.totalHeight / 1.8;
3753
+ const hipsBone = detection.matchedBones.get("hips");
3754
+ let hipsParentScale = 1;
3755
+ if (hipsBone?.parent) {
3756
+ const parentScaleVec = new THREE.Vector3();
3757
+ hipsBone.parent.getWorldScale(parentScaleVec);
3758
+ const avg = (Math.abs(parentScaleVec.x) + Math.abs(parentScaleVec.y) + Math.abs(parentScaleVec.z)) / 3;
3759
+ if (Number.isFinite(avg) && avg > 1e-5) {
3760
+ hipsParentScale = avg;
3761
+ }
3762
+ }
3763
+ this.hipsLocalPerMeter = this.worldUnitsPerMeter / hipsParentScale;
3764
+ const leftArmRest = this.measureArmRest("leftUpperArm", "leftLowerArm", 1);
3765
+ const rightArmRest = this.measureArmRest("rightUpperArm", "rightLowerArm", -1);
3766
+ this.leftArmRestAngleFromDown = leftArmRest.angleFromDown;
3767
+ this.leftArmLowerSign = leftArmRest.lowerSign;
3768
+ this.rightArmRestAngleFromDown = rightArmRest.angleFromDown;
3769
+ this.rightArmLowerSign = rightArmRest.lowerSign;
3770
+ const leftFoot = detection.matchedBones.get("leftFoot");
3771
+ const rightFoot = detection.matchedBones.get("rightFoot");
3772
+ if (leftFoot && rightFoot) {
3773
+ leftFoot.getWorldPosition(this.restLeftFootModelPos);
3774
+ this.modelRoot.worldToLocal(this.restLeftFootModelPos);
3775
+ rightFoot.getWorldPosition(this.restRightFootModelPos);
3776
+ this.modelRoot.worldToLocal(this.restRightFootModelPos);
3777
+ this.hasRestFootPositions = true;
3778
+ } else {
3779
+ this.hasRestFootPositions = false;
3780
+ }
3781
+ }
3782
+ measureArmRest(upperName, lowerName, defaultLateralX) {
3783
+ const upper = this.joints.get(upperName)?.bone;
3784
+ const lower = this.joints.get(lowerName)?.bone;
3785
+ if (!upper || !lower) {
3786
+ return { angleFromDown: 0, lowerSign: defaultLateralX >= 0 ? -1 : 1 };
3787
+ }
3788
+ upper.getWorldPosition(ScratchMath.v0);
3789
+ lower.getWorldPosition(ScratchMath.v1);
3790
+ this.modelRoot.worldToLocal(ScratchMath.v0);
3791
+ this.modelRoot.worldToLocal(ScratchMath.v1);
3792
+ const dir = ScratchMath.v2.subVectors(ScratchMath.v1, ScratchMath.v0);
3793
+ if (dir.lengthSq() < 1e-8) {
3794
+ return { angleFromDown: 0, lowerSign: defaultLateralX >= 0 ? -1 : 1 };
3795
+ }
3796
+ dir.normalize();
3797
+ const dotDown = Math.max(-1, Math.min(1, dir.dot(ScratchMath.DOWN)));
3798
+ const lateralX = Math.abs(dir.x) > 0.05 ? dir.x : defaultLateralX;
3799
+ return {
3800
+ angleFromDown: Math.acos(dotDown),
3801
+ lowerSign: lateralX >= 0 ? -1 : 1
3802
+ };
3803
+ }
3804
+ /**
3805
+ * Applies a rotation delta defined in character model space (+Y up, +Z forward, +X right/left)
3806
+ * relative to the joint's rest pose, converted cleanly into the bone's local coordinate frame.
3807
+ * Allocation-free and rig-agnostic across Mixamo, Rigify, Unreal, VRM, and generic humanoids.
3808
+ */
3809
+ applyModelDeltaRotation(name, modelDeltaQuat, weight = 1, additive = false) {
3810
+ const joint = this.joints.get(name);
3811
+ const restModel = this.restModelQuat.get(name);
3812
+ const restModelInv = this.restModelQuatInv.get(name);
3813
+ if (!joint || !restModel || !restModelInv || weight <= 1e-3) return;
3814
+ ScratchMath.q5.copy(restModelInv).multiply(modelDeltaQuat).multiply(restModel);
3815
+ if (additive) {
3816
+ ScratchMath.q6.copy(joint.targetQuaternion).multiply(ScratchMath.q5);
3817
+ } else {
3818
+ ScratchMath.q6.copy(joint.restQuaternion).multiply(ScratchMath.q5);
3819
+ }
3820
+ if (weight >= 0.999) {
3821
+ joint.targetQuaternion.copy(ScratchMath.q6);
3822
+ } else {
3823
+ joint.targetQuaternion.slerp(ScratchMath.q6, Math.max(0, Math.min(1, weight)));
3824
+ }
3638
3825
  }
3639
3826
  getJoint(name) {
3640
3827
  return this.joints.get(name);
@@ -3701,67 +3888,102 @@ var AUBRSkeleton = class {
3701
3888
  }
3702
3889
  }
3703
3890
  calculateProportions(bones) {
3704
- const box = new THREE.Box3().setFromObject(this.modelRoot);
3705
- const size = box.getSize(new THREE.Vector3());
3706
- const totalHeight = size.y > 0 ? size.y : 1.78;
3891
+ const box = new THREE.Box3();
3892
+ try {
3893
+ box.setFromObject(this.modelRoot);
3894
+ } catch {
3895
+ box.makeEmpty();
3896
+ }
3897
+ const size = box.isEmpty() ? new THREE.Vector3(0, 0, 0) : box.getSize(new THREE.Vector3());
3898
+ let totalHeight = size.y > 0.01 ? size.y : 0;
3899
+ const head = bones.get("head");
3900
+ const leftFoot = bones.get("leftFoot");
3901
+ const rightFoot = bones.get("rightFoot");
3902
+ const hips = bones.get("hips");
3903
+ if (totalHeight <= 0.01 && head && (leftFoot || rightFoot)) {
3904
+ head.getWorldPosition(ScratchMath.v0);
3905
+ (leftFoot || rightFoot).getWorldPosition(ScratchMath.v1);
3906
+ const headToFoot = Math.abs(ScratchMath.v0.y - ScratchMath.v1.y);
3907
+ if (headToFoot > 0.01) {
3908
+ totalHeight = headToFoot * 1.15;
3909
+ }
3910
+ }
3911
+ if (totalHeight <= 0.01 && head && hips) {
3912
+ head.getWorldPosition(ScratchMath.v0);
3913
+ hips.getWorldPosition(ScratchMath.v1);
3914
+ const headToHips = ScratchMath.v0.distanceTo(ScratchMath.v1);
3915
+ if (headToHips > 0.01) {
3916
+ totalHeight = headToHips * 2.2;
3917
+ }
3918
+ }
3919
+ if (totalHeight <= 0.01) {
3920
+ totalHeight = 1.78;
3921
+ }
3707
3922
  const leftUp = bones.get("leftUpperLeg");
3708
3923
  const leftLow = bones.get("leftLowerLeg");
3709
- const leftFoot = bones.get("leftFoot");
3710
- let thighLength = 0.45;
3711
- let shinLength = 0.43;
3712
- let footHeight = 0.08;
3924
+ let thighLength = totalHeight * 0.25;
3925
+ let shinLength = totalHeight * 0.24;
3926
+ let footHeight = totalHeight * 0.045;
3713
3927
  if (leftUp && leftLow) {
3714
3928
  leftUp.getWorldPosition(ScratchMath.v0);
3715
3929
  leftLow.getWorldPosition(ScratchMath.v1);
3716
- thighLength = ScratchMath.v0.distanceTo(ScratchMath.v1);
3930
+ const d = ScratchMath.v0.distanceTo(ScratchMath.v1);
3931
+ if (d > 1e-4) thighLength = d;
3717
3932
  }
3718
3933
  if (leftLow && leftFoot) {
3719
3934
  leftLow.getWorldPosition(ScratchMath.v0);
3720
3935
  leftFoot.getWorldPosition(ScratchMath.v1);
3721
- shinLength = ScratchMath.v0.distanceTo(ScratchMath.v1);
3722
- footHeight = Math.max(leftFoot.position.y, 0.08);
3936
+ const d = ScratchMath.v0.distanceTo(ScratchMath.v1);
3937
+ if (d > 1e-4) shinLength = d;
3938
+ if (!box.isEmpty()) {
3939
+ const rawFootY = ScratchMath.v1.y - box.min.y;
3940
+ footHeight = Math.max(totalHeight * 0.03, Math.min(totalHeight * 0.12, rawFootY));
3941
+ }
3723
3942
  }
3724
3943
  const legLength = thighLength + shinLength + footHeight;
3725
3944
  const spine = bones.get("spine");
3726
- const head = bones.get("head");
3727
- let spineLength = 0.52;
3945
+ let spineLength = totalHeight * 0.29;
3728
3946
  if (spine && head) {
3729
3947
  spine.getWorldPosition(ScratchMath.v0);
3730
3948
  head.getWorldPosition(ScratchMath.v1);
3731
- spineLength = ScratchMath.v0.distanceTo(ScratchMath.v1);
3949
+ const d = ScratchMath.v0.distanceTo(ScratchMath.v1);
3950
+ if (d > 1e-4) spineLength = d;
3732
3951
  }
3733
3952
  const leftArm = bones.get("leftUpperArm");
3734
3953
  const rightArm = bones.get("rightUpperArm");
3735
- let shoulderWidth = 0.42;
3954
+ let shoulderWidth = totalHeight * 0.23;
3736
3955
  if (leftArm && rightArm) {
3737
3956
  leftArm.getWorldPosition(ScratchMath.v0);
3738
3957
  rightArm.getWorldPosition(ScratchMath.v1);
3739
- shoulderWidth = ScratchMath.v0.distanceTo(ScratchMath.v1);
3958
+ const d = ScratchMath.v0.distanceTo(ScratchMath.v1);
3959
+ if (d > 1e-4) shoulderWidth = d;
3740
3960
  }
3741
3961
  const rightUp = bones.get("rightUpperLeg");
3742
- let hipWidth = 0.28;
3962
+ let hipWidth = totalHeight * 0.15;
3743
3963
  if (leftUp && rightUp) {
3744
3964
  leftUp.getWorldPosition(ScratchMath.v0);
3745
3965
  rightUp.getWorldPosition(ScratchMath.v1);
3746
- hipWidth = ScratchMath.v0.distanceTo(ScratchMath.v1);
3966
+ const d = ScratchMath.v0.distanceTo(ScratchMath.v1);
3967
+ if (d > 1e-4) hipWidth = d;
3747
3968
  }
3748
3969
  const leftHand = bones.get("leftHand");
3749
- let armLength = 0.65;
3970
+ let armLength = totalHeight * 0.36;
3750
3971
  if (leftArm && leftHand) {
3751
3972
  leftArm.getWorldPosition(ScratchMath.v0);
3752
3973
  leftHand.getWorldPosition(ScratchMath.v1);
3753
- armLength = ScratchMath.v0.distanceTo(ScratchMath.v1);
3974
+ const d = ScratchMath.v0.distanceTo(ScratchMath.v1);
3975
+ if (d > 1e-4) armLength = d;
3754
3976
  }
3755
3977
  return {
3756
3978
  totalHeight,
3757
- legLength: Math.max(legLength, 0.4),
3758
- thighLength: Math.max(thighLength, 0.2),
3759
- shinLength: Math.max(shinLength, 0.2),
3760
- footHeight: Math.max(footHeight, 0.05),
3761
- spineLength: Math.max(spineLength, 0.3),
3762
- shoulderWidth: Math.max(shoulderWidth, 0.25),
3763
- hipWidth: Math.max(hipWidth, 0.15),
3764
- armLength: Math.max(armLength, 0.35)
3979
+ legLength: Math.max(legLength, totalHeight * 0.25),
3980
+ thighLength: Math.max(thighLength, totalHeight * 0.12),
3981
+ shinLength: Math.max(shinLength, totalHeight * 0.12),
3982
+ footHeight: Math.max(footHeight, totalHeight * 0.025),
3983
+ spineLength: Math.max(spineLength, totalHeight * 0.15),
3984
+ shoulderWidth: Math.max(shoulderWidth, totalHeight * 0.12),
3985
+ hipWidth: Math.max(hipWidth, totalHeight * 0.08),
3986
+ armLength: Math.max(armLength, totalHeight * 0.2)
3765
3987
  };
3766
3988
  }
3767
3989
  };
@@ -3855,35 +4077,91 @@ var TwoBoneIK = class {
3855
4077
  };
3856
4078
  }
3857
4079
  /**
3858
- * Applies solved two-bone IK rotations to the target AUBR joints in-place.
4080
+ * Applies solved two-bone IK rotations to the target AUBR leg joints in-place
4081
+ * using rest-pose-preserving world-space delta rotations so Mixamo, Rigify, Unreal,
4082
+ * VRM, and generic humanoid bone orientations are never distorted.
4083
+ *
4084
+ * `targetFootPosWorld` and `poleDirWorld` are in world space (or model space when
4085
+ * `modelRoot` is omitted and bones are evaluated in local space).
3859
4086
  */
3860
- static applyLegIK(thighBone, shinBone, footBone, thighLength, shinLength, targetFootPosModel, poleVector, weight = 1, modelRoot) {
4087
+ static applyLegIK(thighBone, shinBone, footBone, thighLength, shinLength, targetFootPosWorld, poleDirWorld, weight = 1, _modelRoot) {
3861
4088
  if (weight <= 1e-3) return;
3862
- const rootPos = ScratchMath.v3;
3863
- thighBone.getWorldPosition(rootPos);
3864
- if (modelRoot) {
3865
- modelRoot.worldToLocal(rootPos);
3866
- } else if (thighBone.parent) {
3867
- thighBone.parent.worldToLocal(rootPos);
3868
- }
3869
- const chain = {
3870
- rootBone: thighBone,
3871
- midBone: shinBone,
3872
- endBone: footBone,
3873
- poleVector,
3874
- length1: thighLength,
3875
- length2: shinLength
3876
- };
3877
- const result = this.solveAnalytical(chain, targetFootPosModel, rootPos, poleVector);
3878
- if (weight >= 0.999) {
3879
- thighBone.quaternion.copy(result.rootRotation);
3880
- shinBone.quaternion.copy(result.midRotation);
4089
+ const clampedWeight = Math.max(0, Math.min(1, weight));
4090
+ const hipPos = ScratchMath.v0;
4091
+ const kneePos = ScratchMath.v1;
4092
+ const anklePos = ScratchMath.v2;
4093
+ thighBone.getWorldPosition(hipPos);
4094
+ shinBone.getWorldPosition(kneePos);
4095
+ footBone.getWorldPosition(anklePos);
4096
+ let l1 = hipPos.distanceTo(kneePos);
4097
+ let l2 = kneePos.distanceTo(anklePos);
4098
+ if (l1 < 1e-5) l1 = Math.max(thighLength, 0.05);
4099
+ if (l2 < 1e-5) l2 = Math.max(shinLength, 0.05);
4100
+ const maxReach = l1 + l2;
4101
+ const minReach = Math.abs(l1 - l2);
4102
+ const toTarget = ScratchMath.v3.subVectors(targetFootPosWorld, hipPos);
4103
+ const dist = toTarget.length();
4104
+ const clampedDist = Math.max(minReach + 1e-4, Math.min(dist, maxReach * 0.99999));
4105
+ if (dist < 1e-6) {
4106
+ toTarget.copy(ScratchMath.DOWN);
3881
4107
  } else {
3882
- thighBone.quaternion.slerp(result.rootRotation, weight);
3883
- shinBone.quaternion.slerp(result.midRotation, weight);
4108
+ toTarget.divideScalar(dist);
4109
+ }
4110
+ const bendDir = ScratchMath.v4.copy(poleDirWorld);
4111
+ const poleProj = bendDir.dot(toTarget);
4112
+ bendDir.addScaledVector(toTarget, -poleProj);
4113
+ if (bendDir.lengthSq() < 1e-6) {
4114
+ bendDir.crossVectors(toTarget, ScratchMath.RIGHT);
4115
+ if (bendDir.lengthSq() < 1e-6) {
4116
+ bendDir.crossVectors(toTarget, ScratchMath.FORWARD);
4117
+ }
4118
+ }
4119
+ bendDir.normalize();
4120
+ const cosAlpha = Math.max(
4121
+ -1,
4122
+ Math.min(1, (l1 * l1 + clampedDist * clampedDist - l2 * l2) / (2 * l1 * clampedDist))
4123
+ );
4124
+ const sinAlpha = Math.sqrt(Math.max(0, 1 - cosAlpha * cosAlpha));
4125
+ const desiredThighDir = ScratchMath.v5.copy(toTarget).multiplyScalar(cosAlpha).addScaledVector(bendDir, sinAlpha).normalize();
4126
+ const desiredKneePos = ScratchMath.v6.copy(hipPos).addScaledVector(desiredThighDir, l1);
4127
+ const desiredShinDir = ScratchMath.v7.subVectors(targetFootPosWorld, desiredKneePos);
4128
+ if (desiredShinDir.lengthSq() < 1e-8) {
4129
+ desiredShinDir.copy(toTarget);
4130
+ } else {
4131
+ desiredShinDir.normalize();
4132
+ }
4133
+ const currentThighDir = ScratchMath.v8.subVectors(kneePos, hipPos);
4134
+ if (currentThighDir.lengthSq() > 1e-10) {
4135
+ currentThighDir.normalize();
4136
+ ScratchMath.q0.setFromUnitVectors(currentThighDir, desiredThighDir);
4137
+ thighBone.getWorldQuaternion(ScratchMath.q1);
4138
+ ScratchMath.q2.copy(ScratchMath.q1).invert();
4139
+ ScratchMath.q3.copy(ScratchMath.q2).multiply(ScratchMath.q0).multiply(ScratchMath.q1);
4140
+ ScratchMath.q4.copy(thighBone.quaternion).multiply(ScratchMath.q3);
4141
+ if (clampedWeight >= 0.999) {
4142
+ thighBone.quaternion.copy(ScratchMath.q4);
4143
+ } else {
4144
+ thighBone.quaternion.slerp(ScratchMath.q4, clampedWeight);
4145
+ }
4146
+ thighBone.updateMatrixWorld(true);
4147
+ }
4148
+ shinBone.getWorldPosition(kneePos);
4149
+ footBone.getWorldPosition(anklePos);
4150
+ const currentShinDir = ScratchMath.v8.subVectors(anklePos, kneePos);
4151
+ if (currentShinDir.lengthSq() > 1e-10) {
4152
+ currentShinDir.normalize();
4153
+ ScratchMath.q0.setFromUnitVectors(currentShinDir, desiredShinDir);
4154
+ shinBone.getWorldQuaternion(ScratchMath.q1);
4155
+ ScratchMath.q2.copy(ScratchMath.q1).invert();
4156
+ ScratchMath.q3.copy(ScratchMath.q2).multiply(ScratchMath.q0).multiply(ScratchMath.q1);
4157
+ ScratchMath.q4.copy(shinBone.quaternion).multiply(ScratchMath.q3);
4158
+ if (clampedWeight >= 0.999) {
4159
+ shinBone.quaternion.copy(ScratchMath.q4);
4160
+ } else {
4161
+ shinBone.quaternion.slerp(ScratchMath.q4, clampedWeight);
4162
+ }
4163
+ shinBone.updateMatrixWorld(true);
3884
4164
  }
3885
- ScratchMath.q2.setFromAxisAngle(ScratchMath.RIGHT, 0);
3886
- footBone.quaternion.slerp(ScratchMath.q2, weight * 0.85);
3887
4165
  }
3888
4166
  };
3889
4167
  var _FABRIKSolver = class _FABRIKSolver {
@@ -4036,43 +4314,52 @@ var _FABRIKSolver = class _FABRIKSolver {
4036
4314
  };
4037
4315
  }
4038
4316
  /**
4039
- * Projects middle hinge joint onto plane defined by pole vector.
4317
+ * Projects middle hinge joint onto the half-plane defined by the pole direction vector.
4040
4318
  */
4041
4319
  static applyPoleVector(root, hinge, end, pole) {
4042
- const line = ScratchMath.v1.subVectors(end, root).normalize();
4320
+ const line = ScratchMath.v1.subVectors(end, root);
4321
+ if (line.lengthSq() < 1e-10) return;
4322
+ line.normalize();
4043
4323
  const toHinge = ScratchMath.v2.subVectors(hinge, root);
4044
4324
  const projDist = toHinge.dot(line);
4045
4325
  const projPoint = ScratchMath.v3.copy(root).addScaledVector(line, projDist);
4046
- const currentNormal = ScratchMath.v4.subVectors(hinge, projPoint).normalize();
4047
- const desiredNormal = ScratchMath.v5.subVectors(pole, projPoint).normalize();
4048
- if (currentNormal.lengthSq() > 1e-3 && desiredNormal.lengthSq() > 1e-3) {
4326
+ const desiredNormal = ScratchMath.v5.copy(pole).addScaledVector(line, -pole.dot(line));
4327
+ if (desiredNormal.lengthSq() > 1e-6) {
4328
+ desiredNormal.normalize();
4049
4329
  const hingeDist = hinge.distanceTo(projPoint);
4050
- hinge.copy(projPoint).addScaledVector(desiredNormal, Math.max(hingeDist, 0.05));
4330
+ hinge.copy(projPoint).addScaledVector(desiredNormal, Math.max(hingeDist, 0.02));
4051
4331
  }
4052
4332
  }
4053
4333
  /**
4054
4334
  * Converts solved world positions back into local bone rotations for the Three.js hierarchy.
4335
+ * Calculates delta rotation in world space from current bone direction to solved target direction,
4336
+ * transforming into local bone space to guarantee zero deformation when positions match rest pose.
4055
4337
  */
4056
4338
  static applyPositionsToBones(chain, solvedPositions, weight = 1) {
4057
4339
  if (solvedPositions.length < 2 || weight <= 1e-3) return;
4058
4340
  for (let i = 0; i < chain.bones.length - 1; i++) {
4059
4341
  const bone = chain.bones[i];
4342
+ const nextBone = chain.bones[i + 1];
4060
4343
  const nextPos = solvedPositions[i + 1];
4061
- if (!bone || !nextPos) continue;
4344
+ if (!bone || !nextBone || !nextPos) continue;
4062
4345
  bone.getWorldPosition(ScratchMath.v0);
4346
+ nextBone.getWorldPosition(ScratchMath.v2);
4347
+ const currentDirWorld = ScratchMath.v3.subVectors(ScratchMath.v2, ScratchMath.v0);
4348
+ if (currentDirWorld.lengthSq() < 1e-10) continue;
4349
+ currentDirWorld.normalize();
4063
4350
  const targetDirWorld = ScratchMath.v1.subVectors(nextPos, ScratchMath.v0);
4064
4351
  if (targetDirWorld.lengthSq() < 1e-10) continue;
4065
4352
  targetDirWorld.normalize();
4066
- if (bone.parent) {
4067
- ScratchMath.m0.copy(bone.parent.matrixWorld).invert();
4068
- targetDirWorld.transformDirection(ScratchMath.m0).normalize();
4069
- }
4070
- const restDir = ScratchMath.RIGHT;
4071
- ScratchMath.q0.setFromUnitVectors(restDir, targetDirWorld);
4353
+ ScratchMath.q0.setFromUnitVectors(currentDirWorld, targetDirWorld);
4354
+ bone.getWorldQuaternion(ScratchMath.q1);
4355
+ ScratchMath.q2.copy(ScratchMath.q1).invert();
4356
+ ScratchMath.q3.copy(ScratchMath.q2).multiply(ScratchMath.q0).multiply(ScratchMath.q1);
4357
+ const currentLocalQuat = ScratchMath.q4.copy(bone.quaternion);
4358
+ const targetLocalQuat = ScratchMath.q3.premultiply(currentLocalQuat);
4072
4359
  if (weight >= 0.999) {
4073
- bone.quaternion.copy(ScratchMath.q0);
4360
+ bone.quaternion.copy(targetLocalQuat);
4074
4361
  } else {
4075
- bone.quaternion.slerp(ScratchMath.q0, weight);
4362
+ bone.quaternion.copy(currentLocalQuat).slerp(targetLocalQuat, weight);
4076
4363
  }
4077
4364
  bone.updateMatrixWorld(true);
4078
4365
  }
@@ -4104,21 +4391,25 @@ var IKCoordinator = class {
4104
4391
  }
4105
4392
  /**
4106
4393
  * Solves and applies left leg Two-Bone IK.
4394
+ * `targetFootPosModel` is in `skeleton.modelRoot` local space.
4107
4395
  */
4108
4396
  solveLeftLeg(targetFootPosModel, weight = 1) {
4109
4397
  const thigh = this.skeleton.getJoint("leftUpperLeg");
4110
4398
  const shin = this.skeleton.getJoint("leftLowerLeg");
4111
4399
  const foot = this.skeleton.getJoint("leftFoot");
4112
- if (!thigh || !shin || !foot) return;
4113
- const pole = ScratchMath.v6.copy(ScratchMath.FORWARD);
4400
+ if (!thigh || !shin || !foot || weight <= 1e-3) return;
4401
+ const targetWorld = ScratchMath.v9.copy(targetFootPosModel);
4402
+ this.skeleton.modelRoot.localToWorld(targetWorld);
4403
+ this.skeleton.modelRoot.getWorldQuaternion(ScratchMath.q5);
4404
+ const poleWorld = ScratchMath.v8.copy(ScratchMath.FORWARD).applyQuaternion(ScratchMath.q5).normalize();
4114
4405
  TwoBoneIK.applyLegIK(
4115
4406
  thigh.bone,
4116
4407
  shin.bone,
4117
4408
  foot.bone,
4118
4409
  this.skeleton.proportions.thighLength,
4119
4410
  this.skeleton.proportions.shinLength,
4120
- targetFootPosModel,
4121
- pole,
4411
+ targetWorld,
4412
+ poleWorld,
4122
4413
  weight,
4123
4414
  this.skeleton.modelRoot
4124
4415
  );
@@ -4128,21 +4419,25 @@ var IKCoordinator = class {
4128
4419
  }
4129
4420
  /**
4130
4421
  * Solves and applies right leg Two-Bone IK.
4422
+ * `targetFootPosModel` is in `skeleton.modelRoot` local space.
4131
4423
  */
4132
4424
  solveRightLeg(targetFootPosModel, weight = 1) {
4133
4425
  const thigh = this.skeleton.getJoint("rightUpperLeg");
4134
4426
  const shin = this.skeleton.getJoint("rightLowerLeg");
4135
4427
  const foot = this.skeleton.getJoint("rightFoot");
4136
- if (!thigh || !shin || !foot) return;
4137
- const pole = ScratchMath.v6.copy(ScratchMath.FORWARD);
4428
+ if (!thigh || !shin || !foot || weight <= 1e-3) return;
4429
+ const targetWorld = ScratchMath.v9.copy(targetFootPosModel);
4430
+ this.skeleton.modelRoot.localToWorld(targetWorld);
4431
+ this.skeleton.modelRoot.getWorldQuaternion(ScratchMath.q5);
4432
+ const poleWorld = ScratchMath.v8.copy(ScratchMath.FORWARD).applyQuaternion(ScratchMath.q5).normalize();
4138
4433
  TwoBoneIK.applyLegIK(
4139
4434
  thigh.bone,
4140
4435
  shin.bone,
4141
4436
  foot.bone,
4142
4437
  this.skeleton.proportions.thighLength,
4143
4438
  this.skeleton.proportions.shinLength,
4144
- targetFootPosModel,
4145
- pole,
4439
+ targetWorld,
4440
+ poleWorld,
4146
4441
  weight,
4147
4442
  this.skeleton.modelRoot
4148
4443
  );
@@ -4156,7 +4451,8 @@ var IKCoordinator = class {
4156
4451
  solveArmReach(side, targetPosWorld, weight = 1) {
4157
4452
  const chain = side === "left" ? this.leftArmChain : this.rightArmChain;
4158
4453
  if (!chain || weight <= 1e-3) return;
4159
- const pole = side === "left" ? ScratchMath.v7.set(-0.5, 0, -1).normalize() : ScratchMath.v7.set(0.5, 0, -1).normalize();
4454
+ this.skeleton.modelRoot.getWorldQuaternion(ScratchMath.q5);
4455
+ const pole = (side === "left" ? ScratchMath.v7.set(-0.5, 0, -1).normalize() : ScratchMath.v7.set(0.5, 0, -1).normalize()).applyQuaternion(ScratchMath.q5);
4160
4456
  const result = FABRIKSolver.solve(chain, targetPosWorld, pole);
4161
4457
  FABRIKSolver.applyPositionsToBones(chain, result.solvedPositions, weight);
4162
4458
  const upper = this.skeleton.getJoint(side === "left" ? "leftUpperArm" : "rightUpperArm");
@@ -4180,63 +4476,86 @@ var IdleEngine = class {
4180
4476
  this.targetWeightShift = 0;
4181
4477
  this.nextWeightShiftTime = 3.5;
4182
4478
  }
4479
+ reset() {
4480
+ this.clock = 0;
4481
+ this.breathPhase = 0;
4482
+ this.swayPhase = 0;
4483
+ this.weightShift = 0;
4484
+ this.targetWeightShift = 0;
4485
+ this.nextWeightShiftTime = 3.5;
4486
+ }
4183
4487
  update(delta, weight = 1) {
4184
4488
  if (weight <= 1e-3) return;
4489
+ const w = Math.max(0, Math.min(1, weight));
4185
4490
  this.clock += delta;
4186
4491
  this.breathPhase += delta * 0.28;
4187
- this.swayPhase += delta * 0.12;
4492
+ this.swayPhase += delta * 0.14;
4188
4493
  if (this.clock > this.nextWeightShiftTime) {
4189
- this.targetWeightShift = (Math.random() - 0.5) * 1.6;
4494
+ this.targetWeightShift = (Math.random() - 0.5) * 1.4;
4190
4495
  this.nextWeightShiftTime = this.clock + 4 + Math.random() * 4;
4191
4496
  }
4192
- this.weightShift = ScratchMath.damp(this.weightShift, this.targetWeightShift, 1.2, delta);
4497
+ this.weightShift = ScratchMath.damp(this.weightShift, this.targetWeightShift, 1.4, delta);
4498
+ const localScale = this.skeleton.hipsLocalPerMeter;
4193
4499
  const breathSin = Math.sin(this.breathPhase * Math.PI * 2);
4194
- const spinePitch = breathSin * 0.015;
4195
- const chestPitch = breathSin * 0.022;
4500
+ const spinePitch = breathSin * 0.022 * w;
4501
+ const chestPitch = breathSin * 0.028 * w;
4196
4502
  const swaySin = Math.sin(this.swayPhase * Math.PI * 2);
4197
- const pelvicX = (swaySin * 6e-3 + this.weightShift * 0.012) * weight;
4198
- const pelvicY = breathSin * 4e-3 * weight;
4199
- const pelvicRoll = -this.weightShift * 0.018 * weight;
4503
+ const pelvicX = (swaySin * 8e-3 + this.weightShift * 0.014) * localScale * w;
4504
+ const pelvicY = breathSin * 5e-3 * localScale * w;
4505
+ const pelvicRoll = -this.weightShift * 0.022 * w;
4200
4506
  const hips = this.skeleton.getJoint("hips");
4201
4507
  if (hips) {
4202
4508
  hips.targetPosition.x = hips.restPosition.x + pelvicX;
4203
4509
  hips.targetPosition.y = hips.restPosition.y + pelvicY;
4204
4510
  ScratchMath.e0.set(0, 0, pelvicRoll, "YXZ");
4205
4511
  ScratchMath.q0.setFromEuler(ScratchMath.e0);
4206
- hips.targetQuaternion.copy(hips.restQuaternion).multiply(ScratchMath.q0);
4512
+ this.skeleton.applyModelDeltaRotation("hips", ScratchMath.q0, w);
4207
4513
  }
4208
- const spine = this.skeleton.getJoint("spine");
4209
- if (spine) {
4210
- ScratchMath.e0.set(spinePitch * weight, 0, 0, "YXZ");
4514
+ if (this.skeleton.hasJoint("spine")) {
4515
+ ScratchMath.e0.set(spinePitch, swaySin * 0.012 * w, -pelvicRoll * 0.4, "YXZ");
4211
4516
  ScratchMath.q0.setFromEuler(ScratchMath.e0);
4212
- spine.targetQuaternion.copy(spine.restQuaternion).multiply(ScratchMath.q0);
4517
+ this.skeleton.applyModelDeltaRotation("spine", ScratchMath.q0, w);
4213
4518
  }
4214
- const chest = this.skeleton.getJoint("chest") || this.skeleton.getJoint("upperChest");
4215
- if (chest) {
4216
- ScratchMath.e0.set(chestPitch * weight, 0, 0, "YXZ");
4519
+ const chestName = this.skeleton.hasJoint("chest") ? "chest" : this.skeleton.hasJoint("upperChest") ? "upperChest" : null;
4520
+ if (chestName) {
4521
+ ScratchMath.e0.set(chestPitch, 0, 0, "YXZ");
4217
4522
  ScratchMath.q0.setFromEuler(ScratchMath.e0);
4218
- chest.targetQuaternion.copy(chest.restQuaternion).multiply(ScratchMath.q0);
4523
+ this.skeleton.applyModelDeltaRotation(chestName, ScratchMath.q0, w);
4219
4524
  }
4220
- const head = this.skeleton.getJoint("head");
4221
- if (head) {
4222
- const headCounterRoll = -pelvicRoll * 0.6;
4223
- const headCounterPitch = -spinePitch * 0.5;
4525
+ if (this.skeleton.hasJoint("head")) {
4526
+ const headCounterRoll = -pelvicRoll * 0.5;
4527
+ const headCounterPitch = -spinePitch * 0.45;
4224
4528
  ScratchMath.e0.set(headCounterPitch, 0, headCounterRoll, "YXZ");
4225
4529
  ScratchMath.q0.setFromEuler(ScratchMath.e0);
4226
- head.targetQuaternion.copy(head.restQuaternion).multiply(ScratchMath.q0);
4227
- }
4228
- const lArm = this.skeleton.getJoint("leftUpperArm");
4229
- const rArm = this.skeleton.getJoint("rightUpperArm");
4230
- const armSway = breathSin * 0.012 * weight;
4231
- if (lArm) {
4232
- ScratchMath.e0.set(armSway, 0, armSway * 0.5, "YXZ");
4233
- ScratchMath.q0.setFromEuler(ScratchMath.e0);
4234
- lArm.targetQuaternion.copy(lArm.restQuaternion).multiply(ScratchMath.q0);
4235
- }
4236
- if (rArm) {
4237
- ScratchMath.e0.set(armSway, 0, -armSway * 0.5, "YXZ");
4238
- ScratchMath.q0.setFromEuler(ScratchMath.e0);
4239
- rArm.targetQuaternion.copy(rArm.restQuaternion).multiply(ScratchMath.q0);
4530
+ this.skeleton.applyModelDeltaRotation("head", ScratchMath.q0, w);
4531
+ }
4532
+ const targetStandingArmAngle = 0.28;
4533
+ const armBreathSway = breathSin * 0.025;
4534
+ const armOutSway = breathSin * 0.018;
4535
+ if (this.skeleton.hasJoint("leftUpperArm")) {
4536
+ const relaxAngle = Math.max(0, this.skeleton.leftArmRestAngleFromDown - targetStandingArmAngle);
4537
+ const lowerZ = this.skeleton.leftArmLowerSign * (relaxAngle - armOutSway);
4538
+ ScratchMath.q1.setFromAxisAngle(ScratchMath.FORWARD, lowerZ);
4539
+ ScratchMath.q2.setFromAxisAngle(ScratchMath.RIGHT, -armBreathSway);
4540
+ ScratchMath.q0.copy(ScratchMath.q2).multiply(ScratchMath.q1);
4541
+ this.skeleton.applyModelDeltaRotation("leftUpperArm", ScratchMath.q0, w);
4542
+ }
4543
+ if (this.skeleton.hasJoint("rightUpperArm")) {
4544
+ const relaxAngle = Math.max(0, this.skeleton.rightArmRestAngleFromDown - targetStandingArmAngle);
4545
+ const lowerZ = this.skeleton.rightArmLowerSign * (relaxAngle - armOutSway);
4546
+ ScratchMath.q1.setFromAxisAngle(ScratchMath.FORWARD, lowerZ);
4547
+ ScratchMath.q2.setFromAxisAngle(ScratchMath.RIGHT, -armBreathSway);
4548
+ ScratchMath.q0.copy(ScratchMath.q2).multiply(ScratchMath.q1);
4549
+ this.skeleton.applyModelDeltaRotation("rightUpperArm", ScratchMath.q0, w);
4550
+ }
4551
+ const elbowRelaxFlex = 0.16 + breathSin * 0.015;
4552
+ if (this.skeleton.hasJoint("leftLowerArm")) {
4553
+ ScratchMath.q0.setFromAxisAngle(ScratchMath.RIGHT, -elbowRelaxFlex);
4554
+ this.skeleton.applyModelDeltaRotation("leftLowerArm", ScratchMath.q0, w);
4555
+ }
4556
+ if (this.skeleton.hasJoint("rightLowerArm")) {
4557
+ ScratchMath.q0.setFromAxisAngle(ScratchMath.RIGHT, -elbowRelaxFlex);
4558
+ this.skeleton.applyModelDeltaRotation("rightLowerArm", ScratchMath.q0, w);
4240
4559
  }
4241
4560
  }
4242
4561
  };
@@ -4279,9 +4598,10 @@ var LocomotionEngine = class {
4279
4598
  return this.phase;
4280
4599
  }
4281
4600
  calibrateStride() {
4282
- const legLen = this.skeleton.proportions.legLength;
4283
- this.strideLength = Math.max(0.35, legLen * 0.78);
4284
- this.stepHeight = Math.max(0.04, legLen * 0.095);
4601
+ const unitScale = this.skeleton.hipsLocalPerMeter;
4602
+ const legLenLocal = Math.max(0.4 * unitScale, this.skeleton.proportions.legLength / Math.max(this.skeleton.worldUnitsPerMeter, 1e-4) * unitScale);
4603
+ this.strideLength = Math.max(0.35 * unitScale, legLenLocal * 0.72);
4604
+ this.stepHeight = Math.max(0.04 * unitScale, legLenLocal * 0.11);
4285
4605
  }
4286
4606
  setMotion(params) {
4287
4607
  this.targetSpeed = Math.max(0, params.speed);
@@ -4291,113 +4611,128 @@ var LocomotionEngine = class {
4291
4611
  const stridesPerSecond = params.screenVelocityPx / 180;
4292
4612
  this.baseFrequency = Math.max(0.8, Math.min(3.2, stridesPerSecond * 2));
4293
4613
  } else {
4294
- this.baseFrequency = this.isRunning ? 2.6 : 1.5;
4614
+ this.baseFrequency = this.isRunning ? 2.4 : 1.35;
4295
4615
  }
4296
4616
  }
4297
4617
  stop() {
4298
4618
  this.targetSpeed = 0;
4299
4619
  this.moving = false;
4300
4620
  }
4621
+ cancel() {
4622
+ this.targetSpeed = 0;
4623
+ this.currentSpeed = 0;
4624
+ this.moving = false;
4625
+ this.isRunning = false;
4626
+ }
4301
4627
  update(delta, weight = 1) {
4302
- this.currentSpeed = ScratchMath.damp(this.currentSpeed, this.targetSpeed, 4, delta);
4628
+ const speedDamp = this.moving ? 6.5 : 8;
4629
+ this.currentSpeed = ScratchMath.damp(this.currentSpeed, this.targetSpeed, speedDamp, delta);
4303
4630
  if (this.currentSpeed <= 0.01 && weight <= 1e-3) return;
4304
- const effectiveFreq = this.baseFrequency * Math.max(0.3, this.currentSpeed);
4631
+ const w = Math.max(0, Math.min(1, weight));
4632
+ const speedRatio = Math.min(this.currentSpeed, 2.2);
4633
+ const activeAmplitude = Math.min(1, this.currentSpeed / 0.35) * w;
4634
+ if (activeAmplitude <= 1e-3) return;
4635
+ const effectiveFreq = this.baseFrequency * Math.max(0.35, Math.min(1.6, this.currentSpeed));
4305
4636
  this.phase = (this.phase + effectiveFreq * delta) % 1;
4306
4637
  const phi = this.phase;
4307
4638
  const twoPiPhi = phi * Math.PI * 2;
4308
- const speedRatio = Math.min(this.currentSpeed, 2);
4309
- const bobAmplitude = (this.isRunning ? 0.045 : 0.024) * speedRatio;
4639
+ const localScale = this.skeleton.hipsLocalPerMeter;
4640
+ const bobAmplitude = (this.isRunning ? 0.05 : 0.028) * speedRatio * localScale;
4310
4641
  const pelvicY = -bobAmplitude * Math.abs(Math.sin(twoPiPhi));
4311
- const swayAmplitude = (this.isRunning ? 0.018 : 0.028) * speedRatio;
4642
+ const swayAmplitude = (this.isRunning ? 0.02 : 0.03) * speedRatio * localScale;
4312
4643
  const pelvicX = swayAmplitude * Math.sin(twoPiPhi);
4313
4644
  const yawAmplitude = (this.isRunning ? 0.14 : 0.09) * speedRatio;
4314
4645
  const pelvicYaw = yawAmplitude * Math.cos(twoPiPhi);
4315
- const rollAmplitude = 0.035 * speedRatio;
4646
+ const rollAmplitude = 0.04 * speedRatio;
4316
4647
  const pelvicRoll = -rollAmplitude * Math.sin(twoPiPhi);
4317
4648
  const hips = this.skeleton.getJoint("hips");
4318
4649
  if (hips) {
4319
- hips.targetPosition.x = hips.restPosition.x + pelvicX * weight;
4320
- hips.targetPosition.y = hips.restPosition.y + pelvicY * weight;
4321
- ScratchMath.e0.set(0.04 * speedRatio, pelvicYaw * weight, pelvicRoll * weight, "YXZ");
4650
+ hips.targetPosition.x = hips.restPosition.x + pelvicX * w;
4651
+ hips.targetPosition.y = hips.restPosition.y + pelvicY * w;
4652
+ ScratchMath.e0.set((this.isRunning ? 0.08 : 0.04) * speedRatio, pelvicYaw, pelvicRoll, "YXZ");
4322
4653
  ScratchMath.q0.setFromEuler(ScratchMath.e0);
4323
- hips.targetQuaternion.copy(hips.restQuaternion).multiply(ScratchMath.q0);
4654
+ this.skeleton.applyModelDeltaRotation("hips", ScratchMath.q0, activeAmplitude);
4324
4655
  }
4325
- const spine = this.skeleton.getJoint("spine");
4326
- if (spine) {
4327
- const spineCounterYaw = -pelvicYaw * 0.65 * weight;
4328
- ScratchMath.e0.set(-0.02 * speedRatio, spineCounterYaw, 0, "YXZ");
4656
+ if (this.skeleton.hasJoint("spine")) {
4657
+ const spineCounterYaw = -pelvicYaw * 0.65;
4658
+ ScratchMath.e0.set((this.isRunning ? 0.08 : 0.03) * speedRatio, spineCounterYaw, -pelvicRoll * 0.35, "YXZ");
4329
4659
  ScratchMath.q0.setFromEuler(ScratchMath.e0);
4330
- spine.targetQuaternion.copy(spine.restQuaternion).multiply(ScratchMath.q0);
4660
+ this.skeleton.applyModelDeltaRotation("spine", ScratchMath.q0, activeAmplitude);
4331
4661
  }
4332
- const chest = this.skeleton.getJoint("chest") || this.skeleton.getJoint("upperChest");
4333
- if (chest) {
4334
- const chestCounterYaw = -pelvicYaw * 0.35 * weight;
4335
- ScratchMath.e0.set(0.01, chestCounterYaw, 0, "YXZ");
4662
+ const chestName = this.skeleton.hasJoint("chest") ? "chest" : this.skeleton.hasJoint("upperChest") ? "upperChest" : null;
4663
+ if (chestName) {
4664
+ const chestCounterYaw = -pelvicYaw * 0.35;
4665
+ ScratchMath.e0.set(0.02 * speedRatio, chestCounterYaw, 0, "YXZ");
4336
4666
  ScratchMath.q0.setFromEuler(ScratchMath.e0);
4337
- chest.targetQuaternion.copy(chest.restQuaternion).multiply(ScratchMath.q0);
4338
- }
4339
- const hipWidthHalf = this.skeleton.proportions.hipWidth * 0.5;
4340
- const groundY = 0;
4341
- const halfStride = this.strideLength * 0.5 * speedRatio;
4342
- let leftX = -hipWidthHalf;
4343
- let leftY = groundY;
4344
- let leftZ = 0;
4667
+ this.skeleton.applyModelDeltaRotation(chestName, ScratchMath.q0, activeAmplitude);
4668
+ }
4669
+ const halfStride = this.strideLength * 0.5 * Math.min(speedRatio, 1.45);
4670
+ const liftHeight = this.stepHeight * Math.min(speedRatio, 1.5);
4671
+ const baseLeftX = this.skeleton.hasRestFootPositions ? this.skeleton.restLeftFootModelPos.x : -this.skeleton.proportions.hipWidth * 0.5;
4672
+ const baseLeftY = this.skeleton.hasRestFootPositions ? this.skeleton.restLeftFootModelPos.y : 0;
4673
+ const baseLeftZ = this.skeleton.hasRestFootPositions ? this.skeleton.restLeftFootModelPos.z : 0;
4674
+ const baseRightX = this.skeleton.hasRestFootPositions ? this.skeleton.restRightFootModelPos.x : this.skeleton.proportions.hipWidth * 0.5;
4675
+ const baseRightY = this.skeleton.hasRestFootPositions ? this.skeleton.restRightFootModelPos.y : 0;
4676
+ const baseRightZ = this.skeleton.hasRestFootPositions ? this.skeleton.restRightFootModelPos.z : 0;
4677
+ let leftY = baseLeftY;
4678
+ let leftZ = baseLeftZ;
4345
4679
  if (phi < 0.5) {
4346
4680
  const s = phi / 0.5;
4347
- leftZ = halfStride * (1 - 2 * s);
4348
- leftY = groundY;
4681
+ leftZ = baseLeftZ + halfStride * (1 - 2 * s);
4349
4682
  } else {
4350
4683
  const s = (phi - 0.5) / 0.5;
4351
- leftZ = halfStride * (2 * s - 1);
4352
- leftY = groundY + this.stepHeight * speedRatio * (4 * s * (1 - s));
4684
+ leftZ = baseLeftZ + halfStride * (2 * s - 1);
4685
+ leftY = baseLeftY + liftHeight * (4 * s * (1 - s));
4353
4686
  }
4354
- let rightX = hipWidthHalf;
4355
- let rightY = groundY;
4356
- let rightZ = 0;
4687
+ let rightY = baseRightY;
4688
+ let rightZ = baseRightZ;
4357
4689
  if (phi < 0.5) {
4358
4690
  const s = phi / 0.5;
4359
- rightZ = halfStride * (2 * s - 1);
4360
- rightY = groundY + this.stepHeight * speedRatio * (4 * s * (1 - s));
4691
+ rightZ = baseRightZ + halfStride * (2 * s - 1);
4692
+ rightY = baseRightY + liftHeight * (4 * s * (1 - s));
4361
4693
  } else {
4362
4694
  const s = (phi - 0.5) / 0.5;
4363
- rightZ = halfStride * (1 - 2 * s);
4364
- rightY = groundY;
4695
+ rightZ = baseRightZ + halfStride * (1 - 2 * s);
4365
4696
  }
4366
- this.leftFootTarget.set(leftX, leftY, leftZ);
4367
- this.rightFootTarget.set(rightX, rightY, rightZ);
4697
+ this.leftFootTarget.set(baseLeftX, leftY, leftZ);
4698
+ this.rightFootTarget.set(baseRightX, rightY, rightZ);
4368
4699
  if (this.ik) {
4369
- this.ik.solveLeftLeg(this.leftFootTarget, weight);
4370
- this.ik.solveRightLeg(this.rightFootTarget, weight);
4371
- }
4372
- const armSwingAngle = (this.isRunning ? 0.65 : 0.38) * speedRatio * weight;
4373
- const lArmAngle = -Math.sin(twoPiPhi) * armSwingAngle;
4374
- const rArmAngle = Math.sin(twoPiPhi) * armSwingAngle;
4375
- const lArm = this.skeleton.getJoint("leftUpperArm");
4376
- const rArm = this.skeleton.getJoint("rightUpperArm");
4377
- const lForeArm = this.skeleton.getJoint("leftLowerArm");
4378
- const rForeArm = this.skeleton.getJoint("rightLowerArm");
4379
- if (lArm) {
4380
- ScratchMath.e0.set(lArmAngle, 0, 0.05, "YXZ");
4381
- ScratchMath.q0.setFromEuler(ScratchMath.e0);
4382
- lArm.targetQuaternion.copy(lArm.restQuaternion).multiply(ScratchMath.q0);
4383
- }
4384
- if (rArm) {
4385
- ScratchMath.e0.set(rArmAngle, 0, -0.05, "YXZ");
4386
- ScratchMath.q0.setFromEuler(ScratchMath.e0);
4387
- rArm.targetQuaternion.copy(rArm.restQuaternion).multiply(ScratchMath.q0);
4388
- }
4389
- const elbowFlexBase = this.isRunning ? 0.55 : 0.25;
4390
- if (lForeArm) {
4391
- const flex = elbowFlexBase + Math.max(0, -Math.sin(twoPiPhi)) * 0.35;
4392
- ScratchMath.e0.set(flex * weight, 0, 0, "YXZ");
4393
- ScratchMath.q0.setFromEuler(ScratchMath.e0);
4394
- lForeArm.targetQuaternion.copy(lForeArm.restQuaternion).multiply(ScratchMath.q0);
4395
- }
4396
- if (rForeArm) {
4397
- const flex = elbowFlexBase + Math.max(0, Math.sin(twoPiPhi)) * 0.35;
4398
- ScratchMath.e0.set(flex * weight, 0, 0, "YXZ");
4399
- ScratchMath.q0.setFromEuler(ScratchMath.e0);
4400
- rForeArm.targetQuaternion.copy(rForeArm.restQuaternion).multiply(ScratchMath.q0);
4700
+ if (hips) {
4701
+ hips.bone.position.copy(hips.targetPosition);
4702
+ hips.bone.quaternion.copy(hips.targetQuaternion);
4703
+ hips.bone.updateMatrixWorld(true);
4704
+ }
4705
+ this.ik.solveLeftLeg(this.leftFootTarget, activeAmplitude);
4706
+ this.ik.solveRightLeg(this.rightFootTarget, activeAmplitude);
4707
+ }
4708
+ const targetStandingArmAngle = 0.25;
4709
+ const armSwingAngle = (this.isRunning ? 0.72 : 0.42) * Math.min(speedRatio, 1.5);
4710
+ const lArmForward = -Math.sin(twoPiPhi) * armSwingAngle;
4711
+ const rArmForward = Math.sin(twoPiPhi) * armSwingAngle;
4712
+ if (this.skeleton.hasJoint("leftUpperArm")) {
4713
+ const relaxAngle = Math.max(0, this.skeleton.leftArmRestAngleFromDown - targetStandingArmAngle);
4714
+ ScratchMath.q1.setFromAxisAngle(ScratchMath.FORWARD, this.skeleton.leftArmLowerSign * relaxAngle);
4715
+ ScratchMath.q2.setFromAxisAngle(ScratchMath.RIGHT, -lArmForward);
4716
+ ScratchMath.q0.copy(ScratchMath.q2).multiply(ScratchMath.q1);
4717
+ this.skeleton.applyModelDeltaRotation("leftUpperArm", ScratchMath.q0, activeAmplitude);
4718
+ }
4719
+ if (this.skeleton.hasJoint("rightUpperArm")) {
4720
+ const relaxAngle = Math.max(0, this.skeleton.rightArmRestAngleFromDown - targetStandingArmAngle);
4721
+ ScratchMath.q1.setFromAxisAngle(ScratchMath.FORWARD, this.skeleton.rightArmLowerSign * relaxAngle);
4722
+ ScratchMath.q2.setFromAxisAngle(ScratchMath.RIGHT, -rArmForward);
4723
+ ScratchMath.q0.copy(ScratchMath.q2).multiply(ScratchMath.q1);
4724
+ this.skeleton.applyModelDeltaRotation("rightUpperArm", ScratchMath.q0, activeAmplitude);
4725
+ }
4726
+ const elbowFlexBase = this.isRunning ? 0.85 : 0.28;
4727
+ if (this.skeleton.hasJoint("leftLowerArm")) {
4728
+ const flex = elbowFlexBase + Math.max(0, lArmForward) * 0.45;
4729
+ ScratchMath.q0.setFromAxisAngle(ScratchMath.RIGHT, -flex);
4730
+ this.skeleton.applyModelDeltaRotation("leftLowerArm", ScratchMath.q0, activeAmplitude);
4731
+ }
4732
+ if (this.skeleton.hasJoint("rightLowerArm")) {
4733
+ const flex = elbowFlexBase + Math.max(0, rArmForward) * 0.45;
4734
+ ScratchMath.q0.setFromAxisAngle(ScratchMath.RIGHT, -flex);
4735
+ this.skeleton.applyModelDeltaRotation("rightLowerArm", ScratchMath.q0, activeAmplitude);
4401
4736
  }
4402
4737
  }
4403
4738
  getPhase() {
@@ -4410,18 +4745,26 @@ var LocomotionEngine = class {
4410
4745
 
4411
4746
  // src/avatar/procedural/TurnEngine.ts
4412
4747
  var TurnEngine = class {
4413
- constructor(skeleton) {
4748
+ constructor(skeleton, turnTarget) {
4414
4749
  this.skeleton = skeleton;
4415
4750
  this.currentYaw = 0;
4416
- // Current character yaw in radians
4751
+ // Current character yaw offset in radians
4417
4752
  this.targetYaw = 0;
4418
- // Target desired yaw in radians
4753
+ // Target desired yaw offset in radians
4419
4754
  this.isTurning = false;
4420
4755
  this.turnAngularVelocity = 0;
4756
+ this.turnTarget = turnTarget ?? skeleton.modelRoot;
4757
+ this.baseYaw = this.turnTarget.rotation.y;
4421
4758
  }
4422
4759
  setTargetHeading(targetYawRad) {
4423
4760
  this.targetYaw = targetYawRad;
4424
4761
  }
4762
+ turnLeft(stepRad = Math.PI / 4) {
4763
+ this.targetYaw -= Math.abs(stepRad);
4764
+ }
4765
+ turnRight(stepRad = Math.PI / 4) {
4766
+ this.targetYaw += Math.abs(stepRad);
4767
+ }
4425
4768
  setFacingDirection(direction) {
4426
4769
  switch (direction) {
4427
4770
  case "left":
@@ -4432,32 +4775,45 @@ var TurnEngine = class {
4432
4775
  break;
4433
4776
  case "center":
4434
4777
  case "forward":
4778
+ case "front":
4435
4779
  default:
4436
4780
  this.targetYaw = 0;
4437
4781
  break;
4438
4782
  }
4439
4783
  }
4784
+ reset() {
4785
+ this.currentYaw = 0;
4786
+ this.targetYaw = 0;
4787
+ this.isTurning = false;
4788
+ this.turnAngularVelocity = 0;
4789
+ this.turnTarget.rotation.y = this.baseYaw;
4790
+ }
4440
4791
  update(delta, isLocomoting = false) {
4441
4792
  const diff = ((this.targetYaw - this.currentYaw) % (Math.PI * 2) + Math.PI * 3) % (Math.PI * 2) - Math.PI;
4442
4793
  this.isTurning = Math.abs(diff) > 0.02;
4443
- const damping = isLocomoting ? 6.5 : 4;
4794
+ const damping = isLocomoting ? 6.5 : 5;
4444
4795
  const prevYaw = this.currentYaw;
4445
4796
  this.currentYaw = ScratchMath.dampAngle(this.currentYaw, this.targetYaw, damping, delta);
4446
4797
  this.turnAngularVelocity = (this.currentYaw - prevYaw) / Math.max(delta, 1e-3);
4447
- this.skeleton.modelRoot.rotation.y = this.currentYaw;
4448
- if (isLocomoting && Math.abs(this.turnAngularVelocity) > 0.2) {
4449
- const bankRoll = -Math.max(-0.06, Math.min(0.06, this.turnAngularVelocity * 0.04));
4450
- const hips = this.skeleton.getJoint("hips");
4451
- if (hips) {
4798
+ this.turnTarget.rotation.y = this.baseYaw + this.currentYaw;
4799
+ if (Math.abs(this.turnAngularVelocity) > 0.15) {
4800
+ const bankRoll = -Math.max(-0.06, Math.min(0.06, this.turnAngularVelocity * (isLocomoting ? 0.04 : 0.02)));
4801
+ if (this.skeleton.hasJoint("hips")) {
4452
4802
  ScratchMath.e0.set(0, 0, bankRoll, "YXZ");
4453
4803
  ScratchMath.q0.setFromEuler(ScratchMath.e0);
4454
- hips.targetQuaternion.multiply(ScratchMath.q0);
4804
+ this.skeleton.applyModelDeltaRotation("hips", ScratchMath.q0, 1, true);
4455
4805
  }
4456
4806
  }
4457
4807
  }
4458
4808
  getYaw() {
4459
4809
  return this.currentYaw;
4460
4810
  }
4811
+ getTargetYaw() {
4812
+ return this.targetYaw;
4813
+ }
4814
+ getAngularVelocity() {
4815
+ return this.turnAngularVelocity;
4816
+ }
4461
4817
  isTurnActive() {
4462
4818
  return this.isTurning;
4463
4819
  }
@@ -4473,23 +4829,24 @@ var BalanceEngine = class {
4473
4829
  */
4474
4830
  applyCompensation(reachOffset, weight = 1) {
4475
4831
  if (weight <= 1e-3) return;
4832
+ const localScale = this.skeleton.hipsLocalPerMeter;
4476
4833
  this.targetOffset.set(
4477
- -reachOffset.x * 0.22,
4478
- -Math.abs(reachOffset.z) * 0.05,
4479
- -reachOffset.z * 0.25
4834
+ -reachOffset.x * 0.18 * localScale,
4835
+ -Math.abs(reachOffset.z) * 0.03 * localScale,
4836
+ -reachOffset.z * 0.18 * localScale
4480
4837
  );
4481
4838
  this.centerOfMassOffset.lerp(this.targetOffset, Math.min(1, weight));
4482
4839
  const hips = this.skeleton.getJoint("hips");
4483
4840
  if (hips) {
4484
4841
  hips.targetPosition.add(this.centerOfMassOffset);
4485
4842
  }
4486
- const spine = this.skeleton.getJoint("spine");
4487
- if (spine && this.centerOfMassOffset.lengthSq() > 1e-4) {
4488
- const counterPitch = this.centerOfMassOffset.z * 0.35;
4489
- const counterRoll = -this.centerOfMassOffset.x * 0.35;
4843
+ if (this.skeleton.hasJoint("spine") && this.centerOfMassOffset.lengthSq() > 1e-6) {
4844
+ const invScale = localScale > 1e-4 ? 1 / localScale : 1;
4845
+ const counterPitch = Math.max(-0.12, Math.min(0.12, this.centerOfMassOffset.z * invScale * 0.35));
4846
+ const counterRoll = Math.max(-0.12, Math.min(0.12, -this.centerOfMassOffset.x * invScale * 0.35));
4490
4847
  ScratchMath.e0.set(counterPitch, 0, counterRoll, "YXZ");
4491
4848
  ScratchMath.q0.setFromEuler(ScratchMath.e0);
4492
- spine.targetQuaternion.multiply(ScratchMath.q0);
4849
+ this.skeleton.applyModelDeltaRotation("spine", ScratchMath.q0, 1, true);
4493
4850
  }
4494
4851
  }
4495
4852
  reset() {
@@ -4709,177 +5066,146 @@ var GestureEngine = class {
4709
5066
  return "right";
4710
5067
  }
4711
5068
  }
5069
+ /**
5070
+ * Helper: rotates an upper arm from its rest orientation to a target standing-relative Euler pose in model space.
5071
+ */
5072
+ applyStandingArmDelta(side, pitchForwardRad, yawRad, abductOutRad, weight) {
5073
+ const boneName = side === "left" ? "leftUpperArm" : "rightUpperArm";
5074
+ if (!this.skeleton.hasJoint(boneName)) return;
5075
+ const restAngle = side === "left" ? this.skeleton.leftArmRestAngleFromDown : this.skeleton.rightArmRestAngleFromDown;
5076
+ const lowerSign = side === "left" ? this.skeleton.leftArmLowerSign : this.skeleton.rightArmLowerSign;
5077
+ const relaxToDown = Math.max(0, restAngle - abductOutRad);
5078
+ ScratchMath.q1.setFromAxisAngle(ScratchMath.FORWARD, lowerSign * relaxToDown);
5079
+ ScratchMath.e0.set(-pitchForwardRad, yawRad, 0, "YXZ");
5080
+ ScratchMath.q2.setFromEuler(ScratchMath.e0);
5081
+ ScratchMath.q0.copy(ScratchMath.q2).multiply(ScratchMath.q1);
5082
+ this.skeleton.applyModelDeltaRotation(boneName, ScratchMath.q0, weight);
5083
+ }
4712
5084
  // 1. Wave (right hand oscillates back and forth at shoulder height)
4713
5085
  synthesizeWave(progress, weight) {
4714
- const rUpper = this.skeleton.getJoint("rightUpperArm");
4715
- const rLower = this.skeleton.getJoint("rightLowerArm");
4716
- const rHand = this.skeleton.getJoint("rightHand");
4717
- if (rUpper && rLower) {
4718
- ScratchMath.e0.set(-0.6, 0.4, -1.35 * weight, "YXZ");
5086
+ this.applyStandingArmDelta("right", 0.35, -0.25, 1.35, weight);
5087
+ if (this.skeleton.hasJoint("rightLowerArm")) {
5088
+ const waveOsc = Math.sin(progress * Math.PI * 8) * 0.35;
5089
+ ScratchMath.e0.set(-1.35, 0, waveOsc, "YXZ");
4719
5090
  ScratchMath.q0.setFromEuler(ScratchMath.e0);
4720
- rUpper.targetQuaternion.slerp(ScratchMath.q0, weight);
4721
- ScratchMath.e0.set(1.4 * weight, 0, 0, "YXZ");
5091
+ this.skeleton.applyModelDeltaRotation("rightLowerArm", ScratchMath.q0, weight);
5092
+ }
5093
+ if (this.skeleton.hasJoint("rightHand")) {
5094
+ const handWave = Math.sin(progress * Math.PI * 8) * 0.3;
5095
+ ScratchMath.e0.set(0, 0, handWave, "YXZ");
4722
5096
  ScratchMath.q0.setFromEuler(ScratchMath.e0);
4723
- rLower.targetQuaternion.slerp(ScratchMath.q0, weight);
4724
- if (rHand) {
4725
- const waveAngle = Math.sin(progress * Math.PI * 8) * 0.45 * weight;
4726
- ScratchMath.e0.set(0, 0, waveAngle, "YXZ");
4727
- ScratchMath.q0.setFromEuler(ScratchMath.e0);
4728
- rHand.targetQuaternion.slerp(ScratchMath.q0, weight);
4729
- }
5097
+ this.skeleton.applyModelDeltaRotation("rightHand", ScratchMath.q0, weight);
4730
5098
  }
4731
5099
  }
4732
5100
  // 2. Pointing (arm extends toward target using IK or forward angle)
4733
5101
  synthesizePoint(_progress, weight, targetPos) {
4734
5102
  const isLeft = this.activeGesture?.type === "point_left";
4735
- const armName = isLeft ? "leftUpperArm" : "rightUpperArm";
4736
- const foreName = isLeft ? "leftLowerArm" : "rightLowerArm";
5103
+ const side = isLeft ? "left" : "right";
4737
5104
  if (targetPos && this.ik) {
4738
- this.ik.solveArmReach(isLeft ? "left" : "right", targetPos, weight);
5105
+ this.ik.solveArmReach(side, targetPos, weight);
4739
5106
  } else {
4740
- const upper = this.skeleton.getJoint(armName);
4741
- const fore = this.skeleton.getJoint(foreName);
4742
5107
  const sign = isLeft ? 1 : -1;
4743
- if (upper && fore) {
4744
- ScratchMath.e0.set(-1.45 * weight, sign * 0.25 * weight, sign * 0.15 * weight, "YXZ");
4745
- ScratchMath.q0.setFromEuler(ScratchMath.e0);
4746
- upper.targetQuaternion.slerp(ScratchMath.q0, weight);
4747
- ScratchMath.e0.set(0.15 * weight, 0, 0, "YXZ");
4748
- ScratchMath.q0.setFromEuler(ScratchMath.e0);
4749
- fore.targetQuaternion.slerp(ScratchMath.q0, weight);
5108
+ this.applyStandingArmDelta(side, 1.35, sign * 0.2, 0.25, weight);
5109
+ const foreName = isLeft ? "leftLowerArm" : "rightLowerArm";
5110
+ if (this.skeleton.hasJoint(foreName)) {
5111
+ ScratchMath.q0.setFromAxisAngle(ScratchMath.RIGHT, -0.15);
5112
+ this.skeleton.applyModelDeltaRotation(foreName, ScratchMath.q0, weight);
4750
5113
  }
4751
5114
  }
4752
5115
  }
4753
5116
  // 3. Agree (nod head twice)
4754
5117
  synthesizeAgree(progress, weight) {
4755
- const head = this.skeleton.getJoint("head");
4756
- if (head) {
5118
+ if (this.skeleton.hasJoint("head")) {
4757
5119
  const nodPitch = Math.sin(progress * Math.PI * 4) * 0.24 * weight;
4758
5120
  ScratchMath.e0.set(nodPitch, 0, 0, "YXZ");
4759
5121
  ScratchMath.q0.setFromEuler(ScratchMath.e0);
4760
- head.targetQuaternion.copy(head.restQuaternion).multiply(ScratchMath.q0);
5122
+ this.skeleton.applyModelDeltaRotation("head", ScratchMath.q0, weight);
4761
5123
  }
4762
5124
  }
4763
5125
  // 4. Disagree (shake head side to side)
4764
5126
  synthesizeDisagree(progress, weight) {
4765
- const head = this.skeleton.getJoint("head");
4766
- if (head) {
5127
+ if (this.skeleton.hasJoint("head")) {
4767
5128
  const shakeYaw = Math.sin(progress * Math.PI * 4) * 0.32 * weight;
4768
5129
  ScratchMath.e0.set(0, shakeYaw, 0, "YXZ");
4769
5130
  ScratchMath.q0.setFromEuler(ScratchMath.e0);
4770
- head.targetQuaternion.copy(head.restQuaternion).multiply(ScratchMath.q0);
5131
+ this.skeleton.applyModelDeltaRotation("head", ScratchMath.q0, weight);
4771
5132
  }
4772
5133
  }
4773
5134
  // 5. Shrug (raise shoulders, turn palms up, tilt head)
4774
5135
  synthesizeShrug(_progress, weight) {
4775
- const lUpper = this.skeleton.getJoint("leftUpperArm");
4776
- const rUpper = this.skeleton.getJoint("rightUpperArm");
4777
- const lFore = this.skeleton.getJoint("leftLowerArm");
4778
- const rFore = this.skeleton.getJoint("rightLowerArm");
4779
- const head = this.skeleton.getJoint("head");
4780
- const lift = 0.28 * weight;
4781
- if (lUpper && rUpper) {
4782
- ScratchMath.e0.set(-0.25 * weight, 0, lift, "YXZ");
4783
- ScratchMath.q0.setFromEuler(ScratchMath.e0);
4784
- lUpper.targetQuaternion.slerp(ScratchMath.q0, weight);
4785
- ScratchMath.e0.set(-0.25 * weight, 0, -lift, "YXZ");
5136
+ this.applyStandingArmDelta("left", 0.2, 0, 0.48, weight);
5137
+ this.applyStandingArmDelta("right", 0.2, 0, 0.48, weight);
5138
+ if (this.skeleton.hasJoint("leftLowerArm")) {
5139
+ ScratchMath.e0.set(-0.95, 0.35, 0, "YXZ");
4786
5140
  ScratchMath.q0.setFromEuler(ScratchMath.e0);
4787
- rUpper.targetQuaternion.slerp(ScratchMath.q0, weight);
5141
+ this.skeleton.applyModelDeltaRotation("leftLowerArm", ScratchMath.q0, weight);
4788
5142
  }
4789
- if (lFore && rFore) {
4790
- ScratchMath.e0.set(0.85 * weight, 0.4 * weight, 0, "YXZ");
5143
+ if (this.skeleton.hasJoint("rightLowerArm")) {
5144
+ ScratchMath.e0.set(-0.95, -0.35, 0, "YXZ");
4791
5145
  ScratchMath.q0.setFromEuler(ScratchMath.e0);
4792
- lFore.targetQuaternion.slerp(ScratchMath.q0, weight);
4793
- ScratchMath.e0.set(0.85 * weight, -0.4 * weight, 0, "YXZ");
4794
- ScratchMath.q0.setFromEuler(ScratchMath.e0);
4795
- rFore.targetQuaternion.slerp(ScratchMath.q0, weight);
5146
+ this.skeleton.applyModelDeltaRotation("rightLowerArm", ScratchMath.q0, weight);
4796
5147
  }
4797
- if (head) {
5148
+ if (this.skeleton.hasJoint("head")) {
4798
5149
  ScratchMath.e0.set(0.08 * weight, 0, 0.12 * weight, "YXZ");
4799
5150
  ScratchMath.q0.setFromEuler(ScratchMath.e0);
4800
- head.targetQuaternion.copy(head.restQuaternion).multiply(ScratchMath.q0);
5151
+ this.skeleton.applyModelDeltaRotation("head", ScratchMath.q0, weight);
4801
5152
  }
4802
5153
  }
4803
5154
  // 6. Think (hand to chin / temple, look upward slightly)
4804
5155
  synthesizeThink(_progress, weight) {
4805
- const rUpper = this.skeleton.getJoint("rightUpperArm");
4806
- const rFore = this.skeleton.getJoint("rightLowerArm");
4807
- const head = this.skeleton.getJoint("head");
4808
- if (rUpper && rFore) {
4809
- ScratchMath.e0.set(-1.1 * weight, -0.4 * weight, -0.5 * weight, "YXZ");
4810
- ScratchMath.q0.setFromEuler(ScratchMath.e0);
4811
- rUpper.targetQuaternion.slerp(ScratchMath.q0, weight);
4812
- ScratchMath.e0.set(1.6 * weight, 0, 0, "YXZ");
5156
+ this.applyStandingArmDelta("right", 0.95, 0.35, 0.35, weight);
5157
+ if (this.skeleton.hasJoint("rightLowerArm")) {
5158
+ ScratchMath.e0.set(-1.55, 0, 0, "YXZ");
4813
5159
  ScratchMath.q0.setFromEuler(ScratchMath.e0);
4814
- rFore.targetQuaternion.slerp(ScratchMath.q0, weight);
5160
+ this.skeleton.applyModelDeltaRotation("rightLowerArm", ScratchMath.q0, weight);
4815
5161
  }
4816
- if (head) {
5162
+ if (this.skeleton.hasJoint("head")) {
4817
5163
  ScratchMath.e0.set(-0.15 * weight, 0.18 * weight, 0.08 * weight, "YXZ");
4818
5164
  ScratchMath.q0.setFromEuler(ScratchMath.e0);
4819
- head.targetQuaternion.copy(head.restQuaternion).multiply(ScratchMath.q0);
5165
+ this.skeleton.applyModelDeltaRotation("head", ScratchMath.q0, weight);
4820
5166
  }
4821
5167
  }
4822
5168
  // 7. Explain / Conversational gestures (gentle rhythmic hand gesturing)
4823
5169
  synthesizeExplain(progress, weight) {
4824
- const lUpper = this.skeleton.getJoint("leftUpperArm");
4825
- const rUpper = this.skeleton.getJoint("rightUpperArm");
4826
- const lFore = this.skeleton.getJoint("leftLowerArm");
4827
- const rFore = this.skeleton.getJoint("rightLowerArm");
4828
- const osc = Math.sin(progress * Math.PI * 3) * 0.15 * weight;
4829
- if (lUpper && rUpper) {
4830
- ScratchMath.e0.set(-0.35 * weight + osc, 0.25 * weight, 0.2 * weight, "YXZ");
4831
- ScratchMath.q0.setFromEuler(ScratchMath.e0);
4832
- lUpper.targetQuaternion.slerp(ScratchMath.q0, weight);
4833
- ScratchMath.e0.set(-0.35 * weight - osc, -0.25 * weight, -0.2 * weight, "YXZ");
4834
- ScratchMath.q0.setFromEuler(ScratchMath.e0);
4835
- rUpper.targetQuaternion.slerp(ScratchMath.q0, weight);
5170
+ const osc = Math.sin(progress * Math.PI * 3) * 0.18;
5171
+ this.applyStandingArmDelta("left", 0.35 + osc, 0.2, 0.35, weight);
5172
+ this.applyStandingArmDelta("right", 0.35 - osc, -0.2, 0.35, weight);
5173
+ if (this.skeleton.hasJoint("leftLowerArm")) {
5174
+ ScratchMath.q0.setFromAxisAngle(ScratchMath.RIGHT, -0.9);
5175
+ this.skeleton.applyModelDeltaRotation("leftLowerArm", ScratchMath.q0, weight);
4836
5176
  }
4837
- if (lFore && rFore) {
4838
- ScratchMath.e0.set(0.9 * weight, 0, 0, "YXZ");
4839
- ScratchMath.q0.setFromEuler(ScratchMath.e0);
4840
- lFore.targetQuaternion.slerp(ScratchMath.q0, weight);
4841
- ScratchMath.e0.set(0.9 * weight, 0, 0, "YXZ");
4842
- ScratchMath.q0.setFromEuler(ScratchMath.e0);
4843
- rFore.targetQuaternion.slerp(ScratchMath.q0, weight);
5177
+ if (this.skeleton.hasJoint("rightLowerArm")) {
5178
+ ScratchMath.q0.setFromAxisAngle(ScratchMath.RIGHT, -0.9);
5179
+ this.skeleton.applyModelDeltaRotation("rightLowerArm", ScratchMath.q0, weight);
4844
5180
  }
4845
5181
  }
4846
5182
  // 8. Open Hands (Welcome / Present / Invite)
4847
5183
  synthesizeOpenHands(_progress, weight) {
4848
- const lUpper = this.skeleton.getJoint("leftUpperArm");
4849
- const rUpper = this.skeleton.getJoint("rightUpperArm");
4850
- const lFore = this.skeleton.getJoint("leftLowerArm");
4851
- const rFore = this.skeleton.getJoint("rightLowerArm");
4852
- if (lUpper && rUpper) {
4853
- ScratchMath.e0.set(-0.45 * weight, 0.35 * weight, 0.45 * weight, "YXZ");
5184
+ this.applyStandingArmDelta("left", 0.45, 0.35, 0.55, weight);
5185
+ this.applyStandingArmDelta("right", 0.45, -0.35, 0.55, weight);
5186
+ if (this.skeleton.hasJoint("leftLowerArm")) {
5187
+ ScratchMath.e0.set(-0.75, 0.35, 0, "YXZ");
4854
5188
  ScratchMath.q0.setFromEuler(ScratchMath.e0);
4855
- lUpper.targetQuaternion.slerp(ScratchMath.q0, weight);
4856
- ScratchMath.e0.set(-0.45 * weight, -0.35 * weight, -0.45 * weight, "YXZ");
4857
- ScratchMath.q0.setFromEuler(ScratchMath.e0);
4858
- rUpper.targetQuaternion.slerp(ScratchMath.q0, weight);
5189
+ this.skeleton.applyModelDeltaRotation("leftLowerArm", ScratchMath.q0, weight);
4859
5190
  }
4860
- if (lFore && rFore) {
4861
- ScratchMath.e0.set(0.75 * weight, 0.45 * weight, 0, "YXZ");
4862
- ScratchMath.q0.setFromEuler(ScratchMath.e0);
4863
- lFore.targetQuaternion.slerp(ScratchMath.q0, weight);
4864
- ScratchMath.e0.set(0.75 * weight, -0.45 * weight, 0, "YXZ");
5191
+ if (this.skeleton.hasJoint("rightLowerArm")) {
5192
+ ScratchMath.e0.set(-0.75, -0.35, 0, "YXZ");
4865
5193
  ScratchMath.q0.setFromEuler(ScratchMath.e0);
4866
- rFore.targetQuaternion.slerp(ScratchMath.q0, weight);
5194
+ this.skeleton.applyModelDeltaRotation("rightLowerArm", ScratchMath.q0, weight);
4867
5195
  }
4868
5196
  }
4869
5197
  // 9. Acknowledge (slight bow of head / chest)
4870
5198
  synthesizeAcknowledge(progress, weight) {
4871
- const spine = this.skeleton.getJoint("spine");
4872
- const head = this.skeleton.getJoint("head");
4873
5199
  const bow = Math.sin(progress * Math.PI) * 0.16 * weight;
4874
- if (spine) {
5200
+ if (this.skeleton.hasJoint("spine")) {
4875
5201
  ScratchMath.e0.set(bow, 0, 0, "YXZ");
4876
5202
  ScratchMath.q0.setFromEuler(ScratchMath.e0);
4877
- spine.targetQuaternion.copy(spine.restQuaternion).multiply(ScratchMath.q0);
5203
+ this.skeleton.applyModelDeltaRotation("spine", ScratchMath.q0, weight);
4878
5204
  }
4879
- if (head) {
5205
+ if (this.skeleton.hasJoint("head")) {
4880
5206
  ScratchMath.e0.set(bow * 0.5, 0, 0, "YXZ");
4881
5207
  ScratchMath.q0.setFromEuler(ScratchMath.e0);
4882
- head.targetQuaternion.copy(head.restQuaternion).multiply(ScratchMath.q0);
5208
+ this.skeleton.applyModelDeltaRotation("head", ScratchMath.q0, weight);
4883
5209
  }
4884
5210
  }
4885
5211
  // 10. Reach
@@ -4890,21 +5216,12 @@ var GestureEngine = class {
4890
5216
  }
4891
5217
  // 11. Celebrate (arms raised in victory, chest open)
4892
5218
  synthesizeCelebrate(_progress, weight) {
4893
- const lUpper = this.skeleton.getJoint("leftUpperArm");
4894
- const rUpper = this.skeleton.getJoint("rightUpperArm");
4895
- const spine = this.skeleton.getJoint("spine");
4896
- if (lUpper && rUpper) {
4897
- ScratchMath.e0.set(-1.8 * weight, 0, 1.2 * weight, "YXZ");
4898
- ScratchMath.q0.setFromEuler(ScratchMath.e0);
4899
- lUpper.targetQuaternion.slerp(ScratchMath.q0, weight);
4900
- ScratchMath.e0.set(-1.8 * weight, 0, -1.2 * weight, "YXZ");
4901
- ScratchMath.q0.setFromEuler(ScratchMath.e0);
4902
- rUpper.targetQuaternion.slerp(ScratchMath.q0, weight);
4903
- }
4904
- if (spine) {
5219
+ this.applyStandingArmDelta("left", 0.35, 0, 2.45, weight);
5220
+ this.applyStandingArmDelta("right", 0.35, 0, 2.45, weight);
5221
+ if (this.skeleton.hasJoint("spine")) {
4905
5222
  ScratchMath.e0.set(-0.12 * weight, 0, 0, "YXZ");
4906
5223
  ScratchMath.q0.setFromEuler(ScratchMath.e0);
4907
- spine.targetQuaternion.copy(spine.restQuaternion).multiply(ScratchMath.q0);
5224
+ this.skeleton.applyModelDeltaRotation("spine", ScratchMath.q0, weight);
4908
5225
  }
4909
5226
  }
4910
5227
  };
@@ -5202,13 +5519,17 @@ var MotionCoordinator = _MotionCoordinator;
5202
5519
 
5203
5520
  // src/avatar/procedural/ProceduralMovementEngine.ts
5204
5521
  var ProceduralMovementEngine = class {
5205
- constructor(modelRoot) {
5522
+ constructor(modelRoot, options) {
5206
5523
  this.isEnabled = true;
5207
- this.skeleton = new AUBRSkeleton(modelRoot);
5524
+ this.paused = false;
5525
+ this.locomotionBlendWeight = 0;
5526
+ this.startingTimer = 0;
5527
+ this.balanceReachScratch = new THREE.Vector3();
5528
+ this.skeleton = new AUBRSkeleton(modelRoot, options);
5208
5529
  this.ik = new IKCoordinator(this.skeleton);
5209
5530
  this.idle = new IdleEngine(this.skeleton);
5210
5531
  this.locomotion = new LocomotionEngine(this.skeleton, this.ik);
5211
- this.turn = new TurnEngine(this.skeleton);
5532
+ this.turn = new TurnEngine(this.skeleton, options?.turnTarget);
5212
5533
  this.balance = new BalanceEngine(this.skeleton);
5213
5534
  this.lookAtEngine = new LookAtEngine(this.skeleton);
5214
5535
  this.gesture = new GestureEngine(this.skeleton, this.ik);
@@ -5219,42 +5540,110 @@ var ProceduralMovementEngine = class {
5219
5540
  getCapabilities() {
5220
5541
  return this.skeleton.capabilities;
5221
5542
  }
5543
+ getState() {
5544
+ return this.stateMachine.getState();
5545
+ }
5222
5546
  setEnabled(enabled) {
5223
5547
  this.isEnabled = enabled;
5224
5548
  }
5549
+ pause() {
5550
+ this.paused = true;
5551
+ }
5552
+ resume() {
5553
+ this.paused = false;
5554
+ }
5555
+ isPaused() {
5556
+ return this.paused;
5557
+ }
5558
+ resetToRestPose() {
5559
+ this.startingTimer = 0;
5560
+ this.locomotionBlendWeight = 0;
5561
+ this.locomotion.cancel();
5562
+ this.gesture.stopGesture();
5563
+ this.idle.reset();
5564
+ this.turn.reset();
5565
+ this.balance.reset();
5566
+ this.lookAtEngine.clearTarget();
5567
+ this.facial.setSpeaking(false);
5568
+ this.facial.setExpression("neutral");
5569
+ this.stateMachine.transitionTo("idle", 0.05);
5570
+ this.coordinator.setWeights({ locomotionWeight: 0, idleWeight: 1, gestureWeight: 0 });
5571
+ this.skeleton.resetToRestPose();
5572
+ this.skeleton.updateWorldMatrices();
5573
+ }
5225
5574
  // --- High-Level Movement API ---
5575
+ startIdle() {
5576
+ const current = this.stateMachine.getState();
5577
+ if (current === "walking" || current === "running" || current === "starting") {
5578
+ this.stopLocomotion();
5579
+ return;
5580
+ }
5581
+ this.startingTimer = 0;
5582
+ this.gesture.stopGesture();
5583
+ this.stateMachine.transitionTo("idle", 0.2);
5584
+ this.coordinator.setWeights({ locomotionWeight: 0, idleWeight: 1, gestureWeight: 0 });
5585
+ }
5586
+ startWalking(speed = 1, screenVelocityPx) {
5587
+ if (!this.skeleton.capabilities.proceduralLocomotion) return;
5588
+ this.startingTimer = 0.18;
5589
+ this.stateMachine.transitionTo("starting", 0.18);
5590
+ this.locomotion.setMotion({ speed, isRunning: false, screenVelocityPx });
5591
+ }
5226
5592
  walk(speed = 1, screenVelocityPx) {
5227
- this.clearStopTimer();
5228
- this.stateMachine.transitionTo("walking");
5593
+ if (!this.skeleton.capabilities.proceduralLocomotion) return;
5594
+ this.startingTimer = 0;
5595
+ this.stateMachine.transitionTo("walking", 0.2);
5229
5596
  this.locomotion.setMotion({ speed, isRunning: false, screenVelocityPx });
5230
- this.coordinator.setWeights({ locomotionWeight: 1, idleWeight: 0 });
5231
5597
  }
5232
5598
  run(speed = 1.8, screenVelocityPx) {
5233
- this.clearStopTimer();
5234
- this.stateMachine.transitionTo("running");
5599
+ if (!this.skeleton.capabilities.proceduralRunning) return;
5600
+ this.startingTimer = 0;
5601
+ this.stateMachine.transitionTo("running", 0.2);
5235
5602
  this.locomotion.setMotion({ speed, isRunning: true, screenVelocityPx });
5236
- this.coordinator.setWeights({ locomotionWeight: 1, idleWeight: 0 });
5603
+ }
5604
+ stop() {
5605
+ this.stopLocomotion();
5237
5606
  }
5238
5607
  stopLocomotion() {
5239
- this.clearStopTimer();
5608
+ this.startingTimer = 0;
5240
5609
  this.locomotion.stop();
5241
- this.stateMachine.transitionTo("stopping", 0.2);
5242
- this.stopTimer = setTimeout(() => {
5243
- this.stopTimer = void 0;
5244
- if (this.stateMachine.getState() === "stopping") {
5245
- this.stateMachine.transitionTo("idle", 0.25);
5246
- this.coordinator.setWeights({ locomotionWeight: 0, idleWeight: 1 });
5247
- }
5248
- }, 200);
5249
- }
5250
- dispose() {
5251
- this.clearStopTimer();
5252
- this.isEnabled = false;
5253
- }
5254
- clearStopTimer() {
5255
- if (this.stopTimer) {
5256
- clearTimeout(this.stopTimer);
5257
- this.stopTimer = void 0;
5610
+ const state = this.stateMachine.getState();
5611
+ if (state === "walking" || state === "running" || state === "starting") {
5612
+ this.stateMachine.transitionTo("stopping", 0.22);
5613
+ } else if (state !== "stopping") {
5614
+ this.stateMachine.transitionTo("idle", 0.2);
5615
+ }
5616
+ }
5617
+ cancelLocomotion() {
5618
+ this.startingTimer = 0;
5619
+ this.locomotionBlendWeight = 0;
5620
+ this.locomotion.cancel();
5621
+ this.coordinator.setWeights({ locomotionWeight: 0, idleWeight: 1 });
5622
+ this.stateMachine.transitionTo("idle", 0.05);
5623
+ }
5624
+ turnLeft(stepRad = Math.PI / 4) {
5625
+ if (!this.skeleton.capabilities.turning) return;
5626
+ this.turn.turnLeft(stepRad);
5627
+ if (this.stateMachine.getState() === "idle") {
5628
+ this.stateMachine.transitionTo("turning", 0.15);
5629
+ }
5630
+ }
5631
+ turnRight(stepRad = Math.PI / 4) {
5632
+ if (!this.skeleton.capabilities.turning) return;
5633
+ this.turn.turnRight(stepRad);
5634
+ if (this.stateMachine.getState() === "idle") {
5635
+ this.stateMachine.transitionTo("turning", 0.15);
5636
+ }
5637
+ }
5638
+ turnTo(directionOrYaw) {
5639
+ if (!this.skeleton.capabilities.turning) return;
5640
+ if (typeof directionOrYaw === "number") {
5641
+ this.turn.setTargetHeading(directionOrYaw);
5642
+ } else {
5643
+ this.turn.setFacingDirection(directionOrYaw);
5644
+ }
5645
+ if (this.stateMachine.getState() === "idle") {
5646
+ this.stateMachine.transitionTo("turning", 0.15);
5258
5647
  }
5259
5648
  }
5260
5649
  setFacing(direction) {
@@ -5263,10 +5652,26 @@ var ProceduralMovementEngine = class {
5263
5652
  setTargetHeading(yawRad) {
5264
5653
  this.turn.setTargetHeading(yawRad);
5265
5654
  }
5655
+ dispose() {
5656
+ this.isEnabled = false;
5657
+ this.paused = false;
5658
+ this.startingTimer = 0;
5659
+ this.locomotionBlendWeight = 0;
5660
+ this.locomotion.cancel();
5661
+ this.gesture.stopGesture();
5662
+ this.balance.reset();
5663
+ this.lookAtEngine.clearTarget();
5664
+ }
5266
5665
  playGesture(type, options) {
5666
+ if (!this.skeleton.capabilities.upperBodyGestures && !this.skeleton.capabilities.headLookAt) {
5667
+ return false;
5668
+ }
5267
5669
  const success = this.gesture.playGesture(type, options);
5268
5670
  if (success) {
5269
- this.stateMachine.transitionTo("gesturing");
5671
+ const current = this.stateMachine.getState();
5672
+ if (current === "idle" || current === "speaking" || current === "listening" || current === "thinking") {
5673
+ this.stateMachine.transitionTo("gesturing");
5674
+ }
5270
5675
  this.coordinator.setWeights({ gestureWeight: 1 });
5271
5676
  }
5272
5677
  return success;
@@ -5304,41 +5709,83 @@ var ProceduralMovementEngine = class {
5304
5709
  * Restores bones to reference bind pose to prevent cumulative transform drift.
5305
5710
  */
5306
5711
  preMixerUpdate() {
5307
- if (!this.isEnabled) return;
5712
+ if (!this.isEnabled || this.paused) return;
5308
5713
  this.coordinator.preMixerRestore();
5309
5714
  }
5310
5715
  /**
5311
5716
  * Main procedural pipeline tick.
5312
- * Invoked in FbxRenderer.startLoop() after mixer.update(delta) and before renderer.render().
5717
+ * Invoked in FbxRenderer.startLoop() or Avatar Studio after mixer.update(delta) and before renderer.render().
5313
5718
  */
5314
- update(delta, hasActiveMixerClip) {
5315
- if (!this.isEnabled) return;
5719
+ update(delta, hasActiveMixerClip = false) {
5720
+ if (!this.isEnabled || this.paused) return;
5721
+ const dt = Math.min(Math.max(delta, 0), 0.1);
5316
5722
  if (hasActiveMixerClip) {
5317
5723
  this.coordinator.snapshotMixerOutput();
5318
5724
  this.coordinator.setWeights({ clipWeight: 1 });
5319
5725
  } else {
5320
5726
  this.coordinator.setWeights({ clipWeight: 0 });
5321
5727
  }
5322
- const currentState = this.stateMachine.getState();
5728
+ this.stateMachine.update(dt);
5729
+ let currentState = this.stateMachine.getState();
5730
+ if (currentState === "starting") {
5731
+ this.startingTimer -= dt;
5732
+ if (this.startingTimer <= 0) {
5733
+ this.stateMachine.transitionTo("walking", 0.2);
5734
+ currentState = this.stateMachine.getState();
5735
+ }
5736
+ }
5323
5737
  const isLocomoting = currentState === "walking" || currentState === "running" || currentState === "starting";
5324
- this.stateMachine.update(delta);
5325
- this.turn.update(delta, isLocomoting);
5326
- const locoWeight = isLocomoting ? 1 : this.coordinator.getWeights().locomotionWeight;
5327
- if (locoWeight > 0.01) {
5328
- this.locomotion.update(delta, locoWeight);
5738
+ const targetLocoWeight = isLocomoting ? 1 : 0;
5739
+ const blendRate = isLocomoting ? 7.5 : 8.5;
5740
+ this.locomotionBlendWeight = ScratchMath.damp(
5741
+ this.locomotionBlendWeight,
5742
+ targetLocoWeight,
5743
+ blendRate,
5744
+ dt
5745
+ );
5746
+ if (this.locomotionBlendWeight < 5e-3 && !isLocomoting) {
5747
+ this.locomotionBlendWeight = 0;
5748
+ }
5749
+ if (currentState === "stopping" && this.locomotionBlendWeight < 0.02 && this.locomotion.getSpeed() < 0.05) {
5750
+ this.locomotionBlendWeight = 0;
5751
+ this.stateMachine.transitionTo("idle", 0.15);
5752
+ currentState = "idle";
5753
+ }
5754
+ const locoWeight = this.locomotionBlendWeight;
5755
+ const idleWeight = Math.max(0, 1 - locoWeight);
5756
+ this.coordinator.setWeights({
5757
+ locomotionWeight: locoWeight,
5758
+ idleWeight
5759
+ });
5760
+ if (idleWeight > 5e-3 && this.skeleton.capabilities.proceduralIdle) {
5761
+ this.idle.update(dt, idleWeight);
5762
+ }
5763
+ if (locoWeight > 5e-3 && this.skeleton.capabilities.proceduralLocomotion) {
5764
+ this.locomotion.update(dt, locoWeight);
5329
5765
  }
5330
- const idleWeight = !isLocomoting ? 1 : this.coordinator.getWeights().idleWeight;
5331
- if (idleWeight > 0.01) {
5332
- this.idle.update(delta, idleWeight);
5766
+ if (this.skeleton.capabilities.turning) {
5767
+ this.turn.update(dt, isLocomoting);
5768
+ if (currentState === "turning" && !this.turn.isTurnActive()) {
5769
+ this.stateMachine.transitionTo("idle", 0.15);
5770
+ }
5333
5771
  }
5334
5772
  if (this.gesture.isGestureActive()) {
5335
- this.gesture.update(delta);
5773
+ this.gesture.update(dt);
5336
5774
  this.coordinator.setWeights({ gestureWeight: 1 });
5775
+ this.balanceReachScratch.set(0, 0, 0.15);
5776
+ this.balance.applyCompensation(this.balanceReachScratch, Math.min(1, dt * 6));
5337
5777
  } else {
5338
5778
  this.coordinator.setWeights({ gestureWeight: 0 });
5779
+ if (this.stateMachine.getState() === "gesturing") {
5780
+ this.stateMachine.transitionTo("idle", 0.2);
5781
+ }
5782
+ this.balanceReachScratch.set(0, 0, 0);
5783
+ this.balance.applyCompensation(this.balanceReachScratch, Math.min(1, dt * 6));
5784
+ }
5785
+ if (this.skeleton.capabilities.headLookAt) {
5786
+ this.lookAtEngine.update(dt, 1);
5339
5787
  }
5340
- this.lookAtEngine.update(delta, 1);
5341
- this.facial.update(delta);
5788
+ this.facial.update(dt);
5342
5789
  this.coordinator.blendLayers(hasActiveMixerClip);
5343
5790
  this.skeleton.apply();
5344
5791
  this.skeleton.updateWorldMatrices();
@@ -5376,13 +5823,8 @@ var FbxRenderer = class {
5376
5823
  if (!doc) {
5377
5824
  throw new Error("Unable to access document DOM for 3D avatar viewport");
5378
5825
  }
5379
- this.canvas = doc.createElement("canvas");
5380
- this.canvas.className = "asiyst-avatar-canvas";
5381
- this.canvas.setAttribute("role", "presentation");
5382
- this.canvas.style.opacity = "0";
5383
- this.canvas.style.transition = "opacity 0.25s ease";
5384
- this.canvas.style.background = "transparent";
5385
- const glContext = this.canvas.getContext("webgl2") || this.canvas.getContext("webgl");
5826
+ const probeCanvas = doc.createElement("canvas");
5827
+ const glContext = probeCanvas.getContext("webgl2") || probeCanvas.getContext("webgl");
5386
5828
  if (!glContext) {
5387
5829
  throw new AsiystError({
5388
5830
  code: "WEBGL_UNAVAILABLE",
@@ -5390,6 +5832,12 @@ var FbxRenderer = class {
5390
5832
  hint: "Enable hardware acceleration in your browser or use a WebGL-capable device."
5391
5833
  });
5392
5834
  }
5835
+ this.canvas = doc.createElement("canvas");
5836
+ this.canvas.className = "asiyst-avatar-canvas";
5837
+ this.canvas.setAttribute("role", "presentation");
5838
+ this.canvas.style.opacity = "0";
5839
+ this.canvas.style.transition = "opacity 0.25s ease";
5840
+ this.canvas.style.background = "transparent";
5393
5841
  const isMobile = typeof window !== "undefined" && window.innerWidth <= 640;
5394
5842
  const width = options?.width || (isMobile ? 140 : 190);
5395
5843
  const height = options?.height || (isMobile ? 280 : 360);
@@ -5542,7 +5990,10 @@ var FbxRenderer = class {
5542
5990
  });
5543
5991
  this.allAnimations = [...modelAnimations];
5544
5992
  try {
5545
- this.proceduralEngine = new ProceduralMovementEngine(rawObject);
5993
+ this.proceduralEngine = new ProceduralMovementEngine(rawObject, {
5994
+ embeddedAnimationCount: modelAnimations.length,
5995
+ turnTarget: this.modelRoot
5996
+ });
5546
5997
  } catch (procErr) {
5547
5998
  if (typeof console !== "undefined" && typeof console.warn === "function") {
5548
5999
  console.warn("[asiyst] Procedural movement engine initialization notice:", procErr);
@@ -6133,6 +6584,34 @@ var FbxRenderer = class {
6133
6584
  this.mixer = null;
6134
6585
  }
6135
6586
  if (this.scene) {
6587
+ const disposedTextures = /* @__PURE__ */ new Set();
6588
+ const TEXTURE_SLOTS = [
6589
+ "map",
6590
+ "normalMap",
6591
+ "roughnessMap",
6592
+ "metalnessMap",
6593
+ "aoMap",
6594
+ "emissiveMap",
6595
+ "alphaMap",
6596
+ "bumpMap",
6597
+ "displacementMap",
6598
+ "lightMap",
6599
+ "envMap"
6600
+ ];
6601
+ const disposeMat = (m) => {
6602
+ const matRecord = m;
6603
+ for (const slot of TEXTURE_SLOTS) {
6604
+ const texCandidate = matRecord[slot];
6605
+ if (texCandidate && typeof texCandidate === "object" && texCandidate.isTexture) {
6606
+ const tex = texCandidate;
6607
+ if (!disposedTextures.has(tex)) {
6608
+ disposedTextures.add(tex);
6609
+ tex.dispose();
6610
+ }
6611
+ }
6612
+ }
6613
+ m.dispose();
6614
+ };
6136
6615
  this.scene.traverse((obj) => {
6137
6616
  const mesh = obj;
6138
6617
  if (mesh.isMesh) {
@@ -6142,14 +6621,12 @@ var FbxRenderer = class {
6142
6621
  if (mesh.material) {
6143
6622
  if (Array.isArray(mesh.material)) {
6144
6623
  mesh.material.forEach((m) => {
6145
- const mat = m;
6146
- mat.map?.dispose();
6147
- mat.dispose();
6624
+ if (m && typeof m.dispose === "function") {
6625
+ disposeMat(m);
6626
+ }
6148
6627
  });
6149
- } else {
6150
- const mat = mesh.material;
6151
- mat.map?.dispose();
6152
- mat.dispose();
6628
+ } else if (mesh.material && typeof mesh.material.dispose === "function") {
6629
+ disposeMat(mesh.material);
6153
6630
  }
6154
6631
  }
6155
6632
  }
@@ -7465,6 +7942,8 @@ var MovementEngine = class {
7465
7942
  faceDirection(direction) {
7466
7943
  if (direction === "left" || direction === "right") {
7467
7944
  this.renderer.setFacing(direction);
7945
+ } else if (typeof this.renderer.setFacing === "function") {
7946
+ this.renderer.setFacing(direction);
7468
7947
  }
7469
7948
  }
7470
7949
  viewport() {
@@ -10081,12 +10560,20 @@ var ActionScheduler = class {
10081
10560
  ctx.events.emit("action_started", { actionId: action.id, actionType: action.type });
10082
10561
  try {
10083
10562
  const timeoutMs = typeof action.expirationMs === "number" && action.expirationMs > 0 ? action.expirationMs : 15e3;
10084
- const result = await Promise.race([
10085
- handler(ctx),
10086
- new Promise(
10087
- (_, reject) => setTimeout(() => reject(new Error(`Action ${action.type} timed out after ${timeoutMs}ms`)), timeoutMs)
10088
- )
10089
- ]);
10563
+ let timerId;
10564
+ const timeoutPromise = new Promise((_, reject) => {
10565
+ timerId = setTimeout(() => {
10566
+ reject(new Error(`Action ${action.type} timed out after ${timeoutMs}ms`));
10567
+ }, timeoutMs);
10568
+ });
10569
+ let result;
10570
+ try {
10571
+ result = await Promise.race([handler(ctx), timeoutPromise]);
10572
+ } finally {
10573
+ if (timerId !== void 0) {
10574
+ clearTimeout(timerId);
10575
+ }
10576
+ }
10090
10577
  const durationMs = Date.now() - startTime;
10091
10578
  const ackResult = {
10092
10579
  executionDurationMs: durationMs,
@@ -10734,7 +11221,7 @@ var AsiystRuntime = class {
10734
11221
  apiBaseUrl: options.apiBaseUrl ?? DEFAULT_API_BASE_URL,
10735
11222
  projectId: options.projectId,
10736
11223
  publicKey: options.publicKey,
10737
- installationId: void 0,
11224
+ installationId: this.installationId,
10738
11225
  origin: win.location.origin,
10739
11226
  environment: this.environment
10740
11227
  }) : void 0;
@@ -10816,36 +11303,47 @@ var AsiystRuntime = class {
10816
11303
  (event) => this.cloud.sendRuntimeEvent(event),
10817
11304
  this.events
10818
11305
  );
10819
- this.tourEngine = new TourEngine(
10820
- async (route) => {
11306
+ this.tourEngine = new TourEngine({
11307
+ navigateToRoute: async (route) => {
10821
11308
  const nav = resolveNavigationAdapter(win);
10822
11309
  await nav.navigateToRoute(route);
10823
11310
  },
10824
- async (elId) => {
11311
+ moveToElement: async (elId) => {
10825
11312
  await this.movement.moveTo(elId, "developer");
10826
11313
  },
10827
- async (elId) => {
11314
+ pointAtElement: async (elId) => {
10828
11315
  await this.movement.pointAt(elId, "developer");
10829
11316
  },
10830
- async (elId, style) => {
10831
- const el = this.targetRegistry.get(elId);
10832
- if (el) {
10833
- this.highlights.show(el, style || "spotlight");
11317
+ highlightElement: async (elId, style) => {
11318
+ const resolved = this.resolver.tryResolve(elId, "developer");
11319
+ if (resolved) {
11320
+ this.highlights.show(resolved.element, style || "spotlight");
11321
+ } else {
11322
+ const el = this.targetRegistry.get(elId);
11323
+ if (el) {
11324
+ this.highlights.show(el, style || "spotlight");
11325
+ }
10834
11326
  }
10835
11327
  },
10836
- async (text) => {
11328
+ clearHighlight: () => {
11329
+ this.highlights.hide();
11330
+ },
11331
+ speak: async (text) => {
10837
11332
  await this.speechPlayer.speak(text);
10838
11333
  },
10839
- (event) => {
11334
+ playGesture: (gesture) => {
11335
+ return this.avatar.playGesture(gesture);
11336
+ },
11337
+ onStepChanged: (event) => {
10840
11338
  this.events.emit("asiyst:tour:step_changed", event);
10841
11339
  },
10842
- (tourId) => {
11340
+ onTourCompleted: (tourId) => {
10843
11341
  this.events.emit("asiyst:tour:completed", { tourId });
10844
11342
  },
10845
- (tourId, error) => {
11343
+ onTourError: (tourId, error) => {
10846
11344
  this.events.emit("asiyst:tour:error", { tourId, error });
10847
11345
  }
10848
- );
11346
+ });
10849
11347
  this.actionScheduler = new ActionScheduler(
10850
11348
  (action) => this.createActionContext(action, doc, win)
10851
11349
  );
@@ -11621,8 +12119,8 @@ var AsiystRuntime = class {
11621
12119
  origin
11622
12120
  });
11623
12121
  if (reg?.runtimeSessionId) {
11624
- this.installationId = reg.runtimeSessionId;
11625
- this.runtimeAuth?.setInstallationId(reg.runtimeSessionId);
12122
+ this.visitorSessionId = reg.runtimeSessionId;
12123
+ this.cloud.setSessionId(reg.runtimeSessionId);
11626
12124
  this.registrationSucceeded = true;
11627
12125
  this.connectionStatus = "connected";
11628
12126
  this.verificationStatus = "connected";
@@ -11806,17 +12304,11 @@ var AsiystRuntime = class {
11806
12304
  async avatarSubmitContext() {
11807
12305
  await this.submitWebsiteContext();
11808
12306
  }
11809
- async avatarHighlight(targetId, style) {
11810
- const el = this.targetRegistry.get(targetId);
11811
- if (el) {
11812
- this.highlights.show(el, style ?? "outline");
11813
- }
12307
+ async avatarHighlight(target, style) {
12308
+ await this.avatar.highlight(target, style ?? "outline", "developer");
11814
12309
  }
11815
- async avatarPointAt(targetId) {
11816
- const el = this.targetRegistry.get(targetId);
11817
- if (el) {
11818
- await this.avatar.pointAt(targetId, "developer");
11819
- }
12310
+ async avatarPointAt(target) {
12311
+ await this.avatar.pointAt(target, "developer");
11820
12312
  }
11821
12313
  async avatarNavigateTo(route) {
11822
12314
  const win = this.host?.chrome?.ownerDocument?.defaultView || (typeof window !== "undefined" ? window : void 0);
@@ -11953,15 +12445,10 @@ var AsiystInstanceImpl = class {
11953
12445
  interact: {
11954
12446
  highlight: async (targetId, options) => {
11955
12447
  const style = typeof options === "string" ? options : options?.style;
11956
- await this.requireInstanceRuntime().avatarHighlight(
11957
- typeof targetId === "string" ? targetId : targetId.id || "",
11958
- style
11959
- );
12448
+ await this.requireInstanceRuntime().avatarHighlight(targetId, style);
11960
12449
  },
11961
12450
  pointAt: async (targetId) => {
11962
- await this.requireInstanceRuntime().avatarPointAt(
11963
- typeof targetId === "string" ? targetId : targetId.id || ""
11964
- );
12451
+ await this.requireInstanceRuntime().avatarPointAt(targetId);
11965
12452
  }
11966
12453
  },
11967
12454
  navigation: {
@@ -12267,10 +12754,10 @@ var Asiyst = {
12267
12754
  interact: {
12268
12755
  async highlight(targetId, options) {
12269
12756
  const style = typeof options === "string" ? options : options?.style;
12270
- await requireRuntime().avatarHighlight(typeof targetId === "string" ? targetId : targetId.id || "", style);
12757
+ await requireRuntime().avatarHighlight(targetId, style);
12271
12758
  },
12272
12759
  async pointAt(targetId) {
12273
- await requireRuntime().avatarPointAt(typeof targetId === "string" ? targetId : targetId.id || "");
12760
+ await requireRuntime().avatarPointAt(targetId);
12274
12761
  }
12275
12762
  },
12276
12763
  navigation: {