@needle-tools/three-mesh-effects 0.1.0-alpha → 0.1.0-alpha.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 (86) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +94 -88
  3. package/build/deformation-runtime.js.map +1 -1
  4. package/build/impostor-runtime-needle-engine.js.map +1 -1
  5. package/build/impostor-runtime.js.map +1 -1
  6. package/build/internal/deformation/cloth-surface.js.map +1 -1
  7. package/build/internal/deformation/fur-surface.js.map +1 -1
  8. package/build/internal/deformation/ik-constraints.js.map +1 -1
  9. package/build/internal/deformation/joints-json.js.map +1 -1
  10. package/build/internal/deformation/limits.js.map +1 -1
  11. package/build/internal/deformation/motion-settings.js.map +1 -1
  12. package/build/internal/deformation/pin-cloth.js.map +1 -1
  13. package/build/internal/deformation/pin-ik.js.map +1 -1
  14. package/build/internal/deformation/pin-types.js.map +1 -1
  15. package/build/internal/deformation/pin-wiggle.js.map +1 -1
  16. package/build/internal/deformation/pose-slots.js.map +1 -1
  17. package/build/internal/deformation/pose-timeline.js.map +1 -1
  18. package/build/internal/deformation/rig-drivers.js.map +1 -1
  19. package/build/internal/deformation/rig-extension.js.map +1 -1
  20. package/build/internal/deformation/rig-runtime.js.map +1 -1
  21. package/build/internal/deformation/wind.js.map +1 -1
  22. package/build/internal/impostor/impostor-metadata.js.map +1 -1
  23. package/build/internal/impostor/impostor-node-material.js.map +1 -1
  24. package/build/internal/impostor/impostor-runtime-support.js.map +1 -1
  25. package/build/internal/impostor/octahedral.js.map +1 -1
  26. package/build/internal/pixel-impostor/pixel-impostor-display.js.map +1 -1
  27. package/build/internal/pixel-impostor/pixel-impostor-metadata.js.map +1 -1
  28. package/build/internal/pixel-impostor/pixel-impostor-runtime.js.map +1 -1
  29. package/build/internal/rendering/skin-projection.js +52 -52
  30. package/build/internal/rendering/skin-projection.js.map +2 -2
  31. package/build/internal/rendering/surface-query.js +52 -52
  32. package/build/internal/rendering/surface-query.js.map +2 -2
  33. package/build/internal/rendering/wireframe.js.map +1 -1
  34. package/build/internal/rendering/wrap-preview-settings.js.map +1 -1
  35. package/build/internal/rendering/wrap-preview.js +52 -52
  36. package/build/internal/rendering/wrap-preview.js.map +2 -2
  37. package/build/internal/rendering/wrap-skin.js +52 -52
  38. package/build/internal/rendering/wrap-skin.js.map +2 -2
  39. package/build/pixel-impostor-runtime.js.map +1 -1
  40. package/build/wrap-effects.js +52 -52
  41. package/build/wrap-effects.js.map +2 -2
  42. package/package.json +107 -94
  43. package/src/deformation/cloth-surface.ts +223 -223
  44. package/src/deformation/fur-surface.ts +129 -129
  45. package/src/deformation/ik-constraints.ts +37 -37
  46. package/src/deformation/joints-json.ts +94 -94
  47. package/src/deformation/limits.ts +25 -25
  48. package/src/deformation/motion-settings.ts +9 -9
  49. package/src/deformation/pin-cloth.ts +149 -149
  50. package/src/deformation/pin-ik.ts +582 -582
  51. package/src/deformation/pin-types.ts +22 -22
  52. package/src/deformation/pin-wiggle.ts +119 -119
  53. package/src/deformation/pose-slots.ts +106 -106
  54. package/src/deformation/pose-timeline.ts +242 -242
  55. package/src/deformation/rig-drivers.ts +32 -32
  56. package/src/deformation/rig-extension.ts +87 -87
  57. package/src/deformation/rig-runtime.ts +416 -416
  58. package/src/deformation/wind.ts +36 -36
  59. package/src/impostor/impostor-metadata.js +137 -137
  60. package/src/impostor/impostor-node-material.js +1581 -1581
  61. package/src/impostor/impostor-runtime-support.js +139 -139
  62. package/src/impostor/octahedral.js +280 -280
  63. package/src/pixel-impostor/pixel-impostor-display.js +794 -794
  64. package/src/pixel-impostor/pixel-impostor-metadata.js +74 -74
  65. package/src/pixel-impostor/pixel-impostor-runtime.js +78 -78
  66. package/src/public/deformation-runtime.ts +3 -3
  67. package/src/public/impostor-runtime-needle-engine.js +56 -56
  68. package/src/public/impostor-runtime.js +21 -21
  69. package/src/public/pixel-impostor-runtime.js +5 -5
  70. package/src/public/wrap-effects.js +21 -21
  71. package/src/rendering/skin-projection.js +113 -113
  72. package/src/rendering/surface-query.js +88 -88
  73. package/src/rendering/wireframe.js +115 -115
  74. package/src/rendering/wrap-preview-settings.js +123 -123
  75. package/src/rendering/wrap-preview.js +645 -645
  76. package/src/rendering/wrap-skin.js +109 -109
  77. package/build/internal/public/deformation-runtime.js +0 -2143
  78. package/build/internal/public/deformation-runtime.js.map +0 -7
  79. package/build/internal/public/impostor-runtime-needle-engine.js +0 -42
  80. package/build/internal/public/impostor-runtime-needle-engine.js.map +0 -7
  81. package/build/internal/public/impostor-runtime.js +0 -1333
  82. package/build/internal/public/impostor-runtime.js.map +0 -7
  83. package/build/internal/public/pixel-impostor-runtime.js +0 -876
  84. package/build/internal/public/pixel-impostor-runtime.js.map +0 -7
  85. package/build/internal/public/wrap-effects.js +0 -5640
  86. package/build/internal/public/wrap-effects.js.map +0 -7
@@ -1,87 +1,87 @@
1
- import { validateRig, type JointsRig } from './joints-json.js';
2
- import { ikChain } from './pin-ik.js';
3
- import {DEFAULT_MOTION} from './motion-settings.js';
4
- import type { ClothCollider, ClothLink } from './pin-cloth.js';
5
-
6
- export const NEEDLE_DEFORMATION_RIG = 'NEEDLE_deformation_rig';
7
- export interface RigDescriptor extends JointsRig {
8
- version: 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9;
9
- joints: { node: number; parent: number | null; mass: number }[];
10
- chains: { endpoint: number; joints: number[]; mode: 'ik' | 'secondary' | 'cloth' | 'fur' }[];
11
- helpers?: { node: number; influences: { node: number; weight: number }[] }[];
12
- cloth?: { links: ClothLink[]; colliders: ClothCollider[] };
13
- furSurface?: { heightTexture: number; texCoord?: number };
14
- }
15
- /** Validate before allocating runtime objects or accepting node references. */
16
- export function parseRigDescriptor(value: unknown, nodeCount: number, textureCount = Infinity): RigDescriptor {
17
- if (!value || typeof value !== 'object') throw new Error('Invalid deformation rig.');
18
- const raw = value as RigDescriptor;
19
- if (raw.version !== 1 && raw.version !== 2 && raw.version !== 3 && raw.version !== 4 && raw.version !== 5 && raw.version !== 6 && raw.version !== 7 && raw.version !== 8 && raw.version !== 9) throw new Error('Unsupported NEEDLE_deformation_rig version.');
20
- if (raw.furSurface?.texCoord !== undefined && (raw.version < 8 || !Number.isInteger(raw.furSurface.texCoord) || raw.furSurface.texCoord < 0 || raw.furSurface.texCoord > 1)) throw new Error('Invalid Fur height texture coordinates.');
21
- if (raw.furSurface && (raw.version < 6 || !Number.isInteger(raw.furSurface.heightTexture) || raw.furSurface.heightTexture < 0 || raw.furSurface.heightTexture >= textureCount)) throw new Error('Invalid Fur height texture.');
22
- if (raw.version < 5 && raw.windStrength !== undefined) throw new Error('Wind requires NEEDLE_deformation_rig version 5.');
23
- if (raw.activePose === 0 && raw.version < 9) throw new Error('An unselected authored pose requires NEEDLE_deformation_rig version 9.');
24
- const restSlot=raw.poseSlots?.find(slot=>slot.slot===0);
25
- if(restSlot && raw.version<7)throw new Error('Pose 0 requires NEEDLE_deformation_rig version 7.');
26
- const rig = validateRig({...raw,poseSlots:raw.poseSlots?.filter(slot=>slot.slot!==0)}, raw.version>=3?256:128);
27
- if(restSlot){
28
- if(raw.poseSlots.filter(slot=>slot.slot===0).length!==1 || !Array.isArray(restSlot.poses) || restSlot.poses.length!==rig.pins.length ||
29
- new Set(restSlot.poses.map(p=>p.id)).size!==rig.pins.length || restSlot.poses.some(p=>!rig.pins.some(pin=>pin.id===p.id)))throw new Error('Invalid rest pose clip.');
30
- validateRig({pins:rig.pins,activePose:1,poseSlots:[{slot:1,poses:restSlot.poses}],poseTimes:[]},raw.version>=3?256:128);
31
- }
32
- if (raw.version < 5 && rig.pins.some(pin=>pin.fur)) throw new Error('Fur requires NEEDLE_deformation_rig version 5.');
33
- if (raw.furSurface && !rig.pins.some(pin => pin.fur)) throw new Error('Fur height requires a Fur rig.');
34
- const node = (n: number) => Number.isInteger(n) && n >= 0 && n < nodeCount;
35
- if (!Array.isArray(raw.joints) || raw.joints.length !== rig.pins.length) throw new Error('Invalid rig joint bindings.');
36
- const indices = new Set<number>();
37
- for (const joint of raw.joints) {
38
- if (!joint || !node(joint.node) || indices.has(joint.node) || (joint.parent !== null && !node(joint.parent)) || !Number.isFinite(joint.mass) || joint.mass < 0) throw new Error('Invalid rig node or mass.');
39
- indices.add(joint.node);
40
- }
41
- const parents = new Map(raw.joints.map(j => [j.node, j.parent]));
42
- for (const joint of raw.joints) {
43
- const seen = new Set<number>(); let cursor: number | null = joint.node;
44
- while (cursor !== null) {
45
- if (!indices.has(cursor) || seen.has(cursor)) throw new Error('Invalid or cyclic rig hierarchy.');
46
- seen.add(cursor); cursor = parents.get(cursor)!;
47
- }
48
- }
49
- if (!Array.isArray(raw.chains) || raw.chains.length > rig.pins.length) throw new Error('Invalid motion chains.');
50
- const endpoints = new Set<number>();
51
- for (const chain of raw.chains) {
52
- if (!chain || !indices.has(chain.endpoint) || endpoints.has(chain.endpoint) || !['ik', 'secondary', 'cloth', 'fur'].includes(chain.mode) || !Array.isArray(chain.joints) || !chain.joints.length || chain.joints.length > rig.pins.length || chain.joints.at(-1) !== chain.endpoint || new Set(chain.joints).size !== chain.joints.length || chain.joints.some(n => !indices.has(n))) throw new Error('Invalid motion chain.');
53
- endpoints.add(chain.endpoint);
54
- }
55
- const pinParents = new Map<string, string>(), masses = new Map<string, number>();
56
- raw.joints.forEach((j, i) => { masses.set(rig.pins[i].id, j.mass); if (j.parent !== null) pinParents.set(rig.pins[i].id, rig.pins[raw.joints.findIndex(p => p.node === j.parent)].id); });
57
- for (const [i, pin] of rig.pins.entries()) {
58
- const chain = raw.chains.find(c => c.endpoint === raw.joints[i].node);
59
- const mode = !pin.guide && (pin.ik ? 'ik' : pin.wiggle ? 'secondary' : pin.cloth ? 'cloth' : pin.fur ? 'fur' : null);
60
- if (!mode && chain) throw new Error('Unexpected motion chain.');
61
- if (!mode) continue;
62
- const articulated=raw.version>=3?rig.pins.filter((_,i)=>!raw.helpers?.some(h=>h.node===raw.joints[i].node)):rig.pins;
63
- const expected = ikChain(articulated.map(p=>p.id===pin.id?{...p,motion:{...(p.motion??DEFAULT_MOTION),reach:1}}:p), pinParents, pin.id, masses).map(p => raw.joints[rig.pins.findIndex(pin => pin.id === p.id)].node);
64
- // Chains are derived caches, not authoritative authored topology. Older
65
- // writers used discontinuous reach gates; accept any valid ancestor suffix
66
- // and rebuild the current extent from the saved reach in the runtime.
67
- if (!chain || chain.mode !== mode || chain.joints.length > expected.length || chain.joints.some((n, i) => n !== expected[expected.length - chain.joints.length + i])) throw new Error('Motion chain does not match its hierarchy and reach.');
68
- }
69
- if (raw.helpers !== undefined && (!Array.isArray(raw.helpers) || raw.helpers.length > 256)) throw new Error('Invalid helper bindings.');
70
- const helpers = raw.helpers?.map(helper => {
71
- if (!node(helper.node) || (raw.version<3&&indices.has(helper.node)) || (raw.version>=3&&!parents.has(helper.node)) || !Array.isArray(helper.influences) || helper.influences.length > rig.pins.length) throw new Error('Invalid helper node.');
72
- const seen = new Set<number>(); let sum = 0;
73
- for (const influence of helper.influences) {
74
- if (!parents.has(influence.node) || (raw.version>=3&&raw.helpers?.some(h=>h.node===influence.node)) || seen.has(influence.node) || !Number.isFinite(influence.weight) || influence.weight <= 0) throw new Error('Invalid helper influence.');
75
- seen.add(influence.node); sum += influence.weight;
76
- }
77
- if (helper.influences.length && Math.abs(sum - 1) > 1e-4) throw new Error('Helper weights must sum to one.');
78
- indices.add(helper.node); return { node: helper.node, influences: helper.influences.map(i => ({ ...i })) };
79
- });
80
- if (raw.version >= 2 && !helpers) throw new Error('Rig requires helper bindings.');
81
- if (helpers && new Set(helpers.map(h=>h.node)).size!==helpers.length) throw new Error('Duplicate helper bone.');
82
- if ((raw.version < 4 && (raw.cloth !== undefined || rig.pins.some(p => p.cloth))) || (raw.version === 4 && !raw.cloth) || (rig.pins.some(p => p.cloth) && !raw.cloth) || (raw.cloth && (!Array.isArray(raw.cloth.links) || !Array.isArray(raw.cloth.colliders) || raw.cloth.links.length > 512 || raw.cloth.colliders.length > 4096))) throw new Error('Invalid cloth simulation data.');
83
- const pinById = new Map(rig.pins.map(p => [p.id, p]));
84
- for (const link of raw.cloth?.links ?? []) if (!link || !pinById.has(link.a) || !pinById.has(link.b) || link.a === link.b || !(pinById.get(link.a)?.cloth || pinById.get(link.b)?.cloth) || !Number.isFinite(link.length) || link.length < 0) throw new Error('Invalid cloth link.');
85
- for (const collider of raw.cloth?.colliders ?? []) if (!collider || !Array.isArray(collider.position) || collider.position.length !== 3 || !collider.position.every(Number.isFinite) || !Number.isFinite(collider.radius) || collider.radius <= 0 || !collider.driver || !pinById.has(collider.driver) || pinById.get(collider.driver)?.cloth) throw new Error('Invalid cloth collider.');
86
- return { ...rig,poseSlots:restSlot?[restSlot,...rig.poseSlots]:rig.poseSlots, version: raw.version, joints: raw.joints.map(j => ({ ...j })), chains: raw.chains.map(c => ({ ...c, joints: [...c.joints] })), ...(helpers ? { helpers } : {}), ...(raw.cloth ? { cloth: { links: raw.cloth.links.map(l => ({ ...l })), colliders: raw.cloth.colliders.map(c => ({ ...c, position: [...c.position] as [number,number,number] })) } } : {}), ...(raw.furSurface ? { furSurface: { ...raw.furSurface } } : {}) };
87
- }
1
+ import { validateRig, type JointsRig } from './joints-json.js';
2
+ import { ikChain } from './pin-ik.js';
3
+ import {DEFAULT_MOTION} from './motion-settings.js';
4
+ import type { ClothCollider, ClothLink } from './pin-cloth.js';
5
+
6
+ export const NEEDLE_DEFORMATION_RIG = 'NEEDLE_deformation_rig';
7
+ export interface RigDescriptor extends JointsRig {
8
+ version: 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9;
9
+ joints: { node: number; parent: number | null; mass: number }[];
10
+ chains: { endpoint: number; joints: number[]; mode: 'ik' | 'secondary' | 'cloth' | 'fur' }[];
11
+ helpers?: { node: number; influences: { node: number; weight: number }[] }[];
12
+ cloth?: { links: ClothLink[]; colliders: ClothCollider[] };
13
+ furSurface?: { heightTexture: number; texCoord?: number };
14
+ }
15
+ /** Validate before allocating runtime objects or accepting node references. */
16
+ export function parseRigDescriptor(value: unknown, nodeCount: number, textureCount = Infinity): RigDescriptor {
17
+ if (!value || typeof value !== 'object') throw new Error('Invalid deformation rig.');
18
+ const raw = value as RigDescriptor;
19
+ if (raw.version !== 1 && raw.version !== 2 && raw.version !== 3 && raw.version !== 4 && raw.version !== 5 && raw.version !== 6 && raw.version !== 7 && raw.version !== 8 && raw.version !== 9) throw new Error('Unsupported NEEDLE_deformation_rig version.');
20
+ if (raw.furSurface?.texCoord !== undefined && (raw.version < 8 || !Number.isInteger(raw.furSurface.texCoord) || raw.furSurface.texCoord < 0 || raw.furSurface.texCoord > 1)) throw new Error('Invalid Fur height texture coordinates.');
21
+ if (raw.furSurface && (raw.version < 6 || !Number.isInteger(raw.furSurface.heightTexture) || raw.furSurface.heightTexture < 0 || raw.furSurface.heightTexture >= textureCount)) throw new Error('Invalid Fur height texture.');
22
+ if (raw.version < 5 && raw.windStrength !== undefined) throw new Error('Wind requires NEEDLE_deformation_rig version 5.');
23
+ if (raw.activePose === 0 && raw.version < 9) throw new Error('An unselected authored pose requires NEEDLE_deformation_rig version 9.');
24
+ const restSlot=raw.poseSlots?.find(slot=>slot.slot===0);
25
+ if(restSlot && raw.version<7)throw new Error('Pose 0 requires NEEDLE_deformation_rig version 7.');
26
+ const rig = validateRig({...raw,poseSlots:raw.poseSlots?.filter(slot=>slot.slot!==0)}, raw.version>=3?256:128);
27
+ if(restSlot){
28
+ if(raw.poseSlots.filter(slot=>slot.slot===0).length!==1 || !Array.isArray(restSlot.poses) || restSlot.poses.length!==rig.pins.length ||
29
+ new Set(restSlot.poses.map(p=>p.id)).size!==rig.pins.length || restSlot.poses.some(p=>!rig.pins.some(pin=>pin.id===p.id)))throw new Error('Invalid rest pose clip.');
30
+ validateRig({pins:rig.pins,activePose:1,poseSlots:[{slot:1,poses:restSlot.poses}],poseTimes:[]},raw.version>=3?256:128);
31
+ }
32
+ if (raw.version < 5 && rig.pins.some(pin=>pin.fur)) throw new Error('Fur requires NEEDLE_deformation_rig version 5.');
33
+ if (raw.furSurface && !rig.pins.some(pin => pin.fur)) throw new Error('Fur height requires a Fur rig.');
34
+ const node = (n: number) => Number.isInteger(n) && n >= 0 && n < nodeCount;
35
+ if (!Array.isArray(raw.joints) || raw.joints.length !== rig.pins.length) throw new Error('Invalid rig joint bindings.');
36
+ const indices = new Set<number>();
37
+ for (const joint of raw.joints) {
38
+ if (!joint || !node(joint.node) || indices.has(joint.node) || (joint.parent !== null && !node(joint.parent)) || !Number.isFinite(joint.mass) || joint.mass < 0) throw new Error('Invalid rig node or mass.');
39
+ indices.add(joint.node);
40
+ }
41
+ const parents = new Map(raw.joints.map(j => [j.node, j.parent]));
42
+ for (const joint of raw.joints) {
43
+ const seen = new Set<number>(); let cursor: number | null = joint.node;
44
+ while (cursor !== null) {
45
+ if (!indices.has(cursor) || seen.has(cursor)) throw new Error('Invalid or cyclic rig hierarchy.');
46
+ seen.add(cursor); cursor = parents.get(cursor)!;
47
+ }
48
+ }
49
+ if (!Array.isArray(raw.chains) || raw.chains.length > rig.pins.length) throw new Error('Invalid motion chains.');
50
+ const endpoints = new Set<number>();
51
+ for (const chain of raw.chains) {
52
+ if (!chain || !indices.has(chain.endpoint) || endpoints.has(chain.endpoint) || !['ik', 'secondary', 'cloth', 'fur'].includes(chain.mode) || !Array.isArray(chain.joints) || !chain.joints.length || chain.joints.length > rig.pins.length || chain.joints.at(-1) !== chain.endpoint || new Set(chain.joints).size !== chain.joints.length || chain.joints.some(n => !indices.has(n))) throw new Error('Invalid motion chain.');
53
+ endpoints.add(chain.endpoint);
54
+ }
55
+ const pinParents = new Map<string, string>(), masses = new Map<string, number>();
56
+ raw.joints.forEach((j, i) => { masses.set(rig.pins[i].id, j.mass); if (j.parent !== null) pinParents.set(rig.pins[i].id, rig.pins[raw.joints.findIndex(p => p.node === j.parent)].id); });
57
+ for (const [i, pin] of rig.pins.entries()) {
58
+ const chain = raw.chains.find(c => c.endpoint === raw.joints[i].node);
59
+ const mode = !pin.guide && (pin.ik ? 'ik' : pin.wiggle ? 'secondary' : pin.cloth ? 'cloth' : pin.fur ? 'fur' : null);
60
+ if (!mode && chain) throw new Error('Unexpected motion chain.');
61
+ if (!mode) continue;
62
+ const articulated=raw.version>=3?rig.pins.filter((_,i)=>!raw.helpers?.some(h=>h.node===raw.joints[i].node)):rig.pins;
63
+ const expected = ikChain(articulated.map(p=>p.id===pin.id?{...p,motion:{...(p.motion??DEFAULT_MOTION),reach:1}}:p), pinParents, pin.id, masses).map(p => raw.joints[rig.pins.findIndex(pin => pin.id === p.id)].node);
64
+ // Chains are derived caches, not authoritative authored topology. Older
65
+ // writers used discontinuous reach gates; accept any valid ancestor suffix
66
+ // and rebuild the current extent from the saved reach in the runtime.
67
+ if (!chain || chain.mode !== mode || chain.joints.length > expected.length || chain.joints.some((n, i) => n !== expected[expected.length - chain.joints.length + i])) throw new Error('Motion chain does not match its hierarchy and reach.');
68
+ }
69
+ if (raw.helpers !== undefined && (!Array.isArray(raw.helpers) || raw.helpers.length > 256)) throw new Error('Invalid helper bindings.');
70
+ const helpers = raw.helpers?.map(helper => {
71
+ if (!node(helper.node) || (raw.version<3&&indices.has(helper.node)) || (raw.version>=3&&!parents.has(helper.node)) || !Array.isArray(helper.influences) || helper.influences.length > rig.pins.length) throw new Error('Invalid helper node.');
72
+ const seen = new Set<number>(); let sum = 0;
73
+ for (const influence of helper.influences) {
74
+ if (!parents.has(influence.node) || (raw.version>=3&&raw.helpers?.some(h=>h.node===influence.node)) || seen.has(influence.node) || !Number.isFinite(influence.weight) || influence.weight <= 0) throw new Error('Invalid helper influence.');
75
+ seen.add(influence.node); sum += influence.weight;
76
+ }
77
+ if (helper.influences.length && Math.abs(sum - 1) > 1e-4) throw new Error('Helper weights must sum to one.');
78
+ indices.add(helper.node); return { node: helper.node, influences: helper.influences.map(i => ({ ...i })) };
79
+ });
80
+ if (raw.version >= 2 && !helpers) throw new Error('Rig requires helper bindings.');
81
+ if (helpers && new Set(helpers.map(h=>h.node)).size!==helpers.length) throw new Error('Duplicate helper bone.');
82
+ if ((raw.version < 4 && (raw.cloth !== undefined || rig.pins.some(p => p.cloth))) || (raw.version === 4 && !raw.cloth) || (rig.pins.some(p => p.cloth) && !raw.cloth) || (raw.cloth && (!Array.isArray(raw.cloth.links) || !Array.isArray(raw.cloth.colliders) || raw.cloth.links.length > 512 || raw.cloth.colliders.length > 4096))) throw new Error('Invalid cloth simulation data.');
83
+ const pinById = new Map(rig.pins.map(p => [p.id, p]));
84
+ for (const link of raw.cloth?.links ?? []) if (!link || !pinById.has(link.a) || !pinById.has(link.b) || link.a === link.b || !(pinById.get(link.a)?.cloth || pinById.get(link.b)?.cloth) || !Number.isFinite(link.length) || link.length < 0) throw new Error('Invalid cloth link.');
85
+ for (const collider of raw.cloth?.colliders ?? []) if (!collider || !Array.isArray(collider.position) || collider.position.length !== 3 || !collider.position.every(Number.isFinite) || !Number.isFinite(collider.radius) || collider.radius <= 0 || !collider.driver || !pinById.has(collider.driver) || pinById.get(collider.driver)?.cloth) throw new Error('Invalid cloth collider.');
86
+ return { ...rig,poseSlots:restSlot?[restSlot,...rig.poseSlots]:rig.poseSlots, version: raw.version, joints: raw.joints.map(j => ({ ...j })), chains: raw.chains.map(c => ({ ...c, joints: [...c.joints] })), ...(helpers ? { helpers } : {}), ...(raw.cloth ? { cloth: { links: raw.cloth.links.map(l => ({ ...l })), colliders: raw.cloth.colliders.map(c => ({ ...c, position: [...c.position] as [number,number,number] })) } } : {}), ...(raw.furSurface ? { furSurface: { ...raw.furSurface } } : {}) };
87
+ }