cozyclay 1.9.0 → 2.0.0

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 (286) hide show
  1. package/CHANGELOG.md +171 -0
  2. package/bin/agent/agent-routes.mjs +74 -70
  3. package/bin/agent/agent-runner.mjs +10 -3
  4. package/bin/agent/motion-runtime.mjs +32 -260
  5. package/bin/agent/providers.mjs +46 -3
  6. package/bin/agent/session-store.mjs +41 -5
  7. package/bin/agent/studio-prompt.mjs +1 -1
  8. package/bin/agent/studio-tools.mjs +48 -11
  9. package/bin/cozyclay.mjs +26 -8
  10. package/bin/live/cli.mjs +22 -3
  11. package/bin/mcp-runtime.mjs +35 -20
  12. package/bin/telemetry-state.mjs +5 -2
  13. package/bin/update-check.mjs +15 -0
  14. package/dist/app/index.html +6 -6
  15. package/dist/assets/analytics-MVZOEW8O.js +1 -0
  16. package/dist/assets/app-C1sdVbg4.css +1 -0
  17. package/dist/assets/app-CA55tcI3.js +4876 -0
  18. package/dist/assets/{demo-B-eg1CT8.js → demo-ByeYp-dY.js} +1 -1
  19. package/dist/assets/{first-shot-handoff-CGgnvNu8.js → first-shot-handoff-DgT28ZiV.js} +1 -1
  20. package/dist/assets/{landing-IbRp3FnQ.js → landing-BaXkpFip.js} +1 -1
  21. package/dist/assets/shot-prompt-CbqWcWYi.css +1 -0
  22. package/dist/assets/shot-prompt-Yrt99wZx.js +17 -0
  23. package/dist/assets/{ticket-BgIzOBOf.js → ticket-BcmsnHn5.js} +1 -1
  24. package/dist/assets/workflow-DRdwoDBT.css +1 -0
  25. package/dist/assets/workflow-dNPGY9xz.js +179 -0
  26. package/dist/cozyclay-package.json +1 -1
  27. package/dist/fonts/IBM-Plex-Sans-OFL.txt +92 -0
  28. package/dist/fonts/JetBrains-Mono-OFL.txt +93 -0
  29. package/dist/fonts/README.md +13 -9
  30. package/dist/fonts/ibm-plex-sans-400-latin.woff2 +0 -0
  31. package/dist/fonts/ibm-plex-sans-500-latin.woff2 +0 -0
  32. package/dist/fonts/ibm-plex-sans-600-latin.woff2 +0 -0
  33. package/dist/fonts/jetbrains-mono-400-latin.woff2 +0 -0
  34. package/dist/fonts/jetbrains-mono-500-latin.woff2 +0 -0
  35. package/dist/index.html +7 -7
  36. package/dist/privacy/index.html +13 -8
  37. package/dist/sitemap.xml +6 -6
  38. package/dist/workflow/index.html +5 -5
  39. package/mcp/LIVE-PROTOCOL.md +1 -1
  40. package/mcp/live-hub.mjs +35 -93
  41. package/mcp/mesh-file.mjs +48 -0
  42. package/mcp/server.mjs +6 -77
  43. package/mcp/tool-handlers.mjs +224 -377
  44. package/package.json +2 -1
  45. package/src/App.jsx +1273 -9075
  46. package/src/analytics.js +393 -9
  47. package/src/app-context.js +211 -0
  48. package/src/app-stage.jsx +25 -24
  49. package/src/ardy/auto-fix-panel.css +108 -0
  50. package/src/ardy/collision-blockers.js +9 -3
  51. package/src/ardy/fix-collisions.js +10 -66
  52. package/src/ardy/ground.js +4 -4
  53. package/src/ardy/ik-drag.js +101 -0
  54. package/src/ardy/ik-key-json.js +43 -0
  55. package/src/ardy/ik.js +122 -76
  56. package/src/ardy/physics-panel.css +2 -0
  57. package/src/ardy/physics-panel.jsx +17 -5
  58. package/src/ardy/platform-fit-panel.css +64 -0
  59. package/src/ardy/platform-fit-panel.jsx +40 -0
  60. package/src/ardy/platform-fit.js +326 -0
  61. package/src/ardy/playback.js +166 -20
  62. package/src/ardy/range-pin.js +299 -0
  63. package/src/ardy/timeline-coordinates.js +26 -0
  64. package/src/ardy/timeline.css +1199 -0
  65. package/src/ardy/timeline.jsx +149 -41
  66. package/src/ardy/waypoints.js +2 -2
  67. package/src/asset-pane.css +462 -0
  68. package/src/asset-pane.jsx +461 -232
  69. package/src/command-bus.js +349 -0
  70. package/src/commands/ai.js +31 -0
  71. package/src/commands/cast.js +140 -0
  72. package/src/commands/elements/character.js +13 -0
  73. package/src/commands/elements/motion.js +14 -0
  74. package/src/commands/elements/object.js +22 -0
  75. package/src/commands/elements/scene.js +16 -0
  76. package/src/commands/elements/shot.js +9 -0
  77. package/src/commands/elements/stage.js +11 -0
  78. package/src/commands/elements.js +155 -0
  79. package/src/commands/export.js +20 -0
  80. package/src/commands/index.js +40 -0
  81. package/src/commands/motion.js +231 -0
  82. package/src/commands/objects.js +174 -0
  83. package/src/commands/project.js +53 -0
  84. package/src/commands/scene.js +81 -0
  85. package/src/commands/shared.js +19 -0
  86. package/src/commands/shot.js +103 -0
  87. package/src/commands/stage.js +17 -0
  88. package/src/commands/view.js +42 -0
  89. package/src/document-store.js +144 -0
  90. package/src/domains/cast.js +1007 -0
  91. package/src/domains/motion.js +3219 -0
  92. package/src/domains/objects.js +932 -0
  93. package/src/domains/scenes.js +817 -0
  94. package/src/domains/shots.js +404 -0
  95. package/src/domains/stage.js +93 -0
  96. package/src/dualview.jsx +113 -57
  97. package/src/facing-marks.js +3 -0
  98. package/src/first-success-guide.jsx +14 -14
  99. package/src/grid-view.js +18 -5
  100. package/src/hierarchy-model.js +24 -16
  101. package/src/hierarchy-panel.css +386 -0
  102. package/src/hierarchy-panel.jsx +150 -21
  103. package/src/{fal-motion-client.js → i2v-motion-client.js} +13 -13
  104. package/src/i2v-motion-studio.jsx +180 -0
  105. package/src/ik-camera.js +3 -0
  106. package/src/main.jsx +6 -0
  107. package/src/motion/generation.js +105 -0
  108. package/src/motion-readiness-ui.jsx +4 -4
  109. package/src/motion-readiness.js +11 -0
  110. package/src/motion-trail.js +366 -9
  111. package/src/object-gizmo.jsx +7 -1
  112. package/src/otio.js +2 -1
  113. package/src/panels/CameraPanel.jsx +60 -0
  114. package/src/panels/CharacterTransformPanel.jsx +48 -0
  115. package/src/panels/EnvironmentPanel.jsx +40 -0
  116. package/src/panels/Foldout.jsx +32 -0
  117. package/src/panels/LightPanel.jsx +24 -0
  118. package/src/panels/ObjectTransformPanel.jsx +554 -0
  119. package/src/panels/PosePanel.jsx +97 -0
  120. package/src/panels/ProjectPanel.jsx +33 -0
  121. package/src/panels/PromptBlocksPanel.jsx +442 -0
  122. package/src/panels/PropsPanel.jsx +70 -0
  123. package/src/panels/ReferenceImageField.jsx +91 -0
  124. package/src/panels/RigControlPanel.jsx +78 -0
  125. package/src/panels/RigPanel.jsx +37 -0
  126. package/src/panels/SubjectBox.jsx +47 -0
  127. package/src/panels/SubjectsPanel.jsx +39 -0
  128. package/src/panels/VideoCapturePanel.jsx +180 -0
  129. package/src/panels/details.css +1171 -0
  130. package/src/panels/motion.css +197 -0
  131. package/src/panels/pose.css +39 -0
  132. package/src/planview.jsx +60 -31
  133. package/src/posestudio.jsx +59 -4
  134. package/src/project-browser.css +1073 -0
  135. package/src/project-browser.jsx +284 -123
  136. package/src/range-pin-object-transform.js +19 -0
  137. package/src/range-pin-panel.css +518 -0
  138. package/src/range-pin-panel.jsx +344 -0
  139. package/src/result-modal.jsx +5 -5
  140. package/src/room.jsx +79 -51
  141. package/src/scene-objects.js +37 -1
  142. package/src/scenes.js +5 -0
  143. package/src/semantic-edit.js +2 -0
  144. package/src/settings-menu.jsx +39 -105
  145. package/src/shell/BottomDock.jsx +450 -0
  146. package/src/shell/DetailsSlot.jsx +513 -0
  147. package/src/shell/LibrarySlot.jsx +86 -0
  148. package/src/shell/MenuBar.jsx +601 -0
  149. package/src/shell/OutlinerSlot.jsx +50 -0
  150. package/src/shell/PreferencesDialog.jsx +538 -0
  151. package/src/shell/PreferencesSlot.jsx +40 -0
  152. package/src/shell/StatusBar.jsx +125 -0
  153. package/src/shell/StudioShell.jsx +92 -0
  154. package/src/shell/TopBar.jsx +129 -0
  155. package/src/shell/ViewportToolbar.jsx +656 -0
  156. package/src/shell/agent-glass.css +296 -0
  157. package/src/shell/dock.css +93 -0
  158. package/src/shell/glass-regions.css +985 -0
  159. package/src/shell/glass.css +687 -0
  160. package/src/shell/log-store.js +52 -0
  161. package/src/shell/mode.css +9 -0
  162. package/src/shell/preferences.css +651 -0
  163. package/src/shell/shell.css +282 -0
  164. package/src/shell/studio-shell-context.js +19 -0
  165. package/src/shell/topbar.css +435 -0
  166. package/src/shell/viewport.css +451 -0
  167. package/src/store/authored-intent.js +22 -0
  168. package/src/store/runtime-adapters.js +92 -0
  169. package/src/store/scene-stage.js +21 -0
  170. package/src/store/use-document-store.js +14 -0
  171. package/src/studio-actions.js +210 -0
  172. package/src/studio-agent-commands.js +41 -271
  173. package/src/studio-agent-context.js +39 -10
  174. package/src/studio-agent-motion.js +122 -373
  175. package/src/studio-agent-protocol.js +67 -31
  176. package/src/studio-app-binding.js +379 -0
  177. package/src/studio-contact-sheet.js +73 -0
  178. package/src/studio-elements.js +58 -51
  179. package/src/styles/themes.css +200 -0
  180. package/src/styles/tokens.css +69 -0
  181. package/src/styles.css +460 -1742
  182. package/src/theme.js +44 -0
  183. package/src/timeline-extent.js +16 -0
  184. package/src/trail-key-conflicts.js +39 -0
  185. package/src/trail-pick.js +59 -0
  186. package/src/ui.jsx +8 -8
  187. package/src/use-case-question.jsx +66 -0
  188. package/src/workflow/AgentPanel.jsx +82 -69
  189. package/src/workflow/agent-client.js +12 -2
  190. package/src/workflow/agent-panel.css +34 -32
  191. package/src/workflow/cozy-scene-node.css +2 -0
  192. package/src/workflow/workflow.css +2 -0
  193. package/tools/ardy/__pycache__/cclay_gvhmr_worker.cpython-313.pyc +0 -0
  194. package/tools/ardy/bridge.mjs +164 -146
  195. package/tools/ardy/visual-qa.mjs +5 -5
  196. package/tools/bench/EXP3.md +61 -0
  197. package/tools/bench/cclay_bench_extract_incam.py +125 -0
  198. package/tools/bench/cclay_bench_extract_obs.py +204 -0
  199. package/tools/bench/cclay_bench_runner.py +42 -0
  200. package/tools/bench/cube-contact.mjs +162 -0
  201. package/tools/bench/exp3.mjs +152 -0
  202. package/tools/bench/extract-bench-lib.mjs +216 -0
  203. package/tools/bench/extract-bench.mjs +302 -0
  204. package/tools/bench/fal-generate.mjs +88 -0
  205. package/tools/bench/fit/README.md +189 -0
  206. package/tools/bench/fit/camera.mjs +59 -0
  207. package/tools/bench/fit/contact.mjs +419 -0
  208. package/tools/bench/fit/footlock.mjs +234 -0
  209. package/tools/bench/fit/motion.mjs +85 -0
  210. package/tools/bench/fit/pin.mjs +27 -0
  211. package/tools/bench/fit/remote.mjs +105 -0
  212. package/tools/bench/fit-bench.mjs +126 -0
  213. package/tools/bench/fit-sanity.mjs +45 -0
  214. package/tools/bench/metrics.mjs +313 -0
  215. package/tools/bench/obs/depth.mjs +258 -0
  216. package/tools/bench/obs/extrinsics.mjs +175 -0
  217. package/tools/bench/obs/fit_mannequin_betas.py +340 -0
  218. package/tools/bench/obs/ground.mjs +294 -0
  219. package/tools/bench/obs/heading.mjs +151 -0
  220. package/tools/bench/obs/ladder.mjs +555 -0
  221. package/tools/bench/obs/mannequin-betas.json +124 -0
  222. package/tools/bench/obs/remote.mjs +83 -0
  223. package/tools/bench/obs/rest_joints.py +52 -0
  224. package/tools/bench/obs/ybot-targets.mjs +49 -0
  225. package/tools/bench/obs-bench.mjs +520 -0
  226. package/tools/bench/score.mjs +343 -0
  227. package/tools/bench/summarize.mjs +86 -0
  228. package/tools/dev/pages/privacy.html +13 -8
  229. package/tools/gt-render/browser.mjs +159 -0
  230. package/tools/gt-render/camera-math.mjs +276 -0
  231. package/tools/gt-render/page.mjs +267 -0
  232. package/tools/gt-render/render.mjs +514 -0
  233. package/tools/gt-render/scene-box.mjs +31 -0
  234. package/tools/gt-render/take-transform.mjs +87 -0
  235. package/tools/morphgs/assets/gen_truth.py +39 -0
  236. package/tools/morphgs/assets/mesh_ori_rig.txt +27 -0
  237. package/tools/morphgs/assets/playback-check.mjs +30 -0
  238. package/tools/morphgs/demo-gate.sh +26 -0
  239. package/tools/morphgs/fbx2morphgs.mjs +68 -0
  240. package/tools/morphgs/morphgs-to-cskel27.mjs +105 -0
  241. package/tools/morphgs/patches/preprocess_src-none-mode.patch +76 -0
  242. package/tools/morphgs/setup-on-cluster.sh +126 -0
  243. package/tools/qa/css-rule-usage.mjs +283 -0
  244. package/tools/qa/studio-control-count.mjs +70 -32
  245. package/tools/run-tests.mjs +134 -12
  246. package/tools/track/DESIGN.md +513 -0
  247. package/tools/track/backfill-provenance.mjs +116 -0
  248. package/tools/track/budget.mjs +8 -0
  249. package/tools/track/check-rig.mjs +209 -0
  250. package/tools/track/diagnostics.schema.json +60 -0
  251. package/tools/track/export-rig.mjs +256 -0
  252. package/tools/track/fallback.mjs +7 -0
  253. package/tools/track/fk-parity-fixture.mjs +161 -0
  254. package/tools/track/gate.mjs +349 -0
  255. package/tools/track/masks.mjs +318 -0
  256. package/tools/track/metrics.mjs +201 -0
  257. package/tools/track/publish-obs.mjs +108 -0
  258. package/tools/track/py/check_env.py +61 -0
  259. package/tools/track/py/eval_lr.py +229 -0
  260. package/tools/track/py/lr_viterbi.py +294 -0
  261. package/tools/track/py/masks.py +405 -0
  262. package/tools/track/py/objective.py +382 -0
  263. package/tools/track/py/rig.py +508 -0
  264. package/tools/track/py/scene.py +326 -0
  265. package/tools/track/py/test_joint_indices.py +123 -0
  266. package/tools/track/py/test_lr_viterbi.py +195 -0
  267. package/tools/track/py/test_masks.py +229 -0
  268. package/tools/track/py/test_rig.py +288 -0
  269. package/tools/track/py/test_scene.py +354 -0
  270. package/tools/track/py/test_track.py +328 -0
  271. package/tools/track/py/track.py +411 -0
  272. package/tools/track/remote.mjs +140 -0
  273. package/tools/track/rig-dump.mjs +227 -0
  274. package/tools/track/run-box-tests.mjs +14 -0
  275. package/tools/track/run-box.mjs +168 -0
  276. package/tools/track/setup-box.sh +59 -0
  277. package/tools/track/study-2d.mjs +728 -0
  278. package/dist/assets/analytics-B1hnH66c.js +0 -1
  279. package/dist/assets/app-BWusbqgO.js +0 -4861
  280. package/dist/assets/app-qKDo4PBX.css +0 -1
  281. package/dist/assets/shot-prompt-CHUtw6af.js +0 -17
  282. package/dist/assets/shot-prompt-C_g2BHVc.css +0 -1
  283. package/dist/assets/workflow-CVwzMhz2.js +0 -179
  284. package/dist/assets/workflow-DsudxKHF.css +0 -1
  285. package/src/fal-motion-studio.jsx +0 -180
  286. package/src/scene-history.js +0 -129
