@npcify/web 0.7.0 → 0.7.1

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.
Files changed (3) hide show
  1. package/README.md +1 -0
  2. package/dist/index.js +1064 -44
  3. package/package.json +3 -3
package/README.md CHANGED
@@ -122,6 +122,7 @@ animate()
122
122
  - `character.animation.activeAction`: Active `THREE.AnimationAction | null`.
123
123
  - `await character.animation.play(animationId, { fadeDuration, loop })`: Automatically fetches lazy animations if needed and cross-fades.
124
124
  - `character.animation.playIdle(fadeDuration)` / `character.animation.stop(fadeDuration)`.
125
+ - Each clip keeps the character's hands out of its own torso, hips, skirt and legs: the upper arm turns at the shoulder by the smallest rotation that is necessary (0.7.1). Clips made on a slimmer body otherwise put the hands inside a fuller character.
125
126
  - **Dialogue & Speech State**:
126
127
  - `character.speaking`, `character.thinking`, `character.currentSpeech`, `character.currentText`, `character.revealedText`, `character.options`, `character.cursor`, `character.speech`.
127
128
  - `await character.createConversation()`, `await character.sendMessage(text)`, `await character.endConversation()`.
package/dist/index.js CHANGED
@@ -5309,7 +5309,7 @@ var disposeSceneContext = function(ctx) {
5309
5309
 
5310
5310
  // ../vrm-renderer/src/preview-controller.ts
5311
5311
  import { VRMUtils as VRMUtils4 } from "@pixiv/three-vrm";
5312
- import { Box3, GridHelper, Vector3 as Vector34 } from "three";
5312
+ import { Box3, GridHelper, Vector3 as Vector38 } from "three";
5313
5313
 
5314
5314
  // ../vrm-renderer/src/loaders.ts
5315
5315
  import { VRMLoaderPlugin as VRMLoaderPlugin2, VRMUtils as VRMUtils2 } from "@pixiv/three-vrm";
@@ -5331,13 +5331,1028 @@ import { AnimationMixer, LoopOnce, LoopRepeat } from "three";
5331
5331
  import {
5332
5332
  createVRMAnimationClip as createPixivVRMAnimationClip
5333
5333
  } from "@pixiv/three-vrm-animation";
5334
+
5335
+ // ../vrm-renderer/src/face-reach.ts
5334
5336
  import {
5335
5337
  AnimationClip,
5336
- Euler,
5337
- Quaternion as Quaternion2,
5338
+ Quaternion as Quaternion4,
5338
5339
  QuaternionKeyframeTrack,
5340
+ Vector3 as Vector35
5341
+ } from "three";
5342
+
5343
+ // ../vrm-renderer/src/hand-body.ts
5344
+ import {
5345
+ LinearInterpolant,
5346
+ Quaternion as Quaternion3,
5339
5347
  QuaternionLinearInterpolant,
5340
- Vector3 as Vector33,
5348
+ SkinnedMesh,
5349
+ Vector3 as Vector34
5350
+ } from "three";
5351
+
5352
+ // ../vrm-renderer/src/hand-clearance-core.ts
5353
+ import { Quaternion as Quaternion2, Vector3 as Vector33 } from "three";
5354
+ var MAP_HEIGHT_STEP = 0.02;
5355
+ var MAP_ANGLE_BINS = 24;
5356
+ var perpendicular = function(axis) {
5357
+ const helper = Math.abs(axis.z) < 0.9 ? new Vector33(0, 0, 1) : new Vector33(1, 0, 0);
5358
+ return helper.sub(axis.clone().multiplyScalar(helper.dot(axis))).normalize();
5359
+ };
5360
+ var angleBin = function(angle) {
5361
+ const turn = angle / (2 * Math.PI);
5362
+ const value = (turn - Math.floor(turn)) * MAP_ANGLE_BINS;
5363
+ return value;
5364
+ };
5365
+ var buildRadialMap = function(bone, origin, axis, points) {
5366
+ if (points.length < 16) {
5367
+ return null;
5368
+ }
5369
+ const unit = axis.clone().normalize();
5370
+ const reference = perpendicular(unit);
5371
+ const binormal = new Vector33().crossVectors(unit, reference);
5372
+ let min = Infinity;
5373
+ let max2 = -Infinity;
5374
+ const offset = new Vector33();
5375
+ for (const point of points) {
5376
+ const height = offset.subVectors(point, origin).dot(unit);
5377
+ min = Math.min(min, height);
5378
+ max2 = Math.max(max2, height);
5379
+ }
5380
+ const start = Math.floor(min / MAP_HEIGHT_STEP) * MAP_HEIGHT_STEP;
5381
+ const heights = Math.max(1, Math.ceil((max2 - start) / MAP_HEIGHT_STEP) + 1);
5382
+ const radius = new Float32Array(heights * MAP_ANGLE_BINS);
5383
+ const top = new Float32Array(MAP_ANGLE_BINS).fill(-Infinity);
5384
+ const bottom = new Float32Array(MAP_ANGLE_BINS).fill(Infinity);
5385
+ for (const point of points) {
5386
+ offset.subVectors(point, origin);
5387
+ const height = offset.dot(unit);
5388
+ const x = offset.dot(reference);
5389
+ const y = offset.dot(binormal);
5390
+ const row = Math.min(
5391
+ heights - 1,
5392
+ Math.max(0, Math.round((height - start) / MAP_HEIGHT_STEP))
5393
+ );
5394
+ const column = Math.floor(angleBin(Math.atan2(y, x))) % MAP_ANGLE_BINS;
5395
+ const index = row * MAP_ANGLE_BINS + column;
5396
+ radius[index] = Math.max(radius[index], Math.hypot(x, y));
5397
+ top[column] = Math.max(top[column], height);
5398
+ bottom[column] = Math.min(bottom[column], height);
5399
+ }
5400
+ for (let column = 0; column < MAP_ANGLE_BINS; column += 1) {
5401
+ if (!Number.isFinite(top[column])) {
5402
+ top[column] = min;
5403
+ bottom[column] = min;
5404
+ }
5405
+ }
5406
+ const filled = radius.slice();
5407
+ for (let row = 0; row < heights; row += 1) {
5408
+ for (let column = 0; column < MAP_ANGLE_BINS; column += 1) {
5409
+ const index = row * MAP_ANGLE_BINS + column;
5410
+ if (radius[index] > 0) {
5411
+ continue;
5412
+ }
5413
+ const left = radius[row * MAP_ANGLE_BINS + (column + MAP_ANGLE_BINS - 1) % MAP_ANGLE_BINS];
5414
+ const right = radius[row * MAP_ANGLE_BINS + (column + 1) % MAP_ANGLE_BINS];
5415
+ filled[index] = Math.max(left, right);
5416
+ }
5417
+ }
5418
+ return {
5419
+ bone,
5420
+ origin: origin.clone(),
5421
+ axis: unit,
5422
+ reference,
5423
+ start,
5424
+ heights,
5425
+ radius: filled,
5426
+ top,
5427
+ bottom,
5428
+ maxRadius: filled.reduce((a, b) => Math.max(a, b), 0)
5429
+ };
5430
+ };
5431
+ var scratchOffset = new Vector33();
5432
+ var NO_DIRECTION = new Vector33();
5433
+ var scratchBinormal = new Vector33();
5434
+ var queryRadialMap = function(map, point) {
5435
+ const offset = scratchOffset.subVectors(point, map.origin);
5436
+ const height = offset.dot(map.axis);
5437
+ const binormal = scratchBinormal.crossVectors(map.axis, map.reference);
5438
+ const x = offset.dot(map.reference);
5439
+ const y = offset.dot(binormal);
5440
+ const distance = Math.hypot(x, y);
5441
+ const row = (height - map.start) / MAP_HEIGHT_STEP;
5442
+ if (row < -0.5 || row > map.heights - 0.5) {
5443
+ return { depth: -Infinity, direction: NO_DIRECTION };
5444
+ }
5445
+ if (distance >= map.maxRadius) {
5446
+ return { depth: map.maxRadius - distance, direction: NO_DIRECTION };
5447
+ }
5448
+ const angle = angleBin(Math.atan2(y, x)) - 0.5;
5449
+ const r0 = Math.max(0, Math.min(map.heights - 1, Math.floor(row)));
5450
+ const r1 = Math.min(map.heights - 1, r0 + 1);
5451
+ const c0 = (Math.floor(angle) % MAP_ANGLE_BINS + MAP_ANGLE_BINS) % MAP_ANGLE_BINS;
5452
+ const c1 = (c0 + 1) % MAP_ANGLE_BINS;
5453
+ const fc = angle - Math.floor(angle);
5454
+ const at = (r, c) => map.radius[r * MAP_ANGLE_BINS + c];
5455
+ const surface = Math.max(at(r0, c0), at(r0, c1), at(r1, c0), at(r1, c1));
5456
+ const radial = surface - distance;
5457
+ if (radial <= 0) {
5458
+ return { depth: radial, direction: NO_DIRECTION };
5459
+ }
5460
+ const up = (1 - fc) * map.top[c0] + fc * map.top[c1] - height;
5461
+ const down = height - ((1 - fc) * map.bottom[c0] + fc * map.bottom[c1]);
5462
+ if (up < radial && up <= down) {
5463
+ return { depth: Math.max(0, up), direction: map.axis.clone() };
5464
+ }
5465
+ if (down < radial) {
5466
+ return { depth: Math.max(0, down), direction: map.axis.clone().negate() };
5467
+ }
5468
+ const direction = distance > 1e-6 ? map.reference.clone().multiplyScalar(x / distance).add(binormal.clone().multiplyScalar(y / distance)) : map.reference.clone();
5469
+ return { depth: radial, direction };
5470
+ };
5471
+ var marchPoint = new Vector33();
5472
+ var escapeAlong = function(map, point, direction, maxDistance = 0.3, step = 5e-3) {
5473
+ for (let distance = step; distance <= maxDistance; distance += step) {
5474
+ marchPoint.copy(point).addScaledVector(direction, distance);
5475
+ if (queryRadialMap(map, marchPoint).depth <= 0) {
5476
+ return distance;
5477
+ }
5478
+ }
5479
+ return Infinity;
5480
+ };
5481
+ var rotationBetween = function(a, b) {
5482
+ return new Quaternion2().setFromUnitVectors(
5483
+ a.clone().normalize(),
5484
+ b.clone().normalize()
5485
+ );
5486
+ };
5487
+ var quaternionToRotationVector = function(q) {
5488
+ const w = q.w < 0 ? -q.w : q.w;
5489
+ const sign = q.w < 0 ? -1 : 1;
5490
+ const sinHalf = Math.hypot(q.x, q.y, q.z);
5491
+ if (sinHalf < 1e-9) {
5492
+ return new Vector33(2 * q.x * sign, 2 * q.y * sign, 2 * q.z * sign);
5493
+ }
5494
+ const angle = 2 * Math.atan2(sinHalf, w);
5495
+ return new Vector33(q.x, q.y, q.z).multiplyScalar(sign * angle / sinHalf);
5496
+ };
5497
+ var rotationVectorToQuaternion = function(v) {
5498
+ const angle = v.length();
5499
+ if (angle < 1e-9) {
5500
+ return new Quaternion2(v.x / 2, v.y / 2, v.z / 2, 1).normalize();
5501
+ }
5502
+ return new Quaternion2().setFromAxisAngle(v.clone().divideScalar(angle), angle);
5503
+ };
5504
+ var widenAndSmooth = function(corrections, hold, sigma) {
5505
+ const count = corrections.length;
5506
+ const held = corrections.map((_, frame) => {
5507
+ let best = corrections[frame];
5508
+ for (let k = Math.max(0, frame - hold); k <= Math.min(count - 1, frame + hold); k += 1) {
5509
+ if (corrections[k].lengthSq() > best.lengthSq()) {
5510
+ best = corrections[k];
5511
+ }
5512
+ }
5513
+ return best;
5514
+ });
5515
+ const reach = Math.ceil(3 * sigma);
5516
+ return held.map((_, frame) => {
5517
+ const sum = new Vector33();
5518
+ let weight = 0;
5519
+ for (let k = -reach; k <= reach; k += 1) {
5520
+ const index = frame + k;
5521
+ if (index < 0 || index >= count) {
5522
+ continue;
5523
+ }
5524
+ const w = Math.exp(-0.5 * (k / sigma) ** 2);
5525
+ sum.addScaledVector(held[index], w);
5526
+ weight += w;
5527
+ }
5528
+ return sum.divideScalar(weight);
5529
+ });
5530
+ };
5531
+ var distanceToSegment = function(point, a, b) {
5532
+ const ab = b.clone().sub(a);
5533
+ const t = Math.max(
5534
+ 0,
5535
+ Math.min(1, point.clone().sub(a).dot(ab) / Math.max(ab.lengthSq(), 1e-12))
5536
+ );
5537
+ return point.distanceTo(a.clone().addScaledVector(ab, t));
5538
+ };
5539
+ var HAND_SKIN_DISTANCE = 0.035;
5540
+ var handSkeletonSegments = function(wrist, elbow, fingers) {
5541
+ const segments = [];
5542
+ for (const chain of fingers) {
5543
+ let previous = wrist;
5544
+ for (const joint of chain) {
5545
+ segments.push([previous, joint]);
5546
+ previous = joint;
5547
+ }
5548
+ if (chain.length >= 2) {
5549
+ const last = chain[chain.length - 1];
5550
+ const before = chain[chain.length - 2];
5551
+ segments.push([last, last.clone().add(last.clone().sub(before))]);
5552
+ }
5553
+ }
5554
+ if (!segments.length) {
5555
+ const direction = wrist.clone().sub(elbow).normalize();
5556
+ segments.push([wrist, wrist.clone().addScaledVector(direction, 0.15)]);
5557
+ }
5558
+ return segments;
5559
+ };
5560
+ var isHandSkin = function(point, segments) {
5561
+ return segments.some(
5562
+ ([a, b]) => distanceToSegment(point, a, b) <= HAND_SKIN_DISTANCE
5563
+ );
5564
+ };
5565
+
5566
+ // ../vrm-renderer/src/hand-body.ts
5567
+ var SIDES = ["left", "right"];
5568
+ var TORSO_PARTS = [
5569
+ "hips",
5570
+ "spine",
5571
+ "chest",
5572
+ "upperChest",
5573
+ "neck"
5574
+ ];
5575
+ var LIMB_PARTS = [
5576
+ ["leftUpperLeg", "leftLowerLeg"],
5577
+ ["rightUpperLeg", "rightLowerLeg"],
5578
+ ["leftLowerLeg", "leftFoot"],
5579
+ ["rightLowerLeg", "rightFoot"],
5580
+ ["leftShoulder", "leftUpperArm"],
5581
+ ["rightShoulder", "rightUpperArm"]
5582
+ ];
5583
+ var FINGERS = ["Thumb", "Index", "Middle", "Ring", "Little"];
5584
+ var isHandBone = function(name, side) {
5585
+ if (!name.startsWith(side)) {
5586
+ return false;
5587
+ }
5588
+ const rest = name.slice(side.length);
5589
+ return rest === "Hand" || FINGERS.some((finger) => rest.startsWith(finger));
5590
+ };
5591
+ var MAX_HAND_POINTS = 48;
5592
+ var MAX_PART_POINTS = 6e3;
5593
+ var UP = new Vector34(0, 1, 0);
5594
+ var bodyModels = /* @__PURE__ */ new WeakMap();
5595
+ var humanBoneNames = function(vrm) {
5596
+ return Object.keys(vrm.humanoid.humanBones).filter(
5597
+ (name) => Boolean(vrm.humanoid.getNormalizedBoneNode(name))
5598
+ );
5599
+ };
5600
+ var withRestPose = function(vrm, run) {
5601
+ const names = humanBoneNames(vrm);
5602
+ const saved = names.map((name) => {
5603
+ const node = vrm.humanoid.getNormalizedBoneNode(name);
5604
+ return [node, node.position.clone(), node.quaternion.clone()];
5605
+ });
5606
+ vrm.humanoid.resetNormalizedPose();
5607
+ vrm.humanoid.update();
5608
+ vrm.scene.updateMatrixWorld(true);
5609
+ try {
5610
+ return run();
5611
+ } finally {
5612
+ for (const [node, position, quaternion] of saved) {
5613
+ node.position.copy(position);
5614
+ node.quaternion.copy(quaternion);
5615
+ }
5616
+ vrm.humanoid.update();
5617
+ vrm.scene.updateMatrixWorld(true);
5618
+ }
5619
+ };
5620
+ var restMouth = function(vrm, toRig) {
5621
+ const expression = vrm.expressionManager?.getExpression("aa");
5622
+ const sum = new Vector34();
5623
+ const vertex = new Vector34();
5624
+ let count = 0;
5625
+ for (const bind of expression?._binds ?? []) {
5626
+ if (!bind.primitives || bind.index === void 0) {
5627
+ continue;
5628
+ }
5629
+ for (const mesh of bind.primitives) {
5630
+ const morph = mesh.geometry.morphAttributes.position?.[bind.index];
5631
+ if (!morph || !mesh.isSkinnedMesh) {
5632
+ continue;
5633
+ }
5634
+ for (let i = 0; i < morph.count; i += 1) {
5635
+ if (Math.hypot(morph.getX(i), morph.getY(i), morph.getZ(i)) > 2e-3) {
5636
+ mesh.getVertexPosition(i, vertex);
5637
+ sum.add(vertex.applyMatrix4(mesh.matrixWorld).applyMatrix4(toRig));
5638
+ count += 1;
5639
+ }
5640
+ }
5641
+ }
5642
+ }
5643
+ return count ? sum.divideScalar(count) : null;
5644
+ };
5645
+ var buildBodyModel = function(vrm) {
5646
+ const humanoid = vrm.humanoid;
5647
+ const names = humanBoneNames(vrm);
5648
+ for (const required of [
5649
+ "hips",
5650
+ "leftUpperArm",
5651
+ "rightUpperArm",
5652
+ "leftHand",
5653
+ "rightHand"
5654
+ ]) {
5655
+ if (!names.includes(required)) {
5656
+ return null;
5657
+ }
5658
+ }
5659
+ return withRestPose(vrm, () => {
5660
+ const toRig = humanoid.normalizedHumanBonesRoot.matrixWorld.clone().invert();
5661
+ const rest = /* @__PURE__ */ new Map();
5662
+ const nameOfNormalized = /* @__PURE__ */ new Map();
5663
+ const nameOfRaw = /* @__PURE__ */ new Map();
5664
+ for (const name of names) {
5665
+ const node = humanoid.getNormalizedBoneNode(name);
5666
+ nameOfNormalized.set(node, name);
5667
+ rest.set(name, node.getWorldPosition(new Vector34()).applyMatrix4(toRig));
5668
+ const raw = humanoid.getRawBoneNode(name);
5669
+ if (raw) {
5670
+ nameOfRaw.set(raw, name);
5671
+ }
5672
+ }
5673
+ const parent = /* @__PURE__ */ new Map();
5674
+ const depth = /* @__PURE__ */ new Map();
5675
+ for (const name of names) {
5676
+ let node = humanoid.getNormalizedBoneNode(name).parent;
5677
+ while (node && !nameOfNormalized.has(node)) {
5678
+ node = node.parent;
5679
+ }
5680
+ parent.set(name, node ? nameOfNormalized.get(node) : null);
5681
+ }
5682
+ const depthOf = (name) => {
5683
+ const known = depth.get(name);
5684
+ if (known !== void 0) {
5685
+ return known;
5686
+ }
5687
+ const up = parent.get(name);
5688
+ const value = up ? depthOf(up) + 1 : 0;
5689
+ depth.set(name, value);
5690
+ return value;
5691
+ };
5692
+ const order = [...names].sort((a, b) => depthOf(a) - depthOf(b));
5693
+ const attachCache = /* @__PURE__ */ new Map();
5694
+ const attachOf = (bone) => {
5695
+ if (attachCache.has(bone)) {
5696
+ return attachCache.get(bone);
5697
+ }
5698
+ let node = bone;
5699
+ while (node && !nameOfRaw.has(node)) {
5700
+ node = node.parent;
5701
+ }
5702
+ const result = node ? nameOfRaw.get(node) : null;
5703
+ attachCache.set(bone, result);
5704
+ return result;
5705
+ };
5706
+ const fingerChains = (side) => FINGERS.map(
5707
+ (finger) => ["Metacarpal", "Proximal", "Intermediate", "Distal"].map(
5708
+ (segment) => rest.get(`${side}${finger}${segment}`)
5709
+ ).filter((joint) => Boolean(joint))
5710
+ ).filter((chain) => chain.length > 0);
5711
+ const handSegments = {
5712
+ left: handSkeletonSegments(
5713
+ rest.get("leftHand"),
5714
+ rest.get("leftLowerArm") ?? rest.get("leftHand"),
5715
+ fingerChains("left")
5716
+ ),
5717
+ right: handSkeletonSegments(
5718
+ rest.get("rightHand"),
5719
+ rest.get("rightLowerArm") ?? rest.get("rightHand"),
5720
+ fingerChains("right")
5721
+ )
5722
+ };
5723
+ const partPoints = /* @__PURE__ */ new Map();
5724
+ const handPoints = { left: [], right: [] };
5725
+ let total = 0;
5726
+ vrm.scene.traverse((object) => {
5727
+ if (object instanceof SkinnedMesh && object.visible) {
5728
+ total += object.geometry.getAttribute("position")?.count ?? 0;
5729
+ }
5730
+ });
5731
+ const stride = Math.max(1, Math.floor(total / (MAX_PART_POINTS * 8)));
5732
+ const vertex = new Vector34();
5733
+ let counter = 0;
5734
+ vrm.scene.traverse((object) => {
5735
+ if (!(object instanceof SkinnedMesh) || !object.visible) {
5736
+ return;
5737
+ }
5738
+ const geometry = object.geometry;
5739
+ const position = geometry.getAttribute("position");
5740
+ const skinIndex = geometry.getAttribute("skinIndex");
5741
+ const skinWeight = geometry.getAttribute("skinWeight");
5742
+ if (!position || !skinIndex || !skinWeight) {
5743
+ return;
5744
+ }
5745
+ for (let i = 0; i < position.count; i += 1, counter += 1) {
5746
+ if (counter % stride !== 0) {
5747
+ continue;
5748
+ }
5749
+ let best = 0;
5750
+ let bestWeight = -1;
5751
+ for (let k = 0; k < 4; k += 1) {
5752
+ const weight = skinWeight.getComponent(i, k);
5753
+ if (weight > bestWeight) {
5754
+ bestWeight = weight;
5755
+ best = skinIndex.getComponent(i, k);
5756
+ }
5757
+ }
5758
+ const bone = object.skeleton.bones[best];
5759
+ const attach = bone ? attachOf(bone) : null;
5760
+ if (!attach) {
5761
+ continue;
5762
+ }
5763
+ object.getVertexPosition(i, vertex);
5764
+ const point = vertex.applyMatrix4(object.matrixWorld).applyMatrix4(toRig).clone();
5765
+ const side = SIDES.find((s) => isHandBone(attach, s));
5766
+ if (side && (nameOfRaw.get(bone) !== attach || !isHandSkin(point, handSegments[side]))) {
5767
+ continue;
5768
+ }
5769
+ if (side) {
5770
+ handPoints[side].push({ bone: attach, rest: point });
5771
+ continue;
5772
+ }
5773
+ const list = partPoints.get(attach) ?? [];
5774
+ list.push(point);
5775
+ partPoints.set(attach, list);
5776
+ }
5777
+ });
5778
+ const maps = [];
5779
+ for (const bone of TORSO_PARTS) {
5780
+ const origin = rest.get(bone);
5781
+ const points = partPoints.get(bone);
5782
+ const map = origin && points ? buildRadialMap(bone, origin, UP, points) : null;
5783
+ if (map) maps.push(map);
5784
+ }
5785
+ for (const [bone, end] of LIMB_PARTS) {
5786
+ const origin = rest.get(bone);
5787
+ const target = rest.get(end);
5788
+ const points = partPoints.get(bone);
5789
+ const map = origin && target && points ? buildRadialMap(bone, origin, target.clone().sub(origin), points) : null;
5790
+ if (map) maps.push(map);
5791
+ }
5792
+ const hands = { left: [], right: [] };
5793
+ for (const side of SIDES) {
5794
+ const list = handPoints[side];
5795
+ const step = Math.max(1, Math.floor(list.length / MAX_HAND_POINTS));
5796
+ for (let i = 0; i < list.length; i += step) {
5797
+ hands[side].push(list[i]);
5798
+ }
5799
+ const wrist = `${side}Hand`;
5800
+ hands[side].push({ bone: wrist, rest: rest.get(wrist).clone() });
5801
+ }
5802
+ if (!maps.length) {
5803
+ return null;
5804
+ }
5805
+ const forward = new Vector34(0, 0, 1);
5806
+ const foot = rest.get("leftFoot");
5807
+ const toes = rest.get("leftToes");
5808
+ if (foot && toes) {
5809
+ const step = toes.clone().sub(foot).setY(0);
5810
+ if (step.lengthSq() > 1e-6) {
5811
+ forward.copy(step.normalize());
5812
+ }
5813
+ }
5814
+ const handLength = { left: 0, right: 0 };
5815
+ for (const side of SIDES) {
5816
+ const wrist = rest.get(`${side}Hand`);
5817
+ for (const point of hands[side]) {
5818
+ handLength[side] = Math.max(
5819
+ handLength[side],
5820
+ point.rest.distanceTo(wrist)
5821
+ );
5822
+ }
5823
+ }
5824
+ const mouth = restMouth(vrm, toRig);
5825
+ return { rest, parent, order, maps, hands, forward, mouth, handLength };
5826
+ });
5827
+ };
5828
+ var bodyModelOf = function(vrm) {
5829
+ if (!bodyModels.has(vrm)) {
5830
+ const humanoid = vrm.humanoid;
5831
+ let model = null;
5832
+ if (humanoid?.humanBones && typeof humanoid.getRawBoneNode === "function" && typeof humanoid.resetNormalizedPose === "function") {
5833
+ try {
5834
+ model = buildBodyModel(vrm);
5835
+ } catch {
5836
+ model = null;
5837
+ }
5838
+ }
5839
+ bodyModels.set(vrm, model);
5840
+ }
5841
+ return bodyModels.get(vrm) ?? null;
5842
+ };
5843
+ var solvePose = function(model, local, hips, only, into) {
5844
+ const pose = into ?? { rotation: /* @__PURE__ */ new Map(), position: /* @__PURE__ */ new Map() };
5845
+ for (const name of model.order) {
5846
+ if (only && !only.has(name)) {
5847
+ continue;
5848
+ }
5849
+ const up = model.parent.get(name) ?? null;
5850
+ const q = local.get(name);
5851
+ if (!up) {
5852
+ pose.rotation.set(name, (q ?? new Quaternion3()).clone());
5853
+ pose.position.set(name, (hips ?? model.rest.get(name)).clone());
5854
+ continue;
5855
+ }
5856
+ const parentRotation = pose.rotation.get(up);
5857
+ const parentPosition = pose.position.get(up);
5858
+ pose.rotation.set(
5859
+ name,
5860
+ q ? parentRotation.clone().multiply(q) : parentRotation.clone()
5861
+ );
5862
+ pose.position.set(
5863
+ name,
5864
+ model.rest.get(name).clone().sub(model.rest.get(up)).applyQuaternion(parentRotation).add(parentPosition)
5865
+ );
5866
+ }
5867
+ return pose;
5868
+ };
5869
+ var pointInPose = function(model, pose, point) {
5870
+ return point.rest.clone().sub(model.rest.get(point.bone)).applyQuaternion(pose.rotation.get(point.bone)).add(pose.position.get(point.bone));
5871
+ };
5872
+ var trackOf = function(clip, name) {
5873
+ return clip.tracks.find(
5874
+ (track) => track.name === name && track.times.length > 0
5875
+ );
5876
+ };
5877
+ var sampleClipFrames = function(model, vrm, clip, fps) {
5878
+ if (clip.duration <= 0) {
5879
+ return null;
5880
+ }
5881
+ const count = Math.max(2, Math.round(clip.duration * fps) + 1);
5882
+ const times = Array.from(
5883
+ { length: count },
5884
+ (_, i) => Math.min(clip.duration, i / fps)
5885
+ );
5886
+ const nodeName = (name) => vrm.humanoid.getNormalizedBoneNode(name)?.name;
5887
+ const interpolants = /* @__PURE__ */ new Map();
5888
+ for (const name of model.order) {
5889
+ const node = nodeName(name);
5890
+ const track = node ? trackOf(clip, `${node}.quaternion`) : void 0;
5891
+ if (track) {
5892
+ interpolants.set(
5893
+ name,
5894
+ new QuaternionLinearInterpolant(track.times, track.values, 4)
5895
+ );
5896
+ }
5897
+ }
5898
+ if (!interpolants.has("leftUpperArm") && !interpolants.has("rightUpperArm")) {
5899
+ return null;
5900
+ }
5901
+ const hipsNode = nodeName("hips");
5902
+ const hipsTrack = hipsNode ? trackOf(clip, `${hipsNode}.position`) : void 0;
5903
+ const hipsInterpolant = hipsTrack ? new LinearInterpolant(hipsTrack.times, hipsTrack.values, 3) : void 0;
5904
+ const frames = times.map((time) => {
5905
+ const local = /* @__PURE__ */ new Map();
5906
+ for (const [name, interpolant] of interpolants) {
5907
+ const v = interpolant.evaluate(time);
5908
+ local.set(name, new Quaternion3(v[0], v[1], v[2], v[3]).normalize());
5909
+ }
5910
+ const h = hipsInterpolant?.evaluate(time);
5911
+ return { local, hips: h ? new Vector34(h[0], h[1], h[2]) : null };
5912
+ });
5913
+ return { times, frames, nodeName };
5914
+ };
5915
+
5916
+ // ../vrm-renderer/src/face-reach.ts
5917
+ var SOMA_REST = {
5918
+ hips: [0, 1, 0],
5919
+ spine: [-1e-4, 1.05, -5e-4],
5920
+ chest: [-1e-4, 1.1213, -8e-4],
5921
+ upperChest: [-1e-4, 1.1968, -9e-3],
5922
+ neck: [-2e-3, 1.4599, -0.0145],
5923
+ head: [-11e-4, 1.5994, 0.0241],
5924
+ leftShoulder: [0.0161, 1.4292, 0.0421],
5925
+ leftUpperArm: [0.1653, 1.4292, -0.0129],
5926
+ leftLowerArm: [0.4527, 1.4292, -0.0129],
5927
+ leftHand: [0.7236, 1.4292, -0.0129],
5928
+ rightShoulder: [-0.0139, 1.4286, 0.0431],
5929
+ rightUpperArm: [-0.1643, 1.4286, -0.0123],
5930
+ rightLowerArm: [-0.4517, 1.4286, -0.0123],
5931
+ rightHand: [-0.723, 1.4286, -0.0123]
5932
+ };
5933
+ var SOMA_PARENT = {
5934
+ spine: "hips",
5935
+ chest: "spine",
5936
+ upperChest: "chest",
5937
+ neck: "upperChest",
5938
+ head: "neck",
5939
+ leftShoulder: "upperChest",
5940
+ leftUpperArm: "leftShoulder",
5941
+ leftLowerArm: "leftUpperArm",
5942
+ leftHand: "leftLowerArm",
5943
+ rightShoulder: "upperChest",
5944
+ rightUpperArm: "rightShoulder",
5945
+ rightLowerArm: "rightUpperArm",
5946
+ rightHand: "rightLowerArm"
5947
+ };
5948
+ var SOMA_ORDER = Object.keys(SOMA_REST);
5949
+ var SOMA_MOUTH = new Vector35(2e-4, -0.0257, 0.1011);
5950
+ var SOMA_HAND_LENGTH = 0.19;
5951
+ var PALM_SHARE = 0.45;
5952
+ var NEAR = 0.05;
5953
+ var FAR = 0.09;
5954
+ var isKimodoAnimation = function(animation) {
5955
+ const hips = animation.restHipsPosition;
5956
+ return Math.abs(hips.x) < 1e-3 && Math.abs(hips.y - 1) < 1e-3 && Math.abs(hips.z) < 1e-3;
5957
+ };
5958
+ var smoothstep = function(edge0, edge1, x) {
5959
+ const t = Math.max(0, Math.min(1, (x - edge0) / (edge1 - edge0)));
5960
+ return t * t * (3 - 2 * t);
5961
+ };
5962
+ var somaPose = function(frame) {
5963
+ const pose = { rotation: /* @__PURE__ */ new Map(), position: /* @__PURE__ */ new Map() };
5964
+ for (const name of SOMA_ORDER) {
5965
+ const local = frame.local.get(name) ?? new Quaternion4();
5966
+ const up = SOMA_PARENT[name];
5967
+ const rest = new Vector35().fromArray(SOMA_REST[name]);
5968
+ if (!up) {
5969
+ pose.rotation.set(name, local.clone());
5970
+ pose.position.set(name, rest);
5971
+ continue;
5972
+ }
5973
+ const parentRotation = pose.rotation.get(up);
5974
+ pose.rotation.set(name, parentRotation.clone().multiply(local));
5975
+ pose.position.set(
5976
+ name,
5977
+ rest.sub(new Vector35().fromArray(SOMA_REST[up])).applyQuaternion(parentRotation).add(pose.position.get(up))
5978
+ );
5979
+ }
5980
+ return pose;
5981
+ };
5982
+ var palmOf = function(pose, rest, side, handLength) {
5983
+ const hand = `${side}Hand`;
5984
+ const lower = `${side}LowerArm`;
5985
+ const direction = rest(hand).clone().sub(rest(lower)).normalize();
5986
+ return direction.multiplyScalar(PALM_SHARE * handLength).applyQuaternion(pose.rotation.get(hand)).add(pose.position.get(hand));
5987
+ };
5988
+ var reachTarget = function(model, frame, pose, side) {
5989
+ if (!model.mouth) {
5990
+ return null;
5991
+ }
5992
+ const soma = somaPose(frame);
5993
+ const somaRest = (bone) => new Vector35().fromArray(SOMA_REST[bone]);
5994
+ const somaPalm = palmOf(soma, somaRest, side, SOMA_HAND_LENGTH);
5995
+ const somaHead = soma.rotation.get("head");
5996
+ const somaMouth = SOMA_MOUTH.clone().applyQuaternion(somaHead).add(soma.position.get("head"));
5997
+ const distance = somaPalm.distanceTo(somaMouth);
5998
+ const weight = 1 - smoothstep(NEAR, FAR, distance);
5999
+ if (weight <= 0) {
6000
+ return null;
6001
+ }
6002
+ const head = model.rest.get("head");
6003
+ const faceOffset = model.mouth.clone().sub(head);
6004
+ const scale = faceOffset.length() / SOMA_MOUTH.length();
6005
+ const relative = somaPalm.clone().sub(somaMouth).applyQuaternion(somaHead.clone().invert()).multiplyScalar(scale);
6006
+ const headRotation = pose.rotation.get("head");
6007
+ const mouth = faceOffset.applyQuaternion(headRotation).add(pose.position.get("head"));
6008
+ return {
6009
+ weight,
6010
+ target: relative.applyQuaternion(headRotation).add(mouth)
6011
+ };
6012
+ };
6013
+ var solveReach = function(model, pose, side, target) {
6014
+ const upper = `${side}UpperArm`;
6015
+ const lower = `${side}LowerArm`;
6016
+ const s = pose.position.get(upper);
6017
+ const e = pose.position.get(lower);
6018
+ const rest = (bone) => model.rest.get(bone);
6019
+ const palm = palmOf(pose, rest, side, model.handLength[side]);
6020
+ const forearm = palm.clone().sub(e);
6021
+ const a = e.distanceTo(s);
6022
+ const b = forearm.length();
6023
+ const wanted = Math.max(
6024
+ Math.abs(a - b) + 1e-4,
6025
+ Math.min(a + b - 1e-4, target.distanceTo(s))
6026
+ );
6027
+ const current = palm.distanceTo(s);
6028
+ const angleFor = (c) => Math.acos(Math.max(-1, Math.min(1, (a * a + b * b - c * c) / (2 * a * b))));
6029
+ const bend = angleFor(wanted) - angleFor(current);
6030
+ const upperDirection = e.clone().sub(s);
6031
+ let axis = new Vector35().crossVectors(upperDirection, forearm);
6032
+ if (axis.lengthSq() < 1e-10) {
6033
+ axis = new Vector35().crossVectors(upperDirection, model.forward);
6034
+ }
6035
+ axis.normalize();
6036
+ const elbow = new Quaternion4().setFromAxisAngle(axis, -bend);
6037
+ const bentPalm = forearm.clone().applyQuaternion(elbow).add(e);
6038
+ const shoulder = rotationBetween(bentPalm.sub(s), target.clone().sub(s));
6039
+ return {
6040
+ shoulder: quaternionToRotationVector(shoulder),
6041
+ elbow: quaternionToRotationVector(elbow)
6042
+ };
6043
+ };
6044
+ var rewrite = function(model, frames, bone, corrections, before) {
6045
+ return frames.map((source, index) => {
6046
+ const frame = before ? before(source, index) : source;
6047
+ const pose = solvePose(model, frame.local, frame.hips);
6048
+ const parent = model.parent.get(bone);
6049
+ const parentRotation = pose.rotation.get(parent);
6050
+ const world = parentRotation.clone().multiply(frame.local.get(bone) ?? new Quaternion4());
6051
+ return parentRotation.clone().invert().multiply(rotationVectorToQuaternion(corrections[index]).multiply(world)).normalize();
6052
+ });
6053
+ };
6054
+ var toTrack = function(name, times, values) {
6055
+ const flat = [];
6056
+ let previous = null;
6057
+ for (const q of values) {
6058
+ if (previous && previous.dot(q) < 0) {
6059
+ q.set(-q.x, -q.y, -q.z, -q.w);
6060
+ }
6061
+ previous = q;
6062
+ flat.push(q.x, q.y, q.z, q.w);
6063
+ }
6064
+ return new QuaternionKeyframeTrack(name, [...times], flat);
6065
+ };
6066
+ var applyFaceReach = function(clip, vrm, animation, fps = 30) {
6067
+ if (!isKimodoAnimation(animation)) {
6068
+ return clip;
6069
+ }
6070
+ const model = bodyModelOf(vrm);
6071
+ if (!model?.mouth || !model.rest.get("head")) {
6072
+ return clip;
6073
+ }
6074
+ const sampled = sampleClipFrames(model, vrm, clip, fps);
6075
+ if (!sampled) {
6076
+ return clip;
6077
+ }
6078
+ const { times, frames, nodeName } = sampled;
6079
+ const replaced = /* @__PURE__ */ new Map();
6080
+ for (const side of SIDES) {
6081
+ const upper = `${side}UpperArm`;
6082
+ const lower = `${side}LowerArm`;
6083
+ const upperNode = nodeName(upper);
6084
+ const lowerNode = nodeName(lower);
6085
+ if (!upperNode || !lowerNode || !model.parent.get(upper)) {
6086
+ continue;
6087
+ }
6088
+ const elbows = [];
6089
+ const shoulders = [];
6090
+ let any = false;
6091
+ for (const frame of frames) {
6092
+ const pose = solvePose(model, frame.local, frame.hips);
6093
+ const reach = reachTarget(model, frame, pose, side);
6094
+ if (!reach) {
6095
+ elbows.push(new Vector35());
6096
+ shoulders.push(new Vector35());
6097
+ continue;
6098
+ }
6099
+ any = true;
6100
+ const solved = solveReach(model, pose, side, reach.target);
6101
+ elbows.push(solved.elbow.multiplyScalar(reach.weight));
6102
+ shoulders.push(solved.shoulder.multiplyScalar(reach.weight));
6103
+ }
6104
+ if (!any) {
6105
+ continue;
6106
+ }
6107
+ const elbow = widenAndSmooth(elbows, 0, 2);
6108
+ const shoulder = widenAndSmooth(shoulders, 0, 2);
6109
+ const lowerValues = rewrite(model, frames, lower, elbow);
6110
+ const withElbow = (frame, index) => {
6111
+ const local = new Map(frame.local);
6112
+ local.set(lower, lowerValues[index].clone());
6113
+ return { local, hips: frame.hips };
6114
+ };
6115
+ const upperValues = rewrite(model, frames, upper, shoulder, withElbow);
6116
+ replaced.set(
6117
+ `${upperNode}.quaternion`,
6118
+ toTrack(`${upperNode}.quaternion`, times, upperValues)
6119
+ );
6120
+ replaced.set(
6121
+ `${lowerNode}.quaternion`,
6122
+ toTrack(`${lowerNode}.quaternion`, times, lowerValues)
6123
+ );
6124
+ }
6125
+ if (!replaced.size) {
6126
+ return clip;
6127
+ }
6128
+ const tracks = clip.tracks.filter((track) => !replaced.has(track.name));
6129
+ tracks.push(...replaced.values());
6130
+ return new AnimationClip(clip.name, clip.duration, tracks, clip.blendMode);
6131
+ };
6132
+
6133
+ // ../vrm-renderer/src/hand-clearance.ts
6134
+ import {
6135
+ AnimationClip as AnimationClip2,
6136
+ LinearInterpolant as LinearInterpolant2,
6137
+ Quaternion as Quaternion5,
6138
+ QuaternionKeyframeTrack as QuaternionKeyframeTrack2,
6139
+ QuaternionLinearInterpolant as QuaternionLinearInterpolant2,
6140
+ Vector3 as Vector36
6141
+ } from "three";
6142
+ var MAX_CORRECTION = 35 * Math.PI / 180;
6143
+ var MAX_ITERATIONS = 4;
6144
+ var deepestContact = function(model, pose, side) {
6145
+ let best = null;
6146
+ const points = model.hands[side].map(
6147
+ (handPoint) => pointInPose(model, pose, handPoint)
6148
+ );
6149
+ const inverse = new Quaternion5();
6150
+ const local = new Vector36();
6151
+ for (const map of model.maps) {
6152
+ const bone = map.bone;
6153
+ const rotation = pose.rotation.get(bone);
6154
+ const position = pose.position.get(bone);
6155
+ if (!rotation || !position) {
6156
+ continue;
6157
+ }
6158
+ inverse.copy(rotation).invert();
6159
+ const rest = model.rest.get(bone);
6160
+ const torso = TORSO_PARTS.includes(bone);
6161
+ for (const point of points) {
6162
+ local.copy(point).sub(position).applyQuaternion(inverse).add(rest);
6163
+ const hit = queryRadialMap(map, local);
6164
+ if (hit.depth <= 0) {
6165
+ continue;
6166
+ }
6167
+ if (torso) {
6168
+ const front = escapeAlong(map, local, model.forward);
6169
+ if (front <= 1.5 * hit.depth + 0.01) {
6170
+ hit.depth = front;
6171
+ hit.direction = model.forward.clone();
6172
+ }
6173
+ }
6174
+ if (!best || hit.depth > best.depth) {
6175
+ best = {
6176
+ bone: map.bone,
6177
+ point,
6178
+ depth: hit.depth,
6179
+ direction: hit.direction.clone().applyQuaternion(rotation)
6180
+ };
6181
+ }
6182
+ }
6183
+ }
6184
+ return best;
6185
+ };
6186
+ var ARM_CHAIN = function(model, side) {
6187
+ const result = /* @__PURE__ */ new Set();
6188
+ for (const name of model.order) {
6189
+ let node = name;
6190
+ while (node) {
6191
+ if (node === `${side}UpperArm`) {
6192
+ result.add(name);
6193
+ break;
6194
+ }
6195
+ node = model.parent.get(node) ?? null;
6196
+ }
6197
+ }
6198
+ return result;
6199
+ };
6200
+ var frameCorrections = function(model, frames, side, start, margin, debug) {
6201
+ const upper = `${side}UpperArm`;
6202
+ const chain = ARM_CHAIN(model, side);
6203
+ const upperParent = model.parent.get(upper);
6204
+ return frames.map((frame, index) => {
6205
+ const pose = solvePose(model, frame.local, frame.hips);
6206
+ const original = frame.local.get(upper) ?? new Quaternion5();
6207
+ const parentRotation = pose.rotation.get(upperParent);
6208
+ const parentInverse = parentRotation.clone().invert();
6209
+ const correction = rotationVectorToQuaternion(start[index]);
6210
+ const local = new Map(frame.local);
6211
+ const apply = () => {
6212
+ const world = parentRotation.clone().multiply(original);
6213
+ local.set(
6214
+ upper,
6215
+ parentInverse.clone().multiply(correction.clone().multiply(world))
6216
+ );
6217
+ solvePose(model, local, frame.hips, chain, pose);
6218
+ };
6219
+ apply();
6220
+ const added = new Quaternion5();
6221
+ for (let iteration = 0; iteration < MAX_ITERATIONS; iteration += 1) {
6222
+ const contact = deepestContact(model, pose, side);
6223
+ if (!contact) {
6224
+ break;
6225
+ }
6226
+ const shoulder = pose.position.get(upper);
6227
+ const from = contact.point.clone().sub(shoulder);
6228
+ const to = contact.point.clone().addScaledVector(contact.direction, contact.depth + margin).sub(shoulder);
6229
+ const step = rotationBetween(from, to);
6230
+ const next = step.clone().multiply(added);
6231
+ debug?.({
6232
+ side,
6233
+ frame: index,
6234
+ iteration,
6235
+ bone: contact.bone,
6236
+ depth: contact.depth,
6237
+ direction: contact.direction.toArray(),
6238
+ stepDegrees: quaternionToRotationVector(step).length() * 180 / Math.PI,
6239
+ totalDegrees: quaternionToRotationVector(next).length() * 180 / Math.PI
6240
+ });
6241
+ const combined = step.clone().multiply(correction);
6242
+ if (quaternionToRotationVector(combined).length() > MAX_CORRECTION) {
6243
+ break;
6244
+ }
6245
+ correction.premultiply(step);
6246
+ added.copy(next);
6247
+ apply();
6248
+ }
6249
+ return quaternionToRotationVector(added);
6250
+ });
6251
+ };
6252
+ var applyHandClearance = function(clip, vrm, options = {}) {
6253
+ const model = bodyModelOf(vrm);
6254
+ if (!model || clip.duration <= 0) {
6255
+ return clip;
6256
+ }
6257
+ const margin = options.margin ?? 0.015;
6258
+ const fps = options.fps ?? 30;
6259
+ const sampled = sampleClipFrames(model, vrm, clip, fps);
6260
+ if (!sampled) {
6261
+ return clip;
6262
+ }
6263
+ const { times, frames, nodeName } = sampled;
6264
+ const count = times.length;
6265
+ const replaced = /* @__PURE__ */ new Map();
6266
+ for (const side of SIDES) {
6267
+ const upper = `${side}UpperArm`;
6268
+ const upperNode = nodeName(upper);
6269
+ if (!upperNode || !model.parent.get(upper)) {
6270
+ continue;
6271
+ }
6272
+ let total = frames.map(() => new Vector36());
6273
+ let changed = false;
6274
+ for (let pass = 0; pass < 2; pass += 1) {
6275
+ const raw = frameCorrections(
6276
+ model,
6277
+ frames,
6278
+ side,
6279
+ total,
6280
+ margin,
6281
+ options.onDebug
6282
+ );
6283
+ if (raw.every((v) => v.lengthSq() < 1e-10)) {
6284
+ break;
6285
+ }
6286
+ changed = true;
6287
+ const smooth = widenAndSmooth(raw, 4, 3);
6288
+ total = total.map(
6289
+ (t, i) => quaternionToRotationVector(
6290
+ rotationVectorToQuaternion(smooth[i]).multiply(
6291
+ rotationVectorToQuaternion(t)
6292
+ )
6293
+ )
6294
+ );
6295
+ }
6296
+ if (options.onReport) {
6297
+ const left = frameCorrections(model, frames, side, total, margin);
6298
+ const degrees = total.map((v) => v.length() * 180 / Math.PI);
6299
+ options.onReport({
6300
+ side,
6301
+ maps: model.maps.map((m) => `${m.bone}:${m.heights}`),
6302
+ handPoints: model.hands[side].length,
6303
+ maxDegrees: Math.max(0, ...degrees),
6304
+ correctedFrames: degrees.filter((d) => d > 0.5).length,
6305
+ residualDegrees: Math.max(
6306
+ 0,
6307
+ ...left.map((v) => v.length() * 180 / Math.PI)
6308
+ )
6309
+ });
6310
+ }
6311
+ if (!changed) {
6312
+ continue;
6313
+ }
6314
+ const values = new Float32Array(count * 4);
6315
+ let previous = null;
6316
+ frames.forEach((frame, index) => {
6317
+ const pose = solvePose(model, frame.local, frame.hips);
6318
+ const parentRotation = pose.rotation.get(model.parent.get(upper));
6319
+ const original = frame.local.get(upper) ?? new Quaternion5();
6320
+ const world = parentRotation.clone().multiply(original);
6321
+ const corrected = parentRotation.clone().invert().multiply(rotationVectorToQuaternion(total[index]).multiply(world)).normalize();
6322
+ if (previous && previous.dot(corrected) < 0) {
6323
+ corrected.set(-corrected.x, -corrected.y, -corrected.z, -corrected.w);
6324
+ }
6325
+ previous = corrected;
6326
+ values.set(
6327
+ [corrected.x, corrected.y, corrected.z, corrected.w],
6328
+ index * 4
6329
+ );
6330
+ });
6331
+ replaced.set(
6332
+ `${upperNode}.quaternion`,
6333
+ new QuaternionKeyframeTrack2(
6334
+ `${upperNode}.quaternion`,
6335
+ times,
6336
+ Array.from(values)
6337
+ )
6338
+ );
6339
+ }
6340
+ if (!replaced.size) {
6341
+ return clip;
6342
+ }
6343
+ const tracks = clip.tracks.filter((track) => !replaced.has(track.name));
6344
+ tracks.push(...replaced.values());
6345
+ return new AnimationClip2(clip.name, clip.duration, tracks, clip.blendMode);
6346
+ };
6347
+
6348
+ // ../vrm-renderer/src/animation.ts
6349
+ import {
6350
+ AnimationClip as AnimationClip3,
6351
+ Euler,
6352
+ Quaternion as Quaternion6,
6353
+ QuaternionKeyframeTrack as QuaternionKeyframeTrack3,
6354
+ QuaternionLinearInterpolant as QuaternionLinearInterpolant3,
6355
+ Vector3 as Vector37,
5341
6356
  VectorKeyframeTrack
5342
6357
  } from "three";
5343
6358
  var NPCIFY_VRM_NORMALIZATION_ROOT_NAME = "NPCifyNormalizationRoot";
@@ -5353,8 +6368,8 @@ var PARENT_OF_HEAD_BONES = [
5353
6368
  ...TORSO_BONES,
5354
6369
  "neck"
5355
6370
  ];
5356
- var IDENTITY_QUATERNION = new Quaternion2();
5357
- var UP_AXIS = new Vector33(0, 1, 0);
6371
+ var IDENTITY_QUATERNION = new Quaternion6();
6372
+ var UP_AXIS = new Vector37(0, 1, 0);
5358
6373
  var MAX_HEAD_PITCH_CORRECTION_RAD = 35 * Math.PI / 180;
5359
6374
  var NECK_CORRECTION_SHARE = 0.35;
5360
6375
  var EPSILON = 1e-5;
@@ -5424,13 +6439,13 @@ var evaluateChainOrientation = function(bones, interpolants, time, rootPreRotati
5424
6439
  };
5425
6440
  var rotateQuaternionTrackInParentSpace = function(track, parentBones, interpolants, rootPreRotation, worldCorrection) {
5426
6441
  const values = new Float32Array(track.values.length);
5427
- const parentQuat = new Quaternion2();
5428
- const parentInvQuat = new Quaternion2();
5429
- const localCorrection = new Quaternion2();
5430
- const keyframeQuat = new Quaternion2();
5431
- const prevOrigQuat = new Quaternion2();
5432
- const prevNewQuat = new Quaternion2();
5433
- const scratchQuat = new Quaternion2();
6442
+ const parentQuat = new Quaternion6();
6443
+ const parentInvQuat = new Quaternion6();
6444
+ const localCorrection = new Quaternion6();
6445
+ const keyframeQuat = new Quaternion6();
6446
+ const prevOrigQuat = new Quaternion6();
6447
+ const prevNewQuat = new Quaternion6();
6448
+ const scratchQuat = new Quaternion6();
5434
6449
  const count = track.times.length;
5435
6450
  for (let i = 0; i < count; i += 1) {
5436
6451
  const time = track.times[i] ?? 0;
@@ -5472,7 +6487,7 @@ var rotateQuaternionTrackInParentSpace = function(track, parentBones, interpolan
5472
6487
  values[offset + 2] = keyframeQuat.z;
5473
6488
  values[offset + 3] = keyframeQuat.w;
5474
6489
  }
5475
- return new QuaternionKeyframeTrack(
6490
+ return new QuaternionKeyframeTrack3(
5476
6491
  track.name,
5477
6492
  Array.from(track.times),
5478
6493
  Array.from(values)
@@ -5524,7 +6539,7 @@ var normalizeIdleAnimationClip = function(clip, vrm) {
5524
6539
  if (track) {
5525
6540
  interpolants.set(
5526
6541
  bone,
5527
- new QuaternionLinearInterpolant(track.times, track.values, 4)
6542
+ new QuaternionLinearInterpolant3(track.times, track.values, 4)
5528
6543
  );
5529
6544
  }
5530
6545
  }
@@ -5537,11 +6552,11 @@ var normalizeIdleAnimationClip = function(clip, vrm) {
5537
6552
  for (let i = 0; i < sampleCount; i += 1) {
5538
6553
  sampleTimes.push(duration > 0 ? (i + 0.5) / sampleCount * duration : 0);
5539
6554
  }
5540
- const chainQuat = new Quaternion2();
5541
- const scratchQuat = new Quaternion2();
5542
- const forwardVec = new Vector33();
6555
+ const chainQuat = new Quaternion6();
6556
+ const scratchQuat = new Quaternion6();
6557
+ const forwardVec = new Vector37();
5543
6558
  const rootTorsoBone = TORSO_BONES.find((bone) => interpolants.has(bone));
5544
- const rootPreRotation = new Quaternion2();
6559
+ const rootPreRotation = new Quaternion6();
5545
6560
  let torsoYawCorrection = 0;
5546
6561
  if (rootTorsoBone) {
5547
6562
  let sumX = 0;
@@ -5575,7 +6590,7 @@ var normalizeIdleAnimationClip = function(clip, vrm) {
5575
6590
  let neckWorldCorrection = null;
5576
6591
  let headWorldCorrection = null;
5577
6592
  if (hasNeckTrack || hasHeadTrack) {
5578
- const meanHeadForward = new Vector33();
6593
+ const meanHeadForward = new Vector37();
5579
6594
  for (const time of sampleTimes) {
5580
6595
  evaluateChainOrientation(
5581
6596
  AXIAL_BONES,
@@ -5600,11 +6615,11 @@ var normalizeIdleAnimationClip = function(clip, vrm) {
5600
6615
  Math.min(MAX_HEAD_PITCH_CORRECTION_RAD, rawHeadPitch)
5601
6616
  );
5602
6617
  if (Math.abs(meanHeadYaw) > EPSILON || Math.abs(clampedHeadPitch) > EPSILON) {
5603
- const totalWorldCorrection = new Quaternion2().setFromEuler(
6618
+ const totalWorldCorrection = new Quaternion6().setFromEuler(
5604
6619
  new Euler(-clampedHeadPitch, -meanHeadYaw, 0, "XYZ")
5605
6620
  );
5606
6621
  if (hasNeckTrack && hasHeadTrack) {
5607
- neckWorldCorrection = new Quaternion2().setFromEuler(
6622
+ neckWorldCorrection = new Quaternion6().setFromEuler(
5608
6623
  new Euler(
5609
6624
  -clampedHeadPitch * NECK_CORRECTION_SHARE,
5610
6625
  -meanHeadYaw * NECK_CORRECTION_SHARE,
@@ -5666,17 +6681,22 @@ var normalizeIdleAnimationClip = function(clip, vrm) {
5666
6681
  }
5667
6682
  return track;
5668
6683
  });
5669
- return new AnimationClip(clip.name, clip.duration, nextTracks, clip.blendMode);
6684
+ return new AnimationClip3(clip.name, clip.duration, nextTracks, clip.blendMode);
5670
6685
  };
5671
6686
  var createVRMAnimationClip = function(animation, vrm, options) {
5672
6687
  const clip = createPixivVRMAnimationClip(
5673
6688
  animation,
5674
6689
  canonicalAnimationTarget(vrm)
5675
6690
  );
5676
- if (options?.normalizeIdleOrientation) {
5677
- return normalizeIdleAnimationClip(clip, vrm);
5678
- }
5679
- return clip;
6691
+ const oriented = options?.normalizeIdleOrientation ? normalizeIdleAnimationClip(clip, vrm) : clip;
6692
+ if (options?.handClearance === false) {
6693
+ return oriented;
6694
+ }
6695
+ return applyHandClearance(
6696
+ applyFaceReach(oriented, vrm, animation),
6697
+ vrm,
6698
+ options?.handClearanceOptions
6699
+ );
5680
6700
  };
5681
6701
 
5682
6702
  // ../vrm-renderer/src/session.ts
@@ -8420,7 +9440,7 @@ var FacialPerformanceInspector = class {
8420
9440
  };
8421
9441
 
8422
9442
  // ../../clients/web-client/src/character/idle-face.ts
8423
- import { Vector3 as Vector35 } from "three";
9443
+ import { Vector3 as Vector39 } from "three";
8424
9444
  var IdleFace = class {
8425
9445
  constructor(vrm, random) {
8426
9446
  this.vrm = vrm;
@@ -8428,7 +9448,7 @@ var IdleFace = class {
8428
9448
  }
8429
9449
  time = 0;
8430
9450
  timeline;
8431
- targetPosition = new Vector35();
9451
+ targetPosition = new Vector39();
8432
9452
  /** Advances by `delta` seconds, turns the eyes, and returns the blink weight. */
8433
9453
  update(delta, frame) {
8434
9454
  if (Number.isFinite(delta) && delta > 0) {
@@ -9237,17 +10257,17 @@ import {
9237
10257
  Euler as Euler2,
9238
10258
  Mesh,
9239
10259
  Object3D,
9240
- SkinnedMesh,
9241
- Vector3 as Vector36
10260
+ SkinnedMesh as SkinnedMesh2,
10261
+ Vector3 as Vector310
9242
10262
  } from "three";
9243
10263
  var isSkinnedMesh = function(object) {
9244
- return object instanceof SkinnedMesh || object.isSkinnedMesh === true;
10264
+ return object instanceof SkinnedMesh2 || object.isSkinnedMesh === true;
9245
10265
  };
9246
10266
  var isMesh = function(object) {
9247
10267
  return isSkinnedMesh(object) || object instanceof Mesh || object.isMesh === true;
9248
10268
  };
9249
10269
  var applyVector3 = function(target, value) {
9250
- if (value instanceof Vector36) {
10270
+ if (value instanceof Vector310) {
9251
10271
  target.copy(value);
9252
10272
  return;
9253
10273
  }
@@ -9622,10 +10642,10 @@ var CharacterSceneAccess = class {
9622
10642
  };
9623
10643
 
9624
10644
  // ../../clients/web-client/src/character/listening-reflex.ts
9625
- import { Object3D as Object3D2, Vector3 as Vector38 } from "three";
10645
+ import { Object3D as Object3D2, Vector3 as Vector312 } from "three";
9626
10646
 
9627
10647
  // ../../clients/web-client/src/character/head-nod.ts
9628
- import { Quaternion as Quaternion3, Vector3 as Vector37 } from "three";
10648
+ import { Quaternion as Quaternion7, Vector3 as Vector311 } from "three";
9629
10649
  var SHARES = [
9630
10650
  ["neck", 0.4],
9631
10651
  ["head", 0.6]
@@ -9635,7 +10655,7 @@ var FREQUENCY_TIMES_LENGTH = 1.4;
9635
10655
  var PULSE_PART = 0.3;
9636
10656
  var MAX_STEP_SECONDS = 1 / 240;
9637
10657
  var REST_DEGREES = 0.01;
9638
- var X_AXIS = new Vector37(1, 0, 0);
10658
+ var X_AXIS = new Vector311(1, 0, 0);
9639
10659
  var HeadNod = class {
9640
10660
  pitch = 0;
9641
10661
  speed = 0;
@@ -9644,7 +10664,7 @@ var HeadNod = class {
9644
10664
  amplitude = 0;
9645
10665
  omega = 0;
9646
10666
  applied = [];
9647
- scratch = new Quaternion3();
10667
+ scratch = new Quaternion7();
9648
10668
  /** The current pitch in degrees; positive turns the face down. */
9649
10669
  get pitchDegrees() {
9650
10670
  return this.pitch;
@@ -9736,8 +10756,8 @@ var ListeningReflex = class {
9736
10756
  pendingNod = null;
9737
10757
  releaseGaze = null;
9738
10758
  front = new Object3D2();
9739
- origin = new Vector38();
9740
- forward = new Vector38();
10759
+ origin = new Vector312();
10760
+ forward = new Vector312();
9741
10761
  yielded = false;
9742
10762
  /** Call after the animation mixer and live motion, before the facial mixer and `vrm.update()`. */
9743
10763
  update(delta, frame) {
@@ -9812,7 +10832,7 @@ var ListeningReflex = class {
9812
10832
  };
9813
10833
 
9814
10834
  // ../../clients/web-client/src/character/live-motion.ts
9815
- import { Quaternion as Quaternion4, Vector3 as Vector39 } from "three";
10835
+ import { Quaternion as Quaternion8, Vector3 as Vector313 } from "three";
9816
10836
  var PARENTS = {
9817
10837
  spine: "hips",
9818
10838
  chest: "spine",
@@ -9854,10 +10874,10 @@ var LiveMotionPlayer = class {
9854
10874
  /** The last applied pose, to blend out of it after the frames end. */
9855
10875
  lastPose = null;
9856
10876
  scratch = {
9857
- from: new Quaternion4(),
9858
- to: new Quaternion4(),
10877
+ from: new Quaternion8(),
10878
+ to: new Quaternion8(),
9859
10879
  carry: /* @__PURE__ */ new Map(),
9860
- position: new Vector39()
10880
+ position: new Vector313()
9861
10881
  };
9862
10882
  constructor(options = {}) {
9863
10883
  this.rootMode = options.rootMode ?? "anchored";
@@ -10019,7 +11039,7 @@ var LiveMotionPlayer = class {
10019
11039
  carry.clear();
10020
11040
  track.bones.forEach((bone, slot) => {
10021
11041
  const offset = 3 + slot * 4;
10022
- const rotation = new Quaternion4(
11042
+ const rotation = new Quaternion8(
10023
11043
  values[offset] ?? 0,
10024
11044
  values[offset + 1] ?? 0,
10025
11045
  values[offset + 2] ?? 0,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@npcify/web",
3
- "version": "0.7.0",
3
+ "version": "0.7.1",
4
4
  "description": "Framework-independent Npcify characters, live conversations and baked performances for the web",
5
5
  "type": "module",
6
6
  "license": "UNLICENSED",
@@ -35,8 +35,8 @@
35
35
  "tsup": "^8.5.0",
36
36
  "typescript": "^5.9.3",
37
37
  "@npcify/shared": "^0.1.0",
38
- "@npcify/vrm-renderer": "^0.1.0",
39
- "@npcify/web-client": "^0.0.0"
38
+ "@npcify/web-client": "^0.0.0",
39
+ "@npcify/vrm-renderer": "^0.1.0"
40
40
  },
41
41
  "repository": {
42
42
  "type": "git",