Skip to content

3D engine that AI agents drive

Fix skinned bounds weights, crowd rig skin checks and foot placement NaN (#2369)

Nguyen Xuan And54274bd9884
diff --git a/changes/2369.md b/changes/2369.md
new file mode 100644
index 00000000..f96c6fcf
--- /dev/null
+++ b/changes/2369.md
@@ -0,0 +1 @@
+- Skinned world bounds now divide vertex weights by their total as the renderer does, a crowd rig refuses out of range skin joints and zero weight totals, and a foot placement recovers after a frame with a NaN foot position (#2369, #2370, #2371).
diff --git a/src/animation-ik.js b/src/animation-ik.js
index 531a4ff5..8e771c6d 100644
--- a/src/animation-ik.js
+++ b/src/animation-ik.js
@@ -67,6 +67,7 @@ export function createFootPlacement(options = {}) {
let correction = 0;
function target(time, foot) {
+ if (!foot.every(Number.isFinite)) return [...foot]; // must not leave the eased correction NaN for good
const returned = groundOffset([foot[0], foot[1], foot[2]]);
const offset = lastOffset(returned, Number.isFinite(returned));
const clamped = Math.max(-maxAdjust, Math.min(maxAdjust, offset));
diff --git a/src/crowd-skinning.js b/src/crowd-skinning.js
index c5e75b92..eb6fd0ea 100644
--- a/src/crowd-skinning.js
+++ b/src/crowd-skinning.js
@@ -9,7 +9,7 @@
// the same frame reached by playing.
import { mat4 } from 'gl-matrix';
import { installCrowdShader } from './shaders.js';
-import './skinning.js';
+import { validateSkinGeometry } from './skinning.js';
const FLOATS_PER_MATRIX = 16;
const TEXELS_PER_MATRIX = 4;
@@ -54,6 +54,7 @@ export function createCrowdRig(sourceParts, { morphs = false } = {}) {
for (const part of sourceParts) {
if (!part.geometry?.positions || !part.geometry.indices || !part.material) throw new TypeError(`A crowd part needs a geometry and a material: ${part.name}`);
if (part.skin && !part.geometry.skinIndices) throw new TypeError(`A skinned crowd part needs skin indices and weights: ${part.name}`);
+ if (part.skin) validateCrowdSkin(part);
jointOffsets.push(jointCount);
jointCount += part.skin ? part.skin.jointMatrices.length / FLOATS_PER_MATRIX : 1;
}
@@ -102,6 +103,15 @@ export function createCrowdRig(sourceParts, { morphs = false } = {}) {
};
}
+// A joint index past the part's own joints would read another part's joint, and weights that total zero pack as NaN.
+function validateCrowdSkin(part) {
+ try {
+ validateSkinGeometry(part.geometry, part.skin);
+ } catch (error) {
+ throw new RangeError(`Crowd part ${part.name}: ${error.message}`);
+ }
+}
+
function mergeCrowdGeometry(entries) {
const vertexCount = entries.reduce((total, { part }) => total + part.geometry.positions.length / 3, 0);
const indexCount = entries.reduce((total, { part }) => total + part.geometry.indices.length, 0);
diff --git a/src/skinning.js b/src/skinning.js
index 20006746..7a9653e9 100644
--- a/src/skinning.js
+++ b/src/skinning.js
@@ -334,8 +334,11 @@ export function skinnedWorldBounds(geometry, pose, worldMatrix) {
let matrix = worldMatrix;
if (skinned) {
blended.fill(0);
+ // The renderer divides each vertex's weights by their total, so the bounds do the same.
+ let weightTotal = 0;
+ for (let slot = 0; slot < 4; slot++) weightTotal += skinWeights[vertex * 4 + slot];
for (let slot = 0; slot < 4; slot++) {
- const weight = skinWeights[vertex * 4 + slot];
+ const weight = skinWeights[vertex * 4 + slot] / weightTotal;
if (weight === 0) continue;
const jointOffset = skinIndices[vertex * 4 + slot] * 16;
for (let element = 0; element < 16; element++) blended[element] += weight * palette[jointOffset + element];
diff --git a/tests/ik-skin-hunt.test.js b/tests/ik-skin-hunt.test.js
new file mode 100644
index 00000000..c525753d
--- /dev/null
+++ b/tests/ik-skin-hunt.test.js
@@ -0,0 +1,47 @@
+import assert from 'node:assert/strict';
+import test from 'node:test';
+import { mat4 } from 'gl-matrix';
+import { createFootPlacement } from '../src/animation-graph.js';
+import { createCrowdRig } from '../src/crowd-skinning.js';
+import { createSkinPose, skinnedWorldBounds } from '../src/skinning.js';
+
+const footJoints = { root: 'hip', middle: 'knee', end: 'ankle', groundOffset: () => 0.1 };
+
+function triangleGeometry(skinIndices, skinWeights) {
+ return {
+ positions: new Float32Array([0, 0, 0, 1, 0, 0, 0, 1, 0]),
+ normals: new Float32Array([0, 0, 1, 0, 0, 1, 0, 0, 1]),
+ indices: new Uint32Array([0, 1, 2]),
+ skinIndices: Uint16Array.from(skinIndices),
+ skinWeights: Float32Array.from(skinWeights),
+ };
+}
+
+test('skinned bounds blend by normalized weights, as the renderer does, when the weights do not sum to one', () => {
+ const pose = createSkinPose(1);
+ pose.jointMatrices.set(mat4.fromTranslation(mat4.create(), [10, 0, 0]));
+ const geometry = triangleGeometry(Array(12).fill(0), [0.5, 0, 0, 0, 0.5, 0, 0, 0, 0.5, 0, 0, 0]);
+ const bounds = skinnedWorldBounds(geometry, pose, mat4.create());
+ assert.deepEqual(bounds.minimum, [10, 0, 0]);
+ assert.deepEqual(bounds.maximum, [11, 1, 0]);
+});
+
+test('a crowd rig refuses a skinned part whose joint index is past its joints, naming the part', () => {
+ const skin = createSkinPose(2);
+ const part = { name: 'coat', geometry: triangleGeometry(Array(12).fill(5), Array(12).fill(0.25)), matrix: mat4.create(), material: {}, skin };
+ assert.throws(() => createCrowdRig([part]), /coat.*joint/);
+});
+
+test('a crowd rig refuses a skinned vertex whose weights total zero instead of packing NaN', () => {
+ const skin = createSkinPose(2);
+ const part = { name: 'coat', geometry: triangleGeometry(Array(12).fill(0), Array(12).fill(0)), matrix: mat4.create(), material: {}, skin };
+ assert.throws(() => createCrowdRig([part]), /coat.*weight/);
+});
+
+test('a foot placement recovers after one frame whose foot position is not a number', () => {
+ const foot = createFootPlacement({ ...footJoints, ankleHeight: 0 });
+ foot.target(0, [0, 0, 0]);
+ foot.target(0.016, [0, Number.NaN, 0]);
+ const recovered = foot.target(0.032, [0, 0, 0]);
+ assert.ok(Number.isFinite(recovered[1]), `foot y was ${recovered[1]}`);
+});