@khr3/core 0.2.9 → 0.2.11
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.
- package/README.md +5 -175
- package/dist/KhrAssembly-CKW8I_2z.js +1667 -0
- package/dist/KhrAssembly-CKW8I_2z.js.map +1 -0
- package/dist/KhrAssembly.d.ts +46 -8
- package/dist/KhrCollider.d.ts +21 -7
- package/dist/clone.d.ts +40 -0
- package/dist/{khrExporter.d.ts → exporter.d.ts} +2 -2
- package/dist/{khrGlb.d.ts → glb.d.ts} +4 -3
- package/dist/index.d.ts +10 -10
- package/dist/index.js +1548 -343
- package/dist/index.js.map +1 -1
- package/dist/{khrInertia.d.ts → inertia.d.ts} +22 -8
- package/dist/{jointDynamics.d.ts → joints.d.ts} +8 -7
- package/dist/loader.d.ts +9 -0
- package/dist/types.d.ts +282 -0
- package/dist/useKhrGlb.d.ts +6 -1
- package/dist/vehicle.d.ts +35 -2
- package/dist/vehicle.js +435 -539
- package/dist/vehicle.js.map +1 -1
- package/dist/wheelLoads.d.ts +5 -4
- package/package.json +6 -25
- package/AGENTS.md +0 -12
- package/CLAUDE.md +0 -8
- package/dist/AssemblyRootBody.d.ts +0 -16
- package/dist/BundleSceneHost.d.ts +0 -23
- package/dist/KhrVehicle.d.ts +0 -20
- package/dist/assemblyPlan.d.ts +0 -61
- package/dist/curves-Cf8okB-M.js +0 -542
- package/dist/curves-Cf8okB-M.js.map +0 -1
- package/dist/curves.d.ts +0 -378
- package/dist/curves.js +0 -1
- package/dist/khrBundleFinalize.d.ts +0 -15
- package/dist/khrClone.d.ts +0 -14
- package/dist/khrConfigTypes.d.ts +0 -62
- package/dist/khrExportEncoders.d.ts +0 -8
- package/dist/khrExtras.d.ts +0 -22
- package/dist/khrInertia-DgCUCDTx.js +0 -247
- package/dist/khrInertia-DgCUCDTx.js.map +0 -1
- package/dist/khrJointParser.d.ts +0 -3
- package/dist/khrLoader.d.ts +0 -7
- package/dist/khrMassProps.d.ts +0 -16
- package/dist/khrParsers.d.ts +0 -10
- package/dist/khrPlugin.d.ts +0 -8
- package/dist/khrPluginHelpers.d.ts +0 -26
- package/dist/khrRotorDecoders.d.ts +0 -4
- package/dist/khrShapeParser.d.ts +0 -8
- package/dist/khrTypes.d.ts +0 -141
- package/dist/khrVehicleChassis.d.ts +0 -20
- package/dist/khrVehicleJointHelpers.d.ts +0 -37
- package/dist/khrVehicleJoints.d.ts +0 -30
- package/dist/khrVehicleRotors.d.ts +0 -22
- package/dist/khrVehicleSlots.d.ts +0 -41
- package/dist/khrVehicleTypes.d.ts +0 -52
- package/dist/pacejka.d.ts +0 -22
- package/dist/physicsEcs-C00N5zCh.js +0 -914
- package/dist/physicsEcs-C00N5zCh.js.map +0 -1
- package/dist/physicsEcs.d.ts +0 -25
- package/dist/sand3d/sand3dAssemblyHelpers.d.ts +0 -30
- package/dist/sand3d/sand3dCavityOps.d.ts +0 -15
- package/dist/sand3d/sand3dCompiler.d.ts +0 -121
- package/dist/sand3d/sand3dEvaluator.d.ts +0 -80
- package/dist/sand3d/sand3dFramesAndEdits.d.ts +0 -30
- package/dist/sand3d/sand3dHullAndSweep.d.ts +0 -27
- package/dist/sand3d/sand3dParser.d.ts +0 -6
- package/dist/sand3d/sand3dPrimitives.d.ts +0 -50
- package/dist/sand3d/sand3dProfiles.d.ts +0 -32
- package/dist/sand3d/sand3dRecipes.d.ts +0 -5
- package/dist/sand3d/sand3dShellOp.d.ts +0 -8
- package/dist/sand3d/sand3dSolids.d.ts +0 -32
- package/dist/sand3d/sand3dTokenizer.d.ts +0 -8
- package/dist/sand3d/sand3dTypes.d.ts +0 -157
- package/dist/sand3d-DQ8-OMlo.js +0 -5045
- package/dist/sand3d-DQ8-OMlo.js.map +0 -1
- package/dist/sand3d.d.ts +0 -23
- package/dist/sand3d.js +0 -1
- package/docs/api-reference.md +0 -383
|
@@ -1,914 +0,0 @@
|
|
|
1
|
-
import { u as KhrValidationError } from "./khrInertia-DgCUCDTx.js";
|
|
2
|
-
import { createContext, useCallback, useEffect, useLayoutEffect, useRef } from "react";
|
|
3
|
-
import { BallCollider, CapsuleCollider, CoefficientCombineRule, ConvexHullCollider, CuboidCollider, CylinderCollider, TrimeshCollider } from "@react-three/rapier";
|
|
4
|
-
import { jsx } from "react/jsx-runtime";
|
|
5
|
-
import * as THREE from "three";
|
|
6
|
-
// src/khrClone.ts
|
|
7
|
-
var mountedScenes = /* @__PURE__ */ new WeakSet();
|
|
8
|
-
var clonedScenes = /* @__PURE__ */ new WeakSet();
|
|
9
|
-
var disposedScenes = /* @__PURE__ */ new WeakSet();
|
|
10
|
-
var activeSubtreeRestorers = [];
|
|
11
|
-
function registerMountedSubtree(restoreForClone) {
|
|
12
|
-
activeSubtreeRestorers.push(restoreForClone);
|
|
13
|
-
return () => {
|
|
14
|
-
const idx = activeSubtreeRestorers.indexOf(restoreForClone);
|
|
15
|
-
if (idx >= 0) activeSubtreeRestorers.splice(idx, 1);
|
|
16
|
-
};
|
|
17
|
-
}
|
|
18
|
-
function claimBundleMount(bundle, ownerComponent) {
|
|
19
|
-
if (disposedScenes.has(bundle.scene)) throw new KhrValidationError(`${ownerComponent}: KhrAssetBundle "${bundle.scene.name || bundle.scene.uuid}" has already been disposed`);
|
|
20
|
-
if (mountedScenes.has(bundle.scene)) throw new KhrValidationError(`${ownerComponent}: KhrAssetBundle "${bundle.scene.name || bundle.scene.uuid}" is already mounted; call cloneKhrBundle(template) per concurrent instance`);
|
|
21
|
-
mountedScenes.add(bundle.scene);
|
|
22
|
-
}
|
|
23
|
-
function releaseBundleMount(bundle) {
|
|
24
|
-
mountedScenes.delete(bundle.scene);
|
|
25
|
-
}
|
|
26
|
-
function remapNode(srcRoot, node, dstRoot) {
|
|
27
|
-
if (node === srcRoot) return dstRoot;
|
|
28
|
-
const path = [];
|
|
29
|
-
let cur = node;
|
|
30
|
-
while (cur && cur !== srcRoot) {
|
|
31
|
-
const parent = cur.parent;
|
|
32
|
-
if (!parent) throw new KhrValidationError(`cloneKhrBundle: body node "${node.name}" is detached from bundle.scene`);
|
|
33
|
-
const idx = parent.children.indexOf(cur);
|
|
34
|
-
if (idx < 0) throw new KhrValidationError(`cloneKhrBundle: child index missing for "${node.name}"`);
|
|
35
|
-
path.push(idx);
|
|
36
|
-
cur = parent;
|
|
37
|
-
}
|
|
38
|
-
let out = dstRoot;
|
|
39
|
-
for (let i = path.length - 1; i >= 0; i--) out = out.children[path[i]];
|
|
40
|
-
return out;
|
|
41
|
-
}
|
|
42
|
-
function cloneCurve(curve) {
|
|
43
|
-
if (!curve) return void 0;
|
|
44
|
-
return {
|
|
45
|
-
...curve,
|
|
46
|
-
points: curve.points.map((pt) => ({ ...pt }))
|
|
47
|
-
};
|
|
48
|
-
}
|
|
49
|
-
function cloneCarTune(tune) {
|
|
50
|
-
if (!tune) return void 0;
|
|
51
|
-
return {
|
|
52
|
-
...tune,
|
|
53
|
-
...tune.gearRatios ? { gearRatios: [...tune.gearRatios] } : {},
|
|
54
|
-
...tune.engineTorqueCurveData ? { engineTorqueCurveData: cloneCurve(tune.engineTorqueCurveData) } : {},
|
|
55
|
-
...tune.steerAngleCurveData ? { steerAngleCurveData: cloneCurve(tune.steerAngleCurveData) } : {}
|
|
56
|
-
};
|
|
57
|
-
}
|
|
58
|
-
/**
|
|
59
|
-
* Clones a parsed `KhrAssetBundle` in O(nodes) while sharing parsed `.glb` buffers,
|
|
60
|
-
* GPU `BufferGeometry` / `Material` buffers, and Rapier `Float32Array` collider vertices.
|
|
61
|
-
*/
|
|
62
|
-
function cloneKhrBundle(bundle) {
|
|
63
|
-
if (disposedScenes.has(bundle.scene)) throw new KhrValidationError(`cloneKhrBundle: cannot clone disposed KhrAssetBundle "${bundle.scene.name || bundle.scene.uuid}"`);
|
|
64
|
-
const reattach = [];
|
|
65
|
-
for (let i = activeSubtreeRestorers.length - 1; i >= 0; i--) reattach.push(activeSubtreeRestorers[i]());
|
|
66
|
-
try {
|
|
67
|
-
const scene = bundle.scene.clone(true);
|
|
68
|
-
clonedScenes.add(scene);
|
|
69
|
-
return {
|
|
70
|
-
scene,
|
|
71
|
-
bodies: bundle.bodies.map((b) => {
|
|
72
|
-
const drives = b.joint ? b.joint.drives.map((d) => ({ ...d })) : [];
|
|
73
|
-
return {
|
|
74
|
-
...b,
|
|
75
|
-
centerOfMass: [...b.centerOfMass],
|
|
76
|
-
inertiaDiagonal: [...b.inertiaDiagonal],
|
|
77
|
-
inertiaOrientation: [...b.inertiaOrientation],
|
|
78
|
-
...b.linearVelocity ? { linearVelocity: [...b.linearVelocity] } : {},
|
|
79
|
-
...b.angularVelocity ? { angularVelocity: [...b.angularVelocity] } : {},
|
|
80
|
-
localPosition: [...b.localPosition],
|
|
81
|
-
localQuaternion: [...b.localQuaternion],
|
|
82
|
-
colliders: b.colliders.map((c) => ({
|
|
83
|
-
...c,
|
|
84
|
-
shape: c.shape.type === "box" ? {
|
|
85
|
-
...c.shape,
|
|
86
|
-
size: [...c.shape.size]
|
|
87
|
-
} : { ...c.shape },
|
|
88
|
-
localPosition: [...c.localPosition],
|
|
89
|
-
localQuaternion: [...c.localQuaternion]
|
|
90
|
-
})),
|
|
91
|
-
joint: b.joint ? {
|
|
92
|
-
...b.joint,
|
|
93
|
-
axis: [...b.joint.axis],
|
|
94
|
-
limits: b.joint.limits ? [...b.joint.limits] : null,
|
|
95
|
-
drive: drives[0] ?? (b.joint.drive ? { ...b.joint.drive } : null),
|
|
96
|
-
drives,
|
|
97
|
-
anchor1: [...b.joint.anchor1],
|
|
98
|
-
anchor2: [...b.joint.anchor2]
|
|
99
|
-
} : null,
|
|
100
|
-
...b.breakaway ? { breakaway: { ...b.breakaway } } : {},
|
|
101
|
-
object: remapNode(bundle.scene, b.object, scene)
|
|
102
|
-
};
|
|
103
|
-
}),
|
|
104
|
-
wheels: bundle.wheels.map((w) => ({
|
|
105
|
-
...w,
|
|
106
|
-
object: remapNode(bundle.scene, w.object, scene),
|
|
107
|
-
position: [...w.position],
|
|
108
|
-
...w.quaternion ? { quaternion: [...w.quaternion] } : {},
|
|
109
|
-
...w.lngSlipRatioCurveData ? { lngSlipRatioCurveData: cloneCurve(w.lngSlipRatioCurveData) } : {},
|
|
110
|
-
...w.latSlipRatioCurveData ? { latSlipRatioCurveData: cloneCurve(w.latSlipRatioCurveData) } : {}
|
|
111
|
-
})),
|
|
112
|
-
propellers: bundle.propellers.map((p) => ({
|
|
113
|
-
...p,
|
|
114
|
-
object: remapNode(bundle.scene, p.object, scene),
|
|
115
|
-
position: [...p.position],
|
|
116
|
-
...p.quaternion ? { quaternion: [...p.quaternion] } : {}
|
|
117
|
-
})),
|
|
118
|
-
sockets: Object.fromEntries(Object.entries(bundle.sockets).map(([k, v]) => [k, [...v]])),
|
|
119
|
-
totalDynamicMass: bundle.totalDynamicMass,
|
|
120
|
-
centerOfMass: [...bundle.centerOfMass],
|
|
121
|
-
...bundle.tune ? { tune: cloneCarTune(bundle.tune) } : {},
|
|
122
|
-
...bundle.droneTune ? { droneTune: { ...bundle.droneTune } } : {}
|
|
123
|
-
};
|
|
124
|
-
} finally {
|
|
125
|
-
for (let i = reattach.length - 1; i >= 0; i--) reattach[i]();
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
/**
|
|
129
|
-
* Releases clone mount ownership while keeping shared template GPU resources intact.
|
|
130
|
-
* When called on a root template bundle, disposes unique GPU geometries, materials, and textures once.
|
|
131
|
-
*/
|
|
132
|
-
function disposeKhrBundle(bundle) {
|
|
133
|
-
mountedScenes.delete(bundle.scene);
|
|
134
|
-
if (clonedScenes.has(bundle.scene) || disposedScenes.has(bundle.scene)) {
|
|
135
|
-
disposedScenes.add(bundle.scene);
|
|
136
|
-
return;
|
|
137
|
-
}
|
|
138
|
-
disposedScenes.add(bundle.scene);
|
|
139
|
-
const geos = /* @__PURE__ */ new Set();
|
|
140
|
-
const mats = /* @__PURE__ */ new Set();
|
|
141
|
-
const texs = /* @__PURE__ */ new Set();
|
|
142
|
-
const disposeTree = (obj) => {
|
|
143
|
-
obj.traverse((child) => {
|
|
144
|
-
const mesh = child;
|
|
145
|
-
if (mesh.geometry && !geos.has(mesh.geometry)) {
|
|
146
|
-
geos.add(mesh.geometry);
|
|
147
|
-
mesh.geometry.dispose();
|
|
148
|
-
}
|
|
149
|
-
const matList = Array.isArray(mesh.material) ? mesh.material : mesh.material ? [mesh.material] : [];
|
|
150
|
-
for (const mat of matList) if (!mats.has(mat)) {
|
|
151
|
-
mats.add(mat);
|
|
152
|
-
for (const val of Object.values(mat)) if (val && typeof val === "object" && "isTexture" in val && val.isTexture) {
|
|
153
|
-
const tex = val;
|
|
154
|
-
if (!texs.has(tex)) {
|
|
155
|
-
texs.add(tex);
|
|
156
|
-
tex.dispose();
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
mat.dispose();
|
|
160
|
-
}
|
|
161
|
-
});
|
|
162
|
-
};
|
|
163
|
-
disposeTree(bundle.scene);
|
|
164
|
-
for (const w of bundle.wheels) disposeTree(w.object);
|
|
165
|
-
for (const p of bundle.propellers) disposeTree(p.object);
|
|
166
|
-
}
|
|
167
|
-
// src/KhrCollider.tsx
|
|
168
|
-
var COMBINE_RULES = {
|
|
169
|
-
average: CoefficientCombineRule.Average,
|
|
170
|
-
minimum: CoefficientCombineRule.Min,
|
|
171
|
-
maximum: CoefficientCombineRule.Max,
|
|
172
|
-
multiply: CoefficientCombineRule.Multiply
|
|
173
|
-
};
|
|
174
|
-
function KhrCollider({ c, position, quaternion, massProperties, collisionGroups, solverGroups, ref }) {
|
|
175
|
-
const innerRef = useRef(null);
|
|
176
|
-
const setRef = useCallback((node) => {
|
|
177
|
-
innerRef.current = node;
|
|
178
|
-
if (typeof ref === "function") ref(node);
|
|
179
|
-
else if (ref) ref.current = node;
|
|
180
|
-
}, [ref]);
|
|
181
|
-
useEffect(() => {
|
|
182
|
-
innerRef.current?.parent()?.recomputeMassPropertiesFromColliders();
|
|
183
|
-
}, [c, massProperties]);
|
|
184
|
-
const effCollisionGroups = c.collisionGroups ?? collisionGroups;
|
|
185
|
-
const effSolverGroups = c.collisionGroups === void 0 ? solverGroups : void 0;
|
|
186
|
-
const common = {
|
|
187
|
-
ref: setRef,
|
|
188
|
-
position: position ?? c.localPosition,
|
|
189
|
-
quaternion: quaternion ?? c.localQuaternion,
|
|
190
|
-
...massProperties ? { massProperties } : { density: 0 },
|
|
191
|
-
...c.friction === void 0 ? {} : { friction: c.friction },
|
|
192
|
-
...c.restitution === void 0 ? {} : { restitution: c.restitution },
|
|
193
|
-
...c.frictionCombine === void 0 ? {} : { frictionCombineRule: COMBINE_RULES[c.frictionCombine] },
|
|
194
|
-
...c.restitutionCombine === void 0 ? {} : { restitutionCombineRule: COMBINE_RULES[c.restitutionCombine] },
|
|
195
|
-
...effCollisionGroups === void 0 ? {} : { collisionGroups: effCollisionGroups },
|
|
196
|
-
...effSolverGroups === void 0 ? {} : { solverGroups: effSolverGroups }
|
|
197
|
-
};
|
|
198
|
-
const { shape } = c;
|
|
199
|
-
if (shape.type === "box") {
|
|
200
|
-
const [sx, sy, sz] = shape.size;
|
|
201
|
-
return /* @__PURE__ */ jsx(CuboidCollider, {
|
|
202
|
-
args: [
|
|
203
|
-
sx * .5,
|
|
204
|
-
sy * .5,
|
|
205
|
-
sz * .5
|
|
206
|
-
],
|
|
207
|
-
...common
|
|
208
|
-
});
|
|
209
|
-
}
|
|
210
|
-
if (shape.type === "sphere") return /* @__PURE__ */ jsx(BallCollider, {
|
|
211
|
-
args: [shape.radius],
|
|
212
|
-
...common
|
|
213
|
-
});
|
|
214
|
-
if (shape.type === "cylinder") return /* @__PURE__ */ jsx(CylinderCollider, {
|
|
215
|
-
args: [shape.height * .5, shape.radiusTop],
|
|
216
|
-
...common
|
|
217
|
-
});
|
|
218
|
-
if (shape.type === "capsule") return /* @__PURE__ */ jsx(CapsuleCollider, {
|
|
219
|
-
args: [shape.height * .5, shape.radiusTop],
|
|
220
|
-
...common
|
|
221
|
-
});
|
|
222
|
-
if (shape.type === "convexHull") return /* @__PURE__ */ jsx(ConvexHullCollider, {
|
|
223
|
-
args: [shape.vertices],
|
|
224
|
-
...common
|
|
225
|
-
});
|
|
226
|
-
return /* @__PURE__ */ jsx(TrimeshCollider, {
|
|
227
|
-
args: [shape.vertices, shape.indices],
|
|
228
|
-
...common
|
|
229
|
-
});
|
|
230
|
-
}
|
|
231
|
-
// src/khrMassProps.ts
|
|
232
|
-
var _pos = new THREE.Vector3();
|
|
233
|
-
var _quat = new THREE.Quaternion();
|
|
234
|
-
var _qExtra = new THREE.Quaternion();
|
|
235
|
-
var _comVec = new THREE.Vector3();
|
|
236
|
-
var _one = new THREE.Vector3(1, 1, 1);
|
|
237
|
-
var _mat = new THREE.Matrix4();
|
|
238
|
-
var _tmpMat = new THREE.Matrix4();
|
|
239
|
-
var _rCom = new THREE.Vector3();
|
|
240
|
-
function bodyPoseMatrix(body, target = new THREE.Matrix4()) {
|
|
241
|
-
_pos.fromArray(body.localPosition);
|
|
242
|
-
_quat.fromArray(body.localQuaternion);
|
|
243
|
-
return target.compose(_pos, _quat, _one);
|
|
244
|
-
}
|
|
245
|
-
function jacobiPrincipalInertia(ixx, iyy, izz, ixy, ixz, iyz) {
|
|
246
|
-
const a = [
|
|
247
|
-
[
|
|
248
|
-
ixx,
|
|
249
|
-
ixy,
|
|
250
|
-
ixz
|
|
251
|
-
],
|
|
252
|
-
[
|
|
253
|
-
ixy,
|
|
254
|
-
iyy,
|
|
255
|
-
iyz
|
|
256
|
-
],
|
|
257
|
-
[
|
|
258
|
-
ixz,
|
|
259
|
-
iyz,
|
|
260
|
-
izz
|
|
261
|
-
]
|
|
262
|
-
];
|
|
263
|
-
const v = [
|
|
264
|
-
[
|
|
265
|
-
1,
|
|
266
|
-
0,
|
|
267
|
-
0
|
|
268
|
-
],
|
|
269
|
-
[
|
|
270
|
-
0,
|
|
271
|
-
1,
|
|
272
|
-
0
|
|
273
|
-
],
|
|
274
|
-
[
|
|
275
|
-
0,
|
|
276
|
-
0,
|
|
277
|
-
1
|
|
278
|
-
]
|
|
279
|
-
];
|
|
280
|
-
for (let iter = 0; iter < 32; iter++) {
|
|
281
|
-
const tr = Math.max(a[0][0] + a[1][1] + a[2][2], 0);
|
|
282
|
-
let p = 0, q = 1, maxOff = Math.abs(a[0][1]);
|
|
283
|
-
if (Math.abs(a[0][2]) > maxOff) {
|
|
284
|
-
p = 0;
|
|
285
|
-
q = 2;
|
|
286
|
-
maxOff = Math.abs(a[0][2]);
|
|
287
|
-
}
|
|
288
|
-
if (Math.abs(a[1][2]) > maxOff) {
|
|
289
|
-
p = 1;
|
|
290
|
-
q = 2;
|
|
291
|
-
maxOff = Math.abs(a[1][2]);
|
|
292
|
-
}
|
|
293
|
-
if (maxOff <= 1e-12 * tr) break;
|
|
294
|
-
const apq = a[p][q], app = a[p][p], aqq = a[q][q];
|
|
295
|
-
const tau = (aqq - app) / (2 * apq);
|
|
296
|
-
const t = tau >= 0 ? 1 / (tau + Math.sqrt(1 + tau * tau)) : -1 / (-tau + Math.sqrt(1 + tau * tau));
|
|
297
|
-
const c = 1 / Math.sqrt(1 + t * t), s = t * c;
|
|
298
|
-
const r = 3 - p - q;
|
|
299
|
-
a[p][p] = c * c * app - 2 * s * c * apq + s * s * aqq;
|
|
300
|
-
a[q][q] = s * s * app + 2 * s * c * apq + c * c * aqq;
|
|
301
|
-
a[p][q] = a[q][p] = 0;
|
|
302
|
-
const arp = a[r][p], arq = a[r][q];
|
|
303
|
-
a[r][p] = a[p][r] = c * arp - s * arq;
|
|
304
|
-
a[r][q] = a[q][r] = s * arp + c * arq;
|
|
305
|
-
for (let i = 0; i < 3; i++) {
|
|
306
|
-
const vip = v[i][p], viq = v[i][q];
|
|
307
|
-
v[i][p] = c * vip - s * viq;
|
|
308
|
-
v[i][q] = s * vip + c * viq;
|
|
309
|
-
}
|
|
310
|
-
}
|
|
311
|
-
if (v[0][0] * (v[1][1] * v[2][2] - v[1][2] * v[2][1]) - v[0][1] * (v[1][0] * v[2][2] - v[1][2] * v[2][0]) + v[0][2] * (v[1][0] * v[2][1] - v[1][1] * v[2][0]) < 0) {
|
|
312
|
-
v[0][2] = -v[0][2];
|
|
313
|
-
v[1][2] = -v[1][2];
|
|
314
|
-
v[2][2] = -v[2][2];
|
|
315
|
-
}
|
|
316
|
-
_mat.set(v[0][0], v[0][1], v[0][2], 0, v[1][0], v[1][1], v[1][2], 0, v[2][0], v[2][1], v[2][2], 0, 0, 0, 0, 1);
|
|
317
|
-
_quat.setFromRotationMatrix(_mat).normalize();
|
|
318
|
-
return {
|
|
319
|
-
inertia: [
|
|
320
|
-
a[0][0],
|
|
321
|
-
a[1][1],
|
|
322
|
-
a[2][2]
|
|
323
|
-
],
|
|
324
|
-
inertiaFrame: [
|
|
325
|
-
_quat.x,
|
|
326
|
-
_quat.y,
|
|
327
|
-
_quat.z,
|
|
328
|
-
_quat.w
|
|
329
|
-
]
|
|
330
|
-
};
|
|
331
|
-
}
|
|
332
|
-
function resolvePose(body, invFrame) {
|
|
333
|
-
if (invFrame) {
|
|
334
|
-
_mat.multiplyMatrices(invFrame, bodyPoseMatrix(body, _tmpMat)).decompose(_pos, _quat, _comVec);
|
|
335
|
-
_quat.normalize();
|
|
336
|
-
} else {
|
|
337
|
-
_pos.fromArray(body.localPosition);
|
|
338
|
-
_quat.fromArray(body.localQuaternion).normalize();
|
|
339
|
-
}
|
|
340
|
-
}
|
|
341
|
-
function computeLumpedMassProps(bodies, invFrame) {
|
|
342
|
-
const dyn = bodies.filter((b) => !b.isKinematic && b.mass > 0);
|
|
343
|
-
if (dyn.length === 0) return null;
|
|
344
|
-
let totalMass = 0, mx = 0, my = 0, mz = 0;
|
|
345
|
-
for (const b of dyn) {
|
|
346
|
-
resolvePose(b, invFrame);
|
|
347
|
-
_rCom.fromArray(b.centerOfMass).applyQuaternion(_quat);
|
|
348
|
-
totalMass += b.mass;
|
|
349
|
-
mx += (_pos.x + _rCom.x) * b.mass;
|
|
350
|
-
my += (_pos.y + _rCom.y) * b.mass;
|
|
351
|
-
mz += (_pos.z + _rCom.z) * b.mass;
|
|
352
|
-
}
|
|
353
|
-
if (!(totalMass > 0)) return null;
|
|
354
|
-
const com = [
|
|
355
|
-
mx / totalMass,
|
|
356
|
-
my / totalMass,
|
|
357
|
-
mz / totalMass
|
|
358
|
-
];
|
|
359
|
-
let ixx = 0, iyy = 0, izz = 0, ixy = 0, ixz = 0, iyz = 0;
|
|
360
|
-
const m = _mat.elements;
|
|
361
|
-
for (const b of dyn) {
|
|
362
|
-
resolvePose(b, invFrame);
|
|
363
|
-
_rCom.fromArray(b.centerOfMass).applyQuaternion(_quat);
|
|
364
|
-
const dx = _pos.x + _rCom.x - com[0];
|
|
365
|
-
const dy = _pos.y + _rCom.y - com[1];
|
|
366
|
-
const dz = _pos.z + _rCom.z - com[2];
|
|
367
|
-
_mat.makeRotationFromQuaternion(_quat.multiply(_qExtra.fromArray(b.inertiaOrientation)));
|
|
368
|
-
const [d0, d1, d2] = b.inertiaDiagonal;
|
|
369
|
-
ixx += m[0] * m[0] * d0 + m[4] * m[4] * d1 + m[8] * m[8] * d2 + b.mass * (dy * dy + dz * dz);
|
|
370
|
-
iyy += m[1] * m[1] * d0 + m[5] * m[5] * d1 + m[9] * m[9] * d2 + b.mass * (dx * dx + dz * dz);
|
|
371
|
-
izz += m[2] * m[2] * d0 + m[6] * m[6] * d1 + m[10] * m[10] * d2 + b.mass * (dx * dx + dy * dy);
|
|
372
|
-
ixy += m[0] * m[1] * d0 + m[4] * m[5] * d1 + m[8] * m[9] * d2 - b.mass * dx * dy;
|
|
373
|
-
ixz += m[0] * m[2] * d0 + m[4] * m[6] * d1 + m[8] * m[10] * d2 - b.mass * dx * dz;
|
|
374
|
-
iyz += m[1] * m[2] * d0 + m[5] * m[6] * d1 + m[9] * m[10] * d2 - b.mass * dy * dz;
|
|
375
|
-
}
|
|
376
|
-
if (dyn.length === 1) {
|
|
377
|
-
const b = dyn[0];
|
|
378
|
-
resolvePose(b, invFrame);
|
|
379
|
-
_quat.multiply(_qExtra.fromArray(b.inertiaOrientation)).normalize();
|
|
380
|
-
return {
|
|
381
|
-
mass: totalMass,
|
|
382
|
-
com,
|
|
383
|
-
inertia: [
|
|
384
|
-
b.inertiaDiagonal[0],
|
|
385
|
-
b.inertiaDiagonal[1],
|
|
386
|
-
b.inertiaDiagonal[2]
|
|
387
|
-
],
|
|
388
|
-
inertiaFrame: [
|
|
389
|
-
_quat.x,
|
|
390
|
-
_quat.y,
|
|
391
|
-
_quat.z,
|
|
392
|
-
_quat.w
|
|
393
|
-
]
|
|
394
|
-
};
|
|
395
|
-
}
|
|
396
|
-
const diagSum = Math.max(ixx + iyy + izz, 0);
|
|
397
|
-
if (Math.max(Math.abs(ixy), Math.abs(ixz), Math.abs(iyz)) <= 1e-12 * diagSum) return {
|
|
398
|
-
mass: totalMass,
|
|
399
|
-
com,
|
|
400
|
-
inertia: [
|
|
401
|
-
ixx,
|
|
402
|
-
iyy,
|
|
403
|
-
izz
|
|
404
|
-
],
|
|
405
|
-
inertiaFrame: [
|
|
406
|
-
0,
|
|
407
|
-
0,
|
|
408
|
-
0,
|
|
409
|
-
1
|
|
410
|
-
]
|
|
411
|
-
};
|
|
412
|
-
const { inertia, inertiaFrame } = jacobiPrincipalInertia(ixx, iyy, izz, ixy, ixz, iyz);
|
|
413
|
-
return {
|
|
414
|
-
mass: totalMass,
|
|
415
|
-
com,
|
|
416
|
-
inertia,
|
|
417
|
-
inertiaFrame
|
|
418
|
-
};
|
|
419
|
-
}
|
|
420
|
-
// src/assemblyPlan.ts
|
|
421
|
-
function matrixToRoot(node, sceneRoot) {
|
|
422
|
-
const out = new THREE.Matrix4();
|
|
423
|
-
for (let cur = node; cur && cur !== sceneRoot; cur = cur.parent) {
|
|
424
|
-
if (!cur.parent) throw new KhrValidationError(`Node "${node.name}" is detached from bundle.scene`);
|
|
425
|
-
cur.updateMatrix();
|
|
426
|
-
out.premultiply(cur.matrix);
|
|
427
|
-
}
|
|
428
|
-
return out;
|
|
429
|
-
}
|
|
430
|
-
function lowestCommonAncestor(nodes, sceneRoot) {
|
|
431
|
-
const chain = (n) => {
|
|
432
|
-
const list = [];
|
|
433
|
-
for (let cur = n; cur; cur = cur.parent) {
|
|
434
|
-
list.push(cur);
|
|
435
|
-
if (cur === sceneRoot) break;
|
|
436
|
-
}
|
|
437
|
-
return list;
|
|
438
|
-
};
|
|
439
|
-
let shared = new Set(chain(nodes[0]));
|
|
440
|
-
for (let i = 1; i < nodes.length; i++) {
|
|
441
|
-
const setI = new Set(chain(nodes[i]));
|
|
442
|
-
shared = new Set([...shared].filter((x) => setI.has(x)));
|
|
443
|
-
}
|
|
444
|
-
return chain(nodes[0]).find((c) => shared.has(c)) ?? sceneRoot;
|
|
445
|
-
}
|
|
446
|
-
function toColliderMass(lumped, colMat, colQuat) {
|
|
447
|
-
const com = new THREE.Vector3().fromArray(lumped.com);
|
|
448
|
-
const frame = new THREE.Quaternion().fromArray(lumped.inertiaFrame);
|
|
449
|
-
if (colMat && colQuat) {
|
|
450
|
-
com.applyMatrix4(colMat.clone().invert());
|
|
451
|
-
frame.premultiply(colQuat.clone().invert()).normalize();
|
|
452
|
-
}
|
|
453
|
-
return {
|
|
454
|
-
mass: lumped.mass,
|
|
455
|
-
centerOfMass: {
|
|
456
|
-
x: com.x,
|
|
457
|
-
y: com.y,
|
|
458
|
-
z: com.z
|
|
459
|
-
},
|
|
460
|
-
principalAngularInertia: {
|
|
461
|
-
x: lumped.inertia[0],
|
|
462
|
-
y: lumped.inertia[1],
|
|
463
|
-
z: lumped.inertia[2]
|
|
464
|
-
},
|
|
465
|
-
angularInertiaLocalFrame: {
|
|
466
|
-
x: frame.x,
|
|
467
|
-
y: frame.y,
|
|
468
|
-
z: frame.z,
|
|
469
|
-
w: frame.w
|
|
470
|
-
}
|
|
471
|
-
};
|
|
472
|
-
}
|
|
473
|
-
function planOwnColliders(body, lumped, frameQuat) {
|
|
474
|
-
const one = new THREE.Vector3(1, 1, 1);
|
|
475
|
-
const frameMat = frameQuat ? new THREE.Matrix4().makeRotationFromQuaternion(frameQuat) : null;
|
|
476
|
-
const pos = new THREE.Vector3(), quat = new THREE.Quaternion(), scl = new THREE.Vector3();
|
|
477
|
-
return body.colliders.map((spec, idx) => {
|
|
478
|
-
const colQuat = new THREE.Quaternion().fromArray(spec.localQuaternion);
|
|
479
|
-
const local = new THREE.Matrix4().compose(new THREE.Vector3().fromArray(spec.localPosition), colQuat, one);
|
|
480
|
-
if (frameMat) local.premultiply(frameMat);
|
|
481
|
-
local.decompose(pos, quat, scl);
|
|
482
|
-
quat.normalize();
|
|
483
|
-
const planned = {
|
|
484
|
-
spec,
|
|
485
|
-
local,
|
|
486
|
-
position: [
|
|
487
|
-
pos.x,
|
|
488
|
-
pos.y,
|
|
489
|
-
pos.z
|
|
490
|
-
],
|
|
491
|
-
quaternion: [
|
|
492
|
-
quat.x,
|
|
493
|
-
quat.y,
|
|
494
|
-
quat.z,
|
|
495
|
-
quat.w
|
|
496
|
-
]
|
|
497
|
-
};
|
|
498
|
-
if (idx === 0 && lumped) planned.massProperties = toColliderMass(lumped, local, quat);
|
|
499
|
-
return planned;
|
|
500
|
-
});
|
|
501
|
-
}
|
|
502
|
-
function isSceneDescendant(node, ancestor) {
|
|
503
|
-
for (let cur = node; cur; cur = cur.parent) if (cur === ancestor) return true;
|
|
504
|
-
return false;
|
|
505
|
-
}
|
|
506
|
-
function approxPartInertia(b, axisChild) {
|
|
507
|
-
if (b.joint?.kind === "fixed") return 0;
|
|
508
|
-
if (b.joint?.kind === "prismatic") return b.mass;
|
|
509
|
-
const ax = new THREE.Vector3().fromArray(axisChild).normalize();
|
|
510
|
-
const qInv = new THREE.Quaternion().fromArray(b.inertiaOrientation).invert();
|
|
511
|
-
const axLocal = ax.clone().applyQuaternion(qInv);
|
|
512
|
-
const [d0, d1, d2] = b.inertiaDiagonal;
|
|
513
|
-
const com = new THREE.Vector3().fromArray(b.centerOfMass);
|
|
514
|
-
const along = com.dot(ax);
|
|
515
|
-
return d0 * axLocal.x * axLocal.x + d1 * axLocal.y * axLocal.y + d2 * axLocal.z * axLocal.z + b.mass * (com.lengthSq() - along * along);
|
|
516
|
-
}
|
|
517
|
-
/** Plans independent Rapier bodies for roots and jointed dynamic children connected by Rapier ImpulseJoints. */
|
|
518
|
-
function planAssembly(bundle) {
|
|
519
|
-
const byName = new Map(bundle.bodies.map((b) => [b.nodeName, b]));
|
|
520
|
-
const findRoot = (b, seen = /* @__PURE__ */ new Set()) => {
|
|
521
|
-
if (!b.joint) return b;
|
|
522
|
-
if (seen.has(b.nodeName)) throw new KhrValidationError(`Joint cycle through "${b.nodeName}"`);
|
|
523
|
-
seen.add(b.nodeName);
|
|
524
|
-
const parent = byName.get(b.joint.connectedNodeName);
|
|
525
|
-
if (!parent) throw new KhrValidationError(`Joint on "${b.nodeName}" targets "${b.joint.connectedNodeName}", which is not a KHR body`);
|
|
526
|
-
return findRoot(parent, seen);
|
|
527
|
-
};
|
|
528
|
-
const plans = bundle.bodies.filter((b) => !b.joint).map((root) => {
|
|
529
|
-
const invRoot = bodyPoseMatrix(root).clone().invert();
|
|
530
|
-
const descendants = bundle.bodies.filter((b) => b.joint && findRoot(b) === root);
|
|
531
|
-
const depthOf = (b) => b.joint ? 1 + depthOf(byName.get(b.joint.connectedNodeName)) : 0;
|
|
532
|
-
descendants.sort((a, b) => depthOf(a) - depthOf(b));
|
|
533
|
-
const partByName = /* @__PURE__ */ new Map();
|
|
534
|
-
const parts = descendants.map((b) => {
|
|
535
|
-
const joint = b.joint;
|
|
536
|
-
const parent = partByName.get(joint.connectedNodeName) ?? null;
|
|
537
|
-
if (parent && !isSceneDescendant(b.object, parent.spec.object)) throw new KhrValidationError(`Jointed node "${b.nodeName}" must be a scene-graph descendant of its jointed parent "${joint.connectedNodeName}"`);
|
|
538
|
-
const relParentMat = bodyPoseMatrix(parent ? parent.spec : root).clone().invert().multiply(bodyPoseMatrix(b));
|
|
539
|
-
const relRootMat = invRoot.clone().multiply(bodyPoseMatrix(b));
|
|
540
|
-
const rpPos = new THREE.Vector3(), rpQuat = new THREE.Quaternion(), rpScl = new THREE.Vector3();
|
|
541
|
-
relParentMat.decompose(rpPos, rpQuat, rpScl);
|
|
542
|
-
rpQuat.normalize();
|
|
543
|
-
const rrPos = new THREE.Vector3(), rrQuat = new THREE.Quaternion();
|
|
544
|
-
relRootMat.decompose(rrPos, rrQuat, rpScl);
|
|
545
|
-
rrQuat.normalize();
|
|
546
|
-
const axChildVec = new THREE.Vector3().fromArray(joint.axis).normalize();
|
|
547
|
-
const axParentVec = axChildVec.clone().applyQuaternion(rrQuat).normalize();
|
|
548
|
-
const axisChild = [
|
|
549
|
-
axChildVec.x,
|
|
550
|
-
axChildVec.y,
|
|
551
|
-
axChildVec.z
|
|
552
|
-
];
|
|
553
|
-
const axisParent = [
|
|
554
|
-
axParentVec.x,
|
|
555
|
-
axParentVec.y,
|
|
556
|
-
axParentVec.z
|
|
557
|
-
];
|
|
558
|
-
const anchorParent = parent ? [
|
|
559
|
-
rrPos.x - parent.restRelRootPos[0],
|
|
560
|
-
rrPos.y - parent.restRelRootPos[1],
|
|
561
|
-
rrPos.z - parent.restRelRootPos[2]
|
|
562
|
-
] : [
|
|
563
|
-
rpPos.x,
|
|
564
|
-
rpPos.y,
|
|
565
|
-
rpPos.z
|
|
566
|
-
];
|
|
567
|
-
const bodyAlignInv = new THREE.Matrix4().compose(new THREE.Vector3().fromArray(b.localPosition), new THREE.Quaternion().fromArray(root.localQuaternion), new THREE.Vector3(1, 1, 1)).invert();
|
|
568
|
-
const ownLumped = b.isKinematic ? null : computeLumpedMassProps([b], bodyAlignInv);
|
|
569
|
-
const colliders = planOwnColliders(b, ownLumped, rrQuat);
|
|
570
|
-
const partMass = ownLumped && colliders.length === 0 ? toColliderMass(ownLumped) : void 0;
|
|
571
|
-
const plan = {
|
|
572
|
-
spec: b,
|
|
573
|
-
joint,
|
|
574
|
-
parent,
|
|
575
|
-
restRelParentPos: anchorParent,
|
|
576
|
-
restRelParentQuat: [
|
|
577
|
-
rrQuat.x,
|
|
578
|
-
rrQuat.y,
|
|
579
|
-
rrQuat.z,
|
|
580
|
-
rrQuat.w
|
|
581
|
-
],
|
|
582
|
-
restRelRootPos: [
|
|
583
|
-
rrPos.x,
|
|
584
|
-
rrPos.y,
|
|
585
|
-
rrPos.z
|
|
586
|
-
],
|
|
587
|
-
restRelRootQuat: [
|
|
588
|
-
rrQuat.x,
|
|
589
|
-
rrQuat.y,
|
|
590
|
-
rrQuat.z,
|
|
591
|
-
rrQuat.w
|
|
592
|
-
],
|
|
593
|
-
axisParent,
|
|
594
|
-
axisChild,
|
|
595
|
-
inertia: approxPartInertia(b, axisChild),
|
|
596
|
-
...partMass ? { partMass } : {},
|
|
597
|
-
colliders
|
|
598
|
-
};
|
|
599
|
-
partByName.set(b.nodeName, plan);
|
|
600
|
-
return plan;
|
|
601
|
-
});
|
|
602
|
-
const rootLumped = root.isKinematic ? null : computeLumpedMassProps([root], invRoot);
|
|
603
|
-
const colliders = planOwnColliders(root, rootLumped);
|
|
604
|
-
const rootMass = rootLumped && colliders.length === 0 ? toColliderMass(rootLumped) : void 0;
|
|
605
|
-
const mountNode = lowestCommonAncestor([root.object, ...descendants.map((d) => d.object)], bundle.scene);
|
|
606
|
-
const mountInRoot = invRoot.clone().multiply(matrixToRoot(mountNode, bundle.scene));
|
|
607
|
-
const scenePos = new THREE.Vector3(), sceneQuat = new THREE.Quaternion(), sceneScl = new THREE.Vector3();
|
|
608
|
-
invRoot.decompose(scenePos, sceneQuat, sceneScl);
|
|
609
|
-
sceneQuat.normalize();
|
|
610
|
-
const mountPos = new THREE.Vector3(), mountQuat = new THREE.Quaternion();
|
|
611
|
-
mountInRoot.decompose(mountPos, mountQuat, sceneScl);
|
|
612
|
-
mountQuat.normalize();
|
|
613
|
-
return {
|
|
614
|
-
spec: root,
|
|
615
|
-
sceneInRootPos: [
|
|
616
|
-
scenePos.x,
|
|
617
|
-
scenePos.y,
|
|
618
|
-
scenePos.z
|
|
619
|
-
],
|
|
620
|
-
sceneInRootQuat: [
|
|
621
|
-
sceneQuat.x,
|
|
622
|
-
sceneQuat.y,
|
|
623
|
-
sceneQuat.z,
|
|
624
|
-
sceneQuat.w
|
|
625
|
-
],
|
|
626
|
-
mountNode,
|
|
627
|
-
mountInRoot,
|
|
628
|
-
mountPosition: [
|
|
629
|
-
mountPos.x,
|
|
630
|
-
mountPos.y,
|
|
631
|
-
mountPos.z
|
|
632
|
-
],
|
|
633
|
-
mountQuaternion: [
|
|
634
|
-
mountQuat.x,
|
|
635
|
-
mountQuat.y,
|
|
636
|
-
mountQuat.z,
|
|
637
|
-
mountQuat.w
|
|
638
|
-
],
|
|
639
|
-
...rootMass ? { rootMass } : {},
|
|
640
|
-
colliders,
|
|
641
|
-
parts
|
|
642
|
-
};
|
|
643
|
-
});
|
|
644
|
-
for (const a of plans) for (const b of plans) if (a !== b && isSceneDescendant(b.mountNode, a.mountNode)) throw new KhrValidationError(`Root bodies "${a.spec.nodeName}" and "${b.spec.nodeName}" share scene subtree "${a.mountNode.name}"; split them into separate glTF branches`);
|
|
645
|
-
return plans;
|
|
646
|
-
}
|
|
647
|
-
// src/BundleSceneHost.tsx
|
|
648
|
-
function captureSubtreeOrigins(objects) {
|
|
649
|
-
const parentOrders = /* @__PURE__ */ new Map();
|
|
650
|
-
const origins = /* @__PURE__ */ new Map();
|
|
651
|
-
for (const obj of objects) {
|
|
652
|
-
const origParent = obj.parent;
|
|
653
|
-
let origOrder = /* @__PURE__ */ new Map();
|
|
654
|
-
let origIndex = -1;
|
|
655
|
-
if (origParent) {
|
|
656
|
-
let order = parentOrders.get(origParent);
|
|
657
|
-
if (!order) {
|
|
658
|
-
order = /* @__PURE__ */ new Map();
|
|
659
|
-
origParent.children.forEach((child, idx) => order.set(child, idx));
|
|
660
|
-
parentOrders.set(origParent, order);
|
|
661
|
-
}
|
|
662
|
-
origOrder = order;
|
|
663
|
-
origIndex = order.get(obj) ?? origParent.children.indexOf(obj);
|
|
664
|
-
}
|
|
665
|
-
origins.set(obj, {
|
|
666
|
-
origParent,
|
|
667
|
-
origIndex,
|
|
668
|
-
origOrder,
|
|
669
|
-
origPos: obj.position.clone(),
|
|
670
|
-
origQuat: obj.quaternion.clone(),
|
|
671
|
-
origScale: obj.scale.clone()
|
|
672
|
-
});
|
|
673
|
-
}
|
|
674
|
-
return origins;
|
|
675
|
-
}
|
|
676
|
-
function restoreSubtreeOrigin(object, snap) {
|
|
677
|
-
object.position.copy(snap.origPos);
|
|
678
|
-
object.quaternion.copy(snap.origQuat);
|
|
679
|
-
object.scale.copy(snap.origScale);
|
|
680
|
-
const { origParent, origIndex, origOrder } = snap;
|
|
681
|
-
if (origParent) {
|
|
682
|
-
origParent.add(object);
|
|
683
|
-
const curIdx = origParent.children.indexOf(object);
|
|
684
|
-
if (curIdx >= 0) origParent.children.splice(curIdx, 1);
|
|
685
|
-
let insertAt = origParent.children.length;
|
|
686
|
-
if (origOrder.size > 0) {
|
|
687
|
-
for (let i = 0; i < origParent.children.length; i++) if ((origOrder.get(origParent.children[i]) ?? Infinity) > origIndex) {
|
|
688
|
-
insertAt = i;
|
|
689
|
-
break;
|
|
690
|
-
}
|
|
691
|
-
} else if (origIndex >= 0) insertAt = Math.min(origIndex, origParent.children.length);
|
|
692
|
-
origParent.children.splice(insertAt, 0, object);
|
|
693
|
-
}
|
|
694
|
-
}
|
|
695
|
-
function BundleSceneHost({ bundle, owner }) {
|
|
696
|
-
const hostRef = useRef(null);
|
|
697
|
-
useLayoutEffect(() => {
|
|
698
|
-
const host = hostRef.current;
|
|
699
|
-
if (!host) return;
|
|
700
|
-
claimBundleMount(bundle, owner);
|
|
701
|
-
host.add(bundle.scene);
|
|
702
|
-
return () => {
|
|
703
|
-
if (bundle.scene.parent === host) host.remove(bundle.scene);
|
|
704
|
-
releaseBundleMount(bundle);
|
|
705
|
-
};
|
|
706
|
-
}, [bundle, owner]);
|
|
707
|
-
return /* @__PURE__ */ jsx("group", { ref: hostRef });
|
|
708
|
-
}
|
|
709
|
-
function MountSubtreeSlot({ object, position, quaternion, origin }) {
|
|
710
|
-
const slotRef = useRef(null);
|
|
711
|
-
useLayoutEffect(() => {
|
|
712
|
-
const slot = slotRef.current;
|
|
713
|
-
if (!slot) return;
|
|
714
|
-
const snap = origin ?? captureSubtreeOrigins([object]).get(object);
|
|
715
|
-
if (snap.origParent) snap.origParent.remove(object);
|
|
716
|
-
object.position.set(0, 0, 0);
|
|
717
|
-
object.quaternion.set(0, 0, 0, 1);
|
|
718
|
-
object.scale.set(1, 1, 1);
|
|
719
|
-
slot.add(object);
|
|
720
|
-
const untrack = registerMountedSubtree(() => {
|
|
721
|
-
const p = object.position.clone(), q = object.quaternion.clone(), s = object.scale.clone();
|
|
722
|
-
if (object.parent === slot) slot.remove(object);
|
|
723
|
-
restoreSubtreeOrigin(object, snap);
|
|
724
|
-
return () => {
|
|
725
|
-
if (snap.origParent) snap.origParent.remove(object);
|
|
726
|
-
object.position.copy(p);
|
|
727
|
-
object.quaternion.copy(q);
|
|
728
|
-
object.scale.copy(s);
|
|
729
|
-
slot.add(object);
|
|
730
|
-
};
|
|
731
|
-
});
|
|
732
|
-
return () => {
|
|
733
|
-
untrack();
|
|
734
|
-
if (object.parent === slot) slot.remove(object);
|
|
735
|
-
restoreSubtreeOrigin(object, snap);
|
|
736
|
-
};
|
|
737
|
-
}, [object, origin]);
|
|
738
|
-
return /* @__PURE__ */ jsx("group", {
|
|
739
|
-
ref: slotRef,
|
|
740
|
-
position,
|
|
741
|
-
quaternion
|
|
742
|
-
});
|
|
743
|
-
}
|
|
744
|
-
var _q1 = new THREE.Quaternion();
|
|
745
|
-
var _q2 = new THREE.Quaternion();
|
|
746
|
-
var _qRel = new THREE.Quaternion();
|
|
747
|
-
var _restInv = new THREE.Quaternion();
|
|
748
|
-
var _deltaPos = new THREE.Vector3();
|
|
749
|
-
var _axWorld = new THREE.Vector3();
|
|
750
|
-
function createRapierImpulseJoint(rapier, world, parentBody, childBody, joint, anchorParent, anchorChild, axisParent, axisChild, restFrameRelQuat) {
|
|
751
|
-
const a1 = {
|
|
752
|
-
x: anchorParent[0],
|
|
753
|
-
y: anchorParent[1],
|
|
754
|
-
z: anchorParent[2]
|
|
755
|
-
};
|
|
756
|
-
const a2 = {
|
|
757
|
-
x: anchorChild[0],
|
|
758
|
-
y: anchorChild[1],
|
|
759
|
-
z: anchorChild[2]
|
|
760
|
-
};
|
|
761
|
-
const qRel = _qRel.set(restFrameRelQuat[0], restFrameRelQuat[1], restFrameRelQuat[2], restFrameRelQuat[3]).normalize();
|
|
762
|
-
const axFromChild = _axWorld.set(axisChild[0], axisChild[1], axisChild[2]).applyQuaternion(qRel);
|
|
763
|
-
const ax = (axFromChild.lengthSq() > 1e-12 ? axFromChild : _axWorld.set(axisParent[0], axisParent[1], axisParent[2])).normalize();
|
|
764
|
-
let data;
|
|
765
|
-
if (joint.kind === "fixed") {
|
|
766
|
-
const pr = parentBody.rotation();
|
|
767
|
-
const cr = childBody.rotation();
|
|
768
|
-
const bodyRel = _q1.set(pr.x, pr.y, pr.z, pr.w).invert().multiply(_q2.set(cr.x, cr.y, cr.z, cr.w)).normalize();
|
|
769
|
-
const f1 = Math.abs(bodyRel.x) + Math.abs(bodyRel.y) + Math.abs(bodyRel.z) <= 1e-6 && Math.abs(Math.abs(bodyRel.w) - 1) <= 1e-6 ? {
|
|
770
|
-
x: bodyRel.x,
|
|
771
|
-
y: bodyRel.y,
|
|
772
|
-
z: bodyRel.z,
|
|
773
|
-
w: bodyRel.w
|
|
774
|
-
} : {
|
|
775
|
-
x: qRel.x,
|
|
776
|
-
y: qRel.y,
|
|
777
|
-
z: qRel.z,
|
|
778
|
-
w: qRel.w
|
|
779
|
-
};
|
|
780
|
-
data = rapier.JointData.fixed(a1, f1, a2, {
|
|
781
|
-
x: 0,
|
|
782
|
-
y: 0,
|
|
783
|
-
z: 0,
|
|
784
|
-
w: 1
|
|
785
|
-
});
|
|
786
|
-
} else if (joint.kind === "revolute") {
|
|
787
|
-
data = rapier.JointData.revolute(a1, a2, {
|
|
788
|
-
x: ax.x,
|
|
789
|
-
y: ax.y,
|
|
790
|
-
z: ax.z
|
|
791
|
-
});
|
|
792
|
-
if (joint.limits) {
|
|
793
|
-
data.limitsEnabled = true;
|
|
794
|
-
data.limits = [joint.limits[0], joint.limits[1]];
|
|
795
|
-
}
|
|
796
|
-
} else {
|
|
797
|
-
data = rapier.JointData.prismatic(a1, a2, {
|
|
798
|
-
x: ax.x,
|
|
799
|
-
y: ax.y,
|
|
800
|
-
z: ax.z
|
|
801
|
-
});
|
|
802
|
-
if (joint.limits) {
|
|
803
|
-
data.limitsEnabled = true;
|
|
804
|
-
data.limits = [joint.limits[0], joint.limits[1]];
|
|
805
|
-
}
|
|
806
|
-
}
|
|
807
|
-
const imp = world.createImpulseJoint(data, parentBody, childBody, true);
|
|
808
|
-
imp.setContactsEnabled(Boolean(joint.enableCollision));
|
|
809
|
-
configureJointDrive(rapier, imp, joint.kind, joint.drive, joint.drive?.positionTarget ?? null);
|
|
810
|
-
return imp;
|
|
811
|
-
}
|
|
812
|
-
function configureJointDrive(rapier, imp, kind, drive, targetPos) {
|
|
813
|
-
if (kind === "fixed" || !drive) return;
|
|
814
|
-
const unit = imp;
|
|
815
|
-
unit.configureMotorModel(drive.mode === "force" ? rapier.MotorModel.ForceBased : rapier.MotorModel.AccelerationBased);
|
|
816
|
-
const effTargetPos = targetPos ?? drive.positionTarget ?? (drive.stiffness !== void 0 && drive.stiffness > 0 ? 0 : null);
|
|
817
|
-
if (effTargetPos !== null && drive.stiffness !== void 0) {
|
|
818
|
-
if (drive.velocityTarget !== void 0) unit.configureMotor(effTargetPos, drive.velocityTarget, drive.stiffness, drive.damping ?? 0);
|
|
819
|
-
else unit.configureMotorPosition(effTargetPos, drive.stiffness, drive.damping ?? 0);
|
|
820
|
-
} else if (drive.velocityTarget !== void 0) unit.configureMotorVelocity(drive.velocityTarget, drive.damping ?? 0);
|
|
821
|
-
if (drive.maxForce !== void 0) unit.setMotorMaxForce?.(drive.maxForce);
|
|
822
|
-
}
|
|
823
|
-
function measureJointCoordinate(parentBody, childBody, kind, axisParent, anchorParent, restRelQuat) {
|
|
824
|
-
if (!parentBody || !childBody || kind === "fixed") return 0;
|
|
825
|
-
const pr = parentBody.rotation();
|
|
826
|
-
const cr = childBody.rotation();
|
|
827
|
-
_q1.set(pr.x, pr.y, pr.z, pr.w);
|
|
828
|
-
_q2.set(cr.x, cr.y, cr.z, cr.w);
|
|
829
|
-
if (kind === "prismatic") {
|
|
830
|
-
const pt = parentBody.translation();
|
|
831
|
-
const ct = childBody.translation();
|
|
832
|
-
_deltaPos.set(anchorParent[0], anchorParent[1], anchorParent[2]).applyQuaternion(_q1);
|
|
833
|
-
_deltaPos.set(ct.x - (pt.x + _deltaPos.x), ct.y - (pt.y + _deltaPos.y), ct.z - (pt.z + _deltaPos.z));
|
|
834
|
-
_axWorld.set(axisParent[0], axisParent[1], axisParent[2]).applyQuaternion(_q1).normalize();
|
|
835
|
-
return _deltaPos.dot(_axWorld);
|
|
836
|
-
}
|
|
837
|
-
_restInv.set(restRelQuat[0], restRelQuat[1], restRelQuat[2], restRelQuat[3]).invert();
|
|
838
|
-
_qRel.copy(_q1).invert().multiply(_q2).premultiply(_restInv).normalize();
|
|
839
|
-
if (_qRel.w < 0) _qRel.set(-_qRel.x, -_qRel.y, -_qRel.z, -_qRel.w);
|
|
840
|
-
const sinHalf = _qRel.x * axisParent[0] + _qRel.y * axisParent[1] + _qRel.z * axisParent[2];
|
|
841
|
-
return 2 * Math.atan2(sinHalf, _qRel.w);
|
|
842
|
-
}
|
|
843
|
-
function buildJointRuntime(params) {
|
|
844
|
-
const { rapier, world, nodeName, role, kind, object, axisParent, anchorParent, restRelQuat, limits, drive, breakaway, rapierJoint, parentBody, childBody } = params;
|
|
845
|
-
const rt = {
|
|
846
|
-
nodeName,
|
|
847
|
-
role,
|
|
848
|
-
kind,
|
|
849
|
-
object,
|
|
850
|
-
axis: new THREE.Vector3(axisParent[0], axisParent[1], axisParent[2]).normalize(),
|
|
851
|
-
limits,
|
|
852
|
-
drive,
|
|
853
|
-
positionTarget: drive?.positionTarget ?? null,
|
|
854
|
-
velocityTarget: drive?.velocityTarget ?? 0,
|
|
855
|
-
rapierJoint,
|
|
856
|
-
parentBody,
|
|
857
|
-
childBody,
|
|
858
|
-
unlatched: false,
|
|
859
|
-
detached: false,
|
|
860
|
-
getCoordinate: () => measureJointCoordinate(rt.parentBody, rt.childBody, kind, axisParent, anchorParent, restRelQuat),
|
|
861
|
-
applyTarget: (target) => {
|
|
862
|
-
if (rt.detached || !rt.rapierJoint) return;
|
|
863
|
-
if (!drive || drive.stiffness === void 0) throw new KhrValidationError(`Joint "${nodeName}" has no authored drive stiffness; cannot set a position target`);
|
|
864
|
-
rt.unlatched = false;
|
|
865
|
-
rt.positionTarget = target;
|
|
866
|
-
configureJointDrive(rapier, rt.rapierJoint, kind, drive, target);
|
|
867
|
-
if (parentBody.isDynamic()) parentBody.wakeUp();
|
|
868
|
-
if (childBody.isDynamic()) childBody.wakeUp();
|
|
869
|
-
},
|
|
870
|
-
applyImpactImpulse: (impulseNs) => {
|
|
871
|
-
if (rt.detached || !breakaway || !(impulseNs > 0)) return rt.detached ? "detached" : rt.unlatched ? "unlatched" : "intact";
|
|
872
|
-
if (impulseNs >= breakaway.breakImpulseNs) {
|
|
873
|
-
if (world && rt.rapierJoint && world.getImpulseJoint(rt.rapierJoint.handle)) world.removeImpulseJoint(rt.rapierJoint, true);
|
|
874
|
-
rt.rapierJoint = null;
|
|
875
|
-
rt.detached = true;
|
|
876
|
-
rt.unlatched = true;
|
|
877
|
-
rt.positionTarget = null;
|
|
878
|
-
const ud = childBody.userData;
|
|
879
|
-
if (ud?.ecctrl) childBody.userData = {
|
|
880
|
-
...ud,
|
|
881
|
-
ecctrl: {
|
|
882
|
-
...ud.ecctrl,
|
|
883
|
-
excludeVehicleRay: false
|
|
884
|
-
}
|
|
885
|
-
};
|
|
886
|
-
for (let i = 0; i < childBody.numColliders(); i++) {
|
|
887
|
-
const col = childBody.collider(i);
|
|
888
|
-
const cg = col.collisionGroups();
|
|
889
|
-
if (cg === 196605 || cg === 1073790975) {
|
|
890
|
-
col.setCollisionGroups(4294967295);
|
|
891
|
-
col.setSolverGroups(4294967295);
|
|
892
|
-
}
|
|
893
|
-
}
|
|
894
|
-
if (childBody.isDynamic()) childBody.wakeUp();
|
|
895
|
-
return "detached";
|
|
896
|
-
}
|
|
897
|
-
if (breakaway.latchImpulseNs !== void 0 && impulseNs >= breakaway.latchImpulseNs && !rt.unlatched) {
|
|
898
|
-
rt.unlatched = true;
|
|
899
|
-
rt.positionTarget = null;
|
|
900
|
-
if (kind !== "fixed" && rt.rapierJoint) rt.rapierJoint.configureMotorPosition(0, 0, Math.min(drive?.damping ?? 1, 1.5));
|
|
901
|
-
if (childBody.isDynamic()) childBody.wakeUp();
|
|
902
|
-
return "unlatched";
|
|
903
|
-
}
|
|
904
|
-
return rt.unlatched ? "unlatched" : "intact";
|
|
905
|
-
}
|
|
906
|
-
};
|
|
907
|
-
return rt;
|
|
908
|
-
}
|
|
909
|
-
// src/physicsEcs.ts
|
|
910
|
-
/** Retained as an optional context export for compatibility; Rapier steps ImpulseJoints natively. */
|
|
911
|
-
var JointRegistryContext = createContext(null);
|
|
912
|
-
export { releaseBundleMount as _, MountSubtreeSlot as a, planAssembly as c, computeLumpedMassProps as d, KhrCollider as f, registerMountedSubtree as g, disposeKhrBundle as h, BundleSceneHost as i, toColliderMass as l, cloneKhrBundle as m, buildJointRuntime as n, captureSubtreeOrigins as o, claimBundleMount as p, createRapierImpulseJoint as r, restoreSubtreeOrigin as s, JointRegistryContext as t, bodyPoseMatrix as u };
|
|
913
|
-
|
|
914
|
-
//# sourceMappingURL=physicsEcs-C00N5zCh.js.map
|