@@ -0,0 +1,40 @@
1
+ import React, { useEffect, useSyncExternalStore } from "react";
2
+ import "../panels/pose.css";
3
+
4
+ const FOOT_NAMES = { leftFoot: ["Left foot", "왼발"], rightFoot: ["Right foot", "오른발"] };
5
+ function stepLabel(step, ko) {
6
+ if (step.status === "wall") return `${ko("Wall", "벽")} · F${step.start}`;
7
+ const [en, kr] = FOOT_NAMES[step.foot] ?? [step.foot, step.foot];
8
+ const amount = step.lift !== 0 ? step.lift : step.rise;
9
+ return `${ko(en, kr)} F${step.start}–F${step.end} · ${amount >= 0 ? "+" : ""}${Math.round(amount * 100)}cm`;
10
+ }
11
+
12
+ export function PlatformFitPanel({ ko, disabled, running, progress: progressStore, last, applied, onRun, onApply, onCancel, onRemove, onFrame }) {
13
+ const progress = useSyncExternalStore(progressStore.subscribe, progressStore.getSnapshot);
14
+ const preview = Boolean(last && !applied && last.candidate);
15
+ const steps = last?.steps?.filter((step) => step.lift !== 0 || step.status !== "ok") ?? [];
16
+ useEffect(() => {
17
+ if (!preview) return undefined;
18
+ const onKeyDown = (event) => {
19
+ if (event.metaKey || event.ctrlKey || event.altKey) return;
20
+ if (event.key === "Escape") { event.preventDefault(); onCancel?.(); }
21
+ if (event.key === "Enter") { event.preventDefault(); onApply?.(); }
22
+ };
23
+ window.addEventListener("keydown", onKeyDown);
24
+ return () => window.removeEventListener("keydown", onKeyDown);
25
+ }, [preview, onApply, onCancel]);
26
+ return <section className="platform-fit auto-fix-card pose-section" data-testid="platform-fit-panel" aria-label={ko("Fit to platforms", "발판에 맞추기")}>
27
+ <div className="auto-fix-card-head"><h4>{ko("Fit to platforms", "발판에 맞추기")}</h4><p className="inspector-hint">{ko("Raise feet and body to platforms in the path.", "경로의 발판에 맞춰 발과 몸을 올립니다.")}</p></div>
28
+ <button type="button" data-testid="platform-fit-run" className="btn full primary" disabled={disabled || running || preview} onClick={onRun}>{running ? ko(`Fitting ${progress}%`, `맞추는 중 ${progress}%`) : ko("Run", "실행")}</button>
29
+ {running && <progress max="100" value={progress} aria-label={ko("Platform fit progress", "발판 맞춤 진행률")} />}
30
+ {preview && <div className="pose-preview" data-testid="platform-fit-preview">
31
+ <strong className="pose-preview-title">{ko("Preview", "미리보기")}</strong>
32
+ <span className="pose-preview-summary">{ko(`${last.summary.lifted} steps lifted · ${steps.length} result${steps.length === 1 ? "" : "s"}`, `${last.summary.lifted}개 단계 상승 · 결과 ${steps.length}개`)}</span>
33
+ <ul className="pose-results">{steps.slice(0, 5).map((step) => <li key={step.id} data-status={step.status}><button type="button" className="btn ghost small" onClick={() => onFrame(step.start)}>{stepLabel(step, ko)}</button></li>)}</ul>
34
+ <div className="pose-actions"><button data-testid="platform-fit-cancel" type="button" className="btn" onClick={onCancel}>{ko("Cancel", "취소")}</button><button data-testid="platform-fit-apply" type="button" className="btn primary" disabled={!last.changedFrames?.length || steps.some((step) => step.status === "wall" && step.lift === 0)} onClick={onApply}>{ko("Apply", "적용")}</button></div>
35
+ <span className="pose-keyhint">{ko("Enter applies · Esc cancels", "Enter 적용 · Esc 취소")}</span>
36
+ </div>}
37
+ {applied && <button type="button" data-testid="platform-fit-remove" className="btn full" disabled={disabled || running} onClick={onRemove}>{ko("Remove platform fit", "발판 맞춤 제거")}</button>}
38
+ {last && !preview && <div data-testid="platform-fit-results" className="auto-fix-result"><ul className="platform-fit-results">{steps.slice(0, 5).map((step) => <li key={step.id}><button type="button" className="btn platform-fit-step" data-status={step.status} onClick={() => onFrame(step.start)}>{stepLabel(step, ko)}</button></li>)}</ul>{steps.length === 0 && <p className="inspector-hint">{ko("No platforms in the path.", "경로에 발판이 없어요.")}</p>}</div>}
39
+ </section>;
40
+ }
@@ -0,0 +1,326 @@
1
+ import * as THREE from "three";
2
+ import { findBone, ikEvaluate, solveIk, solveHipsTranslate } from "./ik.js";
3
+ import { createGroundSampler } from "./ground.js";
4
+ import { objectFootprintBounds } from "../scene-objects.js";
5
+ import { copyPhysicsKeys, createSupportSampler, physicsKeyStamp } from "./physics-review.js";
6
+
7
+ /**
8
+ * Platform fit: lift a flat-ground walk onto the boxes/steps placed in the
9
+ * scene. Each planted-foot contact is measured against the surface under its
10
+ * anchor; climbable rises lift that foot (and, smoothed, the pelvis), too-high
11
+ * rises and walls are reported and left alone. The source key map is never
12
+ * modified; the candidate uses the physics-review key format.
13
+ *
14
+ * stepUp/climb are fractions of the measured hip-to-ankle leg length,
15
+ * clearance is metres, reach is the max fraction of leg length a solved leg
16
+ * may extend (or the source extension, whichever is larger).
17
+ */
18
+ export const PLATFORM_FIT_LIMITS = Object.freeze({ stepUp: 0.5, climb: 1.0, clearance: 0.03, reach: 0.995 });
19
+
20
+ const FEET = [
21
+ { id: "leftFoot", toe: "mixamorigLeftToeBase" },
22
+ { id: "rightFoot", toe: "mixamorigRightToeBase" },
23
+ ];
24
+ // A contact within this of its surface is the clip's own contact offset, not a
25
+ // platform. Keeps a flat walk on a flat floor (and a re-run on its own output)
26
+ // from being re-keyed for millimetres.
27
+ const LEVEL_TOLERANCE = 0.02;
28
+ const EPS = 1e-6;
29
+ const V = () => new THREE.Vector3();
30
+ const Q = () => new THREE.Quaternion();
31
+ const clamp = THREE.MathUtils.clamp;
32
+ const smooth = (x) => { x = clamp(x, 0, 1); return x * x * (3 - 2 * x); };
33
+ const quantile = (a, t) => { const s = a.filter(Number.isFinite).sort((x, y) => x - y); return s[Math.floor((s.length - 1) * t)] ?? 0; };
34
+
35
+ /** Edge-renormalised Gaussian, truncated at `radius` frames. */
36
+ function gaussian(values, sigma, radius = Math.ceil(3 * sigma)) {
37
+ if (!(sigma > 0)) return [...values];
38
+ return values.map((_, f) => {
39
+ let sum = 0, weight = 0;
40
+ for (let i = Math.max(0, f - radius); i <= Math.min(values.length - 1, f + radius); i += 1) {
41
+ const k = Math.exp(-0.5 * ((i - f) / sigma) ** 2);
42
+ sum += k * values[i]; weight += k;
43
+ }
44
+ return sum / weight;
45
+ });
46
+ }
47
+ function maxFilter(values, radius) {
48
+ return values.map((_, f) => {
49
+ let best = -Infinity;
50
+ for (let i = Math.max(0, f - radius); i <= Math.min(values.length - 1, f + radius); i += 1) best = Math.max(best, values[i]);
51
+ return best;
52
+ });
53
+ }
54
+ function minFilter(values, radius) {
55
+ return values.map((_, f) => {
56
+ let best = Infinity;
57
+ for (let i = Math.max(0, f - radius); i <= Math.min(values.length - 1, f + radius); i += 1) best = Math.min(best, values[i]);
58
+ return best;
59
+ });
60
+ }
61
+ /** Smooth upper envelope: max-filter then a Gaussian truncated at the same
62
+ * radius, so every output frame is >= the raw requirement at that frame. */
63
+ function envelope(values, sigma) {
64
+ const radius = Math.max(1, Math.ceil(2.5 * sigma));
65
+ return gaussian(maxFilter(values, radius), sigma, radius);
66
+ }
67
+
68
+ /** Per-frame lift from span lifts: exact inside spans, smoothstep between,
69
+ * held before the first and after the last. */
70
+ function spanTrack(count, spans) {
71
+ const out = new Array(count).fill(0);
72
+ if (!spans.length) return out;
73
+ for (let f = 0; f < count; f += 1) {
74
+ const next = spans.find((s) => s.end >= f);
75
+ if (!next) { out[f] = spans[spans.length - 1].lift; continue; }
76
+ if (f >= next.start) { out[f] = next.lift; continue; }
77
+ const prev = [...spans].reverse().find((s) => s.end < f);
78
+ out[f] = prev ? THREE.MathUtils.lerp(prev.lift, next.lift, smooth((f - prev.end) / (next.start - prev.end))) : next.lift;
79
+ }
80
+ return out;
81
+ }
82
+
83
+ function snapshot(chains, hips) {
84
+ return { hips: { p: hips.position.clone(), q: hips.quaternion.clone() },
85
+ chains: Object.fromEntries(FEET.map(({ id }) => [id, chains.get(id).bones.map((b) => ({ p: b.position.clone(), q: b.quaternion.clone() }))])) };
86
+ }
87
+
88
+ /** Planted spans: stationary toe (XZ), at least ~0.1 s, sole no more than a
89
+ * step above the ground below it. Unlike supportIntervals this is not measured
90
+ * against one global floor level, so a foot already standing on a box (a
91
+ * re-run over a previous fit) is still a contact. Deliberately no vertical
92
+ * speed test: a fit only moves feet vertically, so XZ stillness is the signal
93
+ * that detects the SAME spans on the fitted output, which is what makes a
94
+ * re-run idempotent (a one-frame edge shift re-adds swing clearance). */
95
+ function contactSpans(samples, id, fps, groundAt, maxHeight) {
96
+ const count = samples.length, minLength = Math.max(3, Math.round(fps * 0.1)), spans = [];
97
+ const flags = samples.map((row, i) => {
98
+ const lo = Math.max(0, i - 2), hi = Math.min(count - 1, i + 2), a = samples[lo].feet[id], b = samples[hi].feet[id], c = row.feet[id];
99
+ const dt = (hi - lo) / fps || 1 / fps;
100
+ const below = groundAt(c.toe.x, c.toe.z, c.sole + LEVEL_TOLERANCE);
101
+ return c.sole - below <= maxHeight
102
+ && Math.hypot(b.toe.x - a.toe.x, b.toe.z - a.toe.z) / dt < 0.16;
103
+ });
104
+ let start = -1;
105
+ for (let f = 0; f <= count; f += 1) {
106
+ if (flags[f] && start < 0) start = f;
107
+ if ((f === count || !flags[f]) && start >= 0) {
108
+ if (f - start >= minLength) {
109
+ const rows = samples.slice(start, f).map((r) => r.feet[id]);
110
+ const toe = new THREE.Vector3(quantile(rows.map((r) => r.toe.x), 0.5), 0, quantile(rows.map((r) => r.toe.z), 0.5));
111
+ const ankle = new THREE.Vector3(quantile(rows.map((r) => r.ankle.x), 0.5), 0, quantile(rows.map((r) => r.ankle.z), 0.5));
112
+ const wander = Math.max(...rows.map((r) => Math.hypot(r.toe.x - toe.x, r.toe.z - toe.z)));
113
+ if (wander <= 0.10) spans.push({ start, end: f - 1, toe, ankle, currentY: quantile(rows.map((r) => r.sole), 0.25) });
114
+ }
115
+ start = -1;
116
+ }
117
+ }
118
+ return spans;
119
+ }
120
+
121
+ export async function fitPlatforms({ rig, motion, chains, fkJoints, sourceKeys, applyRaw, sceneObjects = [], floorY = 0, onProgress = () => {}, yieldFrame = () => Promise.resolve() }) {
122
+ const hipsJoint = fkJoints?.get("hips"), hips = hipsJoint?.bone;
123
+ if (!hips || !motion || !FEET.every(({ id }) => chains?.get(id))) throw new Error("A loaded motion and complete rig are required");
124
+ const count = motion.frames, fps = motion.fps || 24, limits = PLATFORM_FIT_LIMITS;
125
+ const sourceStamp = physicsKeyStamp(sourceKeys);
126
+ const source = { keys: copyPhysicsKeys(sourceKeys), tracked: new Set([...sourceKeys.values()].flatMap((e) => [...e.keys()])) };
127
+ const candidate = { keys: copyPhysicsKeys(sourceKeys), tracked: new Set(source.tracked) };
128
+ const groundAt = createGroundSampler(sceneObjects, { floorY });
129
+ const owners = groundAt.surfaces.map((s) => sceneObjects.find((o) => {
130
+ const b = objectFootprintBounds(o);
131
+ return b.minX === s.minX && b.maxX === s.maxX && b.minZ === s.minZ && b.maxZ === s.maxZ && b.topY === s.topY && b.baseY === s.baseY;
132
+ })?.id ?? null);
133
+ const surfaceAt = (p, y) => groundAt.surfaces.findIndex((s) => s.topY === y && p.x >= s.minX && p.x <= s.maxX && p.z >= s.minZ && p.z <= s.maxZ);
134
+ const support = createSupportSampler(rig);
135
+ const toeBones = Object.fromEntries(FEET.map(({ id, toe }) => [id, findBone(rig, toe)]));
136
+ const applyBase = (f) => { applyRaw(f); ikEvaluate(chains, source, f, fkJoints, 6); rig.updateMatrixWorld(true); };
137
+
138
+ // 1. Sample the current pose (raw clip + source IK keys) at every frame.
139
+ const samples = [];
140
+ for (let f = 0; f < count; f += 1) {
141
+ applyRaw(f); rig.updateMatrixWorld(true);
142
+ const raw = snapshot(chains, hips);
143
+ applyBase(f);
144
+ const surfaces = support();
145
+ samples.push({ raw, hips: hips.getWorldPosition(V()), feet: Object.fromEntries(FEET.map(({ id }) => {
146
+ const chain = chains.get(id), p = chain.bones.map((b) => b.getWorldPosition(V()));
147
+ return [id, { sole: surfaces[id].floor, ankle: surfaces[id].position.clone(), rotation: surfaces[id].rotation.clone(),
148
+ toe: (toeBones[id] ?? chain.bones[2]).getWorldPosition(V()), hip: p[0], leg: p[0].distanceTo(p[1]) + p[1].distanceTo(p[2]), extension: p[0].distanceTo(p[2]) }];
149
+ })) });
150
+ if (f % 12 === 0) { onProgress(Math.round(50 * f / Math.max(1, count))); await yieldFrame(); }
151
+ }
152
+ const legLengths = Object.fromEntries(FEET.map(({ id }) => [id, quantile(samples.map((r) => r.feet[id].leg), 0.5)]));
153
+ const leg = (legLengths.leftFoot + legLengths.rightFoot) / 2;
154
+ const stepUp = limits.stepUp * leg, climb = limits.climb * leg;
155
+
156
+ // 2-3. Contacts, surfaces, classification.
157
+ const steps = [], spansByFoot = {};
158
+ for (const { id } of FEET) {
159
+ const spans = contactSpans(samples, id, fps, groundAt, stepUp);
160
+ for (const span of spans) {
161
+ const onToe = groundAt(span.toe.x, span.toe.z), onAnkle = groundAt(span.ankle.x, span.ankle.z);
162
+ const surfaceY = Math.max(onToe, onAnkle), point = onToe >= onAnkle ? span.toe : span.ankle;
163
+ const index = surfaceY > floorY ? surfaceAt(point, surfaceY) : -1;
164
+ const rise = surfaceY - span.currentY;
165
+ const status = rise <= stepUp ? "ok" : rise <= climb ? "tooHigh" : "wall";
166
+ let lift = 0;
167
+ if (status === "ok" && Math.abs(rise) > LEVEL_TOLERANCE) {
168
+ let target = surfaceY;
169
+ if (rise < 0) {
170
+ // Lowering undoes an earlier fit whose box moved away. Never
171
+ // push a contact below the raw clip's own height: an elevated
172
+ // performance (a generated stair climb) is not a fit to undo.
173
+ const rawSoles = [];
174
+ for (let f = span.start; f <= span.end; f += 1) { applyRaw(f); rawSoles.push(support()[id].floor); }
175
+ target = Math.max(surfaceY, Math.min(span.currentY, quantile(rawSoles, 0.25)));
176
+ }
177
+ if (Math.abs(target - span.currentY) > LEVEL_TOLERANCE) lift = target - span.currentY;
178
+ }
179
+ Object.assign(span, { surfaceY, rise, status, lift, objectId: index >= 0 ? owners[index] : null });
180
+ }
181
+ spansByFoot[id] = spans;
182
+ steps.push(...spans.map((s) => ({ id: `${id}:${s.start}`, foot: id, start: s.start, end: s.end, surfaceY: s.surfaceY, rise: s.rise, status: s.status, objectId: s.objectId, lift: s.lift })));
183
+ }
184
+ // The pelvis walking into something taller than a climb is a wall even
185
+ // when no planted foot lands on it.
186
+ groundAt.surfaces.forEach((s, index) => {
187
+ const objectId = owners[index];
188
+ if (steps.some((step) => step.status === "wall" && step.objectId === objectId)) return;
189
+ let start = -1;
190
+ for (let f = 0; f <= count; f += 1) {
191
+ const row = samples[f];
192
+ const standing = row && Math.min(row.feet.leftFoot.sole, row.feet.rightFoot.sole);
193
+ const inside = row && row.hips.x >= s.minX && row.hips.x <= s.maxX && row.hips.z >= s.minZ && row.hips.z <= s.maxZ && s.topY - standing > climb;
194
+ if (inside && start < 0) start = f;
195
+ if (!inside && start >= 0) {
196
+ const entry = samples[start], center = { x: (s.minX + s.maxX) / 2, z: (s.minZ + s.maxZ) / 2 };
197
+ const foot = FEET.map(({ id }) => id).sort((a, b) => Math.hypot(entry.feet[a].toe.x - center.x, entry.feet[a].toe.z - center.z) - Math.hypot(entry.feet[b].toe.x - center.x, entry.feet[b].toe.z - center.z))[0];
198
+ const standY = Math.min(entry.feet.leftFoot.sole, entry.feet.rightFoot.sole);
199
+ steps.push({ id: `pelvis:${objectId ?? index}:${start}`, foot, start, end: f - 1, surfaceY: s.topY, rise: s.topY - standY, status: "wall", objectId, lift: 0 });
200
+ break;
201
+ }
202
+ }
203
+ });
204
+ steps.sort((a, b) => a.start - b.start || a.foot.localeCompare(b.foot));
205
+ onProgress(55); await yieldFrame();
206
+
207
+ // 4. Per-foot lift: span lifts with smoothstep transitions, plus swing toe
208
+ // clearance over raised surfaces. The clearance ramps in from each contact
209
+ // by time, not by the source's own toe height, so a re-run on the fitted
210
+ // output measures the same requirement and adds nothing.
211
+ const lifts = {};
212
+ for (const { id } of FEET) {
213
+ const spans = spansByFoot[id], base = spanTrack(count, spans);
214
+ const inSpan = new Array(count).fill(false);
215
+ for (const s of spans) for (let f = s.start; f <= s.end; f += 1) inSpan[f] = true;
216
+ const offsets = spans.flatMap((s) => samples.slice(s.start, s.end + 1).map((r) => r.feet[id].toe.y - r.feet[id].sole));
217
+ const toeOffset = quantile(offsets.length ? offsets : samples.map((r) => r.feet[id].toe.y - r.feet[id].sole), 0.5);
218
+ const ramp = Math.max(2, Math.round(fps * 0.15));
219
+ const excess = samples.map((row, f) => {
220
+ if (inSpan[f]) return 0;
221
+ const toe = row.feet[id].toe, ground = groundAt(toe.x, toe.z);
222
+ if (ground <= floorY + EPS) return 0;
223
+ const under = toe.y - toeOffset;
224
+ if (ground - under > stepUp) return 0; // a wall: reported, never vaulted
225
+ const prev = [...spans].reverse().find((s) => s.end < f), next = spans.find((s) => s.start > f);
226
+ const gap = Math.min(prev ? f - prev.end : Infinity, next ? next.start - f : Infinity);
227
+ return Math.max(0, ground + limits.clearance * smooth(gap / ramp) - under - base[f]);
228
+ });
229
+ const clearance = envelope(excess, 0.06 * fps);
230
+ // The envelope spreads clearance right up to a contact; cutting it off
231
+ // on the lift-off/landing frame is a one-frame foot jump that the leg IK
232
+ // turns into a visible knee pop. Fade the spread to zero at every contact
233
+ // edge, but never below the frame's own requirement (which itself ramps
234
+ // in from the contact), so a re-run still measures nothing left to add.
235
+ const taper = samples.map((_, f) => {
236
+ if (inSpan[f]) return 0;
237
+ const prev = [...spans].reverse().find((s) => s.end < f), next = spans.find((s) => s.start > f);
238
+ return smooth(Math.min(prev ? f - prev.end : Infinity, next ? next.start - f : Infinity) / ramp);
239
+ });
240
+ lifts[id] = base.map((v, f) => inSpan[f] ? v : v + Math.max(excess[f], clearance[f] * taper[f]));
241
+ }
242
+
243
+ // 5. Pelvis: follow the supporting feet, smoothed, never lifted where no
244
+ // foot is lifted, then lowered wherever a leg would over-reach.
245
+ const planted = (id, f) => spansByFoot[id].some((s) => f >= s.start && f <= s.end);
246
+ const supportLift = samples.map((_, f) => {
247
+ const values = FEET.filter(({ id }) => planted(id, f)).map(({ id }) => lifts[id][f]);
248
+ return values.length ? values.reduce((a, b) => a + b, 0) / values.length : NaN;
249
+ });
250
+ const known = supportLift.map((v, f) => Number.isFinite(v) ? f : -1).filter((f) => f >= 0);
251
+ const filled = supportLift.map((v, f) => {
252
+ if (Number.isFinite(v)) return v;
253
+ if (!known.length) return 0;
254
+ const next = known.find((k) => k > f), prev = [...known].reverse().find((k) => k < f);
255
+ if (prev === undefined) return supportLift[next];
256
+ if (next === undefined) return supportLift[prev];
257
+ return THREE.MathUtils.lerp(supportLift[prev], supportLift[next], (f - prev) / (next - prev));
258
+ });
259
+ const pelvis = gaussian(filled, 0.15 * fps).map((v, f) => {
260
+ const values = FEET.map(({ id }) => lifts[id][f]);
261
+ return clamp(v, Math.min(0, ...values), Math.max(0, ...values));
262
+ });
263
+ // Highest pelvis lift at which every leg still reaches its foot target.
264
+ // Smoothing the over-reach and subtracting it spread the correction into
265
+ // frames that did not need it: the leg locked straight at a step-down and
266
+ // the body then sank below the source (a squat). Instead take a smooth
267
+ // LOWER envelope of min(desired, reachable): it never exceeds either, and
268
+ // never drops below the lowest value within its window.
269
+ // Only a planted foot (or one about to land / just lifting off) holds the
270
+ // pelvis down: a trailing swing leg would otherwise keep the body low while
271
+ // the other foot already stands on the box - a squat. Mid-swing feet are
272
+ // raised to stay within reach instead (below).
273
+ const edgeFrames = Math.max(2, Math.round(fps * 0.15));
274
+ const contactGap = (id, f) => Math.min(...spansByFoot[id].map((s) => f < s.start ? s.start - f : f > s.end ? f - s.end : 0), Infinity);
275
+ const slack = (id, f) => {
276
+ const foot = samples[f].feet[id], reach = Math.max(limits.reach * legLengths[id], foot.extension);
277
+ const horizontal = Math.hypot(foot.ankle.x - foot.hip.x, foot.ankle.z - foot.hip.z);
278
+ return foot.ankle.y - foot.hip.y + Math.sqrt(Math.max(0, reach * reach - horizontal * horizontal));
279
+ };
280
+ const reachable = samples.map((_, f) => Math.min(...FEET.filter(({ id }) => contactGap(id, f) <= edgeFrames).map(({ id }) => lifts[id][f] + slack(id, f)), Infinity));
281
+ const sigma = 0.08 * fps, radius = Math.max(1, Math.ceil(2.5 * sigma));
282
+ const capped = gaussian(minFilter(pelvis.map((p, f) => Math.min(p, reachable[f])), radius), sigma, radius);
283
+ capped.forEach((p, f) => { pelvis[f] = Math.abs(p) > EPS ? p : 0; });
284
+ for (const { id } of FEET) for (let f = 0; f < count; f += 1) {
285
+ if (contactGap(id, f) > edgeFrames) lifts[id][f] = Math.max(lifts[id][f], pelvis[f] - slack(id, f));
286
+ }
287
+ onProgress(65); await yieldFrame();
288
+
289
+ // 6. Solve and key every changed frame plus one unchanged frame each side.
290
+ const changed = samples.map((_, f) => f).filter((f) => Math.abs(pelvis[f]) > EPS || FEET.some(({ id }) => Math.abs(lifts[id][f]) > EPS));
291
+ const changedSet = new Set(changed);
292
+ const keyed = [...new Set(changed.flatMap((f) => [f - 1, f, f + 1]))].filter((f) => f >= 0 && f < count).sort((a, b) => a - b);
293
+ for (let n = 0; n < keyed.length; n += 1) {
294
+ const f = keyed[n], row = samples[f];
295
+ applyBase(f);
296
+ if (changedSet.has(f)) {
297
+ solveHipsTranslate(hipsJoint, new THREE.Vector3(0, pelvis[f], 0), hips.position.clone()); rig.updateMatrixWorld(true);
298
+ for (const { id } of FEET) {
299
+ const chain = chains.get(id), foot = row.feet[id];
300
+ solveIk(chain, foot.ankle.clone().setY(foot.ankle.y + lifts[id][f]), { exactHinge: true }); rig.updateMatrixWorld(true);
301
+ // The foot keeps its source world orientation: a lift is a
302
+ // translation of the planted sole, never a toe dip.
303
+ const end = chain.bones[2];
304
+ end.quaternion.copy(end.parent.getWorldQuaternion(Q()).invert().multiply(foot.rotation)); rig.updateMatrixWorld(true);
305
+ }
306
+ }
307
+ const entry = candidate.keys.get(f) ?? new Map();
308
+ entry.set("hips", { q: [hips.quaternion.clone()], p: hips.position.clone(), baseQ: [row.raw.hips.q.clone()], basePos: row.raw.hips.p.clone() });
309
+ candidate.tracked.add("hips");
310
+ for (const { id } of FEET) {
311
+ const bones = chains.get(id).bones, raw = row.raw.chains[id];
312
+ const key = { q: bones.map((b) => b.quaternion.clone()), p: null, baseQ: raw.map((b) => b.q.clone()), keepTranslations: true };
313
+ // Island-edge keys carry translations only when the source changed
314
+ // them; otherwise their ease ramp would smear this frame's bone
315
+ // lengths onto the untouched neighbours.
316
+ if (changedSet.has(f) || bones.some((b, i) => !b.position.equals(raw[i].p))) key.chainP = bones.map((b) => b.position.clone());
317
+ entry.set(id, key); candidate.tracked.add(id);
318
+ }
319
+ candidate.keys.set(f, entry);
320
+ if (n % 12 === 0) { onProgress(65 + Math.round(35 * n / keyed.length)); await yieldFrame(); }
321
+ }
322
+ onProgress(100);
323
+ const summary = { ok: 0, tooHigh: 0, wall: 0, lifted: 0 };
324
+ for (const step of steps) { summary[step.status] += 1; if (step.lift !== 0) summary.lifted += 1; }
325
+ return { candidate, steps, summary, changedFrames: changed, sourceStamp, lifts: { ...lifts, pelvis }, legLength: leg };
326
+ }
@@ -37,6 +37,8 @@
37
37
  * their parents). Bones the map does not touch keep their bind transforms.
38
38
  * Shoulder and arm chains keep their Mixamo bind translations and consume
39
39
  * ARDY rotations only, avoiding clavicle shear between incongruent rigs.
40
+ * Path-fix takes opt into a reference-pose correction: edited wrists retain
41
+ * their rendered orientation and edited legs retain their rendered lengths.
40
42
  */
41
43
 
42
44
  import * as THREE from "three";
@@ -85,12 +87,12 @@ const SKINNING_MAP = CSKEL27_JOINTS.map((name) => {
85
87
  // Mixamo arm chains' authored local translations and retarget rotations only;
86
88
  // the rest of the body still uses ARDY positional skinning for root motion
87
89
  // and foot placement.
88
- const HIERARCHY_PRESERVED_JOINTS = new Set([
90
+ export const HIERARCHY_PRESERVED_JOINTS = new Set([
89
91
  "RightShoulder", "RightArm", "RightForeArm",
90
92
  "LeftShoulder", "LeftArm", "LeftForeArm",
91
93
  ]);
92
94
 
93
- const ARDY_NEUTRAL_MIN_Y = -0.9544128; // toe depth under the hips-origin neutral pose
95
+ export const ARDY_NEUTRAL_MIN_Y = -0.9544128; // toe depth under the hips-origin neutral pose
94
96
 
95
97
  /* No finger forcing: ARDY's cskel27 stops at the wrist, and the fingers
96
98
  * simply keep the rig's own bind pose at all times (the same presentation
@@ -141,7 +143,7 @@ function bindsOf(rig) {
141
143
  * pose and cached. */
142
144
  const rigPreps = new WeakMap();
143
145
 
144
- function prepOf(rig) {
146
+ export function prepOf(rig) {
145
147
  let prep = rigPreps.get(rig);
146
148
  if (prep) return prep;
147
149
  const binds = bindsOf(rig);
@@ -268,6 +270,8 @@ function prepOf(rig) {
268
270
  if (bone) {
269
271
  const bind = binds.get(bone);
270
272
  stretchedLeaves.push({ bone, joint: CSKEL27_JOINTS.indexOf(name), bindLocalPos: bindPositionOf(bone).clone(),
273
+ bindWorldQuat: new THREE.Quaternion().setFromRotationMatrix(worldByNode.get(bone)),
274
+ parentBindQuat: new THREE.Quaternion().setFromRotationMatrix(worldByNode.get(bone.parent)),
271
275
  bindLocalQuat: bind ? new THREE.Quaternion(bind.x, bind.y, bind.z, bind.w) : bone.quaternion.clone() });
272
276
  }
273
277
  }
@@ -350,7 +354,7 @@ export function captureArdyRoot(rig) {
350
354
  * floor-shifted the same way prepOf does, in rig units via prep.scale.
351
355
  * Cached per (rig, boneScale array) — one take, one skeleton. */
352
356
  const scaledOffsetCache = new WeakMap();
353
- function scaledOffsets(prep, boneScale) {
357
+ export function scaledOffsets(prep, boneScale) {
354
358
  let cached = scaledOffsetCache.get(boneScale);
355
359
  if (cached && cached.prep === prep) return cached.offsets;
356
360
  const grown = new Array(CSKEL27_JOINTS.length);
@@ -383,7 +387,7 @@ function scaledOffsets(prep, boneScale) {
383
387
  * rig's own bind bone is not the canonical length (Mixamo forearm 27.6 cm
384
388
  * against cskel27's 23.3), so the factor is re-based onto the rig:
385
389
  * performer / rigBind = boneScale * canonical / rigBind, all in rig units. */
386
- function boneStretch(prep, motion, j) {
390
+ export function boneStretch(prep, motion, j) {
387
391
  if (!motion.boneScale) return 1;
388
392
  // The shoulder girdle is character geometry, like the neck: the rig's
389
393
  // clavicle+arm-root (21 cm straight) is deliberately shorter than
@@ -397,7 +401,7 @@ function boneStretch(prep, motion, j) {
397
401
  if (!(canonical > 1e-6) || !(rigBind > 1e-6)) return motion.boneScale[j];
398
402
  return (motion.boneScale[j] * canonical) / rigBind;
399
403
  }
400
- const GIRDLE_JOINTS = new Set(["LeftShoulder", "RightShoulder", "LeftArm", "RightArm"]);
404
+ export const GIRDLE_JOINTS = new Set(["LeftShoulder", "RightShoulder", "LeftArm", "RightArm"]);
401
405
 
402
406
  /* --- per-frame application -------------------------------------------------- */
403
407
 
@@ -423,6 +427,54 @@ const qDecomp = new THREE.Quaternion();
423
427
  const vDecompScale = new THREE.Vector3();
424
428
  const desiredWorld = new Array(CSKEL27_JOINTS.length).fill(null);
425
429
 
430
+ function rotationsAt(motion, f) {
431
+ for (let j = 0; j < frameLocals.length; j += 1) {
432
+ const o = (f * frameLocals.length + j) * 9;
433
+ const L = frameLocals[j];
434
+ for (let r = 0; r < 3; r += 1) for (let c = 0; c < 3; c += 1) L[r][c] = motion.rotMats[o + r * 3 + c];
435
+ }
436
+ return globalRotations(frameLocals);
437
+ }
438
+
439
+ function rotationQuat(G, target = new THREE.Quaternion()) {
440
+ mGlobal.set(...G[0], ...G[1], ...G[2]);
441
+ return target.setFromRotationMatrix(mWorld.setFromMatrix3(mGlobal));
442
+ }
443
+
444
+ /** Reference corrections need FK and a few transforms, not a second rig pose. */
445
+ function trailReference(prep, { base, chains }, f) {
446
+ const globals = rotationsAt(base, f);
447
+ const offsets = base.boneScale ? scaledOffsets(prep, base.boneScale) : prep.offsets;
448
+ const anchor = Math.max(0, Math.min(base.anchorFrame || 0, base.frames - 1)) * 27 * 3;
449
+ const worlds = new Map(), positions = new Map(), wrists = new Map();
450
+ const world = (j) => {
451
+ if (!worlds.has(j)) {
452
+ const q = rotationQuat(globals[j]);
453
+ const p = new THREE.Vector3().fromArray(base.posedJoints, (f * 27 + j) * 3);
454
+ p.x -= base.posedJoints[anchor]; p.z -= base.posedJoints[anchor + 2];
455
+ p.multiplyScalar(prep.scale).add(offsets[j].clone().applyQuaternion(q));
456
+ worlds.set(j, new THREE.Matrix4().compose(p, q.multiply(prep.bindQuat[j]), prep.bindScale[j]));
457
+ }
458
+ return worlds.get(j);
459
+ };
460
+ for (const track of chains) {
461
+ const side = track.startsWith("left") ? "Left" : "Right";
462
+ if (track.endsWith("Hand")) {
463
+ const leaf = prep.stretchedLeaves.find((item) => CSKEL27_JOINTS[item.joint] === `${side}Hand`);
464
+ if (leaf) wrists.set(leaf.bone, rotationQuat(globals[CSKEL27_PARENTS[leaf.joint]]).multiply(leaf.bindWorldQuat));
465
+ } else {
466
+ for (const suffix of ["UpLeg", "Leg", "Foot", "ToeBase"]) {
467
+ const j = CSKEL27_JOINTS.indexOf(side + suffix), bone = prep.bones[j];
468
+ if (!bone) continue;
469
+ const parent = prep.chainParent[j];
470
+ const parentWorld = parent < 0 ? prep.parentBindWorld[j].clone() : world(parent).clone().multiply(prep.chainRel[j]);
471
+ positions.set(bone, new THREE.Vector3().setFromMatrixPosition(world(j)).applyMatrix4(parentWorld.invert()));
472
+ }
473
+ }
474
+ }
475
+ return { positions, wrists };
476
+ }
477
+
426
478
  /**
427
479
  * Apply one motion frame to the rig with positional skinning: every mapped
428
480
  * bone's rig-space world transform is set to
@@ -439,18 +491,9 @@ export function applyMotionFrame(rig, motion, frame) {
439
491
  const f = Math.max(0, Math.min(Math.round(frame) || 0, motion.frames - 1));
440
492
  const prep = prepOf(rig);
441
493
  const joints = CSKEL27_JOINTS.length;
442
- const base = f * joints * 9;
443
-
444
- // Global (world) rotations of every cskel27 joint at this frame: FK over
445
- // the npz local rotations down the parent chain.
446
- for (let j = 0; j < joints; j += 1) {
447
- const o = base + j * 9;
448
- const L = frameLocals[j];
449
- L[0][0] = motion.rotMats[o]; L[0][1] = motion.rotMats[o + 1]; L[0][2] = motion.rotMats[o + 2];
450
- L[1][0] = motion.rotMats[o + 3]; L[1][1] = motion.rotMats[o + 4]; L[1][2] = motion.rotMats[o + 5];
451
- L[2][0] = motion.rotMats[o + 6]; L[2][1] = motion.rotMats[o + 7]; L[2][2] = motion.rotMats[o + 8];
452
- }
453
- const globals = globalRotations(frameLocals);
494
+ // Opt-in only; unedited takes keep their existing retargeting policy.
495
+ const trail = motion.trailRetarget ? trailReference(prep, motion.trailRetarget, f) : null;
496
+ const globals = rotationsAt(motion, f);
454
497
 
455
498
  // Anchor: the anchor frame's root stays at the rig origin (the Character
456
499
  // group sits at the scene anchor); height stays floor-absolute.
@@ -513,7 +556,11 @@ export function applyMotionFrame(rig, motion, frame) {
513
556
  mParentInv.copy(prep.parentBindWorld[j]);
514
557
  }
515
558
 
516
- if (HIERARCHY_PRESERVED_JOINTS.has(CSKEL27_JOINTS[j])) {
559
+ if (trail?.positions.has(bone)) {
560
+ // Swing the original rendered segment, rather than re-placing its
561
+ // endpoints with different rotated bind offsets (which stretches it).
562
+ vWorld.copy(trail.positions.get(bone)).applyMatrix4(mParentInv);
563
+ } else if (HIERARCHY_PRESERVED_JOINTS.has(CSKEL27_JOINTS[j])) {
517
564
  // A mocap take carries the performer's bone lengths (boneScale, see
518
565
  // npz.js). Positionally skinned bones already sit where the scaled
519
566
  // posedJoints put them; the arm chain rides its own bind
@@ -532,11 +579,110 @@ export function applyMotionFrame(rig, motion, frame) {
532
579
  }
533
580
  for (const leaf of prep.stretchedLeaves) {
534
581
  leaf.bone.position.copy(leaf.bindLocalPos).multiplyScalar(boneStretch(prep, motion, leaf.joint));
535
- leaf.bone.quaternion.copy(leaf.bindLocalQuat);
582
+ const orientation = trail?.wrists.get(leaf.bone);
583
+ if (orientation) {
584
+ // Work below the rig root: placement, yaw and export clones must
585
+ // not leak into the wrist's local compensation.
586
+ rotationQuat(globals[CSKEL27_PARENTS[leaf.joint]], qGlobal).multiply(leaf.parentBindQuat);
587
+ leaf.bone.quaternion.copy(qGlobal.invert().multiply(orientation));
588
+ } else leaf.bone.quaternion.copy(leaf.bindLocalQuat);
536
589
  }
537
590
  rig.updateMatrixWorld(true);
538
591
  }
539
592
 
593
+ /* --- read-only sampling ---------------------------------------------------- */
594
+
595
+ /** Bones-only copy of a rig for off-screen sampling, keyed to the live rig.
596
+ * It carries the live rig's bind snapshot, so its prep is the live prep. */
597
+ const samplerCopies = new WeakMap();
598
+
599
+ function samplerCopyOf(rig) {
600
+ let copy = samplerCopies.get(rig);
601
+ if (copy) return copy;
602
+ // Never create the live rig's fallback here: that would freeze its bind
603
+ // at whatever pose it holds now. Unprimed rigs read the current rotations.
604
+ const binds = rig.userData?.poseBind ?? bindFallback.get(rig) ?? new Map();
605
+ const copyBinds = new Map();
606
+ const hasBone = (node) => node.isBone || node.children.some(hasBone);
607
+ const clone = (node) => {
608
+ const out = node.isBone ? new THREE.Bone() : new THREE.Object3D();
609
+ out.name = node.name;
610
+ out.position.copy(node.position);
611
+ out.quaternion.copy(node.quaternion);
612
+ out.scale.copy(node.scale);
613
+ const bind = node.isBone ? binds.get(node) : null;
614
+ const q = node.quaternion;
615
+ // Without a recorded bind position the copied live position stands in,
616
+ // exactly as bindPositionOf does for the live rig.
617
+ if (node.isBone) copyBinds.set(out, bind ?? { x: q.x, y: q.y, z: q.z, w: q.w });
618
+ for (const child of node.children) if (hasBone(child)) out.add(clone(child));
619
+ return out;
620
+ };
621
+ // The root stays identity: samples are in rig space, below the root.
622
+ copy = new THREE.Object3D();
623
+ for (const child of rig.children) if (hasBone(child)) copy.add(clone(child));
624
+ copy.userData.poseBind = copyBinds;
625
+ samplerCopies.set(rig, copy);
626
+ return copy;
627
+ }
628
+
629
+ /** Per (rig, motion) cache of rendered joint positions. */
630
+ const jointSamples = new WeakMap();
631
+
632
+ /** Pose the sampler copy at frame f and write every driven bone's rig-space
633
+ * position into out[offset..offset+81] (NaN where no bone is driven). */
634
+ function sampleFrame(rig, motion, f, out, offset) {
635
+ const copy = samplerCopyOf(rig);
636
+ const prep = prepOf(copy);
637
+ if (!prep.driven) {
638
+ prep.driven = CSKEL27_JOINTS.map((_, j) => prep.bones[j] ?? prep.stretchedLeaves.find((leaf) => leaf.joint === j)?.bone ?? null);
639
+ }
640
+ applyMotionFrame(copy, motion, f);
641
+ for (let j = 0; j < prep.driven.length; j += 1) {
642
+ const bone = prep.driven[j];
643
+ const o = offset + j * 3;
644
+ if (!bone) {
645
+ out[o] = out[o + 1] = out[o + 2] = NaN;
646
+ continue;
647
+ }
648
+ const e = bone.matrixWorld.elements;
649
+ out[o] = e[12];
650
+ out[o + 1] = e[13];
651
+ out[o + 2] = e[14];
652
+ }
653
+ }
654
+
655
+ /** One frame of motionJointPositions (uncached): Float32Array(27*3), rig space. */
656
+ export function motionJointPositionsAt(rig, motion, frame) {
657
+ if (!rig || !motion?.rotMats || !(motion.frames > 0)) return null;
658
+ const out = new Float32Array(CSKEL27_JOINTS.length * 3);
659
+ sampleFrame(rig, motion, Math.max(0, Math.min(Math.round(frame) || 0, motion.frames - 1)), out, 0);
660
+ return out;
661
+ }
662
+
663
+ /**
664
+ * Rig-space position of every driven cskel27 joint's bone at every frame,
665
+ * exactly as applyMotionFrame places it, without touching the live rig.
666
+ * Returns { positions: Float32Array(frames*27*3) } (NaN where no bone is
667
+ * driven). Cached per rig + motion; treat as read-only.
668
+ */
669
+ export function motionJointPositions(rig, motion) {
670
+ if (!rig || !motion?.rotMats || !(motion.frames > 0)) return null;
671
+ let byRig = jointSamples.get(motion);
672
+ if (!byRig) {
673
+ byRig = new WeakMap();
674
+ jointSamples.set(motion, byRig);
675
+ }
676
+ const cached = byRig.get(rig);
677
+ if (cached) return cached;
678
+ const joints = CSKEL27_JOINTS.length;
679
+ const positions = new Float32Array(motion.frames * joints * 3);
680
+ for (let f = 0; f < motion.frames; f += 1) sampleFrame(rig, motion, f, positions, f * joints * 3);
681
+ const result = { positions };
682
+ byRig.set(rig, result);
683
+ return result;
684
+ }
685
+
540
686
  /* --- snapshot / restore ----------------------------------------------------- */
541
687
 
542
688
  /**