@khr3/core 0.2.10 → 0.2.12
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 +8 -0
- package/dist/KhrAssembly.d.ts +44 -8
- package/dist/KhrCollider.d.ts +25 -7
- package/dist/clone.d.ts +45 -0
- package/dist/decoders.d.ts +30 -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 -9
- package/dist/index.js +1087 -926
- package/dist/index.js.map +1 -1
- package/dist/inertia.d.ts +41 -0
- package/dist/joints-5IgWDD2w.js +865 -0
- package/dist/joints-5IgWDD2w.js.map +1 -0
- package/dist/{jointDynamics.d.ts → joints.d.ts} +10 -7
- package/dist/loader.d.ts +9 -0
- package/dist/types.d.ts +284 -0
- package/dist/useKhrGlb.d.ts +6 -1
- package/dist/vehicle.d.ts +22 -2
- package/dist/vehicle.js +617 -665
- package/dist/vehicle.js.map +1 -1
- package/dist/vehicleJoints.d.ts +50 -0
- package/dist/wheelLoads.d.ts +5 -4
- package/package.json +4 -16
- 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.d.ts +0 -378
- package/dist/curves.js +0 -542
- package/dist/curves.js.map +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.d.ts +0 -18
- 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-Bi0ikONC.js +0 -1156
- package/dist/physicsEcs-Bi0ikONC.js.map +0 -1
- package/dist/physicsEcs.d.ts +0 -25
|
@@ -1,1156 +0,0 @@
|
|
|
1
|
-
import { createContext, useCallback, useEffect, useLayoutEffect, useRef } from "react";
|
|
2
|
-
import { BallCollider, CapsuleCollider, CoefficientCombineRule, ConvexHullCollider, CuboidCollider, CylinderCollider, TrimeshCollider } from "@react-three/rapier";
|
|
3
|
-
import { jsx } from "react/jsx-runtime";
|
|
4
|
-
import * as THREE from "three";
|
|
5
|
-
// src/khrConfigTypes.ts
|
|
6
|
-
var KhrValidationError = class extends Error {
|
|
7
|
-
constructor(message) {
|
|
8
|
-
super(message);
|
|
9
|
-
this.name = "KhrValidationError";
|
|
10
|
-
}
|
|
11
|
-
};
|
|
12
|
-
// src/khrInertia.ts
|
|
13
|
-
var SCALE_EPS = 1e-4;
|
|
14
|
-
function isUnitScalar(v) {
|
|
15
|
-
return typeof v === "number" && Number.isFinite(v) && Math.abs(v - 1) <= 1e-9;
|
|
16
|
-
}
|
|
17
|
-
function assertUnitScale(scale, owner) {
|
|
18
|
-
if (scale === void 0) return;
|
|
19
|
-
let ok = false;
|
|
20
|
-
if (typeof scale === "number") ok = isUnitScalar(scale);
|
|
21
|
-
else if (Array.isArray(scale)) ok = scale.length === 3 && scale.every(isUnitScalar);
|
|
22
|
-
else if (typeof scale === "object" && scale && "x" in scale && "y" in scale && "z" in scale) {
|
|
23
|
-
const v = scale;
|
|
24
|
-
ok = isUnitScalar(v.x) && isUnitScalar(v.y) && isUnitScalar(v.z);
|
|
25
|
-
}
|
|
26
|
-
if (!ok) throw new KhrValidationError(`${owner}: scale must be 1 (got ${typeof scale === "number" && !Number.isFinite(scale) ? String(scale) : JSON.stringify(scale)}); bake scale into the .glb at import`);
|
|
27
|
-
}
|
|
28
|
-
function readFiniteVec3(val, fieldLabel) {
|
|
29
|
-
if (!Array.isArray(val) || val.length !== 3 || !val.every((n) => typeof n === "number" && Number.isFinite(n))) throw new KhrValidationError(`Invalid ${fieldLabel}: expected finite 3-tuple`);
|
|
30
|
-
return [
|
|
31
|
-
val[0],
|
|
32
|
-
val[1],
|
|
33
|
-
val[2]
|
|
34
|
-
];
|
|
35
|
-
}
|
|
36
|
-
function readUnitQuat(val, fieldLabel) {
|
|
37
|
-
if (!Array.isArray(val) || val.length !== 4 || !val.every((n) => typeof n === "number" && Number.isFinite(n))) throw new KhrValidationError(`Invalid ${fieldLabel}: expected finite 4-tuple`);
|
|
38
|
-
const norm = Math.hypot(val[0], val[1], val[2], val[3]);
|
|
39
|
-
if (!(norm > 1e-6)) throw new KhrValidationError(`Invalid ${fieldLabel}: zero-norm quaternion`);
|
|
40
|
-
return [
|
|
41
|
-
val[0] / norm,
|
|
42
|
-
val[1] / norm,
|
|
43
|
-
val[2] / norm,
|
|
44
|
-
val[3] / norm
|
|
45
|
-
];
|
|
46
|
-
}
|
|
47
|
-
function assertIdentityWorldScale(obj, nodeName) {
|
|
48
|
-
const ws = obj.getWorldScale(new THREE.Vector3());
|
|
49
|
-
if (!Number.isFinite(ws.x) || !Number.isFinite(ws.y) || !Number.isFinite(ws.z) || Math.abs(ws.x - 1) > SCALE_EPS || Math.abs(ws.y - 1) > SCALE_EPS || Math.abs(ws.z - 1) > SCALE_EPS) throw new KhrValidationError(`Non-identity world scale [${ws.x.toFixed(4)}, ${ws.y.toFixed(4)}, ${ws.z.toFixed(4)}] on "${nodeName}"; bake scale=[1,1,1]`);
|
|
50
|
-
const ls = obj.scale;
|
|
51
|
-
if (!Number.isFinite(ls.x) || !Number.isFinite(ls.y) || !Number.isFinite(ls.z) || Math.abs(ls.x - 1) > SCALE_EPS || Math.abs(ls.y - 1) > SCALE_EPS || Math.abs(ls.z - 1) > SCALE_EPS) throw new KhrValidationError(`Non-identity local scale [${ls.x.toFixed(4)}, ${ls.y.toFixed(4)}, ${ls.z.toFixed(4)}] on "${nodeName}"; bake scale=[1,1,1]`);
|
|
52
|
-
}
|
|
53
|
-
function assertSceneIdentityWorldScale(root) {
|
|
54
|
-
root.traverse((child) => {
|
|
55
|
-
assertIdentityWorldScale(child, child.name || (child === root ? "gltf.scene" : "unnamed_node"));
|
|
56
|
-
});
|
|
57
|
-
}
|
|
58
|
-
function computeRelativePose(ownerObj, childObj) {
|
|
59
|
-
const ownerPos = ownerObj.getWorldPosition(new THREE.Vector3());
|
|
60
|
-
const ownerInvQuat = ownerObj.getWorldQuaternion(new THREE.Quaternion()).invert();
|
|
61
|
-
const pos = childObj.getWorldPosition(new THREE.Vector3()).sub(ownerPos).applyQuaternion(ownerInvQuat);
|
|
62
|
-
const quat = ownerInvQuat.multiply(childObj.getWorldQuaternion(new THREE.Quaternion()));
|
|
63
|
-
return {
|
|
64
|
-
localPosition: [
|
|
65
|
-
pos.x,
|
|
66
|
-
pos.y,
|
|
67
|
-
pos.z
|
|
68
|
-
],
|
|
69
|
-
localQuaternion: [
|
|
70
|
-
quat.x,
|
|
71
|
-
quat.y,
|
|
72
|
-
quat.z,
|
|
73
|
-
quat.w
|
|
74
|
-
]
|
|
75
|
-
};
|
|
76
|
-
}
|
|
77
|
-
function extractMeshColliderShape(targetObj, meshNodeName, convexHull) {
|
|
78
|
-
assertIdentityWorldScale(targetObj, meshNodeName);
|
|
79
|
-
const invOwner = targetObj.matrixWorld.clone().invert();
|
|
80
|
-
const rel = new THREE.Matrix4();
|
|
81
|
-
const v = new THREE.Vector3();
|
|
82
|
-
const verts = [];
|
|
83
|
-
const indices = [];
|
|
84
|
-
targetObj.traverse((child) => {
|
|
85
|
-
const mesh = child;
|
|
86
|
-
if (!mesh.isMesh || !mesh.geometry) return;
|
|
87
|
-
assertIdentityWorldScale(mesh, mesh.name || meshNodeName);
|
|
88
|
-
const posAttr = mesh.geometry.getAttribute("position");
|
|
89
|
-
if (!posAttr || posAttr.count === 0) return;
|
|
90
|
-
rel.multiplyMatrices(invOwner, mesh.matrixWorld);
|
|
91
|
-
const base = verts.length / 3;
|
|
92
|
-
for (let i = 0; i < posAttr.count; i++) {
|
|
93
|
-
v.fromBufferAttribute(posAttr, i).applyMatrix4(rel);
|
|
94
|
-
if (!Number.isFinite(v.x) || !Number.isFinite(v.y) || !Number.isFinite(v.z)) throw new KhrValidationError(`Non-finite vertex in collider mesh "${meshNodeName}"`);
|
|
95
|
-
verts.push(v.x, v.y, v.z);
|
|
96
|
-
}
|
|
97
|
-
const idx = mesh.geometry.getIndex();
|
|
98
|
-
if (idx) for (let i = 0; i < idx.count; i++) indices.push(base + idx.getX(i));
|
|
99
|
-
else for (let i = 0; i < posAttr.count; i++) indices.push(base + i);
|
|
100
|
-
});
|
|
101
|
-
if (verts.length < 9) throw new KhrValidationError(`Collider node "${meshNodeName}" has fewer than 3 vertices`);
|
|
102
|
-
if (convexHull) return {
|
|
103
|
-
type: "convexHull",
|
|
104
|
-
vertices: new Float32Array(verts),
|
|
105
|
-
meshNodeName
|
|
106
|
-
};
|
|
107
|
-
if (indices.length < 3) throw new KhrValidationError(`Trimesh node "${meshNodeName}" has fewer than 3 indices`);
|
|
108
|
-
return {
|
|
109
|
-
type: "trimesh",
|
|
110
|
-
vertices: new Float32Array(verts),
|
|
111
|
-
indices: new Uint32Array(indices),
|
|
112
|
-
meshNodeName
|
|
113
|
-
};
|
|
114
|
-
}
|
|
115
|
-
function unitInertiaForShape(shape) {
|
|
116
|
-
if (shape.type === "box") {
|
|
117
|
-
const [x, y, z] = shape.size;
|
|
118
|
-
return {
|
|
119
|
-
volume: x * y * z,
|
|
120
|
-
centroid: new THREE.Vector3(),
|
|
121
|
-
unitInertia: [
|
|
122
|
-
(y * y + z * z) / 12,
|
|
123
|
-
(x * x + z * z) / 12,
|
|
124
|
-
(x * x + y * y) / 12
|
|
125
|
-
]
|
|
126
|
-
};
|
|
127
|
-
}
|
|
128
|
-
if (shape.type === "sphere") {
|
|
129
|
-
const r = shape.radius;
|
|
130
|
-
const i = .4 * r * r;
|
|
131
|
-
return {
|
|
132
|
-
volume: 4 / 3 * Math.PI * r * r * r,
|
|
133
|
-
centroid: new THREE.Vector3(),
|
|
134
|
-
unitInertia: [
|
|
135
|
-
i,
|
|
136
|
-
i,
|
|
137
|
-
i
|
|
138
|
-
]
|
|
139
|
-
};
|
|
140
|
-
}
|
|
141
|
-
if (shape.type === "convexHull" || shape.type === "trimesh") {
|
|
142
|
-
const box = new THREE.Box3();
|
|
143
|
-
const pt = new THREE.Vector3();
|
|
144
|
-
const verts = shape.vertices;
|
|
145
|
-
for (let i = 0; i < verts.length; i += 3) box.expandByPoint(pt.set(verts[i], verts[i + 1], verts[i + 2]));
|
|
146
|
-
const s = box.getSize(new THREE.Vector3());
|
|
147
|
-
return {
|
|
148
|
-
volume: s.x * s.y * s.z,
|
|
149
|
-
centroid: box.getCenter(new THREE.Vector3()),
|
|
150
|
-
unitInertia: [
|
|
151
|
-
(s.y * s.y + s.z * s.z) / 12,
|
|
152
|
-
(s.x * s.x + s.z * s.z) / 12,
|
|
153
|
-
(s.x * s.x + s.y * s.y) / 12
|
|
154
|
-
]
|
|
155
|
-
};
|
|
156
|
-
}
|
|
157
|
-
const r = shape.radiusTop;
|
|
158
|
-
const h = shape.height;
|
|
159
|
-
if (shape.type === "capsule") {
|
|
160
|
-
const cylVol = Math.PI * r * r * h;
|
|
161
|
-
const totVol = cylVol + 4 / 3 * Math.PI * r * r * r;
|
|
162
|
-
const fc = cylVol / totVol;
|
|
163
|
-
const fh = 1 - fc;
|
|
164
|
-
const iy = .5 * fc * r * r + .4 * fh * r * r;
|
|
165
|
-
const ix = fc * (h * h / 12 + .25 * r * r) + fh * (.4 * r * r + .25 * h * h + .375 * h * r);
|
|
166
|
-
return {
|
|
167
|
-
volume: totVol,
|
|
168
|
-
centroid: new THREE.Vector3(),
|
|
169
|
-
unitInertia: [
|
|
170
|
-
ix,
|
|
171
|
-
iy,
|
|
172
|
-
ix
|
|
173
|
-
]
|
|
174
|
-
};
|
|
175
|
-
}
|
|
176
|
-
const ix = (3 * r * r + h * h) / 12;
|
|
177
|
-
return {
|
|
178
|
-
volume: Math.PI * r * r * h,
|
|
179
|
-
centroid: new THREE.Vector3(),
|
|
180
|
-
unitInertia: [
|
|
181
|
-
ix,
|
|
182
|
-
.5 * r * r,
|
|
183
|
-
ix
|
|
184
|
-
]
|
|
185
|
-
};
|
|
186
|
-
}
|
|
187
|
-
function computeBodyInertia(colliders, mass, centerOfMass, inertiaOrientation, nodeName) {
|
|
188
|
-
if (colliders.length === 0) throw new KhrValidationError(`Dynamic node "${nodeName}" is missing motion.inertiaDiagonal and has no colliders`);
|
|
189
|
-
const shapes = colliders.map((c) => unitInertiaForShape(c.shape));
|
|
190
|
-
let totalVol = 0;
|
|
191
|
-
for (const s of shapes) totalVol += s.volume;
|
|
192
|
-
const invFrame = new THREE.Quaternion().fromArray(inertiaOrientation).invert();
|
|
193
|
-
const com = new THREE.Vector3().fromArray(centerOfMass);
|
|
194
|
-
const colQuat = new THREE.Quaternion();
|
|
195
|
-
const relQuat = new THREE.Quaternion();
|
|
196
|
-
const offset = new THREE.Vector3();
|
|
197
|
-
const rotMat = new THREE.Matrix4();
|
|
198
|
-
const m = rotMat.elements;
|
|
199
|
-
let ix = 0, iy = 0, iz = 0;
|
|
200
|
-
for (let i = 0; i < colliders.length; i++) {
|
|
201
|
-
const col = colliders[i];
|
|
202
|
-
const shp = shapes[i];
|
|
203
|
-
const mShare = totalVol > 0 ? mass * (shp.volume / totalVol) : mass / colliders.length;
|
|
204
|
-
colQuat.fromArray(col.localQuaternion);
|
|
205
|
-
offset.copy(shp.centroid).applyQuaternion(colQuat).add(new THREE.Vector3().fromArray(col.localPosition)).sub(com).applyQuaternion(invFrame);
|
|
206
|
-
relQuat.copy(invFrame).multiply(colQuat);
|
|
207
|
-
rotMat.makeRotationFromQuaternion(relQuat);
|
|
208
|
-
const [u0, u1, u2] = shp.unitInertia;
|
|
209
|
-
const rx = m[0] * m[0] * u0 + m[4] * m[4] * u1 + m[8] * m[8] * u2;
|
|
210
|
-
const ry = m[1] * m[1] * u0 + m[5] * m[5] * u1 + m[9] * m[9] * u2;
|
|
211
|
-
const rz = m[2] * m[2] * u0 + m[6] * m[6] * u1 + m[10] * m[10] * u2;
|
|
212
|
-
ix += mShare * (rx + offset.y * offset.y + offset.z * offset.z);
|
|
213
|
-
iy += mShare * (ry + offset.x * offset.x + offset.z * offset.z);
|
|
214
|
-
iz += mShare * (rz + offset.x * offset.x + offset.y * offset.y);
|
|
215
|
-
}
|
|
216
|
-
if (!Number.isFinite(ix) || !Number.isFinite(iy) || !Number.isFinite(iz) || ix <= 0 || iy <= 0 || iz <= 0) throw new KhrValidationError(`Dynamic node "${nodeName}" has degenerate collider geometry; cannot compute positive motion.inertiaDiagonal`);
|
|
217
|
-
return [
|
|
218
|
-
ix,
|
|
219
|
-
iy,
|
|
220
|
-
iz
|
|
221
|
-
];
|
|
222
|
-
}
|
|
223
|
-
function computeAssemblyMassAndCom(bodies) {
|
|
224
|
-
let totalDynamicMass = 0, mx = 0, my = 0, mz = 0;
|
|
225
|
-
const q = new THREE.Quaternion();
|
|
226
|
-
const c = new THREE.Vector3();
|
|
227
|
-
for (const b of bodies) {
|
|
228
|
-
if (b.isKinematic) continue;
|
|
229
|
-
totalDynamicMass += b.mass;
|
|
230
|
-
c.fromArray(b.centerOfMass).applyQuaternion(q.fromArray(b.localQuaternion));
|
|
231
|
-
mx += (b.localPosition[0] + c.x) * b.mass;
|
|
232
|
-
my += (b.localPosition[1] + c.y) * b.mass;
|
|
233
|
-
mz += (b.localPosition[2] + c.z) * b.mass;
|
|
234
|
-
}
|
|
235
|
-
return {
|
|
236
|
-
totalDynamicMass,
|
|
237
|
-
centerOfMass: totalDynamicMass > 0 ? [
|
|
238
|
-
mx / totalDynamicMass,
|
|
239
|
-
my / totalDynamicMass,
|
|
240
|
-
mz / totalDynamicMass
|
|
241
|
-
] : [
|
|
242
|
-
0,
|
|
243
|
-
0,
|
|
244
|
-
0
|
|
245
|
-
]
|
|
246
|
-
};
|
|
247
|
-
}
|
|
248
|
-
// src/khrClone.ts
|
|
249
|
-
var mountedScenes = /* @__PURE__ */ new WeakSet();
|
|
250
|
-
var clonedScenes = /* @__PURE__ */ new WeakSet();
|
|
251
|
-
var disposedScenes = /* @__PURE__ */ new WeakSet();
|
|
252
|
-
var activeSubtreeRestorers = [];
|
|
253
|
-
function registerMountedSubtree(restoreForClone) {
|
|
254
|
-
activeSubtreeRestorers.push(restoreForClone);
|
|
255
|
-
return () => {
|
|
256
|
-
const idx = activeSubtreeRestorers.indexOf(restoreForClone);
|
|
257
|
-
if (idx >= 0) activeSubtreeRestorers.splice(idx, 1);
|
|
258
|
-
};
|
|
259
|
-
}
|
|
260
|
-
function claimBundleMount(bundle, ownerComponent) {
|
|
261
|
-
if (disposedScenes.has(bundle.scene)) throw new KhrValidationError(`${ownerComponent}: KhrAssetBundle "${bundle.scene.name || bundle.scene.uuid}" has already been disposed`);
|
|
262
|
-
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`);
|
|
263
|
-
mountedScenes.add(bundle.scene);
|
|
264
|
-
}
|
|
265
|
-
function releaseBundleMount(bundle) {
|
|
266
|
-
mountedScenes.delete(bundle.scene);
|
|
267
|
-
}
|
|
268
|
-
function remapNode(srcRoot, node, dstRoot) {
|
|
269
|
-
if (node === srcRoot) return dstRoot;
|
|
270
|
-
const path = [];
|
|
271
|
-
let cur = node;
|
|
272
|
-
while (cur && cur !== srcRoot) {
|
|
273
|
-
const parent = cur.parent;
|
|
274
|
-
if (!parent) throw new KhrValidationError(`cloneKhrBundle: body node "${node.name}" is detached from bundle.scene`);
|
|
275
|
-
const idx = parent.children.indexOf(cur);
|
|
276
|
-
if (idx < 0) throw new KhrValidationError(`cloneKhrBundle: child index missing for "${node.name}"`);
|
|
277
|
-
path.push(idx);
|
|
278
|
-
cur = parent;
|
|
279
|
-
}
|
|
280
|
-
let out = dstRoot;
|
|
281
|
-
for (let i = path.length - 1; i >= 0; i--) out = out.children[path[i]];
|
|
282
|
-
return out;
|
|
283
|
-
}
|
|
284
|
-
function cloneCurve(curve) {
|
|
285
|
-
if (!curve) return void 0;
|
|
286
|
-
return {
|
|
287
|
-
...curve,
|
|
288
|
-
points: curve.points.map((pt) => ({ ...pt }))
|
|
289
|
-
};
|
|
290
|
-
}
|
|
291
|
-
function cloneCarTune(tune) {
|
|
292
|
-
if (!tune) return void 0;
|
|
293
|
-
return {
|
|
294
|
-
...tune,
|
|
295
|
-
...tune.gearRatios ? { gearRatios: [...tune.gearRatios] } : {},
|
|
296
|
-
...tune.engineTorqueCurveData ? { engineTorqueCurveData: cloneCurve(tune.engineTorqueCurveData) } : {},
|
|
297
|
-
...tune.steerAngleCurveData ? { steerAngleCurveData: cloneCurve(tune.steerAngleCurveData) } : {}
|
|
298
|
-
};
|
|
299
|
-
}
|
|
300
|
-
/**
|
|
301
|
-
* Clones a parsed `KhrAssetBundle` in O(nodes) while sharing parsed `.glb` buffers,
|
|
302
|
-
* GPU `BufferGeometry` / `Material` buffers, and Rapier `Float32Array` collider vertices.
|
|
303
|
-
*/
|
|
304
|
-
function cloneKhrBundle(bundle) {
|
|
305
|
-
if (disposedScenes.has(bundle.scene)) throw new KhrValidationError(`cloneKhrBundle: cannot clone disposed KhrAssetBundle "${bundle.scene.name || bundle.scene.uuid}"`);
|
|
306
|
-
const reattach = [];
|
|
307
|
-
for (let i = activeSubtreeRestorers.length - 1; i >= 0; i--) reattach.push(activeSubtreeRestorers[i]());
|
|
308
|
-
try {
|
|
309
|
-
const scene = bundle.scene.clone(true);
|
|
310
|
-
clonedScenes.add(scene);
|
|
311
|
-
return {
|
|
312
|
-
scene,
|
|
313
|
-
bodies: bundle.bodies.map((b) => {
|
|
314
|
-
const drives = b.joint ? b.joint.drives.map((d) => ({ ...d })) : [];
|
|
315
|
-
return {
|
|
316
|
-
...b,
|
|
317
|
-
centerOfMass: [...b.centerOfMass],
|
|
318
|
-
inertiaDiagonal: [...b.inertiaDiagonal],
|
|
319
|
-
inertiaOrientation: [...b.inertiaOrientation],
|
|
320
|
-
...b.linearVelocity ? { linearVelocity: [...b.linearVelocity] } : {},
|
|
321
|
-
...b.angularVelocity ? { angularVelocity: [...b.angularVelocity] } : {},
|
|
322
|
-
localPosition: [...b.localPosition],
|
|
323
|
-
localQuaternion: [...b.localQuaternion],
|
|
324
|
-
colliders: b.colliders.map((c) => ({
|
|
325
|
-
...c,
|
|
326
|
-
shape: c.shape.type === "box" ? {
|
|
327
|
-
...c.shape,
|
|
328
|
-
size: [...c.shape.size]
|
|
329
|
-
} : { ...c.shape },
|
|
330
|
-
localPosition: [...c.localPosition],
|
|
331
|
-
localQuaternion: [...c.localQuaternion]
|
|
332
|
-
})),
|
|
333
|
-
joint: b.joint ? {
|
|
334
|
-
...b.joint,
|
|
335
|
-
axis: [...b.joint.axis],
|
|
336
|
-
limits: b.joint.limits ? [...b.joint.limits] : null,
|
|
337
|
-
drive: drives[0] ?? (b.joint.drive ? { ...b.joint.drive } : null),
|
|
338
|
-
drives,
|
|
339
|
-
anchor1: [...b.joint.anchor1],
|
|
340
|
-
anchor2: [...b.joint.anchor2]
|
|
341
|
-
} : null,
|
|
342
|
-
...b.breakaway ? { breakaway: { ...b.breakaway } } : {},
|
|
343
|
-
object: remapNode(bundle.scene, b.object, scene)
|
|
344
|
-
};
|
|
345
|
-
}),
|
|
346
|
-
wheels: bundle.wheels.map((w) => ({
|
|
347
|
-
...w,
|
|
348
|
-
object: remapNode(bundle.scene, w.object, scene),
|
|
349
|
-
position: [...w.position],
|
|
350
|
-
...w.quaternion ? { quaternion: [...w.quaternion] } : {},
|
|
351
|
-
...w.lngSlipRatioCurveData ? { lngSlipRatioCurveData: cloneCurve(w.lngSlipRatioCurveData) } : {},
|
|
352
|
-
...w.latSlipRatioCurveData ? { latSlipRatioCurveData: cloneCurve(w.latSlipRatioCurveData) } : {}
|
|
353
|
-
})),
|
|
354
|
-
propellers: bundle.propellers.map((p) => ({
|
|
355
|
-
...p,
|
|
356
|
-
object: remapNode(bundle.scene, p.object, scene),
|
|
357
|
-
position: [...p.position],
|
|
358
|
-
...p.quaternion ? { quaternion: [...p.quaternion] } : {}
|
|
359
|
-
})),
|
|
360
|
-
sockets: Object.fromEntries(Object.entries(bundle.sockets).map(([k, v]) => [k, [...v]])),
|
|
361
|
-
totalDynamicMass: bundle.totalDynamicMass,
|
|
362
|
-
centerOfMass: [...bundle.centerOfMass],
|
|
363
|
-
...bundle.tune ? { tune: cloneCarTune(bundle.tune) } : {},
|
|
364
|
-
...bundle.droneTune ? { droneTune: { ...bundle.droneTune } } : {}
|
|
365
|
-
};
|
|
366
|
-
} finally {
|
|
367
|
-
for (let i = reattach.length - 1; i >= 0; i--) reattach[i]();
|
|
368
|
-
}
|
|
369
|
-
}
|
|
370
|
-
/**
|
|
371
|
-
* Releases clone mount ownership while keeping shared template GPU resources intact.
|
|
372
|
-
* When called on a root template bundle, disposes unique GPU geometries, materials, and textures once.
|
|
373
|
-
*/
|
|
374
|
-
function disposeKhrBundle(bundle) {
|
|
375
|
-
mountedScenes.delete(bundle.scene);
|
|
376
|
-
if (clonedScenes.has(bundle.scene) || disposedScenes.has(bundle.scene)) {
|
|
377
|
-
disposedScenes.add(bundle.scene);
|
|
378
|
-
return;
|
|
379
|
-
}
|
|
380
|
-
disposedScenes.add(bundle.scene);
|
|
381
|
-
const geos = /* @__PURE__ */ new Set();
|
|
382
|
-
const mats = /* @__PURE__ */ new Set();
|
|
383
|
-
const texs = /* @__PURE__ */ new Set();
|
|
384
|
-
const disposeTree = (obj) => {
|
|
385
|
-
obj.traverse((child) => {
|
|
386
|
-
const mesh = child;
|
|
387
|
-
if (mesh.geometry && !geos.has(mesh.geometry)) {
|
|
388
|
-
geos.add(mesh.geometry);
|
|
389
|
-
mesh.geometry.dispose();
|
|
390
|
-
}
|
|
391
|
-
const matList = Array.isArray(mesh.material) ? mesh.material : mesh.material ? [mesh.material] : [];
|
|
392
|
-
for (const mat of matList) if (!mats.has(mat)) {
|
|
393
|
-
mats.add(mat);
|
|
394
|
-
for (const val of Object.values(mat)) if (val && typeof val === "object" && "isTexture" in val && val.isTexture) {
|
|
395
|
-
const tex = val;
|
|
396
|
-
if (!texs.has(tex)) {
|
|
397
|
-
texs.add(tex);
|
|
398
|
-
tex.dispose();
|
|
399
|
-
}
|
|
400
|
-
}
|
|
401
|
-
mat.dispose();
|
|
402
|
-
}
|
|
403
|
-
});
|
|
404
|
-
};
|
|
405
|
-
disposeTree(bundle.scene);
|
|
406
|
-
for (const w of bundle.wheels) disposeTree(w.object);
|
|
407
|
-
for (const p of bundle.propellers) disposeTree(p.object);
|
|
408
|
-
}
|
|
409
|
-
// src/KhrCollider.tsx
|
|
410
|
-
var COMBINE_RULES = {
|
|
411
|
-
average: CoefficientCombineRule.Average,
|
|
412
|
-
minimum: CoefficientCombineRule.Min,
|
|
413
|
-
maximum: CoefficientCombineRule.Max,
|
|
414
|
-
multiply: CoefficientCombineRule.Multiply
|
|
415
|
-
};
|
|
416
|
-
function KhrCollider({ c, position, quaternion, massProperties, collisionGroups, solverGroups, ref }) {
|
|
417
|
-
const innerRef = useRef(null);
|
|
418
|
-
const setRef = useCallback((node) => {
|
|
419
|
-
innerRef.current = node;
|
|
420
|
-
if (typeof ref === "function") ref(node);
|
|
421
|
-
else if (ref) ref.current = node;
|
|
422
|
-
}, [ref]);
|
|
423
|
-
useEffect(() => {
|
|
424
|
-
innerRef.current?.parent()?.recomputeMassPropertiesFromColliders();
|
|
425
|
-
}, [c, massProperties]);
|
|
426
|
-
const effCollisionGroups = c.collisionGroups ?? collisionGroups;
|
|
427
|
-
const effSolverGroups = c.collisionGroups === void 0 ? solverGroups : void 0;
|
|
428
|
-
const common = {
|
|
429
|
-
ref: setRef,
|
|
430
|
-
position: position ?? c.localPosition,
|
|
431
|
-
quaternion: quaternion ?? c.localQuaternion,
|
|
432
|
-
...massProperties ? { massProperties } : { density: 0 },
|
|
433
|
-
...c.friction === void 0 ? {} : { friction: c.friction },
|
|
434
|
-
...c.restitution === void 0 ? {} : { restitution: c.restitution },
|
|
435
|
-
...c.frictionCombine === void 0 ? {} : { frictionCombineRule: COMBINE_RULES[c.frictionCombine] },
|
|
436
|
-
...c.restitutionCombine === void 0 ? {} : { restitutionCombineRule: COMBINE_RULES[c.restitutionCombine] },
|
|
437
|
-
...effCollisionGroups === void 0 ? {} : { collisionGroups: effCollisionGroups },
|
|
438
|
-
...effSolverGroups === void 0 ? {} : { solverGroups: effSolverGroups }
|
|
439
|
-
};
|
|
440
|
-
const { shape } = c;
|
|
441
|
-
if (shape.type === "box") {
|
|
442
|
-
const [sx, sy, sz] = shape.size;
|
|
443
|
-
return /* @__PURE__ */ jsx(CuboidCollider, {
|
|
444
|
-
args: [
|
|
445
|
-
sx * .5,
|
|
446
|
-
sy * .5,
|
|
447
|
-
sz * .5
|
|
448
|
-
],
|
|
449
|
-
...common
|
|
450
|
-
});
|
|
451
|
-
}
|
|
452
|
-
if (shape.type === "sphere") return /* @__PURE__ */ jsx(BallCollider, {
|
|
453
|
-
args: [shape.radius],
|
|
454
|
-
...common
|
|
455
|
-
});
|
|
456
|
-
if (shape.type === "cylinder") return /* @__PURE__ */ jsx(CylinderCollider, {
|
|
457
|
-
args: [shape.height * .5, shape.radiusTop],
|
|
458
|
-
...common
|
|
459
|
-
});
|
|
460
|
-
if (shape.type === "capsule") return /* @__PURE__ */ jsx(CapsuleCollider, {
|
|
461
|
-
args: [shape.height * .5, shape.radiusTop],
|
|
462
|
-
...common
|
|
463
|
-
});
|
|
464
|
-
if (shape.type === "convexHull") return /* @__PURE__ */ jsx(ConvexHullCollider, {
|
|
465
|
-
args: [shape.vertices],
|
|
466
|
-
...common
|
|
467
|
-
});
|
|
468
|
-
return /* @__PURE__ */ jsx(TrimeshCollider, {
|
|
469
|
-
args: [shape.vertices, shape.indices],
|
|
470
|
-
...common
|
|
471
|
-
});
|
|
472
|
-
}
|
|
473
|
-
// src/khrMassProps.ts
|
|
474
|
-
var _pos = new THREE.Vector3();
|
|
475
|
-
var _quat = new THREE.Quaternion();
|
|
476
|
-
var _qExtra = new THREE.Quaternion();
|
|
477
|
-
var _comVec = new THREE.Vector3();
|
|
478
|
-
var _one = new THREE.Vector3(1, 1, 1);
|
|
479
|
-
var _mat = new THREE.Matrix4();
|
|
480
|
-
var _tmpMat = new THREE.Matrix4();
|
|
481
|
-
var _rCom = new THREE.Vector3();
|
|
482
|
-
function bodyPoseMatrix(body, target = new THREE.Matrix4()) {
|
|
483
|
-
_pos.fromArray(body.localPosition);
|
|
484
|
-
_quat.fromArray(body.localQuaternion);
|
|
485
|
-
return target.compose(_pos, _quat, _one);
|
|
486
|
-
}
|
|
487
|
-
function jacobiPrincipalInertia(ixx, iyy, izz, ixy, ixz, iyz) {
|
|
488
|
-
const a = [
|
|
489
|
-
[
|
|
490
|
-
ixx,
|
|
491
|
-
ixy,
|
|
492
|
-
ixz
|
|
493
|
-
],
|
|
494
|
-
[
|
|
495
|
-
ixy,
|
|
496
|
-
iyy,
|
|
497
|
-
iyz
|
|
498
|
-
],
|
|
499
|
-
[
|
|
500
|
-
ixz,
|
|
501
|
-
iyz,
|
|
502
|
-
izz
|
|
503
|
-
]
|
|
504
|
-
];
|
|
505
|
-
const v = [
|
|
506
|
-
[
|
|
507
|
-
1,
|
|
508
|
-
0,
|
|
509
|
-
0
|
|
510
|
-
],
|
|
511
|
-
[
|
|
512
|
-
0,
|
|
513
|
-
1,
|
|
514
|
-
0
|
|
515
|
-
],
|
|
516
|
-
[
|
|
517
|
-
0,
|
|
518
|
-
0,
|
|
519
|
-
1
|
|
520
|
-
]
|
|
521
|
-
];
|
|
522
|
-
for (let iter = 0; iter < 32; iter++) {
|
|
523
|
-
const tr = Math.max(a[0][0] + a[1][1] + a[2][2], 0);
|
|
524
|
-
let p = 0, q = 1, maxOff = Math.abs(a[0][1]);
|
|
525
|
-
if (Math.abs(a[0][2]) > maxOff) {
|
|
526
|
-
p = 0;
|
|
527
|
-
q = 2;
|
|
528
|
-
maxOff = Math.abs(a[0][2]);
|
|
529
|
-
}
|
|
530
|
-
if (Math.abs(a[1][2]) > maxOff) {
|
|
531
|
-
p = 1;
|
|
532
|
-
q = 2;
|
|
533
|
-
maxOff = Math.abs(a[1][2]);
|
|
534
|
-
}
|
|
535
|
-
if (maxOff <= 1e-12 * tr) break;
|
|
536
|
-
const apq = a[p][q], app = a[p][p], aqq = a[q][q];
|
|
537
|
-
const tau = (aqq - app) / (2 * apq);
|
|
538
|
-
const t = tau >= 0 ? 1 / (tau + Math.sqrt(1 + tau * tau)) : -1 / (-tau + Math.sqrt(1 + tau * tau));
|
|
539
|
-
const c = 1 / Math.sqrt(1 + t * t), s = t * c;
|
|
540
|
-
const r = 3 - p - q;
|
|
541
|
-
a[p][p] = c * c * app - 2 * s * c * apq + s * s * aqq;
|
|
542
|
-
a[q][q] = s * s * app + 2 * s * c * apq + c * c * aqq;
|
|
543
|
-
a[p][q] = a[q][p] = 0;
|
|
544
|
-
const arp = a[r][p], arq = a[r][q];
|
|
545
|
-
a[r][p] = a[p][r] = c * arp - s * arq;
|
|
546
|
-
a[r][q] = a[q][r] = s * arp + c * arq;
|
|
547
|
-
for (let i = 0; i < 3; i++) {
|
|
548
|
-
const vip = v[i][p], viq = v[i][q];
|
|
549
|
-
v[i][p] = c * vip - s * viq;
|
|
550
|
-
v[i][q] = s * vip + c * viq;
|
|
551
|
-
}
|
|
552
|
-
}
|
|
553
|
-
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) {
|
|
554
|
-
v[0][2] = -v[0][2];
|
|
555
|
-
v[1][2] = -v[1][2];
|
|
556
|
-
v[2][2] = -v[2][2];
|
|
557
|
-
}
|
|
558
|
-
_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);
|
|
559
|
-
_quat.setFromRotationMatrix(_mat).normalize();
|
|
560
|
-
return {
|
|
561
|
-
inertia: [
|
|
562
|
-
a[0][0],
|
|
563
|
-
a[1][1],
|
|
564
|
-
a[2][2]
|
|
565
|
-
],
|
|
566
|
-
inertiaFrame: [
|
|
567
|
-
_quat.x,
|
|
568
|
-
_quat.y,
|
|
569
|
-
_quat.z,
|
|
570
|
-
_quat.w
|
|
571
|
-
]
|
|
572
|
-
};
|
|
573
|
-
}
|
|
574
|
-
function resolvePose(body, invFrame) {
|
|
575
|
-
if (invFrame) {
|
|
576
|
-
_mat.multiplyMatrices(invFrame, bodyPoseMatrix(body, _tmpMat)).decompose(_pos, _quat, _comVec);
|
|
577
|
-
_quat.normalize();
|
|
578
|
-
} else {
|
|
579
|
-
_pos.fromArray(body.localPosition);
|
|
580
|
-
_quat.fromArray(body.localQuaternion).normalize();
|
|
581
|
-
}
|
|
582
|
-
}
|
|
583
|
-
function computeLumpedMassProps(bodies, invFrame) {
|
|
584
|
-
const dyn = bodies.filter((b) => !b.isKinematic && b.mass > 0);
|
|
585
|
-
if (dyn.length === 0) return null;
|
|
586
|
-
let totalMass = 0, mx = 0, my = 0, mz = 0;
|
|
587
|
-
for (const b of dyn) {
|
|
588
|
-
resolvePose(b, invFrame);
|
|
589
|
-
_rCom.fromArray(b.centerOfMass).applyQuaternion(_quat);
|
|
590
|
-
totalMass += b.mass;
|
|
591
|
-
mx += (_pos.x + _rCom.x) * b.mass;
|
|
592
|
-
my += (_pos.y + _rCom.y) * b.mass;
|
|
593
|
-
mz += (_pos.z + _rCom.z) * b.mass;
|
|
594
|
-
}
|
|
595
|
-
if (!(totalMass > 0)) return null;
|
|
596
|
-
const com = [
|
|
597
|
-
mx / totalMass,
|
|
598
|
-
my / totalMass,
|
|
599
|
-
mz / totalMass
|
|
600
|
-
];
|
|
601
|
-
let ixx = 0, iyy = 0, izz = 0, ixy = 0, ixz = 0, iyz = 0;
|
|
602
|
-
const m = _mat.elements;
|
|
603
|
-
for (const b of dyn) {
|
|
604
|
-
resolvePose(b, invFrame);
|
|
605
|
-
_rCom.fromArray(b.centerOfMass).applyQuaternion(_quat);
|
|
606
|
-
const dx = _pos.x + _rCom.x - com[0];
|
|
607
|
-
const dy = _pos.y + _rCom.y - com[1];
|
|
608
|
-
const dz = _pos.z + _rCom.z - com[2];
|
|
609
|
-
_mat.makeRotationFromQuaternion(_quat.multiply(_qExtra.fromArray(b.inertiaOrientation)));
|
|
610
|
-
const [d0, d1, d2] = b.inertiaDiagonal;
|
|
611
|
-
ixx += m[0] * m[0] * d0 + m[4] * m[4] * d1 + m[8] * m[8] * d2 + b.mass * (dy * dy + dz * dz);
|
|
612
|
-
iyy += m[1] * m[1] * d0 + m[5] * m[5] * d1 + m[9] * m[9] * d2 + b.mass * (dx * dx + dz * dz);
|
|
613
|
-
izz += m[2] * m[2] * d0 + m[6] * m[6] * d1 + m[10] * m[10] * d2 + b.mass * (dx * dx + dy * dy);
|
|
614
|
-
ixy += m[0] * m[1] * d0 + m[4] * m[5] * d1 + m[8] * m[9] * d2 - b.mass * dx * dy;
|
|
615
|
-
ixz += m[0] * m[2] * d0 + m[4] * m[6] * d1 + m[8] * m[10] * d2 - b.mass * dx * dz;
|
|
616
|
-
iyz += m[1] * m[2] * d0 + m[5] * m[6] * d1 + m[9] * m[10] * d2 - b.mass * dy * dz;
|
|
617
|
-
}
|
|
618
|
-
if (dyn.length === 1) {
|
|
619
|
-
const b = dyn[0];
|
|
620
|
-
resolvePose(b, invFrame);
|
|
621
|
-
_quat.multiply(_qExtra.fromArray(b.inertiaOrientation)).normalize();
|
|
622
|
-
return {
|
|
623
|
-
mass: totalMass,
|
|
624
|
-
com,
|
|
625
|
-
inertia: [
|
|
626
|
-
b.inertiaDiagonal[0],
|
|
627
|
-
b.inertiaDiagonal[1],
|
|
628
|
-
b.inertiaDiagonal[2]
|
|
629
|
-
],
|
|
630
|
-
inertiaFrame: [
|
|
631
|
-
_quat.x,
|
|
632
|
-
_quat.y,
|
|
633
|
-
_quat.z,
|
|
634
|
-
_quat.w
|
|
635
|
-
]
|
|
636
|
-
};
|
|
637
|
-
}
|
|
638
|
-
const diagSum = Math.max(ixx + iyy + izz, 0);
|
|
639
|
-
if (Math.max(Math.abs(ixy), Math.abs(ixz), Math.abs(iyz)) <= 1e-12 * diagSum) return {
|
|
640
|
-
mass: totalMass,
|
|
641
|
-
com,
|
|
642
|
-
inertia: [
|
|
643
|
-
ixx,
|
|
644
|
-
iyy,
|
|
645
|
-
izz
|
|
646
|
-
],
|
|
647
|
-
inertiaFrame: [
|
|
648
|
-
0,
|
|
649
|
-
0,
|
|
650
|
-
0,
|
|
651
|
-
1
|
|
652
|
-
]
|
|
653
|
-
};
|
|
654
|
-
const { inertia, inertiaFrame } = jacobiPrincipalInertia(ixx, iyy, izz, ixy, ixz, iyz);
|
|
655
|
-
return {
|
|
656
|
-
mass: totalMass,
|
|
657
|
-
com,
|
|
658
|
-
inertia,
|
|
659
|
-
inertiaFrame
|
|
660
|
-
};
|
|
661
|
-
}
|
|
662
|
-
// src/assemblyPlan.ts
|
|
663
|
-
function matrixToRoot(node, sceneRoot) {
|
|
664
|
-
const out = new THREE.Matrix4();
|
|
665
|
-
for (let cur = node; cur && cur !== sceneRoot; cur = cur.parent) {
|
|
666
|
-
if (!cur.parent) throw new KhrValidationError(`Node "${node.name}" is detached from bundle.scene`);
|
|
667
|
-
cur.updateMatrix();
|
|
668
|
-
out.premultiply(cur.matrix);
|
|
669
|
-
}
|
|
670
|
-
return out;
|
|
671
|
-
}
|
|
672
|
-
function lowestCommonAncestor(nodes, sceneRoot) {
|
|
673
|
-
const chain = (n) => {
|
|
674
|
-
const list = [];
|
|
675
|
-
for (let cur = n; cur; cur = cur.parent) {
|
|
676
|
-
list.push(cur);
|
|
677
|
-
if (cur === sceneRoot) break;
|
|
678
|
-
}
|
|
679
|
-
return list;
|
|
680
|
-
};
|
|
681
|
-
let shared = new Set(chain(nodes[0]));
|
|
682
|
-
for (let i = 1; i < nodes.length; i++) {
|
|
683
|
-
const setI = new Set(chain(nodes[i]));
|
|
684
|
-
shared = new Set([...shared].filter((x) => setI.has(x)));
|
|
685
|
-
}
|
|
686
|
-
return chain(nodes[0]).find((c) => shared.has(c)) ?? sceneRoot;
|
|
687
|
-
}
|
|
688
|
-
function toColliderMass(lumped, colMat, colQuat) {
|
|
689
|
-
const com = new THREE.Vector3().fromArray(lumped.com);
|
|
690
|
-
const frame = new THREE.Quaternion().fromArray(lumped.inertiaFrame);
|
|
691
|
-
if (colMat && colQuat) {
|
|
692
|
-
com.applyMatrix4(colMat.clone().invert());
|
|
693
|
-
frame.premultiply(colQuat.clone().invert()).normalize();
|
|
694
|
-
}
|
|
695
|
-
return {
|
|
696
|
-
mass: lumped.mass,
|
|
697
|
-
centerOfMass: {
|
|
698
|
-
x: com.x,
|
|
699
|
-
y: com.y,
|
|
700
|
-
z: com.z
|
|
701
|
-
},
|
|
702
|
-
principalAngularInertia: {
|
|
703
|
-
x: lumped.inertia[0],
|
|
704
|
-
y: lumped.inertia[1],
|
|
705
|
-
z: lumped.inertia[2]
|
|
706
|
-
},
|
|
707
|
-
angularInertiaLocalFrame: {
|
|
708
|
-
x: frame.x,
|
|
709
|
-
y: frame.y,
|
|
710
|
-
z: frame.z,
|
|
711
|
-
w: frame.w
|
|
712
|
-
}
|
|
713
|
-
};
|
|
714
|
-
}
|
|
715
|
-
function planOwnColliders(body, lumped, frameQuat) {
|
|
716
|
-
const one = new THREE.Vector3(1, 1, 1);
|
|
717
|
-
const frameMat = frameQuat ? new THREE.Matrix4().makeRotationFromQuaternion(frameQuat) : null;
|
|
718
|
-
const pos = new THREE.Vector3(), quat = new THREE.Quaternion(), scl = new THREE.Vector3();
|
|
719
|
-
return body.colliders.map((spec, idx) => {
|
|
720
|
-
const colQuat = new THREE.Quaternion().fromArray(spec.localQuaternion);
|
|
721
|
-
const local = new THREE.Matrix4().compose(new THREE.Vector3().fromArray(spec.localPosition), colQuat, one);
|
|
722
|
-
if (frameMat) local.premultiply(frameMat);
|
|
723
|
-
local.decompose(pos, quat, scl);
|
|
724
|
-
quat.normalize();
|
|
725
|
-
const planned = {
|
|
726
|
-
spec,
|
|
727
|
-
local,
|
|
728
|
-
position: [
|
|
729
|
-
pos.x,
|
|
730
|
-
pos.y,
|
|
731
|
-
pos.z
|
|
732
|
-
],
|
|
733
|
-
quaternion: [
|
|
734
|
-
quat.x,
|
|
735
|
-
quat.y,
|
|
736
|
-
quat.z,
|
|
737
|
-
quat.w
|
|
738
|
-
]
|
|
739
|
-
};
|
|
740
|
-
if (idx === 0 && lumped) planned.massProperties = toColliderMass(lumped, local, quat);
|
|
741
|
-
return planned;
|
|
742
|
-
});
|
|
743
|
-
}
|
|
744
|
-
function isSceneDescendant(node, ancestor) {
|
|
745
|
-
for (let cur = node; cur; cur = cur.parent) if (cur === ancestor) return true;
|
|
746
|
-
return false;
|
|
747
|
-
}
|
|
748
|
-
function approxPartInertia(b, axisChild) {
|
|
749
|
-
if (b.joint?.kind === "fixed") return 0;
|
|
750
|
-
if (b.joint?.kind === "prismatic") return b.mass;
|
|
751
|
-
const ax = new THREE.Vector3().fromArray(axisChild).normalize();
|
|
752
|
-
const qInv = new THREE.Quaternion().fromArray(b.inertiaOrientation).invert();
|
|
753
|
-
const axLocal = ax.clone().applyQuaternion(qInv);
|
|
754
|
-
const [d0, d1, d2] = b.inertiaDiagonal;
|
|
755
|
-
const com = new THREE.Vector3().fromArray(b.centerOfMass);
|
|
756
|
-
const along = com.dot(ax);
|
|
757
|
-
return d0 * axLocal.x * axLocal.x + d1 * axLocal.y * axLocal.y + d2 * axLocal.z * axLocal.z + b.mass * (com.lengthSq() - along * along);
|
|
758
|
-
}
|
|
759
|
-
/** Plans independent Rapier bodies for roots and jointed dynamic children connected by Rapier ImpulseJoints. */
|
|
760
|
-
function planAssembly(bundle) {
|
|
761
|
-
const byName = new Map(bundle.bodies.map((b) => [b.nodeName, b]));
|
|
762
|
-
const findRoot = (b, seen = /* @__PURE__ */ new Set()) => {
|
|
763
|
-
if (!b.joint) return b;
|
|
764
|
-
if (seen.has(b.nodeName)) throw new KhrValidationError(`Joint cycle through "${b.nodeName}"`);
|
|
765
|
-
seen.add(b.nodeName);
|
|
766
|
-
const parent = byName.get(b.joint.connectedNodeName);
|
|
767
|
-
if (!parent) throw new KhrValidationError(`Joint on "${b.nodeName}" targets "${b.joint.connectedNodeName}", which is not a KHR body`);
|
|
768
|
-
return findRoot(parent, seen);
|
|
769
|
-
};
|
|
770
|
-
const plans = bundle.bodies.filter((b) => !b.joint).map((root) => {
|
|
771
|
-
const invRoot = bodyPoseMatrix(root).clone().invert();
|
|
772
|
-
const descendants = bundle.bodies.filter((b) => b.joint && findRoot(b) === root);
|
|
773
|
-
const depthOf = (b) => b.joint ? 1 + depthOf(byName.get(b.joint.connectedNodeName)) : 0;
|
|
774
|
-
descendants.sort((a, b) => depthOf(a) - depthOf(b));
|
|
775
|
-
const partByName = /* @__PURE__ */ new Map();
|
|
776
|
-
const parts = descendants.map((b) => {
|
|
777
|
-
const joint = b.joint;
|
|
778
|
-
const parent = partByName.get(joint.connectedNodeName) ?? null;
|
|
779
|
-
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}"`);
|
|
780
|
-
const relParentMat = bodyPoseMatrix(parent ? parent.spec : root).clone().invert().multiply(bodyPoseMatrix(b));
|
|
781
|
-
const relRootMat = invRoot.clone().multiply(bodyPoseMatrix(b));
|
|
782
|
-
const rpPos = new THREE.Vector3(), rpQuat = new THREE.Quaternion(), rpScl = new THREE.Vector3();
|
|
783
|
-
relParentMat.decompose(rpPos, rpQuat, rpScl);
|
|
784
|
-
rpQuat.normalize();
|
|
785
|
-
const rrPos = new THREE.Vector3(), rrQuat = new THREE.Quaternion();
|
|
786
|
-
relRootMat.decompose(rrPos, rrQuat, rpScl);
|
|
787
|
-
rrQuat.normalize();
|
|
788
|
-
const axChildVec = new THREE.Vector3().fromArray(joint.axis).normalize();
|
|
789
|
-
const axParentVec = axChildVec.clone().applyQuaternion(rrQuat).normalize();
|
|
790
|
-
const axisChild = [
|
|
791
|
-
axChildVec.x,
|
|
792
|
-
axChildVec.y,
|
|
793
|
-
axChildVec.z
|
|
794
|
-
];
|
|
795
|
-
const axisParent = [
|
|
796
|
-
axParentVec.x,
|
|
797
|
-
axParentVec.y,
|
|
798
|
-
axParentVec.z
|
|
799
|
-
];
|
|
800
|
-
const anchorParent = parent ? [
|
|
801
|
-
rrPos.x - parent.restRelRootPos[0],
|
|
802
|
-
rrPos.y - parent.restRelRootPos[1],
|
|
803
|
-
rrPos.z - parent.restRelRootPos[2]
|
|
804
|
-
] : [
|
|
805
|
-
rpPos.x,
|
|
806
|
-
rpPos.y,
|
|
807
|
-
rpPos.z
|
|
808
|
-
];
|
|
809
|
-
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();
|
|
810
|
-
const ownLumped = b.isKinematic ? null : computeLumpedMassProps([b], bodyAlignInv);
|
|
811
|
-
const colliders = planOwnColliders(b, ownLumped, rrQuat);
|
|
812
|
-
const partMass = ownLumped && colliders.length === 0 ? toColliderMass(ownLumped) : void 0;
|
|
813
|
-
const plan = {
|
|
814
|
-
spec: b,
|
|
815
|
-
joint,
|
|
816
|
-
parent,
|
|
817
|
-
restRelParentPos: anchorParent,
|
|
818
|
-
restRelParentQuat: [
|
|
819
|
-
rrQuat.x,
|
|
820
|
-
rrQuat.y,
|
|
821
|
-
rrQuat.z,
|
|
822
|
-
rrQuat.w
|
|
823
|
-
],
|
|
824
|
-
restRelRootPos: [
|
|
825
|
-
rrPos.x,
|
|
826
|
-
rrPos.y,
|
|
827
|
-
rrPos.z
|
|
828
|
-
],
|
|
829
|
-
restRelRootQuat: [
|
|
830
|
-
rrQuat.x,
|
|
831
|
-
rrQuat.y,
|
|
832
|
-
rrQuat.z,
|
|
833
|
-
rrQuat.w
|
|
834
|
-
],
|
|
835
|
-
axisParent,
|
|
836
|
-
axisChild,
|
|
837
|
-
inertia: approxPartInertia(b, axisChild),
|
|
838
|
-
...partMass ? { partMass } : {},
|
|
839
|
-
colliders
|
|
840
|
-
};
|
|
841
|
-
partByName.set(b.nodeName, plan);
|
|
842
|
-
return plan;
|
|
843
|
-
});
|
|
844
|
-
const rootLumped = root.isKinematic ? null : computeLumpedMassProps([root], invRoot);
|
|
845
|
-
const colliders = planOwnColliders(root, rootLumped);
|
|
846
|
-
const rootMass = rootLumped && colliders.length === 0 ? toColliderMass(rootLumped) : void 0;
|
|
847
|
-
const mountNode = lowestCommonAncestor([root.object, ...descendants.map((d) => d.object)], bundle.scene);
|
|
848
|
-
const mountInRoot = invRoot.clone().multiply(matrixToRoot(mountNode, bundle.scene));
|
|
849
|
-
const scenePos = new THREE.Vector3(), sceneQuat = new THREE.Quaternion(), sceneScl = new THREE.Vector3();
|
|
850
|
-
invRoot.decompose(scenePos, sceneQuat, sceneScl);
|
|
851
|
-
sceneQuat.normalize();
|
|
852
|
-
const mountPos = new THREE.Vector3(), mountQuat = new THREE.Quaternion();
|
|
853
|
-
mountInRoot.decompose(mountPos, mountQuat, sceneScl);
|
|
854
|
-
mountQuat.normalize();
|
|
855
|
-
return {
|
|
856
|
-
spec: root,
|
|
857
|
-
sceneInRootPos: [
|
|
858
|
-
scenePos.x,
|
|
859
|
-
scenePos.y,
|
|
860
|
-
scenePos.z
|
|
861
|
-
],
|
|
862
|
-
sceneInRootQuat: [
|
|
863
|
-
sceneQuat.x,
|
|
864
|
-
sceneQuat.y,
|
|
865
|
-
sceneQuat.z,
|
|
866
|
-
sceneQuat.w
|
|
867
|
-
],
|
|
868
|
-
mountNode,
|
|
869
|
-
mountInRoot,
|
|
870
|
-
mountPosition: [
|
|
871
|
-
mountPos.x,
|
|
872
|
-
mountPos.y,
|
|
873
|
-
mountPos.z
|
|
874
|
-
],
|
|
875
|
-
mountQuaternion: [
|
|
876
|
-
mountQuat.x,
|
|
877
|
-
mountQuat.y,
|
|
878
|
-
mountQuat.z,
|
|
879
|
-
mountQuat.w
|
|
880
|
-
],
|
|
881
|
-
...rootMass ? { rootMass } : {},
|
|
882
|
-
colliders,
|
|
883
|
-
parts
|
|
884
|
-
};
|
|
885
|
-
});
|
|
886
|
-
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`);
|
|
887
|
-
return plans;
|
|
888
|
-
}
|
|
889
|
-
// src/BundleSceneHost.tsx
|
|
890
|
-
function captureSubtreeOrigins(objects) {
|
|
891
|
-
const parentOrders = /* @__PURE__ */ new Map();
|
|
892
|
-
const origins = /* @__PURE__ */ new Map();
|
|
893
|
-
for (const obj of objects) {
|
|
894
|
-
const origParent = obj.parent;
|
|
895
|
-
let origOrder = /* @__PURE__ */ new Map();
|
|
896
|
-
let origIndex = -1;
|
|
897
|
-
if (origParent) {
|
|
898
|
-
let order = parentOrders.get(origParent);
|
|
899
|
-
if (!order) {
|
|
900
|
-
order = /* @__PURE__ */ new Map();
|
|
901
|
-
origParent.children.forEach((child, idx) => order.set(child, idx));
|
|
902
|
-
parentOrders.set(origParent, order);
|
|
903
|
-
}
|
|
904
|
-
origOrder = order;
|
|
905
|
-
origIndex = order.get(obj) ?? origParent.children.indexOf(obj);
|
|
906
|
-
}
|
|
907
|
-
origins.set(obj, {
|
|
908
|
-
origParent,
|
|
909
|
-
origIndex,
|
|
910
|
-
origOrder,
|
|
911
|
-
origPos: obj.position.clone(),
|
|
912
|
-
origQuat: obj.quaternion.clone(),
|
|
913
|
-
origScale: obj.scale.clone()
|
|
914
|
-
});
|
|
915
|
-
}
|
|
916
|
-
return origins;
|
|
917
|
-
}
|
|
918
|
-
function restoreSubtreeOrigin(object, snap) {
|
|
919
|
-
object.position.copy(snap.origPos);
|
|
920
|
-
object.quaternion.copy(snap.origQuat);
|
|
921
|
-
object.scale.copy(snap.origScale);
|
|
922
|
-
const { origParent, origIndex, origOrder } = snap;
|
|
923
|
-
if (origParent) {
|
|
924
|
-
origParent.add(object);
|
|
925
|
-
const curIdx = origParent.children.indexOf(object);
|
|
926
|
-
if (curIdx >= 0) origParent.children.splice(curIdx, 1);
|
|
927
|
-
let insertAt = origParent.children.length;
|
|
928
|
-
if (origOrder.size > 0) {
|
|
929
|
-
for (let i = 0; i < origParent.children.length; i++) if ((origOrder.get(origParent.children[i]) ?? 1e400) > origIndex) {
|
|
930
|
-
insertAt = i;
|
|
931
|
-
break;
|
|
932
|
-
}
|
|
933
|
-
} else if (origIndex >= 0) insertAt = Math.min(origIndex, origParent.children.length);
|
|
934
|
-
origParent.children.splice(insertAt, 0, object);
|
|
935
|
-
}
|
|
936
|
-
}
|
|
937
|
-
function BundleSceneHost({ bundle, owner }) {
|
|
938
|
-
const hostRef = useRef(null);
|
|
939
|
-
useLayoutEffect(() => {
|
|
940
|
-
const host = hostRef.current;
|
|
941
|
-
if (!host) return;
|
|
942
|
-
claimBundleMount(bundle, owner);
|
|
943
|
-
host.add(bundle.scene);
|
|
944
|
-
return () => {
|
|
945
|
-
if (bundle.scene.parent === host) host.remove(bundle.scene);
|
|
946
|
-
releaseBundleMount(bundle);
|
|
947
|
-
};
|
|
948
|
-
}, [bundle, owner]);
|
|
949
|
-
return /* @__PURE__ */ jsx("group", { ref: hostRef });
|
|
950
|
-
}
|
|
951
|
-
function MountSubtreeSlot({ object, position, quaternion, origin }) {
|
|
952
|
-
const slotRef = useRef(null);
|
|
953
|
-
useLayoutEffect(() => {
|
|
954
|
-
const slot = slotRef.current;
|
|
955
|
-
if (!slot) return;
|
|
956
|
-
const snap = origin ?? captureSubtreeOrigins([object]).get(object);
|
|
957
|
-
if (snap.origParent) snap.origParent.remove(object);
|
|
958
|
-
object.position.set(0, 0, 0);
|
|
959
|
-
object.quaternion.set(0, 0, 0, 1);
|
|
960
|
-
object.scale.set(1, 1, 1);
|
|
961
|
-
slot.add(object);
|
|
962
|
-
const untrack = registerMountedSubtree(() => {
|
|
963
|
-
const p = object.position.clone(), q = object.quaternion.clone(), s = object.scale.clone();
|
|
964
|
-
if (object.parent === slot) slot.remove(object);
|
|
965
|
-
restoreSubtreeOrigin(object, snap);
|
|
966
|
-
return () => {
|
|
967
|
-
if (snap.origParent) snap.origParent.remove(object);
|
|
968
|
-
object.position.copy(p);
|
|
969
|
-
object.quaternion.copy(q);
|
|
970
|
-
object.scale.copy(s);
|
|
971
|
-
slot.add(object);
|
|
972
|
-
};
|
|
973
|
-
});
|
|
974
|
-
return () => {
|
|
975
|
-
untrack();
|
|
976
|
-
if (object.parent === slot) slot.remove(object);
|
|
977
|
-
restoreSubtreeOrigin(object, snap);
|
|
978
|
-
};
|
|
979
|
-
}, [object, origin]);
|
|
980
|
-
return /* @__PURE__ */ jsx("group", {
|
|
981
|
-
ref: slotRef,
|
|
982
|
-
position,
|
|
983
|
-
quaternion
|
|
984
|
-
});
|
|
985
|
-
}
|
|
986
|
-
var _q1 = new THREE.Quaternion();
|
|
987
|
-
var _q2 = new THREE.Quaternion();
|
|
988
|
-
var _qRel = new THREE.Quaternion();
|
|
989
|
-
var _restInv = new THREE.Quaternion();
|
|
990
|
-
var _deltaPos = new THREE.Vector3();
|
|
991
|
-
var _axWorld = new THREE.Vector3();
|
|
992
|
-
function createRapierImpulseJoint(rapier, world, parentBody, childBody, joint, anchorParent, anchorChild, axisParent, axisChild, restFrameRelQuat) {
|
|
993
|
-
const a1 = {
|
|
994
|
-
x: anchorParent[0],
|
|
995
|
-
y: anchorParent[1],
|
|
996
|
-
z: anchorParent[2]
|
|
997
|
-
};
|
|
998
|
-
const a2 = {
|
|
999
|
-
x: anchorChild[0],
|
|
1000
|
-
y: anchorChild[1],
|
|
1001
|
-
z: anchorChild[2]
|
|
1002
|
-
};
|
|
1003
|
-
const qRel = _qRel.set(restFrameRelQuat[0], restFrameRelQuat[1], restFrameRelQuat[2], restFrameRelQuat[3]).normalize();
|
|
1004
|
-
const axFromChild = _axWorld.set(axisChild[0], axisChild[1], axisChild[2]).applyQuaternion(qRel);
|
|
1005
|
-
const ax = (axFromChild.lengthSq() > 1e-12 ? axFromChild : _axWorld.set(axisParent[0], axisParent[1], axisParent[2])).normalize();
|
|
1006
|
-
let data;
|
|
1007
|
-
if (joint.kind === "fixed") {
|
|
1008
|
-
const pr = parentBody.rotation();
|
|
1009
|
-
const cr = childBody.rotation();
|
|
1010
|
-
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();
|
|
1011
|
-
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 ? {
|
|
1012
|
-
x: bodyRel.x,
|
|
1013
|
-
y: bodyRel.y,
|
|
1014
|
-
z: bodyRel.z,
|
|
1015
|
-
w: bodyRel.w
|
|
1016
|
-
} : {
|
|
1017
|
-
x: qRel.x,
|
|
1018
|
-
y: qRel.y,
|
|
1019
|
-
z: qRel.z,
|
|
1020
|
-
w: qRel.w
|
|
1021
|
-
};
|
|
1022
|
-
data = rapier.JointData.fixed(a1, f1, a2, {
|
|
1023
|
-
x: 0,
|
|
1024
|
-
y: 0,
|
|
1025
|
-
z: 0,
|
|
1026
|
-
w: 1
|
|
1027
|
-
});
|
|
1028
|
-
} else if (joint.kind === "revolute") {
|
|
1029
|
-
data = rapier.JointData.revolute(a1, a2, {
|
|
1030
|
-
x: ax.x,
|
|
1031
|
-
y: ax.y,
|
|
1032
|
-
z: ax.z
|
|
1033
|
-
});
|
|
1034
|
-
if (joint.limits) {
|
|
1035
|
-
data.limitsEnabled = true;
|
|
1036
|
-
data.limits = [joint.limits[0], joint.limits[1]];
|
|
1037
|
-
}
|
|
1038
|
-
} else {
|
|
1039
|
-
data = rapier.JointData.prismatic(a1, a2, {
|
|
1040
|
-
x: ax.x,
|
|
1041
|
-
y: ax.y,
|
|
1042
|
-
z: ax.z
|
|
1043
|
-
});
|
|
1044
|
-
if (joint.limits) {
|
|
1045
|
-
data.limitsEnabled = true;
|
|
1046
|
-
data.limits = [joint.limits[0], joint.limits[1]];
|
|
1047
|
-
}
|
|
1048
|
-
}
|
|
1049
|
-
const imp = world.createImpulseJoint(data, parentBody, childBody, true);
|
|
1050
|
-
imp.setContactsEnabled(Boolean(joint.enableCollision));
|
|
1051
|
-
configureJointDrive(rapier, imp, joint.kind, joint.drive, joint.drive?.positionTarget ?? null);
|
|
1052
|
-
return imp;
|
|
1053
|
-
}
|
|
1054
|
-
function configureJointDrive(rapier, imp, kind, drive, targetPos) {
|
|
1055
|
-
if (kind === "fixed" || !drive) return;
|
|
1056
|
-
const unit = imp;
|
|
1057
|
-
unit.configureMotorModel(drive.mode === "force" ? rapier.MotorModel.ForceBased : rapier.MotorModel.AccelerationBased);
|
|
1058
|
-
const effTargetPos = targetPos ?? drive.positionTarget ?? (drive.stiffness !== void 0 && drive.stiffness > 0 ? 0 : null);
|
|
1059
|
-
if (effTargetPos !== null && drive.stiffness !== void 0) {
|
|
1060
|
-
if (drive.velocityTarget !== void 0) unit.configureMotor(effTargetPos, drive.velocityTarget, drive.stiffness, drive.damping ?? 0);
|
|
1061
|
-
else unit.configureMotorPosition(effTargetPos, drive.stiffness, drive.damping ?? 0);
|
|
1062
|
-
} else if (drive.velocityTarget !== void 0) unit.configureMotorVelocity(drive.velocityTarget, drive.damping ?? 0);
|
|
1063
|
-
if (drive.maxForce !== void 0) unit.setMotorMaxForce?.(drive.maxForce);
|
|
1064
|
-
}
|
|
1065
|
-
function measureJointCoordinate(parentBody, childBody, kind, axisParent, anchorParent, restRelQuat) {
|
|
1066
|
-
if (!parentBody || !childBody || kind === "fixed") return 0;
|
|
1067
|
-
const pr = parentBody.rotation();
|
|
1068
|
-
const cr = childBody.rotation();
|
|
1069
|
-
_q1.set(pr.x, pr.y, pr.z, pr.w);
|
|
1070
|
-
_q2.set(cr.x, cr.y, cr.z, cr.w);
|
|
1071
|
-
if (kind === "prismatic") {
|
|
1072
|
-
const pt = parentBody.translation();
|
|
1073
|
-
const ct = childBody.translation();
|
|
1074
|
-
_deltaPos.set(anchorParent[0], anchorParent[1], anchorParent[2]).applyQuaternion(_q1);
|
|
1075
|
-
_deltaPos.set(ct.x - (pt.x + _deltaPos.x), ct.y - (pt.y + _deltaPos.y), ct.z - (pt.z + _deltaPos.z));
|
|
1076
|
-
_axWorld.set(axisParent[0], axisParent[1], axisParent[2]).applyQuaternion(_q1).normalize();
|
|
1077
|
-
return _deltaPos.dot(_axWorld);
|
|
1078
|
-
}
|
|
1079
|
-
_restInv.set(restRelQuat[0], restRelQuat[1], restRelQuat[2], restRelQuat[3]).invert();
|
|
1080
|
-
_qRel.copy(_q1).invert().multiply(_q2).premultiply(_restInv).normalize();
|
|
1081
|
-
if (_qRel.w < 0) _qRel.set(-_qRel.x, -_qRel.y, -_qRel.z, -_qRel.w);
|
|
1082
|
-
const sinHalf = _qRel.x * axisParent[0] + _qRel.y * axisParent[1] + _qRel.z * axisParent[2];
|
|
1083
|
-
return 2 * Math.atan2(sinHalf, _qRel.w);
|
|
1084
|
-
}
|
|
1085
|
-
function buildJointRuntime(params) {
|
|
1086
|
-
const { rapier, world, nodeName, role, kind, object, axisParent, anchorParent, restRelQuat, limits, drive, breakaway, rapierJoint, parentBody, childBody } = params;
|
|
1087
|
-
const rt = {
|
|
1088
|
-
nodeName,
|
|
1089
|
-
role,
|
|
1090
|
-
kind,
|
|
1091
|
-
object,
|
|
1092
|
-
axis: new THREE.Vector3(axisParent[0], axisParent[1], axisParent[2]).normalize(),
|
|
1093
|
-
limits,
|
|
1094
|
-
drive,
|
|
1095
|
-
positionTarget: drive?.positionTarget ?? null,
|
|
1096
|
-
velocityTarget: drive?.velocityTarget ?? 0,
|
|
1097
|
-
rapierJoint,
|
|
1098
|
-
parentBody,
|
|
1099
|
-
childBody,
|
|
1100
|
-
unlatched: false,
|
|
1101
|
-
detached: false,
|
|
1102
|
-
getCoordinate: () => measureJointCoordinate(rt.parentBody, rt.childBody, kind, axisParent, anchorParent, restRelQuat),
|
|
1103
|
-
applyTarget: (target) => {
|
|
1104
|
-
if (rt.detached || !rt.rapierJoint) return;
|
|
1105
|
-
if (!drive || drive.stiffness === void 0) throw new KhrValidationError(`Joint "${nodeName}" has no authored drive stiffness; cannot set a position target`);
|
|
1106
|
-
rt.unlatched = false;
|
|
1107
|
-
rt.positionTarget = target;
|
|
1108
|
-
configureJointDrive(rapier, rt.rapierJoint, kind, drive, target);
|
|
1109
|
-
if (parentBody.isDynamic()) parentBody.wakeUp();
|
|
1110
|
-
if (childBody.isDynamic()) childBody.wakeUp();
|
|
1111
|
-
},
|
|
1112
|
-
applyImpactImpulse: (impulseNs) => {
|
|
1113
|
-
if (rt.detached || !breakaway || !(impulseNs > 0)) return rt.detached ? "detached" : rt.unlatched ? "unlatched" : "intact";
|
|
1114
|
-
if (impulseNs >= breakaway.breakImpulseNs) {
|
|
1115
|
-
if (world && rt.rapierJoint && world.getImpulseJoint(rt.rapierJoint.handle)) world.removeImpulseJoint(rt.rapierJoint, true);
|
|
1116
|
-
rt.rapierJoint = null;
|
|
1117
|
-
rt.detached = true;
|
|
1118
|
-
rt.unlatched = true;
|
|
1119
|
-
rt.positionTarget = null;
|
|
1120
|
-
const ud = childBody.userData;
|
|
1121
|
-
if (ud?.ecctrl) childBody.userData = {
|
|
1122
|
-
...ud,
|
|
1123
|
-
ecctrl: {
|
|
1124
|
-
...ud.ecctrl,
|
|
1125
|
-
excludeVehicleRay: false
|
|
1126
|
-
}
|
|
1127
|
-
};
|
|
1128
|
-
for (let i = 0; i < childBody.numColliders(); i++) {
|
|
1129
|
-
const col = childBody.collider(i);
|
|
1130
|
-
const cg = col.collisionGroups();
|
|
1131
|
-
if (cg === 196605 || cg === 0x4000bfff) {
|
|
1132
|
-
col.setCollisionGroups(0xffffffff);
|
|
1133
|
-
col.setSolverGroups(0xffffffff);
|
|
1134
|
-
}
|
|
1135
|
-
}
|
|
1136
|
-
if (childBody.isDynamic()) childBody.wakeUp();
|
|
1137
|
-
return "detached";
|
|
1138
|
-
}
|
|
1139
|
-
if (breakaway.latchImpulseNs !== void 0 && impulseNs >= breakaway.latchImpulseNs && !rt.unlatched) {
|
|
1140
|
-
rt.unlatched = true;
|
|
1141
|
-
rt.positionTarget = null;
|
|
1142
|
-
if (kind !== "fixed" && rt.rapierJoint) rt.rapierJoint.configureMotorPosition(0, 0, Math.min(drive?.damping ?? 1, 1.5));
|
|
1143
|
-
if (childBody.isDynamic()) childBody.wakeUp();
|
|
1144
|
-
return "unlatched";
|
|
1145
|
-
}
|
|
1146
|
-
return rt.unlatched ? "unlatched" : "intact";
|
|
1147
|
-
}
|
|
1148
|
-
};
|
|
1149
|
-
return rt;
|
|
1150
|
-
}
|
|
1151
|
-
// src/physicsEcs.ts
|
|
1152
|
-
/** Retained as an optional context export for compatibility; Rapier steps ImpulseJoints natively. */
|
|
1153
|
-
var JointRegistryContext = createContext(null);
|
|
1154
|
-
export { computeRelativePose as C, KhrValidationError as D, readUnitQuat as E, computeBodyInertia as S, readFiniteVec3 as T, releaseBundleMount as _, MountSubtreeSlot as a, assertUnitScale as b, 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, assertIdentityWorldScale as v, extractMeshColliderShape as w, computeAssemblyMassAndCom as x, assertSceneIdentityWorldScale as y };
|
|
1155
|
-
|
|
1156
|
-
//# sourceMappingURL=physicsEcs-Bi0ikONC.js.map
|