@npcify/web 0.7.1 → 0.7.2

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 -1
  2. package/dist/index.js +237 -105
  3. package/package.json +3 -3
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).
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.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);
@@ -5452,20 +5458,26 @@ var queryRadialMap = function(map, point) {
5452
5458
  const c1 = (c0 + 1) % MAP_ANGLE_BINS;
5453
5459
  const fc = angle - Math.floor(angle);
5454
5460
  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));
5461
+ const fr = Math.max(0, Math.min(1, row - r0));
5462
+ const surface = (1 - fr) * ((1 - fc) * at(r0, c0) + fc * at(r0, c1)) + fr * ((1 - fc) * at(r1, c0) + fc * at(r1, c1));
5456
5463
  const radial = surface - distance;
5457
5464
  if (radial <= 0) {
5458
5465
  return { depth: radial, direction: NO_DIRECTION };
5459
5466
  }
5460
5467
  const up = (1 - fc) * map.top[c0] + fc * map.top[c1] - height;
5461
5468
  const down = height - ((1 - fc) * map.bottom[c0] + fc * map.bottom[c1]);
5469
+ const direction = distance > 1e-6 ? map.reference.clone().multiplyScalar(x / distance).add(binormal.clone().multiplyScalar(y / distance)) : map.reference.clone();
5470
+ const side = { depth: radial, direction };
5462
5471
  if (up < radial && up <= down) {
5463
- return { depth: Math.max(0, up), direction: map.axis.clone() };
5472
+ return { depth: Math.max(0, up), direction: map.axis.clone(), side };
5464
5473
  }
5465
5474
  if (down < radial) {
5466
- return { depth: Math.max(0, down), direction: map.axis.clone().negate() };
5475
+ return {
5476
+ depth: Math.max(0, down),
5477
+ direction: map.axis.clone().negate(),
5478
+ side
5479
+ };
5467
5480
  }
5468
- const direction = distance > 1e-6 ? map.reference.clone().multiplyScalar(x / distance).add(binormal.clone().multiplyScalar(y / distance)) : map.reference.clone();
5469
5481
  return { depth: radial, direction };
5470
5482
  };
5471
5483
  var marchPoint = new Vector33();
@@ -5564,13 +5576,13 @@ var isHandSkin = function(point, segments) {
5564
5576
  };
5565
5577
 
5566
5578
  // ../vrm-renderer/src/hand-body.ts
5579
+ var BUST = /bust|breast|oppai/i;
5567
5580
  var SIDES = ["left", "right"];
5568
5581
  var TORSO_PARTS = [
5569
5582
  "hips",
5570
5583
  "spine",
5571
5584
  "chest",
5572
- "upperChest",
5573
- "neck"
5585
+ "upperChest"
5574
5586
  ];
5575
5587
  var LIMB_PARTS = [
5576
5588
  ["leftUpperLeg", "leftLowerLeg"],
@@ -5589,7 +5601,7 @@ var isHandBone = function(name, side) {
5589
5601
  return rest === "Hand" || FINGERS.some((finger) => rest.startsWith(finger));
5590
5602
  };
5591
5603
  var MAX_HAND_POINTS = 48;
5592
- var MAX_PART_POINTS = 6e3;
5604
+ var TRIANGLE_SAMPLE_EDGE = 0.02;
5593
5605
  var UP = new Vector34(0, 1, 0);
5594
5606
  var bodyModels = /* @__PURE__ */ new WeakMap();
5595
5607
  var humanBoneNames = function(vrm) {
@@ -5665,9 +5677,9 @@ var buildBodyModel = function(vrm) {
5665
5677
  const node = humanoid.getNormalizedBoneNode(name);
5666
5678
  nameOfNormalized.set(node, name);
5667
5679
  rest.set(name, node.getWorldPosition(new Vector34()).applyMatrix4(toRig));
5668
- const raw = humanoid.getRawBoneNode(name);
5669
- if (raw) {
5670
- nameOfRaw.set(raw, name);
5680
+ const raw2 = humanoid.getRawBoneNode(name);
5681
+ if (raw2) {
5682
+ nameOfRaw.set(raw2, name);
5671
5683
  }
5672
5684
  }
5673
5685
  const parent = /* @__PURE__ */ new Map();
@@ -5722,15 +5734,16 @@ var buildBodyModel = function(vrm) {
5722
5734
  };
5723
5735
  const partPoints = /* @__PURE__ */ new Map();
5724
5736
  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
5737
  const vertex = new Vector34();
5733
- let counter = 0;
5738
+ const raw = new Vector34();
5739
+ const pushPoint = (key, x, y, z) => {
5740
+ let list = partPoints.get(key);
5741
+ if (!list) {
5742
+ list = [];
5743
+ partPoints.set(key, list);
5744
+ }
5745
+ list.push(x, y, z);
5746
+ };
5734
5747
  vrm.scene.traverse((object) => {
5735
5748
  if (!(object instanceof SkinnedMesh) || !object.visible) {
5736
5749
  return;
@@ -5742,10 +5755,29 @@ var buildBodyModel = function(vrm) {
5742
5755
  if (!position || !skinIndex || !skinWeight) {
5743
5756
  return;
5744
5757
  }
5745
- for (let i = 0; i < position.count; i += 1, counter += 1) {
5746
- if (counter % stride !== 0) {
5747
- continue;
5758
+ const count = position.count;
5759
+ const toRigMesh = toRig.clone().multiply(object.matrixWorld);
5760
+ let bindIsRest = true;
5761
+ for (let k = 0; k < 24 && bindIsRest; k += 1) {
5762
+ const i = Math.floor(k * (count - 1) / 23);
5763
+ object.getVertexPosition(i, vertex);
5764
+ raw.fromBufferAttribute(position, i);
5765
+ bindIsRest = vertex.distanceTo(raw) < 1e-3;
5766
+ }
5767
+ const restPositions = new Float32Array(count * 3);
5768
+ for (let i = 0; i < count; i += 1) {
5769
+ if (bindIsRest) {
5770
+ vertex.fromBufferAttribute(position, i);
5771
+ } else {
5772
+ object.getVertexPosition(i, vertex);
5748
5773
  }
5774
+ vertex.applyMatrix4(toRigMesh);
5775
+ restPositions[i * 3] = vertex.x;
5776
+ restPositions[i * 3 + 1] = vertex.y;
5777
+ restPositions[i * 3 + 2] = vertex.z;
5778
+ }
5779
+ const keys = new Array(count).fill(null);
5780
+ for (let i = 0; i < count; i += 1) {
5749
5781
  let best = 0;
5750
5782
  let bestWeight = -1;
5751
5783
  for (let k = 0; k < 4; k += 1) {
@@ -5760,34 +5792,73 @@ var buildBodyModel = function(vrm) {
5760
5792
  if (!attach) {
5761
5793
  continue;
5762
5794
  }
5763
- object.getVertexPosition(i, vertex);
5764
- const point = vertex.applyMatrix4(object.matrixWorld).applyMatrix4(toRig).clone();
5765
5795
  const side = SIDES.find((s) => isHandBone(attach, s));
5766
- if (side && (nameOfRaw.get(bone) !== attach || !isHandSkin(point, handSegments[side]))) {
5796
+ if (side) {
5797
+ const point = new Vector34().fromArray(restPositions, i * 3);
5798
+ if (nameOfRaw.get(bone) === attach && isHandSkin(point, handSegments[side])) {
5799
+ handPoints[side].push({ bone: attach, rest: point });
5800
+ }
5767
5801
  continue;
5768
5802
  }
5769
- if (side) {
5770
- handPoints[side].push({ bone: attach, rest: point });
5803
+ const body = nameOfRaw.get(bone) === attach || BUST.test(bone.name);
5804
+ const key = `${attach}|${body ? "body" : "cloth"}`;
5805
+ keys[i] = key;
5806
+ pushPoint(
5807
+ key,
5808
+ restPositions[i * 3],
5809
+ restPositions[i * 3 + 1],
5810
+ restPositions[i * 3 + 2]
5811
+ );
5812
+ }
5813
+ const index = geometry.index;
5814
+ const triangles = index ? index.count / 3 : count / 3;
5815
+ const at = (t, k) => index ? index.getX(t * 3 + k) : t * 3 + k;
5816
+ for (let t = 0; t < triangles; t += 1) {
5817
+ const a = at(t, 0);
5818
+ const b = at(t, 1);
5819
+ const c = at(t, 2);
5820
+ const key = keys[a];
5821
+ if (!key || keys[b] !== key || keys[c] !== key) {
5822
+ continue;
5823
+ }
5824
+ const p = restPositions;
5825
+ const ax = p[a * 3], ay = p[a * 3 + 1], az = p[a * 3 + 2];
5826
+ const bx = p[b * 3], by = p[b * 3 + 1], bz = p[b * 3 + 2];
5827
+ const cx = p[c * 3], cy = p[c * 3 + 1], cz = p[c * 3 + 2];
5828
+ const longest = Math.max(
5829
+ Math.hypot(ax - bx, ay - by, az - bz),
5830
+ Math.hypot(bx - cx, by - cy, bz - cz),
5831
+ Math.hypot(cx - ax, cy - ay, cz - az)
5832
+ );
5833
+ if (longest < TRIANGLE_SAMPLE_EDGE) {
5771
5834
  continue;
5772
5835
  }
5773
- const list = partPoints.get(attach) ?? [];
5774
- list.push(point);
5775
- partPoints.set(attach, list);
5836
+ pushPoint(
5837
+ key,
5838
+ (ax + bx + cx) / 3,
5839
+ (ay + by + cy) / 3,
5840
+ (az + bz + cz) / 3
5841
+ );
5842
+ pushPoint(key, (ax + bx) / 2, (ay + by) / 2, (az + bz) / 2);
5843
+ pushPoint(key, (bx + cx) / 2, (by + cy) / 2, (bz + cz) / 2);
5844
+ pushPoint(key, (cx + ax) / 2, (cy + ay) / 2, (cz + az) / 2);
5776
5845
  }
5777
5846
  });
5778
5847
  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);
5848
+ for (const kind of ["body", "cloth"]) {
5849
+ for (const bone of TORSO_PARTS) {
5850
+ const origin = rest.get(bone);
5851
+ const points = partPoints.get(`${bone}|${kind}`);
5852
+ const map = origin && points ? buildRadialMap(bone, origin, UP, points) : null;
5853
+ if (map) maps.push({ ...map, kind });
5854
+ }
5855
+ for (const [bone, end] of LIMB_PARTS) {
5856
+ const origin = rest.get(bone);
5857
+ const target = rest.get(end);
5858
+ const points = partPoints.get(`${bone}|${kind}`);
5859
+ const map = origin && target && points ? buildRadialMap(bone, origin, target.clone().sub(origin), points) : null;
5860
+ if (map) maps.push({ ...map, kind });
5861
+ }
5791
5862
  }
5792
5863
  const hands = { left: [], right: [] };
5793
5864
  for (const side of SIDES) {
@@ -5878,12 +5949,10 @@ var sampleClipFrames = function(model, vrm, clip, fps) {
5878
5949
  if (clip.duration <= 0) {
5879
5950
  return null;
5880
5951
  }
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
5952
  const nodeName = (name) => vrm.humanoid.getNormalizedBoneNode(name)?.name;
5953
+ const armTrack = ["leftUpperArm", "rightUpperArm"].map((bone) => nodeName(bone)).map((node) => node ? trackOf(clip, `${node}.quaternion`) : void 0).find((track) => track && track.times.length >= 2);
5954
+ const count = Math.max(2, Math.round(clip.duration * fps) + 1);
5955
+ const times = armTrack ? Array.from(armTrack.times) : Array.from({ length: count }, (_, i) => Math.min(clip.duration, i / fps));
5887
5956
  const interpolants = /* @__PURE__ */ new Map();
5888
5957
  for (const name of model.order) {
5889
5958
  const node = nodeName(name);
@@ -6140,15 +6209,20 @@ import {
6140
6209
  Vector3 as Vector36
6141
6210
  } from "three";
6142
6211
  var MAX_CORRECTION = 35 * Math.PI / 180;
6143
- var MAX_ITERATIONS = 4;
6144
- var deepestContact = function(model, pose, side) {
6212
+ var CLOTH_CORRECTION_CAP = 10 * Math.PI / 180;
6213
+ var MAX_ITERATIONS = 6;
6214
+ var deepestContact = function(model, pose, side, kind) {
6145
6215
  let best = null;
6216
+ const perMap = [];
6146
6217
  const points = model.hands[side].map(
6147
6218
  (handPoint) => pointInPose(model, pose, handPoint)
6148
6219
  );
6149
6220
  const inverse = new Quaternion5();
6150
6221
  const local = new Vector36();
6151
6222
  for (const map of model.maps) {
6223
+ if ((map.kind ?? "body") !== kind) {
6224
+ continue;
6225
+ }
6152
6226
  const bone = map.bone;
6153
6227
  const rotation = pose.rotation.get(bone);
6154
6228
  const position = pose.position.get(bone);
@@ -6158,6 +6232,7 @@ var deepestContact = function(model, pose, side) {
6158
6232
  inverse.copy(rotation).invert();
6159
6233
  const rest = model.rest.get(bone);
6160
6234
  const torso = TORSO_PARTS.includes(bone);
6235
+ let mapBest = null;
6161
6236
  for (const point of points) {
6162
6237
  local.copy(point).sub(position).applyQuaternion(inverse).add(rest);
6163
6238
  const hit = queryRadialMap(map, local);
@@ -6171,17 +6246,42 @@ var deepestContact = function(model, pose, side) {
6171
6246
  hit.direction = model.forward.clone();
6172
6247
  }
6173
6248
  }
6174
- if (!best || hit.depth > best.depth) {
6175
- best = {
6249
+ if (!mapBest || hit.depth > mapBest.depth) {
6250
+ mapBest = {
6176
6251
  bone: map.bone,
6177
6252
  point,
6178
6253
  depth: hit.depth,
6179
- direction: hit.direction.clone().applyQuaternion(rotation)
6254
+ direction: hit.direction.clone().applyQuaternion(rotation),
6255
+ side: hit.side && {
6256
+ depth: hit.side.depth,
6257
+ direction: hit.side.direction.clone().applyQuaternion(rotation)
6258
+ }
6180
6259
  };
6181
6260
  }
6182
6261
  }
6262
+ if (mapBest) {
6263
+ perMap.push(mapBest);
6264
+ if (!best || mapBest.depth > best.depth) {
6265
+ best = mapBest;
6266
+ }
6267
+ }
6268
+ }
6269
+ if (!best || perMap.length < 2) {
6270
+ return best;
6271
+ }
6272
+ const push = new Vector36();
6273
+ for (const contact of perMap) {
6274
+ push.addScaledVector(contact.direction, contact.depth);
6183
6275
  }
6184
- return best;
6276
+ if (push.length() < best.depth * 0.5) {
6277
+ return best;
6278
+ }
6279
+ return {
6280
+ bone: perMap.map((contact) => contact.bone).join("+"),
6281
+ point: best.point,
6282
+ depth: push.length(),
6283
+ direction: push.normalize()
6284
+ };
6185
6285
  };
6186
6286
  var ARM_CHAIN = function(model, side) {
6187
6287
  const result = /* @__PURE__ */ new Set();
@@ -6197,7 +6297,7 @@ var ARM_CHAIN = function(model, side) {
6197
6297
  }
6198
6298
  return result;
6199
6299
  };
6200
- var frameCorrections = function(model, frames, side, start, margin, debug) {
6300
+ var frameCorrections = function(model, frames, side, start, margin, kind, debug) {
6201
6301
  const upper = `${side}UpperArm`;
6202
6302
  const chain = ARM_CHAIN(model, side);
6203
6303
  const upperParent = model.parent.get(upper);
@@ -6219,17 +6319,22 @@ var frameCorrections = function(model, frames, side, start, margin, debug) {
6219
6319
  apply();
6220
6320
  const added = new Quaternion5();
6221
6321
  for (let iteration = 0; iteration < MAX_ITERATIONS; iteration += 1) {
6222
- const contact = deepestContact(model, pose, side);
6322
+ const contact = deepestContact(model, pose, side, kind);
6223
6323
  if (!contact) {
6224
6324
  break;
6225
6325
  }
6226
6326
  const shoulder = pose.position.get(upper);
6227
6327
  const from = contact.point.clone().sub(shoulder);
6228
- const to = contact.point.clone().addScaledVector(contact.direction, contact.depth + margin).sub(shoulder);
6328
+ let way = { depth: contact.depth, direction: contact.direction };
6329
+ if (contact.side && Math.abs(from.clone().normalize().dot(contact.direction)) > 0.7) {
6330
+ way = contact.side;
6331
+ }
6332
+ const to = contact.point.clone().addScaledVector(way.direction, way.depth + margin).sub(shoulder);
6229
6333
  const step = rotationBetween(from, to);
6230
6334
  const next = step.clone().multiply(added);
6231
6335
  debug?.({
6232
6336
  side,
6337
+ kind,
6233
6338
  frame: index,
6234
6339
  iteration,
6235
6340
  bone: contact.bone,
@@ -6238,6 +6343,22 @@ var frameCorrections = function(model, frames, side, start, margin, debug) {
6238
6343
  stepDegrees: quaternionToRotationVector(step).length() * 180 / Math.PI,
6239
6344
  totalDegrees: quaternionToRotationVector(next).length() * 180 / Math.PI
6240
6345
  });
6346
+ const nextAngle = quaternionToRotationVector(next).length();
6347
+ if (kind === "cloth" && nextAngle > CLOTH_CORRECTION_CAP) {
6348
+ const capped = rotationVectorToQuaternion(
6349
+ quaternionToRotationVector(next).multiplyScalar(
6350
+ CLOTH_CORRECTION_CAP / nextAngle
6351
+ )
6352
+ );
6353
+ const cappedStep = capped.clone().multiply(added.clone().invert());
6354
+ const total = cappedStep.clone().multiply(correction);
6355
+ if (quaternionToRotationVector(total).length() <= MAX_CORRECTION) {
6356
+ correction.premultiply(cappedStep);
6357
+ added.copy(capped);
6358
+ apply();
6359
+ }
6360
+ break;
6361
+ }
6241
6362
  const combined = step.clone().multiply(correction);
6242
6363
  if (quaternionToRotationVector(combined).length() > MAX_CORRECTION) {
6243
6364
  break;
@@ -6249,56 +6370,48 @@ var frameCorrections = function(model, frames, side, start, margin, debug) {
6249
6370
  return quaternionToRotationVector(added);
6250
6371
  });
6251
6372
  };
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();
6373
+ var solveHandClearance = function(model, frames, options = {}) {
6374
+ const margin = options.margin ?? 8e-3;
6375
+ const result = /* @__PURE__ */ new Map();
6266
6376
  for (const side of SIDES) {
6267
6377
  const upper = `${side}UpperArm`;
6268
- const upperNode = nodeName(upper);
6269
- if (!upperNode || !model.parent.get(upper)) {
6378
+ if (!model.parent.get(upper)) {
6270
6379
  continue;
6271
6380
  }
6272
6381
  let total = frames.map(() => new Vector36());
6273
6382
  let changed = false;
6274
- for (let pass = 0; pass < 2; pass += 1) {
6383
+ const compose = (raw) => {
6384
+ const smooth = widenAndSmooth(raw, 4, 3);
6385
+ total = total.map(
6386
+ (t, i) => quaternionToRotationVector(
6387
+ rotationVectorToQuaternion(smooth[i]).multiply(
6388
+ rotationVectorToQuaternion(t)
6389
+ )
6390
+ )
6391
+ );
6392
+ };
6393
+ for (const kind of ["body", "body", "cloth"]) {
6275
6394
  const raw = frameCorrections(
6276
6395
  model,
6277
6396
  frames,
6278
6397
  side,
6279
6398
  total,
6280
6399
  margin,
6400
+ kind,
6281
6401
  options.onDebug
6282
6402
  );
6283
6403
  if (raw.every((v) => v.lengthSq() < 1e-10)) {
6284
- break;
6404
+ continue;
6285
6405
  }
6286
6406
  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
- );
6407
+ compose(raw);
6295
6408
  }
6296
6409
  if (options.onReport) {
6297
- const left = frameCorrections(model, frames, side, total, margin);
6410
+ const left = frameCorrections(model, frames, side, total, margin, "body");
6298
6411
  const degrees = total.map((v) => v.length() * 180 / Math.PI);
6299
6412
  options.onReport({
6300
6413
  side,
6301
- maps: model.maps.map((m) => `${m.bone}:${m.heights}`),
6414
+ maps: model.maps.map((m) => `${m.bone}:${m.kind ?? "body"}`),
6302
6415
  handPoints: model.hands[side].length,
6303
6416
  maxDegrees: Math.max(0, ...degrees),
6304
6417
  correctedFrames: degrees.filter((d) => d > 0.5).length,
@@ -6311,29 +6424,48 @@ var applyHandClearance = function(clip, vrm, options = {}) {
6311
6424
  if (!changed) {
6312
6425
  continue;
6313
6426
  }
6314
- const values = new Float32Array(count * 4);
6315
6427
  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
- });
6428
+ result.set(
6429
+ upper,
6430
+ frames.map((frame, index) => {
6431
+ const pose = solvePose(model, frame.local, frame.hips);
6432
+ const parentRotation = pose.rotation.get(model.parent.get(upper));
6433
+ const original = frame.local.get(upper) ?? new Quaternion5();
6434
+ const world = parentRotation.clone().multiply(original);
6435
+ const corrected = parentRotation.clone().invert().multiply(rotationVectorToQuaternion(total[index]).multiply(world)).normalize();
6436
+ if (previous && previous.dot(corrected) < 0) {
6437
+ corrected.set(-corrected.x, -corrected.y, -corrected.z, -corrected.w);
6438
+ }
6439
+ previous = corrected;
6440
+ return corrected;
6441
+ })
6442
+ );
6443
+ }
6444
+ return result;
6445
+ };
6446
+ var applyHandClearance = function(clip, vrm, options = {}) {
6447
+ const model = bodyModelOf(vrm);
6448
+ if (!model || clip.duration <= 0) {
6449
+ return clip;
6450
+ }
6451
+ const sampled = sampleClipFrames(model, vrm, clip, options.fps ?? 30);
6452
+ if (!sampled) {
6453
+ return clip;
6454
+ }
6455
+ const { times, frames, nodeName } = sampled;
6456
+ const solved = solveHandClearance(model, frames, options);
6457
+ const replaced = /* @__PURE__ */ new Map();
6458
+ for (const [bone, rotations] of solved) {
6459
+ const node = nodeName(bone);
6460
+ if (!node) {
6461
+ continue;
6462
+ }
6331
6463
  replaced.set(
6332
- `${upperNode}.quaternion`,
6464
+ `${node}.quaternion`,
6333
6465
  new QuaternionKeyframeTrack2(
6334
- `${upperNode}.quaternion`,
6466
+ `${node}.quaternion`,
6335
6467
  times,
6336
- Array.from(values)
6468
+ rotations.flatMap((q) => [q.x, q.y, q.z, q.w])
6337
6469
  )
6338
6470
  );
6339
6471
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@npcify/web",
3
- "version": "0.7.1",
3
+ "version": "0.7.2",
4
4
  "description": "Framework-independent Npcify characters, live conversations and baked performances for the web",
5
5
  "type": "module",
6
6
  "license": "UNLICENSED",
@@ -34,9 +34,9 @@
34
34
  "@types/three": "^0.180.0",
35
35
  "tsup": "^8.5.0",
36
36
  "typescript": "^5.9.3",
37
+ "@npcify/vrm-renderer": "^0.1.0",
37
38
  "@npcify/shared": "^0.1.0",
38
- "@npcify/web-client": "^0.0.0",
39
- "@npcify/vrm-renderer": "^0.1.0"
39
+ "@npcify/web-client": "^0.0.0"
40
40
  },
41
41
  "repository": {
42
42
  "type": "git",