@npcify/web 0.8.0 → 0.8.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 +6 -1
  2. package/dist/index.js +126 -21
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -269,7 +269,8 @@ character.onListeningState((state) => {
269
269
  ### Interrupting a reply
270
270
 
271
271
  When the character owner turned on **Players can interrupt replies** (the
272
- Conversation tab; it needs custom replies and no baked conversation), the
272
+ STT tab, Interruption; it needs custom replies and no baked conversation, not
273
+ voice input), the
273
274
  player can stop a reply while it is generated or plays. The reply field and
274
275
  push-to-talk need no lock during the reply:
275
276
 
@@ -396,6 +397,10 @@ character.onSight(({ summary }) => { status.textContent = `The character sees: $
396
397
  `CONVERSATION_TIMEOUT`. A refused conversation fails at once with
397
398
  `CONVERSATION_ACCESS_DENIED` until `endConversation()`; after another join
398
399
  code (`CONVERSATION_JOIN_FAILED` or a newer one) the next call tries again.
400
+ - Spring bones (hair, skirts, tails, wings, the bust of a generated
401
+ character) step at 60 per second on displays faster than 60 Hz (0.8.1), as
402
+ the character generator tunes them; before, a 120 Hz display halved their
403
+ swing. At 60 Hz and slower they run as before.
399
404
  - ESM only; `require()` is not a supported package entry point. Browser APIs are
400
405
  used when creating a view; do not call `mountNpcify` during server rendering.
401
406
 
package/dist/index.js CHANGED
@@ -6498,6 +6498,48 @@ var HumanoidCapsuleCollisionBackend = class {
6498
6498
  }
6499
6499
  };
6500
6500
 
6501
+ // ../vrm-renderer/src/core/spring-pace.ts
6502
+ var SPRING_STEP = 1 / 60;
6503
+ var SHORT_FRAME = 0.75;
6504
+ var JITTER = 1e-4;
6505
+ var paced = /* @__PURE__ */ new WeakSet();
6506
+ var springSteps = function(delta, pending) {
6507
+ if (!(delta > 0)) {
6508
+ return [[], pending];
6509
+ }
6510
+ if (delta >= SPRING_STEP * SHORT_FRAME) {
6511
+ return [[delta], 0];
6512
+ }
6513
+ const gathered = pending + delta;
6514
+ if (gathered >= SPRING_STEP - JITTER) {
6515
+ return [[SPRING_STEP], Math.max(0, gathered - SPRING_STEP)];
6516
+ }
6517
+ return [[], gathered];
6518
+ };
6519
+ var paceSprings = function(vrm) {
6520
+ const manager = vrm.springBoneManager;
6521
+ if (!manager || typeof manager.update !== "function" || paced.has(manager)) {
6522
+ return;
6523
+ }
6524
+ paced.add(manager);
6525
+ const update = manager.update.bind(manager);
6526
+ const reset = manager.reset?.bind(manager);
6527
+ let pending = 0;
6528
+ manager.update = (delta) => {
6529
+ const [steps, rest] = springSteps(delta, pending);
6530
+ pending = rest;
6531
+ for (const step of steps) {
6532
+ update(step);
6533
+ }
6534
+ };
6535
+ if (reset) {
6536
+ manager.reset = () => {
6537
+ pending = 0;
6538
+ reset();
6539
+ };
6540
+ }
6541
+ };
6542
+
6501
6543
  // ../vrm-renderer/src/scene.ts
6502
6544
  var createSceneContext = function(options) {
6503
6545
  const {
@@ -6802,8 +6844,8 @@ var queryRadialMap = function(map, point) {
6802
6844
  if (radial <= 0) {
6803
6845
  return { depth: radial, direction: NO_DIRECTION };
6804
6846
  }
6805
- const up = (1 - fc) * map.top[c0] + fc * map.top[c1] - height;
6806
- const down = height - ((1 - fc) * map.bottom[c0] + fc * map.bottom[c1]);
6847
+ const up = map.openTop === false ? Infinity : (1 - fc) * map.top[c0] + fc * map.top[c1] - height;
6848
+ const down = map.openBottom === false ? Infinity : height - ((1 - fc) * map.bottom[c0] + fc * map.bottom[c1]);
6807
6849
  const direction = distance > 1e-6 ? map.reference.clone().multiplyScalar(x / distance).add(binormal.clone().multiplyScalar(y / distance)) : map.reference.clone();
6808
6850
  const side = { depth: radial, direction };
6809
6851
  if (up < radial && up <= down) {
@@ -7195,18 +7237,48 @@ var buildBodyModel = function(vrm) {
7195
7237
  });
7196
7238
  const maps = [];
7197
7239
  for (const kind of ["body", "cloth"]) {
7240
+ const torsoMaps = [];
7198
7241
  for (const bone of TORSO_PARTS) {
7199
7242
  const origin = rest.get(bone);
7200
7243
  const points = partPoints.get(`${bone}|${kind}`);
7201
7244
  const map = origin && points ? buildRadialMap(bone, origin, UP, points) : null;
7202
- if (map) maps.push({ ...map, kind });
7245
+ if (map) {
7246
+ torsoMaps.push({ ...map, kind });
7247
+ }
7203
7248
  }
7249
+ torsoMaps.forEach((map, index) => {
7250
+ if (kind === "body") {
7251
+ map.openBottom = index === 0;
7252
+ map.openTop = index === torsoMaps.length - 1;
7253
+ } else {
7254
+ map.openBottom = true;
7255
+ map.openTop = map.bone === "chest" || map.bone === "upperChest";
7256
+ }
7257
+ maps.push(map);
7258
+ });
7259
+ const limbMaps = /* @__PURE__ */ new Map();
7204
7260
  for (const [bone, end] of LIMB_PARTS) {
7205
7261
  const origin = rest.get(bone);
7206
7262
  const target = rest.get(end);
7207
7263
  const points = partPoints.get(`${bone}|${kind}`);
7208
7264
  const map = origin && target && points ? buildRadialMap(bone, origin, target.clone().sub(origin), points) : null;
7209
- if (map) maps.push({ ...map, kind });
7265
+ if (map) {
7266
+ limbMaps.set(bone, { ...map, kind });
7267
+ }
7268
+ }
7269
+ for (const [bone, end] of LIMB_PARTS) {
7270
+ const map = limbMaps.get(bone);
7271
+ if (!map) {
7272
+ continue;
7273
+ }
7274
+ if (bone.endsWith("Shoulder")) {
7275
+ map.openBottom = false;
7276
+ map.openTop = false;
7277
+ } else {
7278
+ map.openBottom = false;
7279
+ map.openTop = kind === "cloth" || !limbMaps.has(end);
7280
+ }
7281
+ maps.push(map);
7210
7282
  }
7211
7283
  }
7212
7284
  const hands = { left: [], right: [] };
@@ -7626,9 +7698,17 @@ var deepestContact = function(model, pose, side, kind) {
7626
7698
  if (!best || perMap.length < 2) {
7627
7699
  return best;
7628
7700
  }
7629
- const push = new Vector36();
7630
- for (const contact of perMap) {
7631
- push.addScaledVector(contact.direction, contact.depth);
7701
+ const push = best.direction.clone().multiplyScalar(best.depth);
7702
+ for (let pass = 0; pass < 3; pass += 1) {
7703
+ for (const contact of perMap) {
7704
+ if (contact.direction.dot(best.direction) <= -0.25) {
7705
+ continue;
7706
+ }
7707
+ const missing = contact.depth - push.dot(contact.direction);
7708
+ if (missing > 1e-6) {
7709
+ push.addScaledVector(contact.direction, missing);
7710
+ }
7711
+ }
7632
7712
  }
7633
7713
  if (push.length() < best.depth * 0.5) {
7634
7714
  return best;
@@ -7637,7 +7717,8 @@ var deepestContact = function(model, pose, side, kind) {
7637
7717
  bone: perMap.map((contact) => contact.bone).join("+"),
7638
7718
  point: best.point,
7639
7719
  depth: push.length(),
7640
- direction: push.normalize()
7720
+ direction: push.normalize(),
7721
+ side: best.side
7641
7722
  };
7642
7723
  };
7643
7724
  var ARM_CHAIN = function(model, side) {
@@ -7654,6 +7735,29 @@ var ARM_CHAIN = function(model, side) {
7654
7735
  }
7655
7736
  return result;
7656
7737
  };
7738
+ var capStepToAngle = function(step, correction, maxAngle) {
7739
+ if (quaternionToRotationVector(correction).length() >= maxAngle - 1e-6) {
7740
+ return null;
7741
+ }
7742
+ const stepVector = quaternionToRotationVector(step);
7743
+ let low = 0;
7744
+ let high = 1;
7745
+ for (let i = 0; i < 10; i += 1) {
7746
+ const mid = (low + high) * 0.5;
7747
+ const candidate = rotationVectorToQuaternion(
7748
+ stepVector.clone().multiplyScalar(mid)
7749
+ ).multiply(correction);
7750
+ if (quaternionToRotationVector(candidate).length() <= maxAngle) {
7751
+ low = mid;
7752
+ } else {
7753
+ high = mid;
7754
+ }
7755
+ }
7756
+ if (low <= 1e-4) {
7757
+ return null;
7758
+ }
7759
+ return rotationVectorToQuaternion(stepVector.multiplyScalar(low));
7760
+ };
7657
7761
  var frameCorrections = function(model, frames, side, start, margin, kind, debug) {
7658
7762
  const upper = `${side}UpperArm`;
7659
7763
  const chain = ARM_CHAIN(model, side);
@@ -7716,22 +7820,20 @@ var frameCorrections = function(model, frames, side, start, margin, kind, debug)
7716
7820
  stepDegrees: quaternionToRotationVector(step).length() * 180 / Math.PI,
7717
7821
  totalDegrees: quaternionToRotationVector(next).length() * 180 / Math.PI
7718
7822
  });
7719
- const nextAngle = quaternionToRotationVector(next).length();
7720
- if (kind === "cloth" && nextAngle > CLOTH_CORRECTION_CAP) {
7721
- const capped = rotationVectorToQuaternion(
7722
- quaternionToRotationVector(next).multiplyScalar(
7723
- CLOTH_CORRECTION_CAP / nextAngle
7724
- )
7823
+ const combined = step.clone().multiply(correction);
7824
+ const combinedAngle = quaternionToRotationVector(combined).length();
7825
+ if (kind === "cloth" && combinedAngle > CLOTH_CORRECTION_CAP) {
7826
+ const cappedStep = capStepToAngle(
7827
+ step,
7828
+ correction,
7829
+ CLOTH_CORRECTION_CAP
7725
7830
  );
7726
- const cappedStep = capped.clone().multiply(added.clone().invert());
7727
- const total = cappedStep.clone().multiply(correction);
7728
- if (quaternionToRotationVector(total).length() <= MAX_CORRECTION) {
7729
- attempt(cappedStep, capped);
7831
+ if (cappedStep) {
7832
+ attempt(cappedStep, cappedStep.clone().multiply(added));
7730
7833
  }
7731
7834
  break;
7732
7835
  }
7733
- const combined = step.clone().multiply(correction);
7734
- if (quaternionToRotationVector(combined).length() > MAX_CORRECTION) {
7836
+ if (combinedAngle > MAX_CORRECTION) {
7735
7837
  break;
7736
7838
  }
7737
7839
  if (!attempt(step, next)) {
@@ -14492,7 +14594,10 @@ var CharacterRuntime = class extends CharacterSceneAccess {
14492
14594
  });
14493
14595
  this.visemePlayer.update(delta);
14494
14596
  this.facialMixer.update(delta);
14495
- this.vrm?.update(delta);
14597
+ if (this.vrm) {
14598
+ paceSprings(this.vrm);
14599
+ this.vrm.update(delta);
14600
+ }
14496
14601
  if (this.performanceRecorder.isRecording) {
14497
14602
  this.performanceRecorder.recordFrame({
14498
14603
  audio: this.visemePlayer.getAudioPerformanceSnapshot(),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@npcify/web",
3
- "version": "0.8.0",
3
+ "version": "0.8.1",
4
4
  "description": "Framework-independent Npcify characters, live conversations and baked performances for the web",
5
5
  "type": "module",
6
6
  "license": "UNLICENSED",