Skip to content

armgit / 67.js

Public Imported from GitHub
0

3D engine that AI agents drive

Add retarget addon docs, wiring and crowd measurements

Nguyen Xuan Anabcd42b03fe6
diff --git a/.gitignore b/.gitignore
index 4278ef3c..6b80c54a 100644
--- a/.gitignore
+++ b/.gitignore
@@ -18,6 +18,7 @@ public/mesh-query.md
public/primitives.md
public/shrinkwrap.md
public/spume.md
+public/retarget.md
public/scene-graph.md
public/modeling.md
public/animation-curves.md
diff --git a/README.md b/README.md
index 1fef3b72..1eca958b 100644
--- a/README.md
+++ b/README.md
@@ -210,6 +210,7 @@ Compatible results can become relative GPU morph targets, so animation reuses th
The source [Spume addon](docs/spume.md) plays back liquid baked by the [Spume](https://github.com/codepawl/spume) GPU solver, with whitewater, through `renderer.replaceParts`.
The solver runs offline; the browser only reads the compact cache.
+The source [retarget addon](docs/retarget.md) transfers humanoid clips between rigs (Mixamo, VRM, Unreal, Rigify names) and offsets crowds deterministically. Mixamo files need the owner's Adobe login to download.
The current source adds [surface attachments](docs/surface-attachment.md) for rigid accessories following a selected triangle through morphing and skinning.
The retained frame uses world-space clearance and samples three bound vertices without rebuilding a BVH every frame.
diff --git a/docs/guide.md b/docs/guide.md
index 1a12525c..18d2b5bf 100644
--- a/docs/guide.md
+++ b/docs/guide.md
@@ -162,6 +162,17 @@ Collapsed output triangles cause errors. Projection does not guarantee clearance
The [surface patch contract](/surface.md) defines projector orientation, depth and local clearance.
Patches and shrinkwrap are static operations. They do not follow skin or morph deformation.
+## Humanoid retargeting and crowds
+
+`67js/addons/retarget` is in the current source and not yet published. It moves a humanoid clip onto another skeleton and offsets many copies in time.
+
+1. Build rigs with `createRigFromSkin(asset.skins[0])`. Name families (Mixamo, VRM, Unreal, Rigify, Quaternius) map through `mapHumanoidBones`; fix a miss with `targetOverrides`.
+2. Call `retargetClip(sourceRig, targetRig, clip, { rootMotion })`.
+3. Play it with `createSkinClipPlayer(skin, clip).apply(seconds)`, then `renderer.updateParts(parts)`.
+4. For a crowd, `createCrowd({ count, clip, seed })` gives placement, time offsets and playback rates. Each instance needs its own skin pose.
+
+The [retarget contract](/retarget.md) covers rest-pose assumptions and measured cost. Mixamo clips need the owner's Adobe login to download; the code only consumes files.
+
## Baked liquid
`67js/addons/spume` is in the current source and not yet published. It plays back liquid baked offline by the Spume GPU solver.
diff --git a/docs/retarget.md b/docs/retarget.md
new file mode 100644
index 00000000..db1c96f4
--- /dev/null
+++ b/docs/retarget.md
@@ -0,0 +1,104 @@
+# Humanoid retargeting and crowds
+
+`67js/addons/retarget` is in the current source and not yet published. It moves a humanoid clip from one skeleton onto another and
+places many skinned copies with their own timing. It only consumes files: Mixamo clips need the owner's Adobe login to download,
+and this package never fetches them.
+
+```js
+import { loadGltf } from '67js/addons/gltf';
+import { mapHumanoidBones, createRigFromSkin, retargetClip, createSkinClipPlayer, createCrowd } from '67js/addons/retarget';
+
+const source = createRigFromSkin(mixamoAsset.skins[0]);
+const target = createRigFromSkin(characterAsset.skins[0], { rootMatrix: armatureWorldMatrix });
+const clip = retargetClip(source, target, mixamoClip, { rootMotion: false });
+
+const player = createSkinClipPlayer(characterAsset.skins[0], clip);
+player.apply(seconds);
+renderer.updateParts(characterAsset.parts);
+```
+
+## Rigs
+
+A rig is `{ joints, rootMatrix?, mapping? }`. Each joint is `{ name, parent, translation, rotation, scale }` with the local rest pose,
+`parent` as an index (`-1` for a root) and quaternions in XYZW order.
+`createRigFromSkin(skin)` builds one from an imported skin: the bind pose becomes the rest pose.
+`rootMatrix` is the world matrix of whatever sits above the root joints, such as a scaled Armature node. Pass it when the clip's root
+tracks are authored relative to that node. Uniform scale is assumed throughout.
+
+## Bone names
+
+`mapHumanoidBones(jointNames, { overrides })` returns `{ mapping, unmapped, missing }`. Canonical names are in `HUMANOID_BONES`:
+`hips`, `spine`, `chest`, `upperChest`, `neck`, `head`, then per side `leftShoulder`, `leftUpperArm`, `leftLowerArm`, `leftHand`,
+`leftUpperLeg`, `leftLowerLeg`, `leftFoot`, `leftToes`, and optional fingers such as `leftIndexProximal`.
+`REQUIRED_HUMANOID_BONES` (hips, spine, head, both arms with hands, both legs with feet) must all be mapped.
+
+| Family | Examples |
+| --- | --- |
+| Mixamo | `mixamorig:Hips`, `mixamorig1:LeftUpLeg`, `mixamorigLeftForeArm`; `Spine1`/`Spine2` are chest and upper chest |
+| VRM and Unity humanoid | `leftUpperArm`, `upperChest`, `leftToes`, `J_Bip_L_UpperArm` |
+| Unreal mannequin | `pelvis`, `spine_01`..`spine_03`, `clavicle_l`, `upperarm_l`, `thigh_l`, `calf_l`, `ball_l` |
+| Blender and Rigify | `DEF-spine` is the pelvis, `DEF-upper_arm.L`, `DEF-thigh.L`, `DEF-shin.L`; twist bones (`.001`) stay unmapped |
+| Quaternius | `Abdomen`, `Torso`, `UpperArm.L`, `Fist.L`, `Toes.L` |
+
+Left and right come from `Left`/`Right`, `L`/`R` words or `.L`/`_l` suffixes. The first joint that matches a bone wins, so a second
+neck or spine joint stays unmapped. When a bone is missing or wrong, pass `sourceOverrides` or `targetOverrides` to `retargetClip`
+(or `overrides` to `mapHumanoidBones`) as `{ leftLowerLeg: 'shinbone_left' }`. A rig that misses required bones throws a `RangeError`
+whose `missingBones` array and message list them.
+
+## Retargeting
+
+`retargetClip(sourceRig, targetRig, clip, options)` returns a new `createAnimationClip` clip with one rotation track per shared bone,
+named after the target joints, and a hips translation track. Any code that plays clips by joint name can play it.
+
+For every bone, the world-space rotation change of the source from its rest pose is applied to the target's rest pose. The target's
+local rotation is then taken relative to its own animated parent. T-pose against A-pose, and different local bone axes, therefore end up
+pointing the same way. Unmapped target joints keep their rest pose. The hips move by the source's world offset from its rest position,
+scaled by the ratio of rest hip heights above the feet (hips to foot along Y, averaged over both legs).
+
+| Option | Default | Meaning |
+| --- | --- | --- |
+| `bones` | every shared bone | Canonical bones to transfer |
+| `rootMotion` | `true` | `false` removes horizontal (X and Z) hips travel and keeps vertical bob |
+| `scale` | `1` | Extra multiplier on the hips translation |
+| `frameRate` | `30` | Resampling rate; output keyframes are linear |
+
+Assumptions and limits:
+
+- Both rigs face the same direction, with +Y up. A source that faces the other way needs its root rotated first.
+- The clip is resampled at `frameRate`, so cubic-spline and step curves are baked to linear keys.
+- Source scale and non-hips translation tracks are ignored. Finger and toe bones transfer only when mapped in both rigs.
+- The source and target rest pose must be the bind pose (the node pose equals the inverse bind pose). A file that animates away from it is not detected.
+- No foot-lock, no IK, no retargeting of rotation limits. Different proportions can make feet slide or arms intersect the body.
+
+`createSkinClipPlayer(skin, clip, { rig, meshWorldMatrix })` applies a clip to a skin: `apply(seconds)` samples the clip, composes joint
+world matrices and calls `updateSkinPose`. The renderer still needs `updateParts` afterwards.
+
+## Crowds
+
+`createCrowd({ count, clip, seed, layout, spacing, columns, size, rateJitter, randomYaw })` is deterministic for a seed. `layout` is
+`'grid'` (centred, `columns` wide) or `'scatter'` (rejection sampling with at least `0.75 * spacing` between instances where room allows).
+Each instance has `position`, `yawRadians`, a `timeOffset` in the clip duration, a `playbackRate` within `1 ± rateJitter` and a
+column-major `matrix`. `clipTime(index, seconds)` gives the instance's clip time and `sample(seconds)` returns every instance's tracks
+(buffers are reused on the next call).
+
+The renderer batches skinned parts by pose identity, so each instance needs its own skin pose (its own `createSkinPose` palette, one
+imported asset per instance) and becomes its own batch. Instances cannot share a pose and still differ in time.
+
+### Measured cost
+
+Headless Chrome with GPU on the author's machine (RTX 3060, 960x540, 4x MSAA, one measurement of 60 frames after 10 warm-up frames).
+Each instance had 3,000 skinned vertices, 65 joints and its own pose. It was a synthetic bending chain, not a humanoid, so a real
+character mesh with far more vertices will cost more in the render column. Pose is clip sampling plus joint matrices in JavaScript,
+upload is `renderer.updateParts`, render includes `gl.finish()`.
+
+| Instances | Batches | Pose ms | Upload ms | Render ms | Frame ms |
+| --- | --- | --- | --- | --- | --- |
+| 1 | 2 | 0.13 | 0.21 | 0.29 | 0.63 |
+| 10 | 11 | 0.81 | 1.71 | 0.40 | 2.92 |
+| 50 | 51 | 3.87 | 8.25 | 1.02 | 13.14 |
+| 100 | 101 | 7.85 | 17.04 | 1.63 | 26.52 |
+
+Cost grows linearly at about 0.26 ms per animated instance per frame, mostly from the per-pose palette upload and the JavaScript pose
+work, both on the main thread. At this size roughly 50 animated instances fit a 60 fps budget (16.7 ms) and 100 do not. These are
+single measurements, not a benchmark suite. Fewer animated instances help: poses can be updated at a lower rate or shared by distant
+instances.
diff --git a/package.json b/package.json
index 6b3e12a7..f889e597 100644
--- a/package.json
+++ b/package.json
@@ -76,6 +76,7 @@
"./addons/surface-attachment": "./build/surface-attachment.js",
"./addons/physics": "./build/physics.js",
"./addons/spume": "./build/spume.js",
+ "./addons/retarget": "./build/retarget.js",
"./addons/agent-scene": "./build/agent-scene.js",
"./addons/agent-asset": "./build/agent-asset.js",
"./package.json": "./package.json"
diff --git a/scripts/prepare-release.mjs b/scripts/prepare-release.mjs
index 2bf49817..fd676167 100644
--- a/scripts/prepare-release.mjs
+++ b/scripts/prepare-release.mjs
@@ -40,6 +40,7 @@ const sourceDocuments = [
'scene-graph.md', 'modeling.md', 'animation-curves.md', 'animation-mixer.md',
'gltf-morph.md', 'morph-targets.md', 'skinning.md', 'physics.md',
'export-glb.md', 'sculpt.md', 'surface.md', 'mesh-query.md', 'primitives.md', 'shrinkwrap.md', 'spume.md',
+ 'retarget.md',
'camera-path.md', 'lighting.md', 'volumetric.md', 'film-physics.md',
'surface-attachment.md', 'agent-scene.md', 'mcp.md', 'agent-look.md', 'agent-asset.md', 'agent-render-jobs.md', 'agent-motion.md', 'agent-character-motion.md', 'agent-review.md',
];
diff --git a/skills/build-67js-scene/SKILL.md b/skills/build-67js-scene/SKILL.md
index 53f585d1..b276e4ce 100644
--- a/skills/build-67js-scene/SKILL.md
+++ b/skills/build-67js-scene/SKILL.md
@@ -22,6 +22,7 @@ Use `simplify-code` and `engineering-workflows` for implementation.
- A skeletal asset uses `src/skinning.js`, `src/gltf.js` and the checkout's `docs/skinning.md`.
- Relative facial or shape deformation uses `src/morph.js` and the checkout's `docs/morph-targets.md`. Check source versus published support first.
- Recorded contacts or articulated physics use optional `src/physics.js` and the checkout's `docs/physics.md`.
+- Humanoid clips moved between rigs, or crowds of skinned characters, use optional `src/retarget.js` and the checkout's `docs/retarget.md`. Mixamo downloads need the owner's Adobe login.
- Baked liquid (jets, geysers, torrents, whitewater) uses optional `src/spume.js` and the checkout's `docs/spume.md`. Bake with the Spume solver first; never fake it with displaced primitives.
- A flat scene or axis view uses `src/canvas2d.js`, `src/camera.js` and `src/view2d.js`.
- A GLB asset viewer uses the optional `src/gltf.js` addon and `src/asset-viewer.js`.
diff --git a/vite.lib.config.js b/vite.lib.config.js
index b90d09e4..c4dd14cb 100644
--- a/vite.lib.config.js
+++ b/vite.lib.config.js
@@ -26,6 +26,7 @@ export default defineConfig({
'surface-attachment': 'src/surface-attachment.js',
physics: 'src/physics.js',
spume: 'src/spume.js',
+ retarget: 'src/retarget.js',
'agent-scene': 'src/agent-scene.js',
'agent-asset': 'src/agent-asset.js',
},