@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.cjs CHANGED
@@ -107,7 +107,7 @@ var ALL_ACTION_KINDS = [
107
107
  // src/core/constants.ts
108
108
  function resolveSdkVersion() {
109
109
  {
110
- return "0.1.34";
110
+ return "0.1.35";
111
111
  }
112
112
  }
113
113
  var SDK_VERSION = resolveSdkVersion();
@@ -634,9 +634,30 @@ var HttpTransport = class {
634
634
  if (req.body !== void 0) {
635
635
  defaultHeaders["Content-Type"] = "application/json";
636
636
  }
637
+ const mergedHeaders = new Headers();
638
+ for (const [key, value] of Object.entries(defaultHeaders)) {
639
+ mergedHeaders.set(key, value);
640
+ }
641
+ if (req.headers) {
642
+ if (typeof req.headers.forEach === "function" && !Array.isArray(req.headers)) {
643
+ req.headers.forEach((value, key) => {
644
+ mergedHeaders.set(key, value);
645
+ });
646
+ } else if (Array.isArray(req.headers)) {
647
+ for (const [key, value] of req.headers) {
648
+ mergedHeaders.set(key, value);
649
+ }
650
+ } else if (typeof req.headers === "object") {
651
+ for (const [key, value] of Object.entries(req.headers)) {
652
+ if (value !== void 0 && value !== null) {
653
+ mergedHeaders.set(key, String(value));
654
+ }
655
+ }
656
+ }
657
+ }
637
658
  const response = await fetch(url, {
638
659
  method: req.method,
639
- headers: req.headers ?? defaultHeaders,
660
+ headers: mergedHeaders,
640
661
  body: req.body === void 0 ? void 0 : JSON.stringify(req.body),
641
662
  signal: controller.signal
642
663
  });
@@ -3325,27 +3346,57 @@ var RigDetector = class {
3325
3346
  return rawName.trim().replace(/^.*[:|/]/, "").replace(/^(mixamorig\d*|bip01|armature|root)[:_ ]?/i, "").replace(/[^a-zA-Z0-9._-]/g, "").toLowerCase();
3326
3347
  }
3327
3348
  /**
3328
- * Analyzes an imported 3D model root, identifies all bones, maps them to canonical AUBR,
3329
- * validates hierarchy, and reports capability profile.
3349
+ * Analyzes an imported 3D model root, identifies all bones and skinned meshes,
3350
+ * maps them to canonical AUBR, validates hierarchy, and reports capability profile.
3330
3351
  */
3331
- static detect(modelRoot) {
3352
+ static detect(modelRoot, options) {
3332
3353
  const allBones = [];
3354
+ const seenBones = /* @__PURE__ */ new Set();
3355
+ const skinnedMeshes = [];
3333
3356
  modelRoot.traverse((node) => {
3334
- if (node.isBone) {
3357
+ if (node.isSkinnedMesh) {
3358
+ const sm = node;
3359
+ skinnedMeshes.push(sm);
3360
+ if (sm.skeleton?.bones) {
3361
+ for (const b of sm.skeleton.bones) {
3362
+ if (b && !seenBones.has(b)) {
3363
+ seenBones.add(b);
3364
+ allBones.push(b);
3365
+ }
3366
+ }
3367
+ }
3368
+ }
3369
+ if (node.isBone && !seenBones.has(node)) {
3370
+ seenBones.add(node);
3335
3371
  allBones.push(node);
3336
3372
  }
3337
3373
  });
3338
3374
  if (allBones.length === 0) {
3339
3375
  modelRoot.traverse((node) => {
3376
+ if (node.isMesh) return;
3340
3377
  if (node.name && (node.type === "Bone" || /bone|joint|limb|hips|spine|arm|leg|head/i.test(node.name))) {
3341
- if (!allBones.includes(node)) {
3378
+ if (!seenBones.has(node)) {
3379
+ seenBones.add(node);
3342
3380
  allBones.push(node);
3343
3381
  }
3344
3382
  }
3345
3383
  });
3346
3384
  }
3385
+ 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;
3386
+ const hasEmbeddedAnimations = embeddedAnimationCount > 0;
3387
+ const skinnedMeshCount = skinnedMeshes.length;
3388
+ const hasSkinnedMesh = skinnedMeshCount > 0;
3389
+ const boneCount = allBones.length;
3390
+ const hasSkeleton = boneCount > 0;
3347
3391
  if (allBones.length === 0) {
3348
- const caps = this.createUnsupportedCapabilities("unsupported", ["hips", "spine", "head"]);
3392
+ const caps = this.createUnsupportedCapabilities("unsupported", ["hips", "spine", "head"], {
3393
+ hasSkeleton,
3394
+ hasSkinnedMesh,
3395
+ boneCount,
3396
+ skinnedMeshCount,
3397
+ embeddedAnimationCount,
3398
+ hasEmbeddedAnimations
3399
+ });
3349
3400
  return {
3350
3401
  rigType: "unsupported",
3351
3402
  capabilities: caps,
@@ -3386,26 +3437,43 @@ var RigDetector = class {
3386
3437
  const hasArms = matchedBones.has("leftUpperArm") && matchedBones.has("leftLowerArm") && matchedBones.has("rightUpperArm") && matchedBones.has("rightLowerArm");
3387
3438
  const isHumanoid = hasHipsAndSpine && (hasLegs || hasArms);
3388
3439
  let finalRigType = rigFlavor;
3440
+ const detectedRigFlavor = isHumanoid ? rigFlavor : "unsupported";
3389
3441
  if (!isHumanoid) {
3390
3442
  finalRigType = "unsupported";
3391
3443
  } else if (missingRequired.length > 0) {
3392
3444
  finalRigType = "partial_humanoid";
3393
3445
  }
3446
+ const proceduralIdle = isHumanoid && hasHipsAndSpine && hasHead;
3447
+ const proceduralLocomotion = isHumanoid && hasLegs;
3448
+ const proceduralRunning = isHumanoid && hasLegs;
3449
+ const upperBodyGestures = isHumanoid && hasArms && hasHipsAndSpine;
3450
+ const proceduralAnimationAvailable = proceduralIdle || proceduralLocomotion || upperBodyGestures;
3451
+ const clipPlayback = hasSkeleton && allBones.length > 5 || hasEmbeddedAnimations;
3452
+ const animationSourceMode = proceduralAnimationAvailable && hasEmbeddedAnimations ? "embedded_and_procedural" : proceduralAnimationAvailable ? "procedural_only" : hasEmbeddedAnimations ? "embedded_only" : "static";
3394
3453
  const capabilities = {
3395
3454
  rigType: finalRigType,
3455
+ detectedRigFlavor,
3396
3456
  isHumanoid,
3457
+ hasSkeleton,
3458
+ hasSkinnedMesh,
3459
+ boneCount,
3460
+ skinnedMeshCount,
3397
3461
  confidence: Math.round(confidence * 100) / 100,
3398
- proceduralIdle: hasHipsAndSpine && hasHead,
3399
- proceduralLocomotion: isHumanoid && hasLegs,
3400
- proceduralRunning: isHumanoid && hasLegs,
3462
+ proceduralAnimationAvailable,
3463
+ proceduralIdle,
3464
+ proceduralLocomotion,
3465
+ proceduralRunning,
3401
3466
  turning: isHumanoid,
3402
- legIK: hasLegs,
3403
- armIK: hasArms,
3404
- footPlacement: hasLegs,
3405
- headLookAt: hasHead,
3406
- upperBodyGestures: hasArms && hasHipsAndSpine,
3467
+ legIK: isHumanoid && hasLegs,
3468
+ armIK: isHumanoid && hasArms,
3469
+ footPlacement: isHumanoid && hasLegs,
3470
+ headLookAt: isHumanoid && hasHead,
3471
+ upperBodyGestures,
3407
3472
  facialAnimation: this.detectFacialCapabilities(modelRoot),
3408
- clipPlayback: allBones.length > 5,
3473
+ hasEmbeddedAnimations,
3474
+ embeddedAnimationCount,
3475
+ clipPlayback,
3476
+ animationSourceMode,
3409
3477
  missingRequiredBones: missingRequired,
3410
3478
  missingOptionalBones: missingOptional
3411
3479
  };
@@ -3420,15 +3488,15 @@ var RigDetector = class {
3420
3488
  /**
3421
3489
  * Alias for detect()
3422
3490
  */
3423
- static detectRig(modelRoot) {
3424
- return this.detect(modelRoot);
3491
+ static detectRig(modelRoot, options) {
3492
+ return this.detect(modelRoot, options);
3425
3493
  }
3426
3494
  /**
3427
3495
  * Assesses avatar rig capabilities from detection result or model.
3428
3496
  */
3429
- static assessCapabilities(detection, modelRoot) {
3497
+ static assessCapabilities(detection, modelRoot, options) {
3430
3498
  if (modelRoot) {
3431
- return this.detect(modelRoot).capabilities;
3499
+ return this.detect(modelRoot, options).capabilities;
3432
3500
  }
3433
3501
  return detection.capabilities;
3434
3502
  }
@@ -3493,6 +3561,24 @@ var RigDetector = class {
3493
3561
  if (rightLow && rightFoot && !this.isDescendantOf(rightFoot, rightLow)) {
3494
3562
  matched.delete("rightFoot");
3495
3563
  }
3564
+ const leftUpArm = matched.get("leftUpperArm");
3565
+ const leftLowArm = matched.get("leftLowerArm");
3566
+ const leftHand = matched.get("leftHand");
3567
+ if (leftUpArm && leftLowArm && !this.isDescendantOf(leftLowArm, leftUpArm)) {
3568
+ matched.delete("leftLowerArm");
3569
+ }
3570
+ if (leftLowArm && leftHand && !this.isDescendantOf(leftHand, leftLowArm)) {
3571
+ matched.delete("leftHand");
3572
+ }
3573
+ const rightUpArm = matched.get("rightUpperArm");
3574
+ const rightLowArm = matched.get("rightLowerArm");
3575
+ const rightHand = matched.get("rightHand");
3576
+ if (rightUpArm && rightLowArm && !this.isDescendantOf(rightLowArm, rightUpArm)) {
3577
+ matched.delete("rightLowerArm");
3578
+ }
3579
+ if (rightLowArm && rightHand && !this.isDescendantOf(rightHand, rightLowArm)) {
3580
+ matched.delete("rightHand");
3581
+ }
3496
3582
  const hips = matched.get("hips");
3497
3583
  const spine = matched.get("spine");
3498
3584
  const head = matched.get("head");
@@ -3521,11 +3607,19 @@ var RigDetector = class {
3521
3607
  });
3522
3608
  return hasMorphTargets;
3523
3609
  }
3524
- static createUnsupportedCapabilities(rigType, missingReq) {
3610
+ static createUnsupportedCapabilities(rigType, missingReq, meta) {
3611
+ const hasEmbeddedAnimations = meta?.hasEmbeddedAnimations ?? false;
3612
+ const embeddedAnimationCount = meta?.embeddedAnimationCount ?? 0;
3525
3613
  return {
3526
3614
  rigType,
3615
+ detectedRigFlavor: rigType,
3527
3616
  isHumanoid: false,
3617
+ hasSkeleton: meta?.hasSkeleton ?? false,
3618
+ hasSkinnedMesh: meta?.hasSkinnedMesh ?? false,
3619
+ boneCount: meta?.boneCount ?? 0,
3620
+ skinnedMeshCount: meta?.skinnedMeshCount ?? 0,
3528
3621
  confidence: 0,
3622
+ proceduralAnimationAvailable: false,
3529
3623
  proceduralIdle: false,
3530
3624
  proceduralLocomotion: false,
3531
3625
  proceduralRunning: false,
@@ -3536,7 +3630,10 @@ var RigDetector = class {
3536
3630
  headLookAt: false,
3537
3631
  upperBodyGestures: false,
3538
3632
  facialAnimation: false,
3539
- clipPlayback: false,
3633
+ hasEmbeddedAnimations,
3634
+ embeddedAnimationCount,
3635
+ clipPlayback: hasEmbeddedAnimations,
3636
+ animationSourceMode: hasEmbeddedAnimations ? "embedded_only" : "static",
3540
3637
  missingRequiredBones: missingReq,
3541
3638
  missingOptionalBones: [...OPTIONAL_CANONICAL_BONES]
3542
3639
  };
@@ -3597,12 +3694,16 @@ ScratchMath.v4 = new THREE__namespace.Vector3();
3597
3694
  ScratchMath.v5 = new THREE__namespace.Vector3();
3598
3695
  ScratchMath.v6 = new THREE__namespace.Vector3();
3599
3696
  ScratchMath.v7 = new THREE__namespace.Vector3();
3697
+ ScratchMath.v8 = new THREE__namespace.Vector3();
3698
+ ScratchMath.v9 = new THREE__namespace.Vector3();
3600
3699
  // Quaternion scratch pool
3601
3700
  ScratchMath.q0 = new THREE__namespace.Quaternion();
3602
3701
  ScratchMath.q1 = new THREE__namespace.Quaternion();
3603
3702
  ScratchMath.q2 = new THREE__namespace.Quaternion();
3604
3703
  ScratchMath.q3 = new THREE__namespace.Quaternion();
3605
3704
  ScratchMath.q4 = new THREE__namespace.Quaternion();
3705
+ ScratchMath.q5 = new THREE__namespace.Quaternion();
3706
+ ScratchMath.q6 = new THREE__namespace.Quaternion();
3606
3707
  // Matrix4 scratch pool
3607
3708
  ScratchMath.m0 = new THREE__namespace.Matrix4();
3608
3709
  ScratchMath.m1 = new THREE__namespace.Matrix4();
@@ -3619,16 +3720,29 @@ ScratchMath.LEFT = new THREE__namespace.Vector3(-1, 0, 0);
3619
3720
 
3620
3721
  // src/avatar/procedural/AUBRSkeleton.ts
3621
3722
  var AUBRSkeleton = class {
3622
- constructor(modelRoot) {
3723
+ constructor(modelRoot, options) {
3623
3724
  this.joints = /* @__PURE__ */ new Map();
3725
+ /** Rest foot positions in modelRoot local space */
3726
+ this.restLeftFootModelPos = new THREE__namespace.Vector3();
3727
+ this.restRightFootModelPos = new THREE__namespace.Vector3();
3624
3728
  this.boneToCanonical = /* @__PURE__ */ new Map();
3729
+ this.restModelQuat = /* @__PURE__ */ new Map();
3730
+ this.restModelQuatInv = /* @__PURE__ */ new Map();
3625
3731
  this.modelRoot = modelRoot;
3626
- const detection = RigDetector.detect(modelRoot);
3732
+ this.modelRoot.updateMatrixWorld(true);
3733
+ const detection = RigDetector.detect(modelRoot, options);
3627
3734
  this.capabilities = detection.capabilities;
3735
+ const rootWorldQuatInv = new THREE__namespace.Quaternion();
3736
+ this.modelRoot.getWorldQuaternion(rootWorldQuatInv).invert();
3628
3737
  for (const [canonicalName, bone] of detection.matchedBones.entries()) {
3629
3738
  const restPos = bone.position.clone();
3630
3739
  const restQuat = bone.quaternion.clone();
3631
3740
  const restScale = bone.scale.clone();
3741
+ const boneWorldQuat = new THREE__namespace.Quaternion();
3742
+ bone.getWorldQuaternion(boneWorldQuat);
3743
+ const boneModelQuat = rootWorldQuatInv.clone().multiply(boneWorldQuat).normalize();
3744
+ this.restModelQuat.set(canonicalName, boneModelQuat);
3745
+ this.restModelQuatInv.set(canonicalName, boneModelQuat.clone().invert());
3632
3746
  let restDir = new THREE__namespace.Vector3(0, 1, 0);
3633
3747
  let boneLen = 0.2;
3634
3748
  if (bone.children.length > 0) {
@@ -3657,6 +3771,79 @@ var AUBRSkeleton = class {
3657
3771
  this.boneToCanonical.set(bone, canonicalName);
3658
3772
  }
3659
3773
  this.proportions = this.calculateProportions(detection.matchedBones);
3774
+ this.worldUnitsPerMeter = this.proportions.totalHeight / 1.8;
3775
+ const hipsBone = detection.matchedBones.get("hips");
3776
+ let hipsParentScale = 1;
3777
+ if (hipsBone?.parent) {
3778
+ const parentScaleVec = new THREE__namespace.Vector3();
3779
+ hipsBone.parent.getWorldScale(parentScaleVec);
3780
+ const avg = (Math.abs(parentScaleVec.x) + Math.abs(parentScaleVec.y) + Math.abs(parentScaleVec.z)) / 3;
3781
+ if (Number.isFinite(avg) && avg > 1e-5) {
3782
+ hipsParentScale = avg;
3783
+ }
3784
+ }
3785
+ this.hipsLocalPerMeter = this.worldUnitsPerMeter / hipsParentScale;
3786
+ const leftArmRest = this.measureArmRest("leftUpperArm", "leftLowerArm", 1);
3787
+ const rightArmRest = this.measureArmRest("rightUpperArm", "rightLowerArm", -1);
3788
+ this.leftArmRestAngleFromDown = leftArmRest.angleFromDown;
3789
+ this.leftArmLowerSign = leftArmRest.lowerSign;
3790
+ this.rightArmRestAngleFromDown = rightArmRest.angleFromDown;
3791
+ this.rightArmLowerSign = rightArmRest.lowerSign;
3792
+ const leftFoot = detection.matchedBones.get("leftFoot");
3793
+ const rightFoot = detection.matchedBones.get("rightFoot");
3794
+ if (leftFoot && rightFoot) {
3795
+ leftFoot.getWorldPosition(this.restLeftFootModelPos);
3796
+ this.modelRoot.worldToLocal(this.restLeftFootModelPos);
3797
+ rightFoot.getWorldPosition(this.restRightFootModelPos);
3798
+ this.modelRoot.worldToLocal(this.restRightFootModelPos);
3799
+ this.hasRestFootPositions = true;
3800
+ } else {
3801
+ this.hasRestFootPositions = false;
3802
+ }
3803
+ }
3804
+ measureArmRest(upperName, lowerName, defaultLateralX) {
3805
+ const upper = this.joints.get(upperName)?.bone;
3806
+ const lower = this.joints.get(lowerName)?.bone;
3807
+ if (!upper || !lower) {
3808
+ return { angleFromDown: 0, lowerSign: defaultLateralX >= 0 ? -1 : 1 };
3809
+ }
3810
+ upper.getWorldPosition(ScratchMath.v0);
3811
+ lower.getWorldPosition(ScratchMath.v1);
3812
+ this.modelRoot.worldToLocal(ScratchMath.v0);
3813
+ this.modelRoot.worldToLocal(ScratchMath.v1);
3814
+ const dir = ScratchMath.v2.subVectors(ScratchMath.v1, ScratchMath.v0);
3815
+ if (dir.lengthSq() < 1e-8) {
3816
+ return { angleFromDown: 0, lowerSign: defaultLateralX >= 0 ? -1 : 1 };
3817
+ }
3818
+ dir.normalize();
3819
+ const dotDown = Math.max(-1, Math.min(1, dir.dot(ScratchMath.DOWN)));
3820
+ const lateralX = Math.abs(dir.x) > 0.05 ? dir.x : defaultLateralX;
3821
+ return {
3822
+ angleFromDown: Math.acos(dotDown),
3823
+ lowerSign: lateralX >= 0 ? -1 : 1
3824
+ };
3825
+ }
3826
+ /**
3827
+ * Applies a rotation delta defined in character model space (+Y up, +Z forward, +X right/left)
3828
+ * relative to the joint's rest pose, converted cleanly into the bone's local coordinate frame.
3829
+ * Allocation-free and rig-agnostic across Mixamo, Rigify, Unreal, VRM, and generic humanoids.
3830
+ */
3831
+ applyModelDeltaRotation(name, modelDeltaQuat, weight = 1, additive = false) {
3832
+ const joint = this.joints.get(name);
3833
+ const restModel = this.restModelQuat.get(name);
3834
+ const restModelInv = this.restModelQuatInv.get(name);
3835
+ if (!joint || !restModel || !restModelInv || weight <= 1e-3) return;
3836
+ ScratchMath.q5.copy(restModelInv).multiply(modelDeltaQuat).multiply(restModel);
3837
+ if (additive) {
3838
+ ScratchMath.q6.copy(joint.targetQuaternion).multiply(ScratchMath.q5);
3839
+ } else {
3840
+ ScratchMath.q6.copy(joint.restQuaternion).multiply(ScratchMath.q5);
3841
+ }
3842
+ if (weight >= 0.999) {
3843
+ joint.targetQuaternion.copy(ScratchMath.q6);
3844
+ } else {
3845
+ joint.targetQuaternion.slerp(ScratchMath.q6, Math.max(0, Math.min(1, weight)));
3846
+ }
3660
3847
  }
3661
3848
  getJoint(name) {
3662
3849
  return this.joints.get(name);
@@ -3723,67 +3910,102 @@ var AUBRSkeleton = class {
3723
3910
  }
3724
3911
  }
3725
3912
  calculateProportions(bones) {
3726
- const box = new THREE__namespace.Box3().setFromObject(this.modelRoot);
3727
- const size = box.getSize(new THREE__namespace.Vector3());
3728
- const totalHeight = size.y > 0 ? size.y : 1.78;
3913
+ const box = new THREE__namespace.Box3();
3914
+ try {
3915
+ box.setFromObject(this.modelRoot);
3916
+ } catch {
3917
+ box.makeEmpty();
3918
+ }
3919
+ const size = box.isEmpty() ? new THREE__namespace.Vector3(0, 0, 0) : box.getSize(new THREE__namespace.Vector3());
3920
+ let totalHeight = size.y > 0.01 ? size.y : 0;
3921
+ const head = bones.get("head");
3922
+ const leftFoot = bones.get("leftFoot");
3923
+ const rightFoot = bones.get("rightFoot");
3924
+ const hips = bones.get("hips");
3925
+ if (totalHeight <= 0.01 && head && (leftFoot || rightFoot)) {
3926
+ head.getWorldPosition(ScratchMath.v0);
3927
+ (leftFoot || rightFoot).getWorldPosition(ScratchMath.v1);
3928
+ const headToFoot = Math.abs(ScratchMath.v0.y - ScratchMath.v1.y);
3929
+ if (headToFoot > 0.01) {
3930
+ totalHeight = headToFoot * 1.15;
3931
+ }
3932
+ }
3933
+ if (totalHeight <= 0.01 && head && hips) {
3934
+ head.getWorldPosition(ScratchMath.v0);
3935
+ hips.getWorldPosition(ScratchMath.v1);
3936
+ const headToHips = ScratchMath.v0.distanceTo(ScratchMath.v1);
3937
+ if (headToHips > 0.01) {
3938
+ totalHeight = headToHips * 2.2;
3939
+ }
3940
+ }
3941
+ if (totalHeight <= 0.01) {
3942
+ totalHeight = 1.78;
3943
+ }
3729
3944
  const leftUp = bones.get("leftUpperLeg");
3730
3945
  const leftLow = bones.get("leftLowerLeg");
3731
- const leftFoot = bones.get("leftFoot");
3732
- let thighLength = 0.45;
3733
- let shinLength = 0.43;
3734
- let footHeight = 0.08;
3946
+ let thighLength = totalHeight * 0.25;
3947
+ let shinLength = totalHeight * 0.24;
3948
+ let footHeight = totalHeight * 0.045;
3735
3949
  if (leftUp && leftLow) {
3736
3950
  leftUp.getWorldPosition(ScratchMath.v0);
3737
3951
  leftLow.getWorldPosition(ScratchMath.v1);
3738
- thighLength = ScratchMath.v0.distanceTo(ScratchMath.v1);
3952
+ const d = ScratchMath.v0.distanceTo(ScratchMath.v1);
3953
+ if (d > 1e-4) thighLength = d;
3739
3954
  }
3740
3955
  if (leftLow && leftFoot) {
3741
3956
  leftLow.getWorldPosition(ScratchMath.v0);
3742
3957
  leftFoot.getWorldPosition(ScratchMath.v1);
3743
- shinLength = ScratchMath.v0.distanceTo(ScratchMath.v1);
3744
- footHeight = Math.max(leftFoot.position.y, 0.08);
3958
+ const d = ScratchMath.v0.distanceTo(ScratchMath.v1);
3959
+ if (d > 1e-4) shinLength = d;
3960
+ if (!box.isEmpty()) {
3961
+ const rawFootY = ScratchMath.v1.y - box.min.y;
3962
+ footHeight = Math.max(totalHeight * 0.03, Math.min(totalHeight * 0.12, rawFootY));
3963
+ }
3745
3964
  }
3746
3965
  const legLength = thighLength + shinLength + footHeight;
3747
3966
  const spine = bones.get("spine");
3748
- const head = bones.get("head");
3749
- let spineLength = 0.52;
3967
+ let spineLength = totalHeight * 0.29;
3750
3968
  if (spine && head) {
3751
3969
  spine.getWorldPosition(ScratchMath.v0);
3752
3970
  head.getWorldPosition(ScratchMath.v1);
3753
- spineLength = ScratchMath.v0.distanceTo(ScratchMath.v1);
3971
+ const d = ScratchMath.v0.distanceTo(ScratchMath.v1);
3972
+ if (d > 1e-4) spineLength = d;
3754
3973
  }
3755
3974
  const leftArm = bones.get("leftUpperArm");
3756
3975
  const rightArm = bones.get("rightUpperArm");
3757
- let shoulderWidth = 0.42;
3976
+ let shoulderWidth = totalHeight * 0.23;
3758
3977
  if (leftArm && rightArm) {
3759
3978
  leftArm.getWorldPosition(ScratchMath.v0);
3760
3979
  rightArm.getWorldPosition(ScratchMath.v1);
3761
- shoulderWidth = ScratchMath.v0.distanceTo(ScratchMath.v1);
3980
+ const d = ScratchMath.v0.distanceTo(ScratchMath.v1);
3981
+ if (d > 1e-4) shoulderWidth = d;
3762
3982
  }
3763
3983
  const rightUp = bones.get("rightUpperLeg");
3764
- let hipWidth = 0.28;
3984
+ let hipWidth = totalHeight * 0.15;
3765
3985
  if (leftUp && rightUp) {
3766
3986
  leftUp.getWorldPosition(ScratchMath.v0);
3767
3987
  rightUp.getWorldPosition(ScratchMath.v1);
3768
- hipWidth = ScratchMath.v0.distanceTo(ScratchMath.v1);
3988
+ const d = ScratchMath.v0.distanceTo(ScratchMath.v1);
3989
+ if (d > 1e-4) hipWidth = d;
3769
3990
  }
3770
3991
  const leftHand = bones.get("leftHand");
3771
- let armLength = 0.65;
3992
+ let armLength = totalHeight * 0.36;
3772
3993
  if (leftArm && leftHand) {
3773
3994
  leftArm.getWorldPosition(ScratchMath.v0);
3774
3995
  leftHand.getWorldPosition(ScratchMath.v1);
3775
- armLength = ScratchMath.v0.distanceTo(ScratchMath.v1);
3996
+ const d = ScratchMath.v0.distanceTo(ScratchMath.v1);
3997
+ if (d > 1e-4) armLength = d;
3776
3998
  }
3777
3999
  return {
3778
4000
  totalHeight,
3779
- legLength: Math.max(legLength, 0.4),
3780
- thighLength: Math.max(thighLength, 0.2),
3781
- shinLength: Math.max(shinLength, 0.2),
3782
- footHeight: Math.max(footHeight, 0.05),
3783
- spineLength: Math.max(spineLength, 0.3),
3784
- shoulderWidth: Math.max(shoulderWidth, 0.25),
3785
- hipWidth: Math.max(hipWidth, 0.15),
3786
- armLength: Math.max(armLength, 0.35)
4001
+ legLength: Math.max(legLength, totalHeight * 0.25),
4002
+ thighLength: Math.max(thighLength, totalHeight * 0.12),
4003
+ shinLength: Math.max(shinLength, totalHeight * 0.12),
4004
+ footHeight: Math.max(footHeight, totalHeight * 0.025),
4005
+ spineLength: Math.max(spineLength, totalHeight * 0.15),
4006
+ shoulderWidth: Math.max(shoulderWidth, totalHeight * 0.12),
4007
+ hipWidth: Math.max(hipWidth, totalHeight * 0.08),
4008
+ armLength: Math.max(armLength, totalHeight * 0.2)
3787
4009
  };
3788
4010
  }
3789
4011
  };
@@ -3877,35 +4099,91 @@ var TwoBoneIK = class {
3877
4099
  };
3878
4100
  }
3879
4101
  /**
3880
- * Applies solved two-bone IK rotations to the target AUBR joints in-place.
4102
+ * Applies solved two-bone IK rotations to the target AUBR leg joints in-place
4103
+ * using rest-pose-preserving world-space delta rotations so Mixamo, Rigify, Unreal,
4104
+ * VRM, and generic humanoid bone orientations are never distorted.
4105
+ *
4106
+ * `targetFootPosWorld` and `poleDirWorld` are in world space (or model space when
4107
+ * `modelRoot` is omitted and bones are evaluated in local space).
3881
4108
  */
3882
- static applyLegIK(thighBone, shinBone, footBone, thighLength, shinLength, targetFootPosModel, poleVector, weight = 1, modelRoot) {
4109
+ static applyLegIK(thighBone, shinBone, footBone, thighLength, shinLength, targetFootPosWorld, poleDirWorld, weight = 1, _modelRoot) {
3883
4110
  if (weight <= 1e-3) return;
3884
- const rootPos = ScratchMath.v3;
3885
- thighBone.getWorldPosition(rootPos);
3886
- if (modelRoot) {
3887
- modelRoot.worldToLocal(rootPos);
3888
- } else if (thighBone.parent) {
3889
- thighBone.parent.worldToLocal(rootPos);
3890
- }
3891
- const chain = {
3892
- rootBone: thighBone,
3893
- midBone: shinBone,
3894
- endBone: footBone,
3895
- poleVector,
3896
- length1: thighLength,
3897
- length2: shinLength
3898
- };
3899
- const result = this.solveAnalytical(chain, targetFootPosModel, rootPos, poleVector);
3900
- if (weight >= 0.999) {
3901
- thighBone.quaternion.copy(result.rootRotation);
3902
- shinBone.quaternion.copy(result.midRotation);
4111
+ const clampedWeight = Math.max(0, Math.min(1, weight));
4112
+ const hipPos = ScratchMath.v0;
4113
+ const kneePos = ScratchMath.v1;
4114
+ const anklePos = ScratchMath.v2;
4115
+ thighBone.getWorldPosition(hipPos);
4116
+ shinBone.getWorldPosition(kneePos);
4117
+ footBone.getWorldPosition(anklePos);
4118
+ let l1 = hipPos.distanceTo(kneePos);
4119
+ let l2 = kneePos.distanceTo(anklePos);
4120
+ if (l1 < 1e-5) l1 = Math.max(thighLength, 0.05);
4121
+ if (l2 < 1e-5) l2 = Math.max(shinLength, 0.05);
4122
+ const maxReach = l1 + l2;
4123
+ const minReach = Math.abs(l1 - l2);
4124
+ const toTarget = ScratchMath.v3.subVectors(targetFootPosWorld, hipPos);
4125
+ const dist = toTarget.length();
4126
+ const clampedDist = Math.max(minReach + 1e-4, Math.min(dist, maxReach * 0.99999));
4127
+ if (dist < 1e-6) {
4128
+ toTarget.copy(ScratchMath.DOWN);
3903
4129
  } else {
3904
- thighBone.quaternion.slerp(result.rootRotation, weight);
3905
- shinBone.quaternion.slerp(result.midRotation, weight);
4130
+ toTarget.divideScalar(dist);
4131
+ }
4132
+ const bendDir = ScratchMath.v4.copy(poleDirWorld);
4133
+ const poleProj = bendDir.dot(toTarget);
4134
+ bendDir.addScaledVector(toTarget, -poleProj);
4135
+ if (bendDir.lengthSq() < 1e-6) {
4136
+ bendDir.crossVectors(toTarget, ScratchMath.RIGHT);
4137
+ if (bendDir.lengthSq() < 1e-6) {
4138
+ bendDir.crossVectors(toTarget, ScratchMath.FORWARD);
4139
+ }
4140
+ }
4141
+ bendDir.normalize();
4142
+ const cosAlpha = Math.max(
4143
+ -1,
4144
+ Math.min(1, (l1 * l1 + clampedDist * clampedDist - l2 * l2) / (2 * l1 * clampedDist))
4145
+ );
4146
+ const sinAlpha = Math.sqrt(Math.max(0, 1 - cosAlpha * cosAlpha));
4147
+ const desiredThighDir = ScratchMath.v5.copy(toTarget).multiplyScalar(cosAlpha).addScaledVector(bendDir, sinAlpha).normalize();
4148
+ const desiredKneePos = ScratchMath.v6.copy(hipPos).addScaledVector(desiredThighDir, l1);
4149
+ const desiredShinDir = ScratchMath.v7.subVectors(targetFootPosWorld, desiredKneePos);
4150
+ if (desiredShinDir.lengthSq() < 1e-8) {
4151
+ desiredShinDir.copy(toTarget);
4152
+ } else {
4153
+ desiredShinDir.normalize();
4154
+ }
4155
+ const currentThighDir = ScratchMath.v8.subVectors(kneePos, hipPos);
4156
+ if (currentThighDir.lengthSq() > 1e-10) {
4157
+ currentThighDir.normalize();
4158
+ ScratchMath.q0.setFromUnitVectors(currentThighDir, desiredThighDir);
4159
+ thighBone.getWorldQuaternion(ScratchMath.q1);
4160
+ ScratchMath.q2.copy(ScratchMath.q1).invert();
4161
+ ScratchMath.q3.copy(ScratchMath.q2).multiply(ScratchMath.q0).multiply(ScratchMath.q1);
4162
+ ScratchMath.q4.copy(thighBone.quaternion).multiply(ScratchMath.q3);
4163
+ if (clampedWeight >= 0.999) {
4164
+ thighBone.quaternion.copy(ScratchMath.q4);
4165
+ } else {
4166
+ thighBone.quaternion.slerp(ScratchMath.q4, clampedWeight);
4167
+ }
4168
+ thighBone.updateMatrixWorld(true);
4169
+ }
4170
+ shinBone.getWorldPosition(kneePos);
4171
+ footBone.getWorldPosition(anklePos);
4172
+ const currentShinDir = ScratchMath.v8.subVectors(anklePos, kneePos);
4173
+ if (currentShinDir.lengthSq() > 1e-10) {
4174
+ currentShinDir.normalize();
4175
+ ScratchMath.q0.setFromUnitVectors(currentShinDir, desiredShinDir);
4176
+ shinBone.getWorldQuaternion(ScratchMath.q1);
4177
+ ScratchMath.q2.copy(ScratchMath.q1).invert();
4178
+ ScratchMath.q3.copy(ScratchMath.q2).multiply(ScratchMath.q0).multiply(ScratchMath.q1);
4179
+ ScratchMath.q4.copy(shinBone.quaternion).multiply(ScratchMath.q3);
4180
+ if (clampedWeight >= 0.999) {
4181
+ shinBone.quaternion.copy(ScratchMath.q4);
4182
+ } else {
4183
+ shinBone.quaternion.slerp(ScratchMath.q4, clampedWeight);
4184
+ }
4185
+ shinBone.updateMatrixWorld(true);
3906
4186
  }
3907
- ScratchMath.q2.setFromAxisAngle(ScratchMath.RIGHT, 0);
3908
- footBone.quaternion.slerp(ScratchMath.q2, weight * 0.85);
3909
4187
  }
3910
4188
  };
3911
4189
  var _FABRIKSolver = class _FABRIKSolver {
@@ -4058,43 +4336,52 @@ var _FABRIKSolver = class _FABRIKSolver {
4058
4336
  };
4059
4337
  }
4060
4338
  /**
4061
- * Projects middle hinge joint onto plane defined by pole vector.
4339
+ * Projects middle hinge joint onto the half-plane defined by the pole direction vector.
4062
4340
  */
4063
4341
  static applyPoleVector(root, hinge, end, pole) {
4064
- const line = ScratchMath.v1.subVectors(end, root).normalize();
4342
+ const line = ScratchMath.v1.subVectors(end, root);
4343
+ if (line.lengthSq() < 1e-10) return;
4344
+ line.normalize();
4065
4345
  const toHinge = ScratchMath.v2.subVectors(hinge, root);
4066
4346
  const projDist = toHinge.dot(line);
4067
4347
  const projPoint = ScratchMath.v3.copy(root).addScaledVector(line, projDist);
4068
- const currentNormal = ScratchMath.v4.subVectors(hinge, projPoint).normalize();
4069
- const desiredNormal = ScratchMath.v5.subVectors(pole, projPoint).normalize();
4070
- if (currentNormal.lengthSq() > 1e-3 && desiredNormal.lengthSq() > 1e-3) {
4348
+ const desiredNormal = ScratchMath.v5.copy(pole).addScaledVector(line, -pole.dot(line));
4349
+ if (desiredNormal.lengthSq() > 1e-6) {
4350
+ desiredNormal.normalize();
4071
4351
  const hingeDist = hinge.distanceTo(projPoint);
4072
- hinge.copy(projPoint).addScaledVector(desiredNormal, Math.max(hingeDist, 0.05));
4352
+ hinge.copy(projPoint).addScaledVector(desiredNormal, Math.max(hingeDist, 0.02));
4073
4353
  }
4074
4354
  }
4075
4355
  /**
4076
4356
  * Converts solved world positions back into local bone rotations for the Three.js hierarchy.
4357
+ * Calculates delta rotation in world space from current bone direction to solved target direction,
4358
+ * transforming into local bone space to guarantee zero deformation when positions match rest pose.
4077
4359
  */
4078
4360
  static applyPositionsToBones(chain, solvedPositions, weight = 1) {
4079
4361
  if (solvedPositions.length < 2 || weight <= 1e-3) return;
4080
4362
  for (let i = 0; i < chain.bones.length - 1; i++) {
4081
4363
  const bone = chain.bones[i];
4364
+ const nextBone = chain.bones[i + 1];
4082
4365
  const nextPos = solvedPositions[i + 1];
4083
- if (!bone || !nextPos) continue;
4366
+ if (!bone || !nextBone || !nextPos) continue;
4084
4367
  bone.getWorldPosition(ScratchMath.v0);
4368
+ nextBone.getWorldPosition(ScratchMath.v2);
4369
+ const currentDirWorld = ScratchMath.v3.subVectors(ScratchMath.v2, ScratchMath.v0);
4370
+ if (currentDirWorld.lengthSq() < 1e-10) continue;
4371
+ currentDirWorld.normalize();
4085
4372
  const targetDirWorld = ScratchMath.v1.subVectors(nextPos, ScratchMath.v0);
4086
4373
  if (targetDirWorld.lengthSq() < 1e-10) continue;
4087
4374
  targetDirWorld.normalize();
4088
- if (bone.parent) {
4089
- ScratchMath.m0.copy(bone.parent.matrixWorld).invert();
4090
- targetDirWorld.transformDirection(ScratchMath.m0).normalize();
4091
- }
4092
- const restDir = ScratchMath.RIGHT;
4093
- ScratchMath.q0.setFromUnitVectors(restDir, targetDirWorld);
4375
+ ScratchMath.q0.setFromUnitVectors(currentDirWorld, targetDirWorld);
4376
+ bone.getWorldQuaternion(ScratchMath.q1);
4377
+ ScratchMath.q2.copy(ScratchMath.q1).invert();
4378
+ ScratchMath.q3.copy(ScratchMath.q2).multiply(ScratchMath.q0).multiply(ScratchMath.q1);
4379
+ const currentLocalQuat = ScratchMath.q4.copy(bone.quaternion);
4380
+ const targetLocalQuat = ScratchMath.q3.premultiply(currentLocalQuat);
4094
4381
  if (weight >= 0.999) {
4095
- bone.quaternion.copy(ScratchMath.q0);
4382
+ bone.quaternion.copy(targetLocalQuat);
4096
4383
  } else {
4097
- bone.quaternion.slerp(ScratchMath.q0, weight);
4384
+ bone.quaternion.copy(currentLocalQuat).slerp(targetLocalQuat, weight);
4098
4385
  }
4099
4386
  bone.updateMatrixWorld(true);
4100
4387
  }
@@ -4126,21 +4413,25 @@ var IKCoordinator = class {
4126
4413
  }
4127
4414
  /**
4128
4415
  * Solves and applies left leg Two-Bone IK.
4416
+ * `targetFootPosModel` is in `skeleton.modelRoot` local space.
4129
4417
  */
4130
4418
  solveLeftLeg(targetFootPosModel, weight = 1) {
4131
4419
  const thigh = this.skeleton.getJoint("leftUpperLeg");
4132
4420
  const shin = this.skeleton.getJoint("leftLowerLeg");
4133
4421
  const foot = this.skeleton.getJoint("leftFoot");
4134
- if (!thigh || !shin || !foot) return;
4135
- const pole = ScratchMath.v6.copy(ScratchMath.FORWARD);
4422
+ if (!thigh || !shin || !foot || weight <= 1e-3) return;
4423
+ const targetWorld = ScratchMath.v9.copy(targetFootPosModel);
4424
+ this.skeleton.modelRoot.localToWorld(targetWorld);
4425
+ this.skeleton.modelRoot.getWorldQuaternion(ScratchMath.q5);
4426
+ const poleWorld = ScratchMath.v8.copy(ScratchMath.FORWARD).applyQuaternion(ScratchMath.q5).normalize();
4136
4427
  TwoBoneIK.applyLegIK(
4137
4428
  thigh.bone,
4138
4429
  shin.bone,
4139
4430
  foot.bone,
4140
4431
  this.skeleton.proportions.thighLength,
4141
4432
  this.skeleton.proportions.shinLength,
4142
- targetFootPosModel,
4143
- pole,
4433
+ targetWorld,
4434
+ poleWorld,
4144
4435
  weight,
4145
4436
  this.skeleton.modelRoot
4146
4437
  );
@@ -4150,21 +4441,25 @@ var IKCoordinator = class {
4150
4441
  }
4151
4442
  /**
4152
4443
  * Solves and applies right leg Two-Bone IK.
4444
+ * `targetFootPosModel` is in `skeleton.modelRoot` local space.
4153
4445
  */
4154
4446
  solveRightLeg(targetFootPosModel, weight = 1) {
4155
4447
  const thigh = this.skeleton.getJoint("rightUpperLeg");
4156
4448
  const shin = this.skeleton.getJoint("rightLowerLeg");
4157
4449
  const foot = this.skeleton.getJoint("rightFoot");
4158
- if (!thigh || !shin || !foot) return;
4159
- const pole = ScratchMath.v6.copy(ScratchMath.FORWARD);
4450
+ if (!thigh || !shin || !foot || weight <= 1e-3) return;
4451
+ const targetWorld = ScratchMath.v9.copy(targetFootPosModel);
4452
+ this.skeleton.modelRoot.localToWorld(targetWorld);
4453
+ this.skeleton.modelRoot.getWorldQuaternion(ScratchMath.q5);
4454
+ const poleWorld = ScratchMath.v8.copy(ScratchMath.FORWARD).applyQuaternion(ScratchMath.q5).normalize();
4160
4455
  TwoBoneIK.applyLegIK(
4161
4456
  thigh.bone,
4162
4457
  shin.bone,
4163
4458
  foot.bone,
4164
4459
  this.skeleton.proportions.thighLength,
4165
4460
  this.skeleton.proportions.shinLength,
4166
- targetFootPosModel,
4167
- pole,
4461
+ targetWorld,
4462
+ poleWorld,
4168
4463
  weight,
4169
4464
  this.skeleton.modelRoot
4170
4465
  );
@@ -4178,7 +4473,8 @@ var IKCoordinator = class {
4178
4473
  solveArmReach(side, targetPosWorld, weight = 1) {
4179
4474
  const chain = side === "left" ? this.leftArmChain : this.rightArmChain;
4180
4475
  if (!chain || weight <= 1e-3) return;
4181
- const pole = side === "left" ? ScratchMath.v7.set(-0.5, 0, -1).normalize() : ScratchMath.v7.set(0.5, 0, -1).normalize();
4476
+ this.skeleton.modelRoot.getWorldQuaternion(ScratchMath.q5);
4477
+ const pole = (side === "left" ? ScratchMath.v7.set(-0.5, 0, -1).normalize() : ScratchMath.v7.set(0.5, 0, -1).normalize()).applyQuaternion(ScratchMath.q5);
4182
4478
  const result = FABRIKSolver.solve(chain, targetPosWorld, pole);
4183
4479
  FABRIKSolver.applyPositionsToBones(chain, result.solvedPositions, weight);
4184
4480
  const upper = this.skeleton.getJoint(side === "left" ? "leftUpperArm" : "rightUpperArm");
@@ -4202,63 +4498,86 @@ var IdleEngine = class {
4202
4498
  this.targetWeightShift = 0;
4203
4499
  this.nextWeightShiftTime = 3.5;
4204
4500
  }
4501
+ reset() {
4502
+ this.clock = 0;
4503
+ this.breathPhase = 0;
4504
+ this.swayPhase = 0;
4505
+ this.weightShift = 0;
4506
+ this.targetWeightShift = 0;
4507
+ this.nextWeightShiftTime = 3.5;
4508
+ }
4205
4509
  update(delta, weight = 1) {
4206
4510
  if (weight <= 1e-3) return;
4511
+ const w = Math.max(0, Math.min(1, weight));
4207
4512
  this.clock += delta;
4208
4513
  this.breathPhase += delta * 0.28;
4209
- this.swayPhase += delta * 0.12;
4514
+ this.swayPhase += delta * 0.14;
4210
4515
  if (this.clock > this.nextWeightShiftTime) {
4211
- this.targetWeightShift = (Math.random() - 0.5) * 1.6;
4516
+ this.targetWeightShift = (Math.random() - 0.5) * 1.4;
4212
4517
  this.nextWeightShiftTime = this.clock + 4 + Math.random() * 4;
4213
4518
  }
4214
- this.weightShift = ScratchMath.damp(this.weightShift, this.targetWeightShift, 1.2, delta);
4519
+ this.weightShift = ScratchMath.damp(this.weightShift, this.targetWeightShift, 1.4, delta);
4520
+ const localScale = this.skeleton.hipsLocalPerMeter;
4215
4521
  const breathSin = Math.sin(this.breathPhase * Math.PI * 2);
4216
- const spinePitch = breathSin * 0.015;
4217
- const chestPitch = breathSin * 0.022;
4522
+ const spinePitch = breathSin * 0.022 * w;
4523
+ const chestPitch = breathSin * 0.028 * w;
4218
4524
  const swaySin = Math.sin(this.swayPhase * Math.PI * 2);
4219
- const pelvicX = (swaySin * 6e-3 + this.weightShift * 0.012) * weight;
4220
- const pelvicY = breathSin * 4e-3 * weight;
4221
- const pelvicRoll = -this.weightShift * 0.018 * weight;
4525
+ const pelvicX = (swaySin * 8e-3 + this.weightShift * 0.014) * localScale * w;
4526
+ const pelvicY = breathSin * 5e-3 * localScale * w;
4527
+ const pelvicRoll = -this.weightShift * 0.022 * w;
4222
4528
  const hips = this.skeleton.getJoint("hips");
4223
4529
  if (hips) {
4224
4530
  hips.targetPosition.x = hips.restPosition.x + pelvicX;
4225
4531
  hips.targetPosition.y = hips.restPosition.y + pelvicY;
4226
4532
  ScratchMath.e0.set(0, 0, pelvicRoll, "YXZ");
4227
4533
  ScratchMath.q0.setFromEuler(ScratchMath.e0);
4228
- hips.targetQuaternion.copy(hips.restQuaternion).multiply(ScratchMath.q0);
4534
+ this.skeleton.applyModelDeltaRotation("hips", ScratchMath.q0, w);
4229
4535
  }
4230
- const spine = this.skeleton.getJoint("spine");
4231
- if (spine) {
4232
- ScratchMath.e0.set(spinePitch * weight, 0, 0, "YXZ");
4536
+ if (this.skeleton.hasJoint("spine")) {
4537
+ ScratchMath.e0.set(spinePitch, swaySin * 0.012 * w, -pelvicRoll * 0.4, "YXZ");
4233
4538
  ScratchMath.q0.setFromEuler(ScratchMath.e0);
4234
- spine.targetQuaternion.copy(spine.restQuaternion).multiply(ScratchMath.q0);
4539
+ this.skeleton.applyModelDeltaRotation("spine", ScratchMath.q0, w);
4235
4540
  }
4236
- const chest = this.skeleton.getJoint("chest") || this.skeleton.getJoint("upperChest");
4237
- if (chest) {
4238
- ScratchMath.e0.set(chestPitch * weight, 0, 0, "YXZ");
4541
+ const chestName = this.skeleton.hasJoint("chest") ? "chest" : this.skeleton.hasJoint("upperChest") ? "upperChest" : null;
4542
+ if (chestName) {
4543
+ ScratchMath.e0.set(chestPitch, 0, 0, "YXZ");
4239
4544
  ScratchMath.q0.setFromEuler(ScratchMath.e0);
4240
- chest.targetQuaternion.copy(chest.restQuaternion).multiply(ScratchMath.q0);
4545
+ this.skeleton.applyModelDeltaRotation(chestName, ScratchMath.q0, w);
4241
4546
  }
4242
- const head = this.skeleton.getJoint("head");
4243
- if (head) {
4244
- const headCounterRoll = -pelvicRoll * 0.6;
4245
- const headCounterPitch = -spinePitch * 0.5;
4547
+ if (this.skeleton.hasJoint("head")) {
4548
+ const headCounterRoll = -pelvicRoll * 0.5;
4549
+ const headCounterPitch = -spinePitch * 0.45;
4246
4550
  ScratchMath.e0.set(headCounterPitch, 0, headCounterRoll, "YXZ");
4247
4551
  ScratchMath.q0.setFromEuler(ScratchMath.e0);
4248
- head.targetQuaternion.copy(head.restQuaternion).multiply(ScratchMath.q0);
4249
- }
4250
- const lArm = this.skeleton.getJoint("leftUpperArm");
4251
- const rArm = this.skeleton.getJoint("rightUpperArm");
4252
- const armSway = breathSin * 0.012 * weight;
4253
- if (lArm) {
4254
- ScratchMath.e0.set(armSway, 0, armSway * 0.5, "YXZ");
4255
- ScratchMath.q0.setFromEuler(ScratchMath.e0);
4256
- lArm.targetQuaternion.copy(lArm.restQuaternion).multiply(ScratchMath.q0);
4257
- }
4258
- if (rArm) {
4259
- ScratchMath.e0.set(armSway, 0, -armSway * 0.5, "YXZ");
4260
- ScratchMath.q0.setFromEuler(ScratchMath.e0);
4261
- rArm.targetQuaternion.copy(rArm.restQuaternion).multiply(ScratchMath.q0);
4552
+ this.skeleton.applyModelDeltaRotation("head", ScratchMath.q0, w);
4553
+ }
4554
+ const targetStandingArmAngle = 0.28;
4555
+ const armBreathSway = breathSin * 0.025;
4556
+ const armOutSway = breathSin * 0.018;
4557
+ if (this.skeleton.hasJoint("leftUpperArm")) {
4558
+ const relaxAngle = Math.max(0, this.skeleton.leftArmRestAngleFromDown - targetStandingArmAngle);
4559
+ const lowerZ = this.skeleton.leftArmLowerSign * (relaxAngle - armOutSway);
4560
+ ScratchMath.q1.setFromAxisAngle(ScratchMath.FORWARD, lowerZ);
4561
+ ScratchMath.q2.setFromAxisAngle(ScratchMath.RIGHT, -armBreathSway);
4562
+ ScratchMath.q0.copy(ScratchMath.q2).multiply(ScratchMath.q1);
4563
+ this.skeleton.applyModelDeltaRotation("leftUpperArm", ScratchMath.q0, w);
4564
+ }
4565
+ if (this.skeleton.hasJoint("rightUpperArm")) {
4566
+ const relaxAngle = Math.max(0, this.skeleton.rightArmRestAngleFromDown - targetStandingArmAngle);
4567
+ const lowerZ = this.skeleton.rightArmLowerSign * (relaxAngle - armOutSway);
4568
+ ScratchMath.q1.setFromAxisAngle(ScratchMath.FORWARD, lowerZ);
4569
+ ScratchMath.q2.setFromAxisAngle(ScratchMath.RIGHT, -armBreathSway);
4570
+ ScratchMath.q0.copy(ScratchMath.q2).multiply(ScratchMath.q1);
4571
+ this.skeleton.applyModelDeltaRotation("rightUpperArm", ScratchMath.q0, w);
4572
+ }
4573
+ const elbowRelaxFlex = 0.16 + breathSin * 0.015;
4574
+ if (this.skeleton.hasJoint("leftLowerArm")) {
4575
+ ScratchMath.q0.setFromAxisAngle(ScratchMath.RIGHT, -elbowRelaxFlex);
4576
+ this.skeleton.applyModelDeltaRotation("leftLowerArm", ScratchMath.q0, w);
4577
+ }
4578
+ if (this.skeleton.hasJoint("rightLowerArm")) {
4579
+ ScratchMath.q0.setFromAxisAngle(ScratchMath.RIGHT, -elbowRelaxFlex);
4580
+ this.skeleton.applyModelDeltaRotation("rightLowerArm", ScratchMath.q0, w);
4262
4581
  }
4263
4582
  }
4264
4583
  };
@@ -4301,9 +4620,10 @@ var LocomotionEngine = class {
4301
4620
  return this.phase;
4302
4621
  }
4303
4622
  calibrateStride() {
4304
- const legLen = this.skeleton.proportions.legLength;
4305
- this.strideLength = Math.max(0.35, legLen * 0.78);
4306
- this.stepHeight = Math.max(0.04, legLen * 0.095);
4623
+ const unitScale = this.skeleton.hipsLocalPerMeter;
4624
+ const legLenLocal = Math.max(0.4 * unitScale, this.skeleton.proportions.legLength / Math.max(this.skeleton.worldUnitsPerMeter, 1e-4) * unitScale);
4625
+ this.strideLength = Math.max(0.35 * unitScale, legLenLocal * 0.72);
4626
+ this.stepHeight = Math.max(0.04 * unitScale, legLenLocal * 0.11);
4307
4627
  }
4308
4628
  setMotion(params) {
4309
4629
  this.targetSpeed = Math.max(0, params.speed);
@@ -4313,113 +4633,128 @@ var LocomotionEngine = class {
4313
4633
  const stridesPerSecond = params.screenVelocityPx / 180;
4314
4634
  this.baseFrequency = Math.max(0.8, Math.min(3.2, stridesPerSecond * 2));
4315
4635
  } else {
4316
- this.baseFrequency = this.isRunning ? 2.6 : 1.5;
4636
+ this.baseFrequency = this.isRunning ? 2.4 : 1.35;
4317
4637
  }
4318
4638
  }
4319
4639
  stop() {
4320
4640
  this.targetSpeed = 0;
4321
4641
  this.moving = false;
4322
4642
  }
4643
+ cancel() {
4644
+ this.targetSpeed = 0;
4645
+ this.currentSpeed = 0;
4646
+ this.moving = false;
4647
+ this.isRunning = false;
4648
+ }
4323
4649
  update(delta, weight = 1) {
4324
- this.currentSpeed = ScratchMath.damp(this.currentSpeed, this.targetSpeed, 4, delta);
4650
+ const speedDamp = this.moving ? 6.5 : 8;
4651
+ this.currentSpeed = ScratchMath.damp(this.currentSpeed, this.targetSpeed, speedDamp, delta);
4325
4652
  if (this.currentSpeed <= 0.01 && weight <= 1e-3) return;
4326
- const effectiveFreq = this.baseFrequency * Math.max(0.3, this.currentSpeed);
4653
+ const w = Math.max(0, Math.min(1, weight));
4654
+ const speedRatio = Math.min(this.currentSpeed, 2.2);
4655
+ const activeAmplitude = Math.min(1, this.currentSpeed / 0.35) * w;
4656
+ if (activeAmplitude <= 1e-3) return;
4657
+ const effectiveFreq = this.baseFrequency * Math.max(0.35, Math.min(1.6, this.currentSpeed));
4327
4658
  this.phase = (this.phase + effectiveFreq * delta) % 1;
4328
4659
  const phi = this.phase;
4329
4660
  const twoPiPhi = phi * Math.PI * 2;
4330
- const speedRatio = Math.min(this.currentSpeed, 2);
4331
- const bobAmplitude = (this.isRunning ? 0.045 : 0.024) * speedRatio;
4661
+ const localScale = this.skeleton.hipsLocalPerMeter;
4662
+ const bobAmplitude = (this.isRunning ? 0.05 : 0.028) * speedRatio * localScale;
4332
4663
  const pelvicY = -bobAmplitude * Math.abs(Math.sin(twoPiPhi));
4333
- const swayAmplitude = (this.isRunning ? 0.018 : 0.028) * speedRatio;
4664
+ const swayAmplitude = (this.isRunning ? 0.02 : 0.03) * speedRatio * localScale;
4334
4665
  const pelvicX = swayAmplitude * Math.sin(twoPiPhi);
4335
4666
  const yawAmplitude = (this.isRunning ? 0.14 : 0.09) * speedRatio;
4336
4667
  const pelvicYaw = yawAmplitude * Math.cos(twoPiPhi);
4337
- const rollAmplitude = 0.035 * speedRatio;
4668
+ const rollAmplitude = 0.04 * speedRatio;
4338
4669
  const pelvicRoll = -rollAmplitude * Math.sin(twoPiPhi);
4339
4670
  const hips = this.skeleton.getJoint("hips");
4340
4671
  if (hips) {
4341
- hips.targetPosition.x = hips.restPosition.x + pelvicX * weight;
4342
- hips.targetPosition.y = hips.restPosition.y + pelvicY * weight;
4343
- ScratchMath.e0.set(0.04 * speedRatio, pelvicYaw * weight, pelvicRoll * weight, "YXZ");
4672
+ hips.targetPosition.x = hips.restPosition.x + pelvicX * w;
4673
+ hips.targetPosition.y = hips.restPosition.y + pelvicY * w;
4674
+ ScratchMath.e0.set((this.isRunning ? 0.08 : 0.04) * speedRatio, pelvicYaw, pelvicRoll, "YXZ");
4344
4675
  ScratchMath.q0.setFromEuler(ScratchMath.e0);
4345
- hips.targetQuaternion.copy(hips.restQuaternion).multiply(ScratchMath.q0);
4676
+ this.skeleton.applyModelDeltaRotation("hips", ScratchMath.q0, activeAmplitude);
4346
4677
  }
4347
- const spine = this.skeleton.getJoint("spine");
4348
- if (spine) {
4349
- const spineCounterYaw = -pelvicYaw * 0.65 * weight;
4350
- ScratchMath.e0.set(-0.02 * speedRatio, spineCounterYaw, 0, "YXZ");
4678
+ if (this.skeleton.hasJoint("spine")) {
4679
+ const spineCounterYaw = -pelvicYaw * 0.65;
4680
+ ScratchMath.e0.set((this.isRunning ? 0.08 : 0.03) * speedRatio, spineCounterYaw, -pelvicRoll * 0.35, "YXZ");
4351
4681
  ScratchMath.q0.setFromEuler(ScratchMath.e0);
4352
- spine.targetQuaternion.copy(spine.restQuaternion).multiply(ScratchMath.q0);
4682
+ this.skeleton.applyModelDeltaRotation("spine", ScratchMath.q0, activeAmplitude);
4353
4683
  }
4354
- const chest = this.skeleton.getJoint("chest") || this.skeleton.getJoint("upperChest");
4355
- if (chest) {
4356
- const chestCounterYaw = -pelvicYaw * 0.35 * weight;
4357
- ScratchMath.e0.set(0.01, chestCounterYaw, 0, "YXZ");
4684
+ const chestName = this.skeleton.hasJoint("chest") ? "chest" : this.skeleton.hasJoint("upperChest") ? "upperChest" : null;
4685
+ if (chestName) {
4686
+ const chestCounterYaw = -pelvicYaw * 0.35;
4687
+ ScratchMath.e0.set(0.02 * speedRatio, chestCounterYaw, 0, "YXZ");
4358
4688
  ScratchMath.q0.setFromEuler(ScratchMath.e0);
4359
- chest.targetQuaternion.copy(chest.restQuaternion).multiply(ScratchMath.q0);
4360
- }
4361
- const hipWidthHalf = this.skeleton.proportions.hipWidth * 0.5;
4362
- const groundY = 0;
4363
- const halfStride = this.strideLength * 0.5 * speedRatio;
4364
- let leftX = -hipWidthHalf;
4365
- let leftY = groundY;
4366
- let leftZ = 0;
4689
+ this.skeleton.applyModelDeltaRotation(chestName, ScratchMath.q0, activeAmplitude);
4690
+ }
4691
+ const halfStride = this.strideLength * 0.5 * Math.min(speedRatio, 1.45);
4692
+ const liftHeight = this.stepHeight * Math.min(speedRatio, 1.5);
4693
+ const baseLeftX = this.skeleton.hasRestFootPositions ? this.skeleton.restLeftFootModelPos.x : -this.skeleton.proportions.hipWidth * 0.5;
4694
+ const baseLeftY = this.skeleton.hasRestFootPositions ? this.skeleton.restLeftFootModelPos.y : 0;
4695
+ const baseLeftZ = this.skeleton.hasRestFootPositions ? this.skeleton.restLeftFootModelPos.z : 0;
4696
+ const baseRightX = this.skeleton.hasRestFootPositions ? this.skeleton.restRightFootModelPos.x : this.skeleton.proportions.hipWidth * 0.5;
4697
+ const baseRightY = this.skeleton.hasRestFootPositions ? this.skeleton.restRightFootModelPos.y : 0;
4698
+ const baseRightZ = this.skeleton.hasRestFootPositions ? this.skeleton.restRightFootModelPos.z : 0;
4699
+ let leftY = baseLeftY;
4700
+ let leftZ = baseLeftZ;
4367
4701
  if (phi < 0.5) {
4368
4702
  const s = phi / 0.5;
4369
- leftZ = halfStride * (1 - 2 * s);
4370
- leftY = groundY;
4703
+ leftZ = baseLeftZ + halfStride * (1 - 2 * s);
4371
4704
  } else {
4372
4705
  const s = (phi - 0.5) / 0.5;
4373
- leftZ = halfStride * (2 * s - 1);
4374
- leftY = groundY + this.stepHeight * speedRatio * (4 * s * (1 - s));
4706
+ leftZ = baseLeftZ + halfStride * (2 * s - 1);
4707
+ leftY = baseLeftY + liftHeight * (4 * s * (1 - s));
4375
4708
  }
4376
- let rightX = hipWidthHalf;
4377
- let rightY = groundY;
4378
- let rightZ = 0;
4709
+ let rightY = baseRightY;
4710
+ let rightZ = baseRightZ;
4379
4711
  if (phi < 0.5) {
4380
4712
  const s = phi / 0.5;
4381
- rightZ = halfStride * (2 * s - 1);
4382
- rightY = groundY + this.stepHeight * speedRatio * (4 * s * (1 - s));
4713
+ rightZ = baseRightZ + halfStride * (2 * s - 1);
4714
+ rightY = baseRightY + liftHeight * (4 * s * (1 - s));
4383
4715
  } else {
4384
4716
  const s = (phi - 0.5) / 0.5;
4385
- rightZ = halfStride * (1 - 2 * s);
4386
- rightY = groundY;
4717
+ rightZ = baseRightZ + halfStride * (1 - 2 * s);
4387
4718
  }
4388
- this.leftFootTarget.set(leftX, leftY, leftZ);
4389
- this.rightFootTarget.set(rightX, rightY, rightZ);
4719
+ this.leftFootTarget.set(baseLeftX, leftY, leftZ);
4720
+ this.rightFootTarget.set(baseRightX, rightY, rightZ);
4390
4721
  if (this.ik) {
4391
- this.ik.solveLeftLeg(this.leftFootTarget, weight);
4392
- this.ik.solveRightLeg(this.rightFootTarget, weight);
4393
- }
4394
- const armSwingAngle = (this.isRunning ? 0.65 : 0.38) * speedRatio * weight;
4395
- const lArmAngle = -Math.sin(twoPiPhi) * armSwingAngle;
4396
- const rArmAngle = Math.sin(twoPiPhi) * armSwingAngle;
4397
- const lArm = this.skeleton.getJoint("leftUpperArm");
4398
- const rArm = this.skeleton.getJoint("rightUpperArm");
4399
- const lForeArm = this.skeleton.getJoint("leftLowerArm");
4400
- const rForeArm = this.skeleton.getJoint("rightLowerArm");
4401
- if (lArm) {
4402
- ScratchMath.e0.set(lArmAngle, 0, 0.05, "YXZ");
4403
- ScratchMath.q0.setFromEuler(ScratchMath.e0);
4404
- lArm.targetQuaternion.copy(lArm.restQuaternion).multiply(ScratchMath.q0);
4405
- }
4406
- if (rArm) {
4407
- ScratchMath.e0.set(rArmAngle, 0, -0.05, "YXZ");
4408
- ScratchMath.q0.setFromEuler(ScratchMath.e0);
4409
- rArm.targetQuaternion.copy(rArm.restQuaternion).multiply(ScratchMath.q0);
4410
- }
4411
- const elbowFlexBase = this.isRunning ? 0.55 : 0.25;
4412
- if (lForeArm) {
4413
- const flex = elbowFlexBase + Math.max(0, -Math.sin(twoPiPhi)) * 0.35;
4414
- ScratchMath.e0.set(flex * weight, 0, 0, "YXZ");
4415
- ScratchMath.q0.setFromEuler(ScratchMath.e0);
4416
- lForeArm.targetQuaternion.copy(lForeArm.restQuaternion).multiply(ScratchMath.q0);
4417
- }
4418
- if (rForeArm) {
4419
- const flex = elbowFlexBase + Math.max(0, Math.sin(twoPiPhi)) * 0.35;
4420
- ScratchMath.e0.set(flex * weight, 0, 0, "YXZ");
4421
- ScratchMath.q0.setFromEuler(ScratchMath.e0);
4422
- rForeArm.targetQuaternion.copy(rForeArm.restQuaternion).multiply(ScratchMath.q0);
4722
+ if (hips) {
4723
+ hips.bone.position.copy(hips.targetPosition);
4724
+ hips.bone.quaternion.copy(hips.targetQuaternion);
4725
+ hips.bone.updateMatrixWorld(true);
4726
+ }
4727
+ this.ik.solveLeftLeg(this.leftFootTarget, activeAmplitude);
4728
+ this.ik.solveRightLeg(this.rightFootTarget, activeAmplitude);
4729
+ }
4730
+ const targetStandingArmAngle = 0.25;
4731
+ const armSwingAngle = (this.isRunning ? 0.72 : 0.42) * Math.min(speedRatio, 1.5);
4732
+ const lArmForward = -Math.sin(twoPiPhi) * armSwingAngle;
4733
+ const rArmForward = Math.sin(twoPiPhi) * armSwingAngle;
4734
+ if (this.skeleton.hasJoint("leftUpperArm")) {
4735
+ const relaxAngle = Math.max(0, this.skeleton.leftArmRestAngleFromDown - targetStandingArmAngle);
4736
+ ScratchMath.q1.setFromAxisAngle(ScratchMath.FORWARD, this.skeleton.leftArmLowerSign * relaxAngle);
4737
+ ScratchMath.q2.setFromAxisAngle(ScratchMath.RIGHT, -lArmForward);
4738
+ ScratchMath.q0.copy(ScratchMath.q2).multiply(ScratchMath.q1);
4739
+ this.skeleton.applyModelDeltaRotation("leftUpperArm", ScratchMath.q0, activeAmplitude);
4740
+ }
4741
+ if (this.skeleton.hasJoint("rightUpperArm")) {
4742
+ const relaxAngle = Math.max(0, this.skeleton.rightArmRestAngleFromDown - targetStandingArmAngle);
4743
+ ScratchMath.q1.setFromAxisAngle(ScratchMath.FORWARD, this.skeleton.rightArmLowerSign * relaxAngle);
4744
+ ScratchMath.q2.setFromAxisAngle(ScratchMath.RIGHT, -rArmForward);
4745
+ ScratchMath.q0.copy(ScratchMath.q2).multiply(ScratchMath.q1);
4746
+ this.skeleton.applyModelDeltaRotation("rightUpperArm", ScratchMath.q0, activeAmplitude);
4747
+ }
4748
+ const elbowFlexBase = this.isRunning ? 0.85 : 0.28;
4749
+ if (this.skeleton.hasJoint("leftLowerArm")) {
4750
+ const flex = elbowFlexBase + Math.max(0, lArmForward) * 0.45;
4751
+ ScratchMath.q0.setFromAxisAngle(ScratchMath.RIGHT, -flex);
4752
+ this.skeleton.applyModelDeltaRotation("leftLowerArm", ScratchMath.q0, activeAmplitude);
4753
+ }
4754
+ if (this.skeleton.hasJoint("rightLowerArm")) {
4755
+ const flex = elbowFlexBase + Math.max(0, rArmForward) * 0.45;
4756
+ ScratchMath.q0.setFromAxisAngle(ScratchMath.RIGHT, -flex);
4757
+ this.skeleton.applyModelDeltaRotation("rightLowerArm", ScratchMath.q0, activeAmplitude);
4423
4758
  }
4424
4759
  }
4425
4760
  getPhase() {
@@ -4432,18 +4767,26 @@ var LocomotionEngine = class {
4432
4767
 
4433
4768
  // src/avatar/procedural/TurnEngine.ts
4434
4769
  var TurnEngine = class {
4435
- constructor(skeleton) {
4770
+ constructor(skeleton, turnTarget) {
4436
4771
  this.skeleton = skeleton;
4437
4772
  this.currentYaw = 0;
4438
- // Current character yaw in radians
4773
+ // Current character yaw offset in radians
4439
4774
  this.targetYaw = 0;
4440
- // Target desired yaw in radians
4775
+ // Target desired yaw offset in radians
4441
4776
  this.isTurning = false;
4442
4777
  this.turnAngularVelocity = 0;
4778
+ this.turnTarget = turnTarget ?? skeleton.modelRoot;
4779
+ this.baseYaw = this.turnTarget.rotation.y;
4443
4780
  }
4444
4781
  setTargetHeading(targetYawRad) {
4445
4782
  this.targetYaw = targetYawRad;
4446
4783
  }
4784
+ turnLeft(stepRad = Math.PI / 4) {
4785
+ this.targetYaw -= Math.abs(stepRad);
4786
+ }
4787
+ turnRight(stepRad = Math.PI / 4) {
4788
+ this.targetYaw += Math.abs(stepRad);
4789
+ }
4447
4790
  setFacingDirection(direction) {
4448
4791
  switch (direction) {
4449
4792
  case "left":
@@ -4454,32 +4797,45 @@ var TurnEngine = class {
4454
4797
  break;
4455
4798
  case "center":
4456
4799
  case "forward":
4800
+ case "front":
4457
4801
  default:
4458
4802
  this.targetYaw = 0;
4459
4803
  break;
4460
4804
  }
4461
4805
  }
4806
+ reset() {
4807
+ this.currentYaw = 0;
4808
+ this.targetYaw = 0;
4809
+ this.isTurning = false;
4810
+ this.turnAngularVelocity = 0;
4811
+ this.turnTarget.rotation.y = this.baseYaw;
4812
+ }
4462
4813
  update(delta, isLocomoting = false) {
4463
4814
  const diff = ((this.targetYaw - this.currentYaw) % (Math.PI * 2) + Math.PI * 3) % (Math.PI * 2) - Math.PI;
4464
4815
  this.isTurning = Math.abs(diff) > 0.02;
4465
- const damping = isLocomoting ? 6.5 : 4;
4816
+ const damping = isLocomoting ? 6.5 : 5;
4466
4817
  const prevYaw = this.currentYaw;
4467
4818
  this.currentYaw = ScratchMath.dampAngle(this.currentYaw, this.targetYaw, damping, delta);
4468
4819
  this.turnAngularVelocity = (this.currentYaw - prevYaw) / Math.max(delta, 1e-3);
4469
- this.skeleton.modelRoot.rotation.y = this.currentYaw;
4470
- if (isLocomoting && Math.abs(this.turnAngularVelocity) > 0.2) {
4471
- const bankRoll = -Math.max(-0.06, Math.min(0.06, this.turnAngularVelocity * 0.04));
4472
- const hips = this.skeleton.getJoint("hips");
4473
- if (hips) {
4820
+ this.turnTarget.rotation.y = this.baseYaw + this.currentYaw;
4821
+ if (Math.abs(this.turnAngularVelocity) > 0.15) {
4822
+ const bankRoll = -Math.max(-0.06, Math.min(0.06, this.turnAngularVelocity * (isLocomoting ? 0.04 : 0.02)));
4823
+ if (this.skeleton.hasJoint("hips")) {
4474
4824
  ScratchMath.e0.set(0, 0, bankRoll, "YXZ");
4475
4825
  ScratchMath.q0.setFromEuler(ScratchMath.e0);
4476
- hips.targetQuaternion.multiply(ScratchMath.q0);
4826
+ this.skeleton.applyModelDeltaRotation("hips", ScratchMath.q0, 1, true);
4477
4827
  }
4478
4828
  }
4479
4829
  }
4480
4830
  getYaw() {
4481
4831
  return this.currentYaw;
4482
4832
  }
4833
+ getTargetYaw() {
4834
+ return this.targetYaw;
4835
+ }
4836
+ getAngularVelocity() {
4837
+ return this.turnAngularVelocity;
4838
+ }
4483
4839
  isTurnActive() {
4484
4840
  return this.isTurning;
4485
4841
  }
@@ -4495,23 +4851,24 @@ var BalanceEngine = class {
4495
4851
  */
4496
4852
  applyCompensation(reachOffset, weight = 1) {
4497
4853
  if (weight <= 1e-3) return;
4854
+ const localScale = this.skeleton.hipsLocalPerMeter;
4498
4855
  this.targetOffset.set(
4499
- -reachOffset.x * 0.22,
4500
- -Math.abs(reachOffset.z) * 0.05,
4501
- -reachOffset.z * 0.25
4856
+ -reachOffset.x * 0.18 * localScale,
4857
+ -Math.abs(reachOffset.z) * 0.03 * localScale,
4858
+ -reachOffset.z * 0.18 * localScale
4502
4859
  );
4503
4860
  this.centerOfMassOffset.lerp(this.targetOffset, Math.min(1, weight));
4504
4861
  const hips = this.skeleton.getJoint("hips");
4505
4862
  if (hips) {
4506
4863
  hips.targetPosition.add(this.centerOfMassOffset);
4507
4864
  }
4508
- const spine = this.skeleton.getJoint("spine");
4509
- if (spine && this.centerOfMassOffset.lengthSq() > 1e-4) {
4510
- const counterPitch = this.centerOfMassOffset.z * 0.35;
4511
- const counterRoll = -this.centerOfMassOffset.x * 0.35;
4865
+ if (this.skeleton.hasJoint("spine") && this.centerOfMassOffset.lengthSq() > 1e-6) {
4866
+ const invScale = localScale > 1e-4 ? 1 / localScale : 1;
4867
+ const counterPitch = Math.max(-0.12, Math.min(0.12, this.centerOfMassOffset.z * invScale * 0.35));
4868
+ const counterRoll = Math.max(-0.12, Math.min(0.12, -this.centerOfMassOffset.x * invScale * 0.35));
4512
4869
  ScratchMath.e0.set(counterPitch, 0, counterRoll, "YXZ");
4513
4870
  ScratchMath.q0.setFromEuler(ScratchMath.e0);
4514
- spine.targetQuaternion.multiply(ScratchMath.q0);
4871
+ this.skeleton.applyModelDeltaRotation("spine", ScratchMath.q0, 1, true);
4515
4872
  }
4516
4873
  }
4517
4874
  reset() {
@@ -4731,177 +5088,146 @@ var GestureEngine = class {
4731
5088
  return "right";
4732
5089
  }
4733
5090
  }
5091
+ /**
5092
+ * Helper: rotates an upper arm from its rest orientation to a target standing-relative Euler pose in model space.
5093
+ */
5094
+ applyStandingArmDelta(side, pitchForwardRad, yawRad, abductOutRad, weight) {
5095
+ const boneName = side === "left" ? "leftUpperArm" : "rightUpperArm";
5096
+ if (!this.skeleton.hasJoint(boneName)) return;
5097
+ const restAngle = side === "left" ? this.skeleton.leftArmRestAngleFromDown : this.skeleton.rightArmRestAngleFromDown;
5098
+ const lowerSign = side === "left" ? this.skeleton.leftArmLowerSign : this.skeleton.rightArmLowerSign;
5099
+ const relaxToDown = Math.max(0, restAngle - abductOutRad);
5100
+ ScratchMath.q1.setFromAxisAngle(ScratchMath.FORWARD, lowerSign * relaxToDown);
5101
+ ScratchMath.e0.set(-pitchForwardRad, yawRad, 0, "YXZ");
5102
+ ScratchMath.q2.setFromEuler(ScratchMath.e0);
5103
+ ScratchMath.q0.copy(ScratchMath.q2).multiply(ScratchMath.q1);
5104
+ this.skeleton.applyModelDeltaRotation(boneName, ScratchMath.q0, weight);
5105
+ }
4734
5106
  // 1. Wave (right hand oscillates back and forth at shoulder height)
4735
5107
  synthesizeWave(progress, weight) {
4736
- const rUpper = this.skeleton.getJoint("rightUpperArm");
4737
- const rLower = this.skeleton.getJoint("rightLowerArm");
4738
- const rHand = this.skeleton.getJoint("rightHand");
4739
- if (rUpper && rLower) {
4740
- ScratchMath.e0.set(-0.6, 0.4, -1.35 * weight, "YXZ");
5108
+ this.applyStandingArmDelta("right", 0.35, -0.25, 1.35, weight);
5109
+ if (this.skeleton.hasJoint("rightLowerArm")) {
5110
+ const waveOsc = Math.sin(progress * Math.PI * 8) * 0.35;
5111
+ ScratchMath.e0.set(-1.35, 0, waveOsc, "YXZ");
4741
5112
  ScratchMath.q0.setFromEuler(ScratchMath.e0);
4742
- rUpper.targetQuaternion.slerp(ScratchMath.q0, weight);
4743
- ScratchMath.e0.set(1.4 * weight, 0, 0, "YXZ");
5113
+ this.skeleton.applyModelDeltaRotation("rightLowerArm", ScratchMath.q0, weight);
5114
+ }
5115
+ if (this.skeleton.hasJoint("rightHand")) {
5116
+ const handWave = Math.sin(progress * Math.PI * 8) * 0.3;
5117
+ ScratchMath.e0.set(0, 0, handWave, "YXZ");
4744
5118
  ScratchMath.q0.setFromEuler(ScratchMath.e0);
4745
- rLower.targetQuaternion.slerp(ScratchMath.q0, weight);
4746
- if (rHand) {
4747
- const waveAngle = Math.sin(progress * Math.PI * 8) * 0.45 * weight;
4748
- ScratchMath.e0.set(0, 0, waveAngle, "YXZ");
4749
- ScratchMath.q0.setFromEuler(ScratchMath.e0);
4750
- rHand.targetQuaternion.slerp(ScratchMath.q0, weight);
4751
- }
5119
+ this.skeleton.applyModelDeltaRotation("rightHand", ScratchMath.q0, weight);
4752
5120
  }
4753
5121
  }
4754
5122
  // 2. Pointing (arm extends toward target using IK or forward angle)
4755
5123
  synthesizePoint(_progress, weight, targetPos) {
4756
5124
  const isLeft = this.activeGesture?.type === "point_left";
4757
- const armName = isLeft ? "leftUpperArm" : "rightUpperArm";
4758
- const foreName = isLeft ? "leftLowerArm" : "rightLowerArm";
5125
+ const side = isLeft ? "left" : "right";
4759
5126
  if (targetPos && this.ik) {
4760
- this.ik.solveArmReach(isLeft ? "left" : "right", targetPos, weight);
5127
+ this.ik.solveArmReach(side, targetPos, weight);
4761
5128
  } else {
4762
- const upper = this.skeleton.getJoint(armName);
4763
- const fore = this.skeleton.getJoint(foreName);
4764
5129
  const sign = isLeft ? 1 : -1;
4765
- if (upper && fore) {
4766
- ScratchMath.e0.set(-1.45 * weight, sign * 0.25 * weight, sign * 0.15 * weight, "YXZ");
4767
- ScratchMath.q0.setFromEuler(ScratchMath.e0);
4768
- upper.targetQuaternion.slerp(ScratchMath.q0, weight);
4769
- ScratchMath.e0.set(0.15 * weight, 0, 0, "YXZ");
4770
- ScratchMath.q0.setFromEuler(ScratchMath.e0);
4771
- fore.targetQuaternion.slerp(ScratchMath.q0, weight);
5130
+ this.applyStandingArmDelta(side, 1.35, sign * 0.2, 0.25, weight);
5131
+ const foreName = isLeft ? "leftLowerArm" : "rightLowerArm";
5132
+ if (this.skeleton.hasJoint(foreName)) {
5133
+ ScratchMath.q0.setFromAxisAngle(ScratchMath.RIGHT, -0.15);
5134
+ this.skeleton.applyModelDeltaRotation(foreName, ScratchMath.q0, weight);
4772
5135
  }
4773
5136
  }
4774
5137
  }
4775
5138
  // 3. Agree (nod head twice)
4776
5139
  synthesizeAgree(progress, weight) {
4777
- const head = this.skeleton.getJoint("head");
4778
- if (head) {
5140
+ if (this.skeleton.hasJoint("head")) {
4779
5141
  const nodPitch = Math.sin(progress * Math.PI * 4) * 0.24 * weight;
4780
5142
  ScratchMath.e0.set(nodPitch, 0, 0, "YXZ");
4781
5143
  ScratchMath.q0.setFromEuler(ScratchMath.e0);
4782
- head.targetQuaternion.copy(head.restQuaternion).multiply(ScratchMath.q0);
5144
+ this.skeleton.applyModelDeltaRotation("head", ScratchMath.q0, weight);
4783
5145
  }
4784
5146
  }
4785
5147
  // 4. Disagree (shake head side to side)
4786
5148
  synthesizeDisagree(progress, weight) {
4787
- const head = this.skeleton.getJoint("head");
4788
- if (head) {
5149
+ if (this.skeleton.hasJoint("head")) {
4789
5150
  const shakeYaw = Math.sin(progress * Math.PI * 4) * 0.32 * weight;
4790
5151
  ScratchMath.e0.set(0, shakeYaw, 0, "YXZ");
4791
5152
  ScratchMath.q0.setFromEuler(ScratchMath.e0);
4792
- head.targetQuaternion.copy(head.restQuaternion).multiply(ScratchMath.q0);
5153
+ this.skeleton.applyModelDeltaRotation("head", ScratchMath.q0, weight);
4793
5154
  }
4794
5155
  }
4795
5156
  // 5. Shrug (raise shoulders, turn palms up, tilt head)
4796
5157
  synthesizeShrug(_progress, weight) {
4797
- const lUpper = this.skeleton.getJoint("leftUpperArm");
4798
- const rUpper = this.skeleton.getJoint("rightUpperArm");
4799
- const lFore = this.skeleton.getJoint("leftLowerArm");
4800
- const rFore = this.skeleton.getJoint("rightLowerArm");
4801
- const head = this.skeleton.getJoint("head");
4802
- const lift = 0.28 * weight;
4803
- if (lUpper && rUpper) {
4804
- ScratchMath.e0.set(-0.25 * weight, 0, lift, "YXZ");
4805
- ScratchMath.q0.setFromEuler(ScratchMath.e0);
4806
- lUpper.targetQuaternion.slerp(ScratchMath.q0, weight);
4807
- ScratchMath.e0.set(-0.25 * weight, 0, -lift, "YXZ");
5158
+ this.applyStandingArmDelta("left", 0.2, 0, 0.48, weight);
5159
+ this.applyStandingArmDelta("right", 0.2, 0, 0.48, weight);
5160
+ if (this.skeleton.hasJoint("leftLowerArm")) {
5161
+ ScratchMath.e0.set(-0.95, 0.35, 0, "YXZ");
4808
5162
  ScratchMath.q0.setFromEuler(ScratchMath.e0);
4809
- rUpper.targetQuaternion.slerp(ScratchMath.q0, weight);
5163
+ this.skeleton.applyModelDeltaRotation("leftLowerArm", ScratchMath.q0, weight);
4810
5164
  }
4811
- if (lFore && rFore) {
4812
- ScratchMath.e0.set(0.85 * weight, 0.4 * weight, 0, "YXZ");
5165
+ if (this.skeleton.hasJoint("rightLowerArm")) {
5166
+ ScratchMath.e0.set(-0.95, -0.35, 0, "YXZ");
4813
5167
  ScratchMath.q0.setFromEuler(ScratchMath.e0);
4814
- lFore.targetQuaternion.slerp(ScratchMath.q0, weight);
4815
- ScratchMath.e0.set(0.85 * weight, -0.4 * weight, 0, "YXZ");
4816
- ScratchMath.q0.setFromEuler(ScratchMath.e0);
4817
- rFore.targetQuaternion.slerp(ScratchMath.q0, weight);
5168
+ this.skeleton.applyModelDeltaRotation("rightLowerArm", ScratchMath.q0, weight);
4818
5169
  }
4819
- if (head) {
5170
+ if (this.skeleton.hasJoint("head")) {
4820
5171
  ScratchMath.e0.set(0.08 * weight, 0, 0.12 * weight, "YXZ");
4821
5172
  ScratchMath.q0.setFromEuler(ScratchMath.e0);
4822
- head.targetQuaternion.copy(head.restQuaternion).multiply(ScratchMath.q0);
5173
+ this.skeleton.applyModelDeltaRotation("head", ScratchMath.q0, weight);
4823
5174
  }
4824
5175
  }
4825
5176
  // 6. Think (hand to chin / temple, look upward slightly)
4826
5177
  synthesizeThink(_progress, weight) {
4827
- const rUpper = this.skeleton.getJoint("rightUpperArm");
4828
- const rFore = this.skeleton.getJoint("rightLowerArm");
4829
- const head = this.skeleton.getJoint("head");
4830
- if (rUpper && rFore) {
4831
- ScratchMath.e0.set(-1.1 * weight, -0.4 * weight, -0.5 * weight, "YXZ");
4832
- ScratchMath.q0.setFromEuler(ScratchMath.e0);
4833
- rUpper.targetQuaternion.slerp(ScratchMath.q0, weight);
4834
- ScratchMath.e0.set(1.6 * weight, 0, 0, "YXZ");
5178
+ this.applyStandingArmDelta("right", 0.95, 0.35, 0.35, weight);
5179
+ if (this.skeleton.hasJoint("rightLowerArm")) {
5180
+ ScratchMath.e0.set(-1.55, 0, 0, "YXZ");
4835
5181
  ScratchMath.q0.setFromEuler(ScratchMath.e0);
4836
- rFore.targetQuaternion.slerp(ScratchMath.q0, weight);
5182
+ this.skeleton.applyModelDeltaRotation("rightLowerArm", ScratchMath.q0, weight);
4837
5183
  }
4838
- if (head) {
5184
+ if (this.skeleton.hasJoint("head")) {
4839
5185
  ScratchMath.e0.set(-0.15 * weight, 0.18 * weight, 0.08 * weight, "YXZ");
4840
5186
  ScratchMath.q0.setFromEuler(ScratchMath.e0);
4841
- head.targetQuaternion.copy(head.restQuaternion).multiply(ScratchMath.q0);
5187
+ this.skeleton.applyModelDeltaRotation("head", ScratchMath.q0, weight);
4842
5188
  }
4843
5189
  }
4844
5190
  // 7. Explain / Conversational gestures (gentle rhythmic hand gesturing)
4845
5191
  synthesizeExplain(progress, weight) {
4846
- const lUpper = this.skeleton.getJoint("leftUpperArm");
4847
- const rUpper = this.skeleton.getJoint("rightUpperArm");
4848
- const lFore = this.skeleton.getJoint("leftLowerArm");
4849
- const rFore = this.skeleton.getJoint("rightLowerArm");
4850
- const osc = Math.sin(progress * Math.PI * 3) * 0.15 * weight;
4851
- if (lUpper && rUpper) {
4852
- ScratchMath.e0.set(-0.35 * weight + osc, 0.25 * weight, 0.2 * weight, "YXZ");
4853
- ScratchMath.q0.setFromEuler(ScratchMath.e0);
4854
- lUpper.targetQuaternion.slerp(ScratchMath.q0, weight);
4855
- ScratchMath.e0.set(-0.35 * weight - osc, -0.25 * weight, -0.2 * weight, "YXZ");
4856
- ScratchMath.q0.setFromEuler(ScratchMath.e0);
4857
- rUpper.targetQuaternion.slerp(ScratchMath.q0, weight);
5192
+ const osc = Math.sin(progress * Math.PI * 3) * 0.18;
5193
+ this.applyStandingArmDelta("left", 0.35 + osc, 0.2, 0.35, weight);
5194
+ this.applyStandingArmDelta("right", 0.35 - osc, -0.2, 0.35, weight);
5195
+ if (this.skeleton.hasJoint("leftLowerArm")) {
5196
+ ScratchMath.q0.setFromAxisAngle(ScratchMath.RIGHT, -0.9);
5197
+ this.skeleton.applyModelDeltaRotation("leftLowerArm", ScratchMath.q0, weight);
4858
5198
  }
4859
- if (lFore && rFore) {
4860
- ScratchMath.e0.set(0.9 * weight, 0, 0, "YXZ");
4861
- ScratchMath.q0.setFromEuler(ScratchMath.e0);
4862
- lFore.targetQuaternion.slerp(ScratchMath.q0, weight);
4863
- ScratchMath.e0.set(0.9 * weight, 0, 0, "YXZ");
4864
- ScratchMath.q0.setFromEuler(ScratchMath.e0);
4865
- rFore.targetQuaternion.slerp(ScratchMath.q0, weight);
5199
+ if (this.skeleton.hasJoint("rightLowerArm")) {
5200
+ ScratchMath.q0.setFromAxisAngle(ScratchMath.RIGHT, -0.9);
5201
+ this.skeleton.applyModelDeltaRotation("rightLowerArm", ScratchMath.q0, weight);
4866
5202
  }
4867
5203
  }
4868
5204
  // 8. Open Hands (Welcome / Present / Invite)
4869
5205
  synthesizeOpenHands(_progress, weight) {
4870
- const lUpper = this.skeleton.getJoint("leftUpperArm");
4871
- const rUpper = this.skeleton.getJoint("rightUpperArm");
4872
- const lFore = this.skeleton.getJoint("leftLowerArm");
4873
- const rFore = this.skeleton.getJoint("rightLowerArm");
4874
- if (lUpper && rUpper) {
4875
- ScratchMath.e0.set(-0.45 * weight, 0.35 * weight, 0.45 * weight, "YXZ");
5206
+ this.applyStandingArmDelta("left", 0.45, 0.35, 0.55, weight);
5207
+ this.applyStandingArmDelta("right", 0.45, -0.35, 0.55, weight);
5208
+ if (this.skeleton.hasJoint("leftLowerArm")) {
5209
+ ScratchMath.e0.set(-0.75, 0.35, 0, "YXZ");
4876
5210
  ScratchMath.q0.setFromEuler(ScratchMath.e0);
4877
- lUpper.targetQuaternion.slerp(ScratchMath.q0, weight);
4878
- ScratchMath.e0.set(-0.45 * weight, -0.35 * weight, -0.45 * weight, "YXZ");
4879
- ScratchMath.q0.setFromEuler(ScratchMath.e0);
4880
- rUpper.targetQuaternion.slerp(ScratchMath.q0, weight);
5211
+ this.skeleton.applyModelDeltaRotation("leftLowerArm", ScratchMath.q0, weight);
4881
5212
  }
4882
- if (lFore && rFore) {
4883
- ScratchMath.e0.set(0.75 * weight, 0.45 * weight, 0, "YXZ");
4884
- ScratchMath.q0.setFromEuler(ScratchMath.e0);
4885
- lFore.targetQuaternion.slerp(ScratchMath.q0, weight);
4886
- ScratchMath.e0.set(0.75 * weight, -0.45 * weight, 0, "YXZ");
5213
+ if (this.skeleton.hasJoint("rightLowerArm")) {
5214
+ ScratchMath.e0.set(-0.75, -0.35, 0, "YXZ");
4887
5215
  ScratchMath.q0.setFromEuler(ScratchMath.e0);
4888
- rFore.targetQuaternion.slerp(ScratchMath.q0, weight);
5216
+ this.skeleton.applyModelDeltaRotation("rightLowerArm", ScratchMath.q0, weight);
4889
5217
  }
4890
5218
  }
4891
5219
  // 9. Acknowledge (slight bow of head / chest)
4892
5220
  synthesizeAcknowledge(progress, weight) {
4893
- const spine = this.skeleton.getJoint("spine");
4894
- const head = this.skeleton.getJoint("head");
4895
5221
  const bow = Math.sin(progress * Math.PI) * 0.16 * weight;
4896
- if (spine) {
5222
+ if (this.skeleton.hasJoint("spine")) {
4897
5223
  ScratchMath.e0.set(bow, 0, 0, "YXZ");
4898
5224
  ScratchMath.q0.setFromEuler(ScratchMath.e0);
4899
- spine.targetQuaternion.copy(spine.restQuaternion).multiply(ScratchMath.q0);
5225
+ this.skeleton.applyModelDeltaRotation("spine", ScratchMath.q0, weight);
4900
5226
  }
4901
- if (head) {
5227
+ if (this.skeleton.hasJoint("head")) {
4902
5228
  ScratchMath.e0.set(bow * 0.5, 0, 0, "YXZ");
4903
5229
  ScratchMath.q0.setFromEuler(ScratchMath.e0);
4904
- head.targetQuaternion.copy(head.restQuaternion).multiply(ScratchMath.q0);
5230
+ this.skeleton.applyModelDeltaRotation("head", ScratchMath.q0, weight);
4905
5231
  }
4906
5232
  }
4907
5233
  // 10. Reach
@@ -4912,21 +5238,12 @@ var GestureEngine = class {
4912
5238
  }
4913
5239
  // 11. Celebrate (arms raised in victory, chest open)
4914
5240
  synthesizeCelebrate(_progress, weight) {
4915
- const lUpper = this.skeleton.getJoint("leftUpperArm");
4916
- const rUpper = this.skeleton.getJoint("rightUpperArm");
4917
- const spine = this.skeleton.getJoint("spine");
4918
- if (lUpper && rUpper) {
4919
- ScratchMath.e0.set(-1.8 * weight, 0, 1.2 * weight, "YXZ");
4920
- ScratchMath.q0.setFromEuler(ScratchMath.e0);
4921
- lUpper.targetQuaternion.slerp(ScratchMath.q0, weight);
4922
- ScratchMath.e0.set(-1.8 * weight, 0, -1.2 * weight, "YXZ");
4923
- ScratchMath.q0.setFromEuler(ScratchMath.e0);
4924
- rUpper.targetQuaternion.slerp(ScratchMath.q0, weight);
4925
- }
4926
- if (spine) {
5241
+ this.applyStandingArmDelta("left", 0.35, 0, 2.45, weight);
5242
+ this.applyStandingArmDelta("right", 0.35, 0, 2.45, weight);
5243
+ if (this.skeleton.hasJoint("spine")) {
4927
5244
  ScratchMath.e0.set(-0.12 * weight, 0, 0, "YXZ");
4928
5245
  ScratchMath.q0.setFromEuler(ScratchMath.e0);
4929
- spine.targetQuaternion.copy(spine.restQuaternion).multiply(ScratchMath.q0);
5246
+ this.skeleton.applyModelDeltaRotation("spine", ScratchMath.q0, weight);
4930
5247
  }
4931
5248
  }
4932
5249
  };
@@ -5224,13 +5541,17 @@ var MotionCoordinator = _MotionCoordinator;
5224
5541
 
5225
5542
  // src/avatar/procedural/ProceduralMovementEngine.ts
5226
5543
  var ProceduralMovementEngine = class {
5227
- constructor(modelRoot) {
5544
+ constructor(modelRoot, options) {
5228
5545
  this.isEnabled = true;
5229
- this.skeleton = new AUBRSkeleton(modelRoot);
5546
+ this.paused = false;
5547
+ this.locomotionBlendWeight = 0;
5548
+ this.startingTimer = 0;
5549
+ this.balanceReachScratch = new THREE__namespace.Vector3();
5550
+ this.skeleton = new AUBRSkeleton(modelRoot, options);
5230
5551
  this.ik = new IKCoordinator(this.skeleton);
5231
5552
  this.idle = new IdleEngine(this.skeleton);
5232
5553
  this.locomotion = new LocomotionEngine(this.skeleton, this.ik);
5233
- this.turn = new TurnEngine(this.skeleton);
5554
+ this.turn = new TurnEngine(this.skeleton, options?.turnTarget);
5234
5555
  this.balance = new BalanceEngine(this.skeleton);
5235
5556
  this.lookAtEngine = new LookAtEngine(this.skeleton);
5236
5557
  this.gesture = new GestureEngine(this.skeleton, this.ik);
@@ -5241,42 +5562,110 @@ var ProceduralMovementEngine = class {
5241
5562
  getCapabilities() {
5242
5563
  return this.skeleton.capabilities;
5243
5564
  }
5565
+ getState() {
5566
+ return this.stateMachine.getState();
5567
+ }
5244
5568
  setEnabled(enabled) {
5245
5569
  this.isEnabled = enabled;
5246
5570
  }
5571
+ pause() {
5572
+ this.paused = true;
5573
+ }
5574
+ resume() {
5575
+ this.paused = false;
5576
+ }
5577
+ isPaused() {
5578
+ return this.paused;
5579
+ }
5580
+ resetToRestPose() {
5581
+ this.startingTimer = 0;
5582
+ this.locomotionBlendWeight = 0;
5583
+ this.locomotion.cancel();
5584
+ this.gesture.stopGesture();
5585
+ this.idle.reset();
5586
+ this.turn.reset();
5587
+ this.balance.reset();
5588
+ this.lookAtEngine.clearTarget();
5589
+ this.facial.setSpeaking(false);
5590
+ this.facial.setExpression("neutral");
5591
+ this.stateMachine.transitionTo("idle", 0.05);
5592
+ this.coordinator.setWeights({ locomotionWeight: 0, idleWeight: 1, gestureWeight: 0 });
5593
+ this.skeleton.resetToRestPose();
5594
+ this.skeleton.updateWorldMatrices();
5595
+ }
5247
5596
  // --- High-Level Movement API ---
5597
+ startIdle() {
5598
+ const current = this.stateMachine.getState();
5599
+ if (current === "walking" || current === "running" || current === "starting") {
5600
+ this.stopLocomotion();
5601
+ return;
5602
+ }
5603
+ this.startingTimer = 0;
5604
+ this.gesture.stopGesture();
5605
+ this.stateMachine.transitionTo("idle", 0.2);
5606
+ this.coordinator.setWeights({ locomotionWeight: 0, idleWeight: 1, gestureWeight: 0 });
5607
+ }
5608
+ startWalking(speed = 1, screenVelocityPx) {
5609
+ if (!this.skeleton.capabilities.proceduralLocomotion) return;
5610
+ this.startingTimer = 0.18;
5611
+ this.stateMachine.transitionTo("starting", 0.18);
5612
+ this.locomotion.setMotion({ speed, isRunning: false, screenVelocityPx });
5613
+ }
5248
5614
  walk(speed = 1, screenVelocityPx) {
5249
- this.clearStopTimer();
5250
- this.stateMachine.transitionTo("walking");
5615
+ if (!this.skeleton.capabilities.proceduralLocomotion) return;
5616
+ this.startingTimer = 0;
5617
+ this.stateMachine.transitionTo("walking", 0.2);
5251
5618
  this.locomotion.setMotion({ speed, isRunning: false, screenVelocityPx });
5252
- this.coordinator.setWeights({ locomotionWeight: 1, idleWeight: 0 });
5253
5619
  }
5254
5620
  run(speed = 1.8, screenVelocityPx) {
5255
- this.clearStopTimer();
5256
- this.stateMachine.transitionTo("running");
5621
+ if (!this.skeleton.capabilities.proceduralRunning) return;
5622
+ this.startingTimer = 0;
5623
+ this.stateMachine.transitionTo("running", 0.2);
5257
5624
  this.locomotion.setMotion({ speed, isRunning: true, screenVelocityPx });
5258
- this.coordinator.setWeights({ locomotionWeight: 1, idleWeight: 0 });
5625
+ }
5626
+ stop() {
5627
+ this.stopLocomotion();
5259
5628
  }
5260
5629
  stopLocomotion() {
5261
- this.clearStopTimer();
5630
+ this.startingTimer = 0;
5262
5631
  this.locomotion.stop();
5263
- this.stateMachine.transitionTo("stopping", 0.2);
5264
- this.stopTimer = setTimeout(() => {
5265
- this.stopTimer = void 0;
5266
- if (this.stateMachine.getState() === "stopping") {
5267
- this.stateMachine.transitionTo("idle", 0.25);
5268
- this.coordinator.setWeights({ locomotionWeight: 0, idleWeight: 1 });
5269
- }
5270
- }, 200);
5271
- }
5272
- dispose() {
5273
- this.clearStopTimer();
5274
- this.isEnabled = false;
5275
- }
5276
- clearStopTimer() {
5277
- if (this.stopTimer) {
5278
- clearTimeout(this.stopTimer);
5279
- this.stopTimer = void 0;
5632
+ const state = this.stateMachine.getState();
5633
+ if (state === "walking" || state === "running" || state === "starting") {
5634
+ this.stateMachine.transitionTo("stopping", 0.22);
5635
+ } else if (state !== "stopping") {
5636
+ this.stateMachine.transitionTo("idle", 0.2);
5637
+ }
5638
+ }
5639
+ cancelLocomotion() {
5640
+ this.startingTimer = 0;
5641
+ this.locomotionBlendWeight = 0;
5642
+ this.locomotion.cancel();
5643
+ this.coordinator.setWeights({ locomotionWeight: 0, idleWeight: 1 });
5644
+ this.stateMachine.transitionTo("idle", 0.05);
5645
+ }
5646
+ turnLeft(stepRad = Math.PI / 4) {
5647
+ if (!this.skeleton.capabilities.turning) return;
5648
+ this.turn.turnLeft(stepRad);
5649
+ if (this.stateMachine.getState() === "idle") {
5650
+ this.stateMachine.transitionTo("turning", 0.15);
5651
+ }
5652
+ }
5653
+ turnRight(stepRad = Math.PI / 4) {
5654
+ if (!this.skeleton.capabilities.turning) return;
5655
+ this.turn.turnRight(stepRad);
5656
+ if (this.stateMachine.getState() === "idle") {
5657
+ this.stateMachine.transitionTo("turning", 0.15);
5658
+ }
5659
+ }
5660
+ turnTo(directionOrYaw) {
5661
+ if (!this.skeleton.capabilities.turning) return;
5662
+ if (typeof directionOrYaw === "number") {
5663
+ this.turn.setTargetHeading(directionOrYaw);
5664
+ } else {
5665
+ this.turn.setFacingDirection(directionOrYaw);
5666
+ }
5667
+ if (this.stateMachine.getState() === "idle") {
5668
+ this.stateMachine.transitionTo("turning", 0.15);
5280
5669
  }
5281
5670
  }
5282
5671
  setFacing(direction) {
@@ -5285,10 +5674,26 @@ var ProceduralMovementEngine = class {
5285
5674
  setTargetHeading(yawRad) {
5286
5675
  this.turn.setTargetHeading(yawRad);
5287
5676
  }
5677
+ dispose() {
5678
+ this.isEnabled = false;
5679
+ this.paused = false;
5680
+ this.startingTimer = 0;
5681
+ this.locomotionBlendWeight = 0;
5682
+ this.locomotion.cancel();
5683
+ this.gesture.stopGesture();
5684
+ this.balance.reset();
5685
+ this.lookAtEngine.clearTarget();
5686
+ }
5288
5687
  playGesture(type, options) {
5688
+ if (!this.skeleton.capabilities.upperBodyGestures && !this.skeleton.capabilities.headLookAt) {
5689
+ return false;
5690
+ }
5289
5691
  const success = this.gesture.playGesture(type, options);
5290
5692
  if (success) {
5291
- this.stateMachine.transitionTo("gesturing");
5693
+ const current = this.stateMachine.getState();
5694
+ if (current === "idle" || current === "speaking" || current === "listening" || current === "thinking") {
5695
+ this.stateMachine.transitionTo("gesturing");
5696
+ }
5292
5697
  this.coordinator.setWeights({ gestureWeight: 1 });
5293
5698
  }
5294
5699
  return success;
@@ -5326,41 +5731,83 @@ var ProceduralMovementEngine = class {
5326
5731
  * Restores bones to reference bind pose to prevent cumulative transform drift.
5327
5732
  */
5328
5733
  preMixerUpdate() {
5329
- if (!this.isEnabled) return;
5734
+ if (!this.isEnabled || this.paused) return;
5330
5735
  this.coordinator.preMixerRestore();
5331
5736
  }
5332
5737
  /**
5333
5738
  * Main procedural pipeline tick.
5334
- * Invoked in FbxRenderer.startLoop() after mixer.update(delta) and before renderer.render().
5739
+ * Invoked in FbxRenderer.startLoop() or Avatar Studio after mixer.update(delta) and before renderer.render().
5335
5740
  */
5336
- update(delta, hasActiveMixerClip) {
5337
- if (!this.isEnabled) return;
5741
+ update(delta, hasActiveMixerClip = false) {
5742
+ if (!this.isEnabled || this.paused) return;
5743
+ const dt = Math.min(Math.max(delta, 0), 0.1);
5338
5744
  if (hasActiveMixerClip) {
5339
5745
  this.coordinator.snapshotMixerOutput();
5340
5746
  this.coordinator.setWeights({ clipWeight: 1 });
5341
5747
  } else {
5342
5748
  this.coordinator.setWeights({ clipWeight: 0 });
5343
5749
  }
5344
- const currentState = this.stateMachine.getState();
5750
+ this.stateMachine.update(dt);
5751
+ let currentState = this.stateMachine.getState();
5752
+ if (currentState === "starting") {
5753
+ this.startingTimer -= dt;
5754
+ if (this.startingTimer <= 0) {
5755
+ this.stateMachine.transitionTo("walking", 0.2);
5756
+ currentState = this.stateMachine.getState();
5757
+ }
5758
+ }
5345
5759
  const isLocomoting = currentState === "walking" || currentState === "running" || currentState === "starting";
5346
- this.stateMachine.update(delta);
5347
- this.turn.update(delta, isLocomoting);
5348
- const locoWeight = isLocomoting ? 1 : this.coordinator.getWeights().locomotionWeight;
5349
- if (locoWeight > 0.01) {
5350
- this.locomotion.update(delta, locoWeight);
5760
+ const targetLocoWeight = isLocomoting ? 1 : 0;
5761
+ const blendRate = isLocomoting ? 7.5 : 8.5;
5762
+ this.locomotionBlendWeight = ScratchMath.damp(
5763
+ this.locomotionBlendWeight,
5764
+ targetLocoWeight,
5765
+ blendRate,
5766
+ dt
5767
+ );
5768
+ if (this.locomotionBlendWeight < 5e-3 && !isLocomoting) {
5769
+ this.locomotionBlendWeight = 0;
5770
+ }
5771
+ if (currentState === "stopping" && this.locomotionBlendWeight < 0.02 && this.locomotion.getSpeed() < 0.05) {
5772
+ this.locomotionBlendWeight = 0;
5773
+ this.stateMachine.transitionTo("idle", 0.15);
5774
+ currentState = "idle";
5775
+ }
5776
+ const locoWeight = this.locomotionBlendWeight;
5777
+ const idleWeight = Math.max(0, 1 - locoWeight);
5778
+ this.coordinator.setWeights({
5779
+ locomotionWeight: locoWeight,
5780
+ idleWeight
5781
+ });
5782
+ if (idleWeight > 5e-3 && this.skeleton.capabilities.proceduralIdle) {
5783
+ this.idle.update(dt, idleWeight);
5784
+ }
5785
+ if (locoWeight > 5e-3 && this.skeleton.capabilities.proceduralLocomotion) {
5786
+ this.locomotion.update(dt, locoWeight);
5351
5787
  }
5352
- const idleWeight = !isLocomoting ? 1 : this.coordinator.getWeights().idleWeight;
5353
- if (idleWeight > 0.01) {
5354
- this.idle.update(delta, idleWeight);
5788
+ if (this.skeleton.capabilities.turning) {
5789
+ this.turn.update(dt, isLocomoting);
5790
+ if (currentState === "turning" && !this.turn.isTurnActive()) {
5791
+ this.stateMachine.transitionTo("idle", 0.15);
5792
+ }
5355
5793
  }
5356
5794
  if (this.gesture.isGestureActive()) {
5357
- this.gesture.update(delta);
5795
+ this.gesture.update(dt);
5358
5796
  this.coordinator.setWeights({ gestureWeight: 1 });
5797
+ this.balanceReachScratch.set(0, 0, 0.15);
5798
+ this.balance.applyCompensation(this.balanceReachScratch, Math.min(1, dt * 6));
5359
5799
  } else {
5360
5800
  this.coordinator.setWeights({ gestureWeight: 0 });
5801
+ if (this.stateMachine.getState() === "gesturing") {
5802
+ this.stateMachine.transitionTo("idle", 0.2);
5803
+ }
5804
+ this.balanceReachScratch.set(0, 0, 0);
5805
+ this.balance.applyCompensation(this.balanceReachScratch, Math.min(1, dt * 6));
5806
+ }
5807
+ if (this.skeleton.capabilities.headLookAt) {
5808
+ this.lookAtEngine.update(dt, 1);
5361
5809
  }
5362
- this.lookAtEngine.update(delta, 1);
5363
- this.facial.update(delta);
5810
+ this.facial.update(dt);
5364
5811
  this.coordinator.blendLayers(hasActiveMixerClip);
5365
5812
  this.skeleton.apply();
5366
5813
  this.skeleton.updateWorldMatrices();
@@ -5398,13 +5845,8 @@ var FbxRenderer = class {
5398
5845
  if (!doc) {
5399
5846
  throw new Error("Unable to access document DOM for 3D avatar viewport");
5400
5847
  }
5401
- this.canvas = doc.createElement("canvas");
5402
- this.canvas.className = "asiyst-avatar-canvas";
5403
- this.canvas.setAttribute("role", "presentation");
5404
- this.canvas.style.opacity = "0";
5405
- this.canvas.style.transition = "opacity 0.25s ease";
5406
- this.canvas.style.background = "transparent";
5407
- const glContext = this.canvas.getContext("webgl2") || this.canvas.getContext("webgl");
5848
+ const probeCanvas = doc.createElement("canvas");
5849
+ const glContext = probeCanvas.getContext("webgl2") || probeCanvas.getContext("webgl");
5408
5850
  if (!glContext) {
5409
5851
  throw new AsiystError({
5410
5852
  code: "WEBGL_UNAVAILABLE",
@@ -5412,6 +5854,12 @@ var FbxRenderer = class {
5412
5854
  hint: "Enable hardware acceleration in your browser or use a WebGL-capable device."
5413
5855
  });
5414
5856
  }
5857
+ this.canvas = doc.createElement("canvas");
5858
+ this.canvas.className = "asiyst-avatar-canvas";
5859
+ this.canvas.setAttribute("role", "presentation");
5860
+ this.canvas.style.opacity = "0";
5861
+ this.canvas.style.transition = "opacity 0.25s ease";
5862
+ this.canvas.style.background = "transparent";
5415
5863
  const isMobile = typeof window !== "undefined" && window.innerWidth <= 640;
5416
5864
  const width = options?.width || (isMobile ? 140 : 190);
5417
5865
  const height = options?.height || (isMobile ? 280 : 360);
@@ -5564,7 +6012,10 @@ var FbxRenderer = class {
5564
6012
  });
5565
6013
  this.allAnimations = [...modelAnimations];
5566
6014
  try {
5567
- this.proceduralEngine = new ProceduralMovementEngine(rawObject);
6015
+ this.proceduralEngine = new ProceduralMovementEngine(rawObject, {
6016
+ embeddedAnimationCount: modelAnimations.length,
6017
+ turnTarget: this.modelRoot
6018
+ });
5568
6019
  } catch (procErr) {
5569
6020
  if (typeof console !== "undefined" && typeof console.warn === "function") {
5570
6021
  console.warn("[asiyst] Procedural movement engine initialization notice:", procErr);
@@ -6155,6 +6606,34 @@ var FbxRenderer = class {
6155
6606
  this.mixer = null;
6156
6607
  }
6157
6608
  if (this.scene) {
6609
+ const disposedTextures = /* @__PURE__ */ new Set();
6610
+ const TEXTURE_SLOTS = [
6611
+ "map",
6612
+ "normalMap",
6613
+ "roughnessMap",
6614
+ "metalnessMap",
6615
+ "aoMap",
6616
+ "emissiveMap",
6617
+ "alphaMap",
6618
+ "bumpMap",
6619
+ "displacementMap",
6620
+ "lightMap",
6621
+ "envMap"
6622
+ ];
6623
+ const disposeMat = (m) => {
6624
+ const matRecord = m;
6625
+ for (const slot of TEXTURE_SLOTS) {
6626
+ const texCandidate = matRecord[slot];
6627
+ if (texCandidate && typeof texCandidate === "object" && texCandidate.isTexture) {
6628
+ const tex = texCandidate;
6629
+ if (!disposedTextures.has(tex)) {
6630
+ disposedTextures.add(tex);
6631
+ tex.dispose();
6632
+ }
6633
+ }
6634
+ }
6635
+ m.dispose();
6636
+ };
6158
6637
  this.scene.traverse((obj) => {
6159
6638
  const mesh = obj;
6160
6639
  if (mesh.isMesh) {
@@ -6164,14 +6643,12 @@ var FbxRenderer = class {
6164
6643
  if (mesh.material) {
6165
6644
  if (Array.isArray(mesh.material)) {
6166
6645
  mesh.material.forEach((m) => {
6167
- const mat = m;
6168
- mat.map?.dispose();
6169
- mat.dispose();
6646
+ if (m && typeof m.dispose === "function") {
6647
+ disposeMat(m);
6648
+ }
6170
6649
  });
6171
- } else {
6172
- const mat = mesh.material;
6173
- mat.map?.dispose();
6174
- mat.dispose();
6650
+ } else if (mesh.material && typeof mesh.material.dispose === "function") {
6651
+ disposeMat(mesh.material);
6175
6652
  }
6176
6653
  }
6177
6654
  }
@@ -7487,6 +7964,8 @@ var MovementEngine = class {
7487
7964
  faceDirection(direction) {
7488
7965
  if (direction === "left" || direction === "right") {
7489
7966
  this.renderer.setFacing(direction);
7967
+ } else if (typeof this.renderer.setFacing === "function") {
7968
+ this.renderer.setFacing(direction);
7490
7969
  }
7491
7970
  }
7492
7971
  viewport() {
@@ -10103,12 +10582,20 @@ var ActionScheduler = class {
10103
10582
  ctx.events.emit("action_started", { actionId: action.id, actionType: action.type });
10104
10583
  try {
10105
10584
  const timeoutMs = typeof action.expirationMs === "number" && action.expirationMs > 0 ? action.expirationMs : 15e3;
10106
- const result = await Promise.race([
10107
- handler(ctx),
10108
- new Promise(
10109
- (_, reject) => setTimeout(() => reject(new Error(`Action ${action.type} timed out after ${timeoutMs}ms`)), timeoutMs)
10110
- )
10111
- ]);
10585
+ let timerId;
10586
+ const timeoutPromise = new Promise((_, reject) => {
10587
+ timerId = setTimeout(() => {
10588
+ reject(new Error(`Action ${action.type} timed out after ${timeoutMs}ms`));
10589
+ }, timeoutMs);
10590
+ });
10591
+ let result;
10592
+ try {
10593
+ result = await Promise.race([handler(ctx), timeoutPromise]);
10594
+ } finally {
10595
+ if (timerId !== void 0) {
10596
+ clearTimeout(timerId);
10597
+ }
10598
+ }
10112
10599
  const durationMs = Date.now() - startTime;
10113
10600
  const ackResult = {
10114
10601
  executionDurationMs: durationMs,
@@ -10756,7 +11243,7 @@ var AsiystRuntime = class {
10756
11243
  apiBaseUrl: options.apiBaseUrl ?? DEFAULT_API_BASE_URL,
10757
11244
  projectId: options.projectId,
10758
11245
  publicKey: options.publicKey,
10759
- installationId: void 0,
11246
+ installationId: this.installationId,
10760
11247
  origin: win.location.origin,
10761
11248
  environment: this.environment
10762
11249
  }) : void 0;
@@ -10838,36 +11325,47 @@ var AsiystRuntime = class {
10838
11325
  (event) => this.cloud.sendRuntimeEvent(event),
10839
11326
  this.events
10840
11327
  );
10841
- this.tourEngine = new TourEngine(
10842
- async (route) => {
11328
+ this.tourEngine = new TourEngine({
11329
+ navigateToRoute: async (route) => {
10843
11330
  const nav = resolveNavigationAdapter(win);
10844
11331
  await nav.navigateToRoute(route);
10845
11332
  },
10846
- async (elId) => {
11333
+ moveToElement: async (elId) => {
10847
11334
  await this.movement.moveTo(elId, "developer");
10848
11335
  },
10849
- async (elId) => {
11336
+ pointAtElement: async (elId) => {
10850
11337
  await this.movement.pointAt(elId, "developer");
10851
11338
  },
10852
- async (elId, style) => {
10853
- const el = this.targetRegistry.get(elId);
10854
- if (el) {
10855
- this.highlights.show(el, style || "spotlight");
11339
+ highlightElement: async (elId, style) => {
11340
+ const resolved = this.resolver.tryResolve(elId, "developer");
11341
+ if (resolved) {
11342
+ this.highlights.show(resolved.element, style || "spotlight");
11343
+ } else {
11344
+ const el = this.targetRegistry.get(elId);
11345
+ if (el) {
11346
+ this.highlights.show(el, style || "spotlight");
11347
+ }
10856
11348
  }
10857
11349
  },
10858
- async (text) => {
11350
+ clearHighlight: () => {
11351
+ this.highlights.hide();
11352
+ },
11353
+ speak: async (text) => {
10859
11354
  await this.speechPlayer.speak(text);
10860
11355
  },
10861
- (event) => {
11356
+ playGesture: (gesture) => {
11357
+ return this.avatar.playGesture(gesture);
11358
+ },
11359
+ onStepChanged: (event) => {
10862
11360
  this.events.emit("asiyst:tour:step_changed", event);
10863
11361
  },
10864
- (tourId) => {
11362
+ onTourCompleted: (tourId) => {
10865
11363
  this.events.emit("asiyst:tour:completed", { tourId });
10866
11364
  },
10867
- (tourId, error) => {
11365
+ onTourError: (tourId, error) => {
10868
11366
  this.events.emit("asiyst:tour:error", { tourId, error });
10869
11367
  }
10870
- );
11368
+ });
10871
11369
  this.actionScheduler = new ActionScheduler(
10872
11370
  (action) => this.createActionContext(action, doc, win)
10873
11371
  );
@@ -11643,8 +12141,8 @@ var AsiystRuntime = class {
11643
12141
  origin
11644
12142
  });
11645
12143
  if (reg?.runtimeSessionId) {
11646
- this.installationId = reg.runtimeSessionId;
11647
- this.runtimeAuth?.setInstallationId(reg.runtimeSessionId);
12144
+ this.visitorSessionId = reg.runtimeSessionId;
12145
+ this.cloud.setSessionId(reg.runtimeSessionId);
11648
12146
  this.registrationSucceeded = true;
11649
12147
  this.connectionStatus = "connected";
11650
12148
  this.verificationStatus = "connected";
@@ -11828,17 +12326,11 @@ var AsiystRuntime = class {
11828
12326
  async avatarSubmitContext() {
11829
12327
  await this.submitWebsiteContext();
11830
12328
  }
11831
- async avatarHighlight(targetId, style) {
11832
- const el = this.targetRegistry.get(targetId);
11833
- if (el) {
11834
- this.highlights.show(el, style ?? "outline");
11835
- }
12329
+ async avatarHighlight(target, style) {
12330
+ await this.avatar.highlight(target, style ?? "outline", "developer");
11836
12331
  }
11837
- async avatarPointAt(targetId) {
11838
- const el = this.targetRegistry.get(targetId);
11839
- if (el) {
11840
- await this.avatar.pointAt(targetId, "developer");
11841
- }
12332
+ async avatarPointAt(target) {
12333
+ await this.avatar.pointAt(target, "developer");
11842
12334
  }
11843
12335
  async avatarNavigateTo(route) {
11844
12336
  const win = this.host?.chrome?.ownerDocument?.defaultView || (typeof window !== "undefined" ? window : void 0);
@@ -11975,15 +12467,10 @@ var AsiystInstanceImpl = class {
11975
12467
  interact: {
11976
12468
  highlight: async (targetId, options) => {
11977
12469
  const style = typeof options === "string" ? options : options?.style;
11978
- await this.requireInstanceRuntime().avatarHighlight(
11979
- typeof targetId === "string" ? targetId : targetId.id || "",
11980
- style
11981
- );
12470
+ await this.requireInstanceRuntime().avatarHighlight(targetId, style);
11982
12471
  },
11983
12472
  pointAt: async (targetId) => {
11984
- await this.requireInstanceRuntime().avatarPointAt(
11985
- typeof targetId === "string" ? targetId : targetId.id || ""
11986
- );
12473
+ await this.requireInstanceRuntime().avatarPointAt(targetId);
11987
12474
  }
11988
12475
  },
11989
12476
  navigation: {
@@ -12289,10 +12776,10 @@ var Asiyst = {
12289
12776
  interact: {
12290
12777
  async highlight(targetId, options) {
12291
12778
  const style = typeof options === "string" ? options : options?.style;
12292
- await requireRuntime().avatarHighlight(typeof targetId === "string" ? targetId : targetId.id || "", style);
12779
+ await requireRuntime().avatarHighlight(targetId, style);
12293
12780
  },
12294
12781
  async pointAt(targetId) {
12295
- await requireRuntime().avatarPointAt(typeof targetId === "string" ? targetId : targetId.id || "");
12782
+ await requireRuntime().avatarPointAt(targetId);
12296
12783
  }
12297
12784
  },
12298
12785
  navigation: {