@npcify/web 0.7.1 → 0.7.3

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/README.md CHANGED
@@ -122,7 +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
+ - Each clip keeps the character's hands out of its own torso, hips and legs: the upper arm turns at the shoulder by the smallest rotation that is necessary (0.7.1). Loose cloth on spring bones (skirts, coat tails) turns the arm by 10 degrees at most, and a clip without contact stays exactly as authored (0.7.2). The clip changes only within about 0.3 s around a contact of a hand with the body; turn it off with the option `handClearance: false` (0.7.3; for `mountNpcify`, `new Npcify` and `createCharacter`/`loadCharacter`).
126
126
  - **Dialogue & Speech State**:
127
127
  - `character.speaking`, `character.thinking`, `character.currentSpeech`, `character.currentText`, `character.revealedText`, `character.options`, `character.cursor`, `character.speech`.
128
128
  - `await character.createConversation()`, `await character.sendMessage(text)`, `await character.endConversation()`.
package/dist/index.d.ts CHANGED
@@ -803,6 +803,8 @@ interface AnimationControllerOptions {
803
803
  * and neck/head orientation face canonical `+Z`.
804
804
  */
805
805
  normalizeIdleOrientation?: boolean;
806
+ /** Keep the hands out of the character's body (default `true`). */
807
+ handClearance?: boolean;
806
808
  }
807
809
  interface CharacterAnimationPlayOptions {
808
810
  fadeDuration?: number;
@@ -841,6 +843,7 @@ declare class AnimationController implements AnimationControllerLike {
841
843
  private readonly normalizedIdleClips;
842
844
  private readonly configs;
843
845
  private readonly normalizeIdleOrientation;
846
+ private readonly handClearance;
844
847
  private previewMixer;
845
848
  private restorePreview;
846
849
  private activeAction;
@@ -1432,6 +1435,13 @@ interface NpcifyOptions {
1432
1435
  * `Character` (`loadCharacter` / `createCharacter` Three.js game mode).
1433
1436
  */
1434
1437
  normalizeIdleOrientation?: boolean;
1438
+ /**
1439
+ * Keep the character's hands out of its own torso, hips and legs in VRMA
1440
+ * clips (the upper arm turns at the shoulder; loose cloth at most 10
1441
+ * degrees; the clip changes only within about 0.3 s around a contact).
1442
+ * Defaults to `true`.
1443
+ */
1444
+ handClearance?: boolean;
1435
1445
  /**
1436
1446
  * Playback of live motion frames (characters with real-time motion). The
1437
1447
  * default `rootMode: 'anchored'` keeps the character where it stands.
@@ -2096,6 +2106,7 @@ declare class Character extends CharacterRuntime {
2096
2106
  private hostedWebChat;
2097
2107
  private memoryIdentifier;
2098
2108
  protected readonly normalizeIdleOrientation: boolean;
2109
+ protected readonly handClearance: boolean;
2099
2110
  private readonly bakedAsset?;
2100
2111
  private animationRefreshId;
2101
2112
  private readonly voice;
@@ -2295,6 +2306,8 @@ interface MountNpcifyOptions {
2295
2306
  serverRendering?: boolean;
2296
2307
  assetProfile?: 'original' | 'web-optimized-v1';
2297
2308
  normalizeIdleOrientation?: boolean;
2309
+ /** Keep the hands out of the character's body in VRMA clips (default `true`). */
2310
+ handClearance?: boolean;
2298
2311
  /** How the player can reply: `'options'`, `'text'`, `'voice'` (docs/voice-input.md). */
2299
2312
  inputModes?: InputMode[];
2300
2313
  /** Player memory identifier for SDK conversations; empty/null uses the character owner's dashboard memory bucket. */
package/dist/index.js CHANGED
@@ -5363,7 +5363,11 @@ var angleBin = function(angle) {
5363
5363
  return value;
5364
5364
  };
5365
5365
  var buildRadialMap = function(bone, origin, axis, points) {
5366
- if (points.length < 16) {
5366
+ const flat = points.length > 0 && typeof points[0] === "number" ? points : null;
5367
+ const count = flat ? Math.floor(flat.length / 3) : points.length;
5368
+ const pointAt = (i, target) => flat ? target.set(flat[i * 3], flat[i * 3 + 1], flat[i * 3 + 2]) : target.copy(points[i]);
5369
+ const point = new Vector33();
5370
+ if (count < 16) {
5367
5371
  return null;
5368
5372
  }
5369
5373
  const unit = axis.clone().normalize();
@@ -5372,7 +5376,8 @@ var buildRadialMap = function(bone, origin, axis, points) {
5372
5376
  let min = Infinity;
5373
5377
  let max2 = -Infinity;
5374
5378
  const offset = new Vector33();
5375
- for (const point of points) {
5379
+ for (let i = 0; i < count; i += 1) {
5380
+ pointAt(i, point);
5376
5381
  const height = offset.subVectors(point, origin).dot(unit);
5377
5382
  min = Math.min(min, height);
5378
5383
  max2 = Math.max(max2, height);
@@ -5382,7 +5387,8 @@ var buildRadialMap = function(bone, origin, axis, points) {
5382
5387
  const radius = new Float32Array(heights * MAP_ANGLE_BINS);
5383
5388
  const top = new Float32Array(MAP_ANGLE_BINS).fill(-Infinity);
5384
5389
  const bottom = new Float32Array(MAP_ANGLE_BINS).fill(Infinity);
5385
- for (const point of points) {
5390
+ for (let i = 0; i < count; i += 1) {
5391
+ pointAt(i, point);
5386
5392
  offset.subVectors(point, origin);
5387
5393
  const height = offset.dot(unit);
5388
5394
  const x = offset.dot(reference);
@@ -5403,6 +5409,20 @@ var buildRadialMap = function(bone, origin, axis, points) {
5403
5409
  bottom[column] = min;
5404
5410
  }
5405
5411
  }
5412
+ const despiked = radius.slice();
5413
+ const at = (row, column) => radius[row * MAP_ANGLE_BINS + (column + MAP_ANGLE_BINS) % MAP_ANGLE_BINS];
5414
+ for (let row = 0; row < heights; row += 1) {
5415
+ for (let column = 0; column < MAP_ANGLE_BINS; column += 1) {
5416
+ const value = at(row, column);
5417
+ const left = Math.max(at(row, column - 2), at(row, column - 3));
5418
+ const right = Math.max(at(row, column + 2), at(row, column + 3));
5419
+ const support = Math.max(left, right);
5420
+ if (support > 0 && value > support * 1.35 && value - support > 0.05) {
5421
+ despiked[row * MAP_ANGLE_BINS + column] = support;
5422
+ }
5423
+ }
5424
+ }
5425
+ radius.set(despiked);
5406
5426
  const filled = radius.slice();
5407
5427
  for (let row = 0; row < heights; row += 1) {
5408
5428
  for (let column = 0; column < MAP_ANGLE_BINS; column += 1) {
@@ -5452,20 +5472,26 @@ var queryRadialMap = function(map, point) {
5452
5472
  const c1 = (c0 + 1) % MAP_ANGLE_BINS;
5453
5473
  const fc = angle - Math.floor(angle);
5454
5474
  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));
5475
+ const fr = Math.max(0, Math.min(1, row - r0));
5476
+ const surface = (1 - fr) * ((1 - fc) * at(r0, c0) + fc * at(r0, c1)) + fr * ((1 - fc) * at(r1, c0) + fc * at(r1, c1));
5456
5477
  const radial = surface - distance;
5457
5478
  if (radial <= 0) {
5458
5479
  return { depth: radial, direction: NO_DIRECTION };
5459
5480
  }
5460
5481
  const up = (1 - fc) * map.top[c0] + fc * map.top[c1] - height;
5461
5482
  const down = height - ((1 - fc) * map.bottom[c0] + fc * map.bottom[c1]);
5483
+ const direction = distance > 1e-6 ? map.reference.clone().multiplyScalar(x / distance).add(binormal.clone().multiplyScalar(y / distance)) : map.reference.clone();
5484
+ const side = { depth: radial, direction };
5462
5485
  if (up < radial && up <= down) {
5463
- return { depth: Math.max(0, up), direction: map.axis.clone() };
5486
+ return { depth: Math.max(0, up), direction: map.axis.clone(), side };
5464
5487
  }
5465
5488
  if (down < radial) {
5466
- return { depth: Math.max(0, down), direction: map.axis.clone().negate() };
5489
+ return {
5490
+ depth: Math.max(0, down),
5491
+ direction: map.axis.clone().negate(),
5492
+ side
5493
+ };
5467
5494
  }
5468
- const direction = distance > 1e-6 ? map.reference.clone().multiplyScalar(x / distance).add(binormal.clone().multiplyScalar(y / distance)) : map.reference.clone();
5469
5495
  return { depth: radial, direction };
5470
5496
  };
5471
5497
  var marchPoint = new Vector33();
@@ -5564,13 +5590,15 @@ var isHandSkin = function(point, segments) {
5564
5590
  };
5565
5591
 
5566
5592
  // ../vrm-renderer/src/hand-body.ts
5593
+ var BUST = /bust|breast|oppai/i;
5594
+ var ARM_BONE = /^(left|right)(UpperArm|LowerArm|Hand|Thumb|Index|Middle|Ring|Little)/;
5595
+ var ARM_WEIGHT_LIMIT = 0.05;
5567
5596
  var SIDES = ["left", "right"];
5568
5597
  var TORSO_PARTS = [
5569
5598
  "hips",
5570
5599
  "spine",
5571
5600
  "chest",
5572
- "upperChest",
5573
- "neck"
5601
+ "upperChest"
5574
5602
  ];
5575
5603
  var LIMB_PARTS = [
5576
5604
  ["leftUpperLeg", "leftLowerLeg"],
@@ -5589,7 +5617,7 @@ var isHandBone = function(name, side) {
5589
5617
  return rest === "Hand" || FINGERS.some((finger) => rest.startsWith(finger));
5590
5618
  };
5591
5619
  var MAX_HAND_POINTS = 48;
5592
- var MAX_PART_POINTS = 6e3;
5620
+ var TRIANGLE_SAMPLE_EDGE = 0.02;
5593
5621
  var UP = new Vector34(0, 1, 0);
5594
5622
  var bodyModels = /* @__PURE__ */ new WeakMap();
5595
5623
  var humanBoneNames = function(vrm) {
@@ -5665,9 +5693,9 @@ var buildBodyModel = function(vrm) {
5665
5693
  const node = humanoid.getNormalizedBoneNode(name);
5666
5694
  nameOfNormalized.set(node, name);
5667
5695
  rest.set(name, node.getWorldPosition(new Vector34()).applyMatrix4(toRig));
5668
- const raw = humanoid.getRawBoneNode(name);
5669
- if (raw) {
5670
- nameOfRaw.set(raw, name);
5696
+ const raw2 = humanoid.getRawBoneNode(name);
5697
+ if (raw2) {
5698
+ nameOfRaw.set(raw2, name);
5671
5699
  }
5672
5700
  }
5673
5701
  const parent = /* @__PURE__ */ new Map();
@@ -5722,15 +5750,16 @@ var buildBodyModel = function(vrm) {
5722
5750
  };
5723
5751
  const partPoints = /* @__PURE__ */ new Map();
5724
5752
  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
5753
  const vertex = new Vector34();
5733
- let counter = 0;
5754
+ const raw = new Vector34();
5755
+ const pushPoint = (key, x, y, z) => {
5756
+ let list = partPoints.get(key);
5757
+ if (!list) {
5758
+ list = [];
5759
+ partPoints.set(key, list);
5760
+ }
5761
+ list.push(x, y, z);
5762
+ };
5734
5763
  vrm.scene.traverse((object) => {
5735
5764
  if (!(object instanceof SkinnedMesh) || !object.visible) {
5736
5765
  return;
@@ -5742,52 +5771,119 @@ var buildBodyModel = function(vrm) {
5742
5771
  if (!position || !skinIndex || !skinWeight) {
5743
5772
  return;
5744
5773
  }
5745
- for (let i = 0; i < position.count; i += 1, counter += 1) {
5746
- if (counter % stride !== 0) {
5747
- continue;
5774
+ const count = position.count;
5775
+ const toRigMesh = toRig.clone().multiply(object.matrixWorld);
5776
+ let bindIsRest = true;
5777
+ for (let k = 0; k < 24 && bindIsRest; k += 1) {
5778
+ const i = Math.floor(k * (count - 1) / 23);
5779
+ object.getVertexPosition(i, vertex);
5780
+ raw.fromBufferAttribute(position, i);
5781
+ bindIsRest = vertex.distanceTo(raw) < 1e-3;
5782
+ }
5783
+ const restPositions = new Float32Array(count * 3);
5784
+ for (let i = 0; i < count; i += 1) {
5785
+ if (bindIsRest) {
5786
+ vertex.fromBufferAttribute(position, i);
5787
+ } else {
5788
+ object.getVertexPosition(i, vertex);
5748
5789
  }
5790
+ vertex.applyMatrix4(toRigMesh);
5791
+ restPositions[i * 3] = vertex.x;
5792
+ restPositions[i * 3 + 1] = vertex.y;
5793
+ restPositions[i * 3 + 2] = vertex.z;
5794
+ }
5795
+ const keys = new Array(count).fill(null);
5796
+ for (let i = 0; i < count; i += 1) {
5749
5797
  let best = 0;
5750
5798
  let bestWeight = -1;
5799
+ let armWeight = 0;
5751
5800
  for (let k = 0; k < 4; k += 1) {
5752
5801
  const weight = skinWeight.getComponent(i, k);
5753
5802
  if (weight > bestWeight) {
5754
5803
  bestWeight = weight;
5755
5804
  best = skinIndex.getComponent(i, k);
5756
5805
  }
5806
+ const part = object.skeleton.bones[skinIndex.getComponent(i, k)];
5807
+ const partAttach = part && weight > 0 ? attachOf(part) : null;
5808
+ if (partAttach && ARM_BONE.test(partAttach)) {
5809
+ armWeight += weight;
5810
+ }
5757
5811
  }
5758
5812
  const bone = object.skeleton.bones[best];
5759
5813
  const attach = bone ? attachOf(bone) : null;
5760
5814
  if (!attach) {
5761
5815
  continue;
5762
5816
  }
5763
- object.getVertexPosition(i, vertex);
5764
- const point = vertex.applyMatrix4(object.matrixWorld).applyMatrix4(toRig).clone();
5765
5817
  const side = SIDES.find((s) => isHandBone(attach, s));
5766
- if (side && (nameOfRaw.get(bone) !== attach || !isHandSkin(point, handSegments[side]))) {
5818
+ if (side) {
5819
+ const point = new Vector34().fromArray(restPositions, i * 3);
5820
+ if (nameOfRaw.get(bone) === attach && isHandSkin(point, handSegments[side])) {
5821
+ handPoints[side].push({ bone: attach, rest: point });
5822
+ }
5767
5823
  continue;
5768
5824
  }
5769
- if (side) {
5770
- handPoints[side].push({ bone: attach, rest: point });
5825
+ if (armWeight > ARM_WEIGHT_LIMIT) {
5771
5826
  continue;
5772
5827
  }
5773
- const list = partPoints.get(attach) ?? [];
5774
- list.push(point);
5775
- partPoints.set(attach, list);
5828
+ const body = nameOfRaw.get(bone) === attach || BUST.test(bone.name);
5829
+ const key = `${attach}|${body ? "body" : "cloth"}`;
5830
+ keys[i] = key;
5831
+ pushPoint(
5832
+ key,
5833
+ restPositions[i * 3],
5834
+ restPositions[i * 3 + 1],
5835
+ restPositions[i * 3 + 2]
5836
+ );
5837
+ }
5838
+ const index = geometry.index;
5839
+ const triangles = index ? index.count / 3 : count / 3;
5840
+ const at = (t, k) => index ? index.getX(t * 3 + k) : t * 3 + k;
5841
+ for (let t = 0; t < triangles; t += 1) {
5842
+ const a = at(t, 0);
5843
+ const b = at(t, 1);
5844
+ const c = at(t, 2);
5845
+ const key = keys[a];
5846
+ if (!key || keys[b] !== key || keys[c] !== key) {
5847
+ continue;
5848
+ }
5849
+ const p = restPositions;
5850
+ const ax = p[a * 3], ay = p[a * 3 + 1], az = p[a * 3 + 2];
5851
+ const bx = p[b * 3], by = p[b * 3 + 1], bz = p[b * 3 + 2];
5852
+ const cx = p[c * 3], cy = p[c * 3 + 1], cz = p[c * 3 + 2];
5853
+ const longest = Math.max(
5854
+ Math.hypot(ax - bx, ay - by, az - bz),
5855
+ Math.hypot(bx - cx, by - cy, bz - cz),
5856
+ Math.hypot(cx - ax, cy - ay, cz - az)
5857
+ );
5858
+ if (longest < TRIANGLE_SAMPLE_EDGE) {
5859
+ continue;
5860
+ }
5861
+ pushPoint(
5862
+ key,
5863
+ (ax + bx + cx) / 3,
5864
+ (ay + by + cy) / 3,
5865
+ (az + bz + cz) / 3
5866
+ );
5867
+ pushPoint(key, (ax + bx) / 2, (ay + by) / 2, (az + bz) / 2);
5868
+ pushPoint(key, (bx + cx) / 2, (by + cy) / 2, (bz + cz) / 2);
5869
+ pushPoint(key, (cx + ax) / 2, (cy + ay) / 2, (cz + az) / 2);
5776
5870
  }
5777
5871
  });
5778
5872
  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);
5873
+ for (const kind of ["body", "cloth"]) {
5874
+ for (const bone of TORSO_PARTS) {
5875
+ const origin = rest.get(bone);
5876
+ const points = partPoints.get(`${bone}|${kind}`);
5877
+ const map = origin && points ? buildRadialMap(bone, origin, UP, points) : null;
5878
+ if (map) maps.push({ ...map, kind });
5879
+ }
5880
+ for (const [bone, end] of LIMB_PARTS) {
5881
+ const origin = rest.get(bone);
5882
+ const target = rest.get(end);
5883
+ const points = partPoints.get(`${bone}|${kind}`);
5884
+ const map = origin && target && points ? buildRadialMap(bone, origin, target.clone().sub(origin), points) : null;
5885
+ if (map) maps.push({ ...map, kind });
5886
+ }
5791
5887
  }
5792
5888
  const hands = { left: [], right: [] };
5793
5889
  for (const side of SIDES) {
@@ -5878,12 +5974,10 @@ var sampleClipFrames = function(model, vrm, clip, fps) {
5878
5974
  if (clip.duration <= 0) {
5879
5975
  return null;
5880
5976
  }
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
5977
  const nodeName = (name) => vrm.humanoid.getNormalizedBoneNode(name)?.name;
5978
+ const armTrack = ["leftUpperArm", "rightUpperArm"].map((bone) => nodeName(bone)).map((node) => node ? trackOf(clip, `${node}.quaternion`) : void 0).find((track) => track && track.times.length >= 2);
5979
+ const count = Math.max(2, Math.round(clip.duration * fps) + 1);
5980
+ const times = armTrack ? Array.from(armTrack.times) : Array.from({ length: count }, (_, i) => Math.min(clip.duration, i / fps));
5887
5981
  const interpolants = /* @__PURE__ */ new Map();
5888
5982
  for (const name of model.order) {
5889
5983
  const node = nodeName(name);
@@ -6140,15 +6234,28 @@ import {
6140
6234
  Vector3 as Vector36
6141
6235
  } from "three";
6142
6236
  var MAX_CORRECTION = 35 * Math.PI / 180;
6143
- var MAX_ITERATIONS = 4;
6144
- var deepestContact = function(model, pose, side) {
6237
+ var CLOTH_CORRECTION_CAP = 10 * Math.PI / 180;
6238
+ var MAX_ITERATIONS = 6;
6239
+ var HOLD_FRAMES = 2;
6240
+ var SMOOTH_SIGMA = 2;
6241
+ var frontOffset = new Vector36();
6242
+ var inFront = function(map, point, forward) {
6243
+ frontOffset.copy(point).sub(map.origin);
6244
+ frontOffset.addScaledVector(map.axis, -frontOffset.dot(map.axis));
6245
+ return frontOffset.dot(forward) > Math.SQRT1_2 * frontOffset.length();
6246
+ };
6247
+ var deepestContact = function(model, pose, side, kind) {
6145
6248
  let best = null;
6249
+ const perMap = [];
6146
6250
  const points = model.hands[side].map(
6147
6251
  (handPoint) => pointInPose(model, pose, handPoint)
6148
6252
  );
6149
6253
  const inverse = new Quaternion5();
6150
6254
  const local = new Vector36();
6151
6255
  for (const map of model.maps) {
6256
+ if ((map.kind ?? "body") !== kind) {
6257
+ continue;
6258
+ }
6152
6259
  const bone = map.bone;
6153
6260
  const rotation = pose.rotation.get(bone);
6154
6261
  const position = pose.position.get(bone);
@@ -6158,30 +6265,56 @@ var deepestContact = function(model, pose, side) {
6158
6265
  inverse.copy(rotation).invert();
6159
6266
  const rest = model.rest.get(bone);
6160
6267
  const torso = TORSO_PARTS.includes(bone);
6268
+ let mapBest = null;
6161
6269
  for (const point of points) {
6162
6270
  local.copy(point).sub(position).applyQuaternion(inverse).add(rest);
6163
6271
  const hit = queryRadialMap(map, local);
6164
6272
  if (hit.depth <= 0) {
6165
6273
  continue;
6166
6274
  }
6167
- if (torso) {
6275
+ if (torso && inFront(map, local, model.forward)) {
6168
6276
  const front = escapeAlong(map, local, model.forward);
6169
6277
  if (front <= 1.5 * hit.depth + 0.01) {
6170
6278
  hit.depth = front;
6171
6279
  hit.direction = model.forward.clone();
6172
6280
  }
6173
6281
  }
6174
- if (!best || hit.depth > best.depth) {
6175
- best = {
6282
+ if (!mapBest || hit.depth > mapBest.depth) {
6283
+ mapBest = {
6176
6284
  bone: map.bone,
6177
6285
  point,
6178
6286
  depth: hit.depth,
6179
- direction: hit.direction.clone().applyQuaternion(rotation)
6287
+ direction: hit.direction.clone().applyQuaternion(rotation),
6288
+ side: hit.side && {
6289
+ depth: hit.side.depth,
6290
+ direction: hit.side.direction.clone().applyQuaternion(rotation)
6291
+ }
6180
6292
  };
6181
6293
  }
6182
6294
  }
6295
+ if (mapBest) {
6296
+ perMap.push(mapBest);
6297
+ if (!best || mapBest.depth > best.depth) {
6298
+ best = mapBest;
6299
+ }
6300
+ }
6301
+ }
6302
+ if (!best || perMap.length < 2) {
6303
+ return best;
6304
+ }
6305
+ const push = new Vector36();
6306
+ for (const contact of perMap) {
6307
+ push.addScaledVector(contact.direction, contact.depth);
6183
6308
  }
6184
- return best;
6309
+ if (push.length() < best.depth * 0.5) {
6310
+ return best;
6311
+ }
6312
+ return {
6313
+ bone: perMap.map((contact) => contact.bone).join("+"),
6314
+ point: best.point,
6315
+ depth: push.length(),
6316
+ direction: push.normalize()
6317
+ };
6185
6318
  };
6186
6319
  var ARM_CHAIN = function(model, side) {
6187
6320
  const result = /* @__PURE__ */ new Set();
@@ -6197,7 +6330,7 @@ var ARM_CHAIN = function(model, side) {
6197
6330
  }
6198
6331
  return result;
6199
6332
  };
6200
- var frameCorrections = function(model, frames, side, start, margin, debug) {
6333
+ var frameCorrections = function(model, frames, side, start, margin, kind, debug) {
6201
6334
  const upper = `${side}UpperArm`;
6202
6335
  const chain = ARM_CHAIN(model, side);
6203
6336
  const upperParent = model.parent.get(upper);
@@ -6218,18 +6351,39 @@ var frameCorrections = function(model, frames, side, start, margin, debug) {
6218
6351
  };
6219
6352
  apply();
6220
6353
  const added = new Quaternion5();
6354
+ let contact = deepestContact(model, pose, side, kind);
6355
+ const attempt = (step, next) => {
6356
+ const keptCorrection = correction.clone();
6357
+ const keptAdded = added.clone();
6358
+ correction.premultiply(step);
6359
+ added.copy(next);
6360
+ apply();
6361
+ const after = deepestContact(model, pose, side, kind);
6362
+ if (after && contact && after.depth >= contact.depth) {
6363
+ correction.copy(keptCorrection);
6364
+ added.copy(keptAdded);
6365
+ apply();
6366
+ return false;
6367
+ }
6368
+ contact = after;
6369
+ return true;
6370
+ };
6221
6371
  for (let iteration = 0; iteration < MAX_ITERATIONS; iteration += 1) {
6222
- const contact = deepestContact(model, pose, side);
6223
6372
  if (!contact) {
6224
6373
  break;
6225
6374
  }
6226
6375
  const shoulder = pose.position.get(upper);
6227
6376
  const from = contact.point.clone().sub(shoulder);
6228
- const to = contact.point.clone().addScaledVector(contact.direction, contact.depth + margin).sub(shoulder);
6377
+ let way = { depth: contact.depth, direction: contact.direction };
6378
+ if (contact.side && Math.abs(from.clone().normalize().dot(contact.direction)) > 0.7) {
6379
+ way = contact.side;
6380
+ }
6381
+ const to = contact.point.clone().addScaledVector(way.direction, way.depth + margin).sub(shoulder);
6229
6382
  const step = rotationBetween(from, to);
6230
6383
  const next = step.clone().multiply(added);
6231
6384
  debug?.({
6232
6385
  side,
6386
+ kind,
6233
6387
  frame: index,
6234
6388
  iteration,
6235
6389
  bone: contact.bone,
@@ -6238,67 +6392,117 @@ var frameCorrections = function(model, frames, side, start, margin, debug) {
6238
6392
  stepDegrees: quaternionToRotationVector(step).length() * 180 / Math.PI,
6239
6393
  totalDegrees: quaternionToRotationVector(next).length() * 180 / Math.PI
6240
6394
  });
6395
+ const nextAngle = quaternionToRotationVector(next).length();
6396
+ if (kind === "cloth" && nextAngle > CLOTH_CORRECTION_CAP) {
6397
+ const capped = rotationVectorToQuaternion(
6398
+ quaternionToRotationVector(next).multiplyScalar(
6399
+ CLOTH_CORRECTION_CAP / nextAngle
6400
+ )
6401
+ );
6402
+ const cappedStep = capped.clone().multiply(added.clone().invert());
6403
+ const total = cappedStep.clone().multiply(correction);
6404
+ if (quaternionToRotationVector(total).length() <= MAX_CORRECTION) {
6405
+ attempt(cappedStep, capped);
6406
+ }
6407
+ break;
6408
+ }
6241
6409
  const combined = step.clone().multiply(correction);
6242
6410
  if (quaternionToRotationVector(combined).length() > MAX_CORRECTION) {
6243
6411
  break;
6244
6412
  }
6245
- correction.premultiply(step);
6246
- added.copy(next);
6247
- apply();
6413
+ if (!attempt(step, next)) {
6414
+ break;
6415
+ }
6248
6416
  }
6249
6417
  return quaternionToRotationVector(added);
6250
6418
  });
6251
6419
  };
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();
6420
+ var bodyDepthWith = function(model, frame, side, vector) {
6421
+ const upper = `${side}UpperArm`;
6422
+ const pose = solvePose(model, frame.local, frame.hips);
6423
+ const parentRotation = pose.rotation.get(model.parent.get(upper));
6424
+ const original = frame.local.get(upper) ?? new Quaternion5();
6425
+ const local = new Map(frame.local);
6426
+ local.set(
6427
+ upper,
6428
+ parentRotation.clone().invert().multiply(
6429
+ rotationVectorToQuaternion(vector).multiply(
6430
+ parentRotation.clone().multiply(original)
6431
+ )
6432
+ )
6433
+ );
6434
+ solvePose(model, local, frame.hips, ARM_CHAIN(model, side), pose);
6435
+ return deepestContact(model, pose, side, "body")?.depth ?? 0;
6436
+ };
6437
+ var SCALES = [0.75, 0.5, 0.25, 0];
6438
+ var keepNoDeeper = function(model, frames, side, total) {
6439
+ return total.map((vector, index) => {
6440
+ if (vector.length() < 0.5 * Math.PI / 180) {
6441
+ return vector;
6442
+ }
6443
+ const frame = frames[index];
6444
+ const untouched = bodyDepthWith(model, frame, side, new Vector36());
6445
+ let best = bodyDepthWith(model, frame, side, vector);
6446
+ if (best <= untouched + 2e-3) {
6447
+ return vector;
6448
+ }
6449
+ let result = vector;
6450
+ for (const scale of SCALES) {
6451
+ const scaled = vector.clone().multiplyScalar(scale);
6452
+ const depth = bodyDepthWith(model, frame, side, scaled);
6453
+ if (depth < best - 1e-3) {
6454
+ best = depth;
6455
+ result = scaled;
6456
+ }
6457
+ }
6458
+ return result;
6459
+ });
6460
+ };
6461
+ var solveHandClearance = function(model, frames, options = {}) {
6462
+ const margin = options.margin ?? 8e-3;
6463
+ const result = /* @__PURE__ */ new Map();
6266
6464
  for (const side of SIDES) {
6267
6465
  const upper = `${side}UpperArm`;
6268
- const upperNode = nodeName(upper);
6269
- if (!upperNode || !model.parent.get(upper)) {
6466
+ if (!model.parent.get(upper)) {
6270
6467
  continue;
6271
6468
  }
6272
6469
  let total = frames.map(() => new Vector36());
6273
6470
  let changed = false;
6274
- for (let pass = 0; pass < 2; pass += 1) {
6471
+ const compose = (raw) => {
6472
+ const smooth = widenAndSmooth(raw, HOLD_FRAMES, SMOOTH_SIGMA);
6473
+ total = total.map(
6474
+ (t, i) => quaternionToRotationVector(
6475
+ rotationVectorToQuaternion(smooth[i]).multiply(
6476
+ rotationVectorToQuaternion(t)
6477
+ )
6478
+ )
6479
+ );
6480
+ };
6481
+ for (const kind of ["body", "body", "cloth"]) {
6275
6482
  const raw = frameCorrections(
6276
6483
  model,
6277
6484
  frames,
6278
6485
  side,
6279
6486
  total,
6280
6487
  margin,
6488
+ kind,
6281
6489
  options.onDebug
6282
6490
  );
6283
6491
  if (raw.every((v) => v.lengthSq() < 1e-10)) {
6284
- break;
6492
+ continue;
6285
6493
  }
6286
6494
  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
- );
6495
+ compose(raw);
6496
+ }
6497
+ if (changed) {
6498
+ total = keepNoDeeper(model, frames, side, total);
6295
6499
  }
6296
6500
  if (options.onReport) {
6297
- const left = frameCorrections(model, frames, side, total, margin);
6501
+ const left = frameCorrections(model, frames, side, total, margin, "body");
6298
6502
  const degrees = total.map((v) => v.length() * 180 / Math.PI);
6299
6503
  options.onReport({
6300
6504
  side,
6301
- maps: model.maps.map((m) => `${m.bone}:${m.heights}`),
6505
+ maps: model.maps.map((m) => `${m.bone}:${m.kind ?? "body"}`),
6302
6506
  handPoints: model.hands[side].length,
6303
6507
  maxDegrees: Math.max(0, ...degrees),
6304
6508
  correctedFrames: degrees.filter((d) => d > 0.5).length,
@@ -6311,29 +6515,48 @@ var applyHandClearance = function(clip, vrm, options = {}) {
6311
6515
  if (!changed) {
6312
6516
  continue;
6313
6517
  }
6314
- const values = new Float32Array(count * 4);
6315
6518
  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
- });
6519
+ result.set(
6520
+ upper,
6521
+ frames.map((frame, index) => {
6522
+ const pose = solvePose(model, frame.local, frame.hips);
6523
+ const parentRotation = pose.rotation.get(model.parent.get(upper));
6524
+ const original = frame.local.get(upper) ?? new Quaternion5();
6525
+ const world = parentRotation.clone().multiply(original);
6526
+ const corrected = parentRotation.clone().invert().multiply(rotationVectorToQuaternion(total[index]).multiply(world)).normalize();
6527
+ if (previous && previous.dot(corrected) < 0) {
6528
+ corrected.set(-corrected.x, -corrected.y, -corrected.z, -corrected.w);
6529
+ }
6530
+ previous = corrected;
6531
+ return corrected;
6532
+ })
6533
+ );
6534
+ }
6535
+ return result;
6536
+ };
6537
+ var applyHandClearance = function(clip, vrm, options = {}) {
6538
+ const model = bodyModelOf(vrm);
6539
+ if (!model || clip.duration <= 0) {
6540
+ return clip;
6541
+ }
6542
+ const sampled = sampleClipFrames(model, vrm, clip, options.fps ?? 30);
6543
+ if (!sampled) {
6544
+ return clip;
6545
+ }
6546
+ const { times, frames, nodeName } = sampled;
6547
+ const solved = solveHandClearance(model, frames, options);
6548
+ const replaced = /* @__PURE__ */ new Map();
6549
+ for (const [bone, rotations] of solved) {
6550
+ const node = nodeName(bone);
6551
+ if (!node) {
6552
+ continue;
6553
+ }
6331
6554
  replaced.set(
6332
- `${upperNode}.quaternion`,
6555
+ `${node}.quaternion`,
6333
6556
  new QuaternionKeyframeTrack2(
6334
- `${upperNode}.quaternion`,
6557
+ `${node}.quaternion`,
6335
6558
  times,
6336
- Array.from(values)
6559
+ rotations.flatMap((q) => [q.x, q.y, q.z, q.w])
6337
6560
  )
6338
6561
  );
6339
6562
  }
@@ -7038,6 +7261,7 @@ var AnimationController = class {
7038
7261
  this.mixer = new AnimationMixer2(this.vrm.scene);
7039
7262
  this.idleAnimationId = idleAnimationId ?? null;
7040
7263
  this.normalizeIdleOrientation = options?.normalizeIdleOrientation ?? false;
7264
+ this.handClearance = options?.handClearance ?? true;
7041
7265
  this.mixer.addEventListener("finished", this.handleActionFinished);
7042
7266
  }
7043
7267
  prepareAnimations;
@@ -7047,6 +7271,7 @@ var AnimationController = class {
7047
7271
  normalizedIdleClips = /* @__PURE__ */ new Map();
7048
7272
  configs = /* @__PURE__ */ new Map();
7049
7273
  normalizeIdleOrientation;
7274
+ handClearance;
7050
7275
  previewMixer = null;
7051
7276
  restorePreview = null;
7052
7277
  activeAction = null;
@@ -7136,7 +7361,9 @@ var AnimationController = class {
7136
7361
  return this.clips.has(id);
7137
7362
  }
7138
7363
  addAnimation(animationId, animation, config) {
7139
- const rawClip = createVRMAnimationClip(animation, this.vrm);
7364
+ const rawClip = createVRMAnimationClip(animation, this.vrm, {
7365
+ handClearance: this.handClearance
7366
+ });
7140
7367
  this.rawClips.set(animationId, rawClip);
7141
7368
  this.normalizedIdleClips.delete(animationId);
7142
7369
  this.clips.set(animationId, this.resolveClipForId(animationId, rawClip));
@@ -7147,7 +7374,9 @@ var AnimationController = class {
7147
7374
  }
7148
7375
  previewAnimation(animation) {
7149
7376
  this.restorePreview?.();
7150
- const clip = createVRMAnimationClip(animation, this.vrm);
7377
+ const clip = createVRMAnimationClip(animation, this.vrm, {
7378
+ handClearance: this.handClearance
7379
+ });
7151
7380
  const previewMixer = new AnimationMixer2(this.vrm.scene);
7152
7381
  previewMixer.clipAction(clip).setLoop(LoopRepeat2, Infinity).reset().play();
7153
7382
  this.previewMixer = previewMixer;
@@ -14909,6 +15138,7 @@ var Character = class extends CharacterRuntime {
14909
15138
  hostedWebChat;
14910
15139
  memoryIdentifier;
14911
15140
  normalizeIdleOrientation;
15141
+ handClearance;
14912
15142
  bakedAsset;
14913
15143
  animationRefreshId = 0;
14914
15144
  voice;
@@ -14937,6 +15167,7 @@ var Character = class extends CharacterRuntime {
14937
15167
  this.hostedWebChat = options.hostedWebChat === true && !resolvedToken;
14938
15168
  this.memoryIdentifier = normalizeMemoryIdentifier(options.memoryIdentifier);
14939
15169
  this.normalizeIdleOrientation = options.normalizeIdleOrientation ?? false;
15170
+ this.handClearance = options.handClearance ?? true;
14940
15171
  this.bakedAsset = options.bakedConversation;
14941
15172
  this.initialAssetInfo = options.initialAssetInfo ?? null;
14942
15173
  this.inputs = new InputModeRules(
@@ -15075,7 +15306,10 @@ var Character = class extends CharacterRuntime {
15075
15306
  const animationController = new AnimationController(
15076
15307
  this.vrm,
15077
15308
  info.idleAnimationId ?? null,
15078
- { normalizeIdleOrientation: this.normalizeIdleOrientation }
15309
+ {
15310
+ normalizeIdleOrientation: this.normalizeIdleOrientation,
15311
+ handClearance: this.handClearance
15312
+ }
15079
15313
  );
15080
15314
  animationController.prepareAnimations = (ids) => this.lazyAnimations.ensureMany(ids);
15081
15315
  this.animationController = animationController;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@npcify/web",
3
- "version": "0.7.1",
3
+ "version": "0.7.3",
4
4
  "description": "Framework-independent Npcify characters, live conversations and baked performances for the web",
5
5
  "type": "module",
6
6
  "license": "UNLICENSED",