@khr3/core 0.2.11 → 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/dist/KhrAssembly.d.ts +4 -6
- package/dist/KhrCollider.d.ts +5 -1
- package/dist/clone.d.ts +5 -0
- package/dist/decoders.d.ts +30 -0
- package/dist/index.d.ts +4 -4
- package/dist/index.js +755 -245
- package/dist/index.js.map +1 -1
- package/dist/inertia.d.ts +23 -14
- package/dist/joints-5IgWDD2w.js +865 -0
- package/dist/joints-5IgWDD2w.js.map +1 -0
- package/dist/joints.d.ts +2 -0
- package/dist/types.d.ts +2 -0
- package/dist/vehicle.d.ts +4 -17
- package/dist/vehicle.js +465 -422
- package/dist/vehicle.js.map +1 -1
- package/dist/vehicleJoints.d.ts +50 -0
- package/package.json +1 -1
- package/dist/KhrAssembly-CKW8I_2z.js +0 -1667
- package/dist/KhrAssembly-CKW8I_2z.js.map +0 -1
package/dist/vehicle.js
CHANGED
|
@@ -4,8 +4,355 @@ import { useFrame } from "@react-three/fiber";
|
|
|
4
4
|
import { RigidBody, useRapier } from "@react-three/rapier";
|
|
5
5
|
import { useCustomGravity } from "@pinkaphant/ecctrl/gravity";
|
|
6
6
|
import { EcctrlVehicle, ShapeCastWheel, ThrustPropeller } from "@pinkaphant/ecctrl/vehicle";
|
|
7
|
-
import { C as
|
|
7
|
+
import { C as applyRapierBodyMass, D as bodyPoseMatrix, E as assertUnitScale, F as JointRegistryContext, I as KhrValidationError, L as assignRef, O as buildBodyMassTarget, T as assertSceneIdentityWorldScale, f as KhrCollider, h as captureSubtreeOrigins, i as closedTargetForLimits, l as openTargetForLimits, m as MountSubtreeSlot, o as createRapierImpulseJoint, p as BundleSceneHost, r as buildJointRuntime, s as isJointOpen, t as ATTACHED_PANEL_INTERACTION_GROUPS, u as toggleDrivenJoint, x as restoreSubtreeOrigin, y as registerMountedSubtree } from "./joints-5IgWDD2w.js";
|
|
8
8
|
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
|
|
9
|
+
// src/vehicleJoints.tsx
|
|
10
|
+
function mergeVehiclePanelUserData(userData) {
|
|
11
|
+
const base = userData && typeof userData === "object" ? userData : {};
|
|
12
|
+
const ecctrl = base.ecctrl && typeof base.ecctrl === "object" ? base.ecctrl : {};
|
|
13
|
+
return {
|
|
14
|
+
...base,
|
|
15
|
+
ecctrl: {
|
|
16
|
+
...ecctrl,
|
|
17
|
+
excludeVehicleRay: true
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
function planVehicleJointParts(bodies) {
|
|
22
|
+
const dyn = bodies.filter((b) => !b.isKinematic);
|
|
23
|
+
const byName = new Map(dyn.map((b) => [b.nodeName, b]));
|
|
24
|
+
const jointed = dyn.filter((b) => b.joint !== null);
|
|
25
|
+
if (jointed.length === 0) return [];
|
|
26
|
+
const depthMemo = /* @__PURE__ */ new Map();
|
|
27
|
+
const depthOf = (b, seen = /* @__PURE__ */ new Set()) => {
|
|
28
|
+
const hit = depthMemo.get(b.nodeName);
|
|
29
|
+
if (hit !== void 0) return hit;
|
|
30
|
+
if (!b.joint) {
|
|
31
|
+
depthMemo.set(b.nodeName, 0);
|
|
32
|
+
return 0;
|
|
33
|
+
}
|
|
34
|
+
if (seen.has(b.nodeName)) throw new KhrValidationError(`Joint cycle through "${b.nodeName}"`);
|
|
35
|
+
seen.add(b.nodeName);
|
|
36
|
+
const parent = byName.get(b.joint.connectedNodeName);
|
|
37
|
+
if (!parent) throw new KhrValidationError(`Vehicle joint on "${b.nodeName}" targets "${b.joint.connectedNodeName}", which is not a dynamic KHR body`);
|
|
38
|
+
const d = 1 + depthOf(parent, seen);
|
|
39
|
+
depthMemo.set(b.nodeName, d);
|
|
40
|
+
return d;
|
|
41
|
+
};
|
|
42
|
+
jointed.sort((a, b) => depthOf(a) - depthOf(b));
|
|
43
|
+
const one = new THREE.Vector3(1, 1, 1);
|
|
44
|
+
const rpPos = new THREE.Vector3(), rpQuat = new THREE.Quaternion();
|
|
45
|
+
const rcPos = new THREE.Vector3(), rcQuat = new THREE.Quaternion(), scl = new THREE.Vector3();
|
|
46
|
+
const partByName = /* @__PURE__ */ new Map();
|
|
47
|
+
return jointed.map((b) => {
|
|
48
|
+
const joint = b.joint;
|
|
49
|
+
const parent = partByName.get(joint.connectedNodeName) ?? null;
|
|
50
|
+
const bodyMat = bodyPoseMatrix(b);
|
|
51
|
+
(parent ? bodyPoseMatrix(parent.spec) : new THREE.Matrix4()).clone().invert().multiply(bodyMat).decompose(rpPos, rpQuat, scl);
|
|
52
|
+
bodyMat.decompose(rcPos, rcQuat, scl);
|
|
53
|
+
rcQuat.normalize();
|
|
54
|
+
const axChildVec = new THREE.Vector3().fromArray(joint.axis).normalize();
|
|
55
|
+
const axParentVec = axChildVec.clone().applyQuaternion(rcQuat).normalize();
|
|
56
|
+
const anchorParent = parent ? [
|
|
57
|
+
rcPos.x - parent.restRelChassisPos[0],
|
|
58
|
+
rcPos.y - parent.restRelChassisPos[1],
|
|
59
|
+
rcPos.z - parent.restRelChassisPos[2]
|
|
60
|
+
] : [
|
|
61
|
+
rpPos.x,
|
|
62
|
+
rpPos.y,
|
|
63
|
+
rpPos.z
|
|
64
|
+
];
|
|
65
|
+
const rcQuatTuple = [
|
|
66
|
+
rcQuat.x,
|
|
67
|
+
rcQuat.y,
|
|
68
|
+
rcQuat.z,
|
|
69
|
+
rcQuat.w
|
|
70
|
+
];
|
|
71
|
+
const massTarget = buildBodyMassTarget(b, rcQuatTuple);
|
|
72
|
+
const density = massTarget && !massTarget.hasExplicitInertia ? 1 : 0;
|
|
73
|
+
const frameMat = new THREE.Matrix4().makeRotationFromQuaternion(rcQuat);
|
|
74
|
+
const cPos = new THREE.Vector3(), cQuat = new THREE.Quaternion();
|
|
75
|
+
const colliders = b.colliders.map((spec) => {
|
|
76
|
+
const colQuat = new THREE.Quaternion().fromArray(spec.localQuaternion);
|
|
77
|
+
new THREE.Matrix4().compose(new THREE.Vector3().fromArray(spec.localPosition), colQuat, one).premultiply(frameMat).decompose(cPos, cQuat, scl);
|
|
78
|
+
cQuat.normalize();
|
|
79
|
+
return {
|
|
80
|
+
spec: spec.collisionGroups === void 0 ? {
|
|
81
|
+
...spec,
|
|
82
|
+
collisionGroups: ATTACHED_PANEL_INTERACTION_GROUPS
|
|
83
|
+
} : spec,
|
|
84
|
+
position: [
|
|
85
|
+
cPos.x,
|
|
86
|
+
cPos.y,
|
|
87
|
+
cPos.z
|
|
88
|
+
],
|
|
89
|
+
quaternion: [
|
|
90
|
+
cQuat.x,
|
|
91
|
+
cQuat.y,
|
|
92
|
+
cQuat.z,
|
|
93
|
+
cQuat.w
|
|
94
|
+
],
|
|
95
|
+
density
|
|
96
|
+
};
|
|
97
|
+
});
|
|
98
|
+
const plan = {
|
|
99
|
+
spec: b,
|
|
100
|
+
joint,
|
|
101
|
+
parent,
|
|
102
|
+
restRelParentPos: anchorParent,
|
|
103
|
+
restRelParentQuat: rcQuatTuple,
|
|
104
|
+
restRelChassisPos: [
|
|
105
|
+
rcPos.x,
|
|
106
|
+
rcPos.y,
|
|
107
|
+
rcPos.z
|
|
108
|
+
],
|
|
109
|
+
restRelChassisQuat: rcQuatTuple,
|
|
110
|
+
axisParent: [
|
|
111
|
+
axParentVec.x,
|
|
112
|
+
axParentVec.y,
|
|
113
|
+
axParentVec.z
|
|
114
|
+
],
|
|
115
|
+
axisChild: [
|
|
116
|
+
axChildVec.x,
|
|
117
|
+
axChildVec.y,
|
|
118
|
+
axChildVec.z
|
|
119
|
+
],
|
|
120
|
+
massTarget,
|
|
121
|
+
colliders
|
|
122
|
+
};
|
|
123
|
+
partByName.set(b.nodeName, plan);
|
|
124
|
+
return plan;
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
function VehicleArticulatedPanel({ part, spawnPos, spawnQuat, linearVelocity, angularVelocity, gravityScale, panelUserData, parentBody, origin, onBody, onJointReady }) {
|
|
128
|
+
const { rapier, world } = useRapier();
|
|
129
|
+
const registry = useContext(JointRegistryContext);
|
|
130
|
+
const rtRef = useRef(null);
|
|
131
|
+
const [childBody, setChildBody] = useState(null);
|
|
132
|
+
const worldPose = useMemo(() => {
|
|
133
|
+
const sq = new THREE.Quaternion(spawnQuat[0], spawnQuat[1], spawnQuat[2], spawnQuat[3]).normalize();
|
|
134
|
+
const pos = new THREE.Vector3().fromArray(part.restRelChassisPos).applyQuaternion(sq).add(new THREE.Vector3(spawnPos[0], spawnPos[1], spawnPos[2]));
|
|
135
|
+
return {
|
|
136
|
+
pos: [
|
|
137
|
+
pos.x,
|
|
138
|
+
pos.y,
|
|
139
|
+
pos.z
|
|
140
|
+
],
|
|
141
|
+
quat: [
|
|
142
|
+
sq.x,
|
|
143
|
+
sq.y,
|
|
144
|
+
sq.z,
|
|
145
|
+
sq.w
|
|
146
|
+
]
|
|
147
|
+
};
|
|
148
|
+
}, [
|
|
149
|
+
part.restRelChassisPos,
|
|
150
|
+
spawnPos,
|
|
151
|
+
spawnQuat
|
|
152
|
+
]);
|
|
153
|
+
const assignChildRef = useCallback((rb) => {
|
|
154
|
+
setChildBody(rb);
|
|
155
|
+
onBody(part.spec.nodeName, rb);
|
|
156
|
+
}, [part.spec.nodeName, onBody]);
|
|
157
|
+
useEffect(() => () => onBody(part.spec.nodeName, null), [part.spec.nodeName, onBody]);
|
|
158
|
+
useEffect(() => {
|
|
159
|
+
if (childBody && part.massTarget) applyRapierBodyMass(childBody, part.massTarget);
|
|
160
|
+
}, [childBody, part.massTarget]);
|
|
161
|
+
useEffect(() => {
|
|
162
|
+
if (!parentBody || !childBody) return;
|
|
163
|
+
const imp = createRapierImpulseJoint(rapier, world, parentBody, childBody, part.joint, part.restRelParentPos, [
|
|
164
|
+
0,
|
|
165
|
+
0,
|
|
166
|
+
0
|
|
167
|
+
], part.axisParent, part.axisChild, part.restRelParentQuat);
|
|
168
|
+
const rt = buildJointRuntime({
|
|
169
|
+
rapier,
|
|
170
|
+
world,
|
|
171
|
+
nodeName: part.spec.nodeName,
|
|
172
|
+
role: part.joint.role,
|
|
173
|
+
kind: part.joint.kind,
|
|
174
|
+
object: part.spec.object,
|
|
175
|
+
axisParent: part.axisParent,
|
|
176
|
+
anchorParent: part.restRelParentPos,
|
|
177
|
+
limits: part.joint.limits,
|
|
178
|
+
drive: part.joint.drive,
|
|
179
|
+
breakaway: part.spec.breakaway,
|
|
180
|
+
rapierJoint: imp,
|
|
181
|
+
parentBody,
|
|
182
|
+
childBody
|
|
183
|
+
});
|
|
184
|
+
rtRef.current = rt;
|
|
185
|
+
registry?.add(rt);
|
|
186
|
+
onJointReady(rt.nodeName, part.spec.object, rt);
|
|
187
|
+
return () => {
|
|
188
|
+
rtRef.current = null;
|
|
189
|
+
registry?.delete(rt);
|
|
190
|
+
onJointReady(rt.nodeName, part.spec.object, null);
|
|
191
|
+
if (rt.rapierJoint && world.getImpulseJoint(rt.rapierJoint.handle)) world.removeImpulseJoint(rt.rapierJoint, true);
|
|
192
|
+
};
|
|
193
|
+
}, [
|
|
194
|
+
rapier,
|
|
195
|
+
world,
|
|
196
|
+
part,
|
|
197
|
+
parentBody,
|
|
198
|
+
childBody,
|
|
199
|
+
registry,
|
|
200
|
+
onJointReady
|
|
201
|
+
]);
|
|
202
|
+
const onForce = useMemo(() => part.spec.breakaway ? (p) => {
|
|
203
|
+
if (world.timestep > 0) rtRef.current?.applyImpactImpulse(p.totalForceMagnitude * world.timestep);
|
|
204
|
+
} : void 0, [part.spec.breakaway, world]);
|
|
205
|
+
const linVel = part.spec.linearVelocity ?? linearVelocity;
|
|
206
|
+
const angVel = part.spec.angularVelocity ?? angularVelocity;
|
|
207
|
+
const gravScale = part.spec.gravityFactor ?? gravityScale;
|
|
208
|
+
return /* @__PURE__ */ jsxs(RigidBody, {
|
|
209
|
+
ref: assignChildRef,
|
|
210
|
+
type: part.spec.isKinematic ? "kinematicPosition" : "dynamic",
|
|
211
|
+
position: worldPose.pos,
|
|
212
|
+
quaternion: worldPose.quat,
|
|
213
|
+
colliders: false,
|
|
214
|
+
userData: panelUserData,
|
|
215
|
+
onContactForce: onForce,
|
|
216
|
+
...linVel ? { linearVelocity: linVel } : {},
|
|
217
|
+
...angVel ? { angularVelocity: angVel } : {},
|
|
218
|
+
...gravScale === void 0 ? {} : { gravityScale: gravScale },
|
|
219
|
+
children: [/* @__PURE__ */ jsx(MountSubtreeSlot, {
|
|
220
|
+
object: part.spec.object,
|
|
221
|
+
position: [
|
|
222
|
+
0,
|
|
223
|
+
0,
|
|
224
|
+
0
|
|
225
|
+
],
|
|
226
|
+
quaternion: part.restRelChassisQuat,
|
|
227
|
+
origin
|
|
228
|
+
}), part.colliders.map((col, cIdx) => /* @__PURE__ */ jsx(KhrCollider, {
|
|
229
|
+
c: col.spec,
|
|
230
|
+
position: col.position,
|
|
231
|
+
quaternion: col.quaternion,
|
|
232
|
+
density: col.density
|
|
233
|
+
}, `vj_${part.spec.nodeName}:${cIdx}`))]
|
|
234
|
+
});
|
|
235
|
+
}
|
|
236
|
+
var _pPos = new THREE.Vector3();
|
|
237
|
+
var _pQuat = new THREE.Quaternion();
|
|
238
|
+
var _anchor = new THREE.Vector3();
|
|
239
|
+
function KhrVehicleJoints({ bundleScene, bodies, vehicleRef, chassisBody, spawnPos, spawnQuat, enableCustomGravity = false, linearVelocity, angularVelocity, gravityScale, userData, jointsRef }) {
|
|
240
|
+
const { world } = useRapier();
|
|
241
|
+
const applyGravityField = useCustomGravity((state) => state.applyGravityField);
|
|
242
|
+
const parts = useMemo(() => planVehicleJointParts(bodies), [bodies]);
|
|
243
|
+
const origins = useMemo(() => parts.length > 0 ? captureSubtreeOrigins(parts.map((p) => p.spec.object), bundleScene) : null, [parts, bundleScene]);
|
|
244
|
+
const panelUserData = useMemo(() => mergeVehiclePanelUserData(userData), [userData]);
|
|
245
|
+
const bodyMapRef = useRef(/* @__PURE__ */ new Map());
|
|
246
|
+
const [partBodies, setPartBodies] = useState(() => /* @__PURE__ */ new Map());
|
|
247
|
+
const runtimeMapRef = useRef(/* @__PURE__ */ new Map());
|
|
248
|
+
const byObjectRef = useRef(/* @__PURE__ */ new Map());
|
|
249
|
+
useFrame(() => {
|
|
250
|
+
if (!enableCustomGravity || parts.length === 0 || world.timestep <= 0) return;
|
|
251
|
+
for (const rb of bodyMapRef.current.values()) if (rb.isDynamic()) applyGravityField(rb, world.timestep);
|
|
252
|
+
});
|
|
253
|
+
const handleBody = useCallback((nodeName, rb) => {
|
|
254
|
+
if (rb) bodyMapRef.current.set(nodeName, rb);
|
|
255
|
+
else bodyMapRef.current.delete(nodeName);
|
|
256
|
+
setPartBodies((prev) => {
|
|
257
|
+
if ((prev.get(nodeName) ?? null) === rb) return prev;
|
|
258
|
+
const next = new Map(prev);
|
|
259
|
+
if (rb) next.set(nodeName, rb);
|
|
260
|
+
else next.delete(nodeName);
|
|
261
|
+
return next;
|
|
262
|
+
});
|
|
263
|
+
}, []);
|
|
264
|
+
const handleJointReady = useCallback((nodeName, obj, rt) => {
|
|
265
|
+
if (rt) {
|
|
266
|
+
runtimeMapRef.current.set(nodeName, rt);
|
|
267
|
+
byObjectRef.current.set(obj, rt);
|
|
268
|
+
} else {
|
|
269
|
+
runtimeMapRef.current.delete(nodeName);
|
|
270
|
+
byObjectRef.current.delete(obj);
|
|
271
|
+
}
|
|
272
|
+
}, []);
|
|
273
|
+
useImperativeHandle(jointsRef, () => ({
|
|
274
|
+
get hasJoints() {
|
|
275
|
+
return parts.length > 0;
|
|
276
|
+
},
|
|
277
|
+
get jointNames() {
|
|
278
|
+
return parts.map((p) => p.spec.nodeName);
|
|
279
|
+
},
|
|
280
|
+
getBody: (nodeName) => bodyMapRef.current.get(nodeName) ?? null,
|
|
281
|
+
getJointRuntime: (nodeName) => runtimeMapRef.current.get(nodeName) ?? null,
|
|
282
|
+
getJointAngle: (nodeName) => runtimeMapRef.current.get(nodeName)?.getCoordinate() ?? null,
|
|
283
|
+
setJointTarget: (nodeName, target) => {
|
|
284
|
+
runtimeMapRef.current.get(nodeName)?.applyTarget(target);
|
|
285
|
+
},
|
|
286
|
+
setRoleTarget: (role, target) => {
|
|
287
|
+
for (const rt of runtimeMapRef.current.values()) if (rt.role === role) rt.applyTarget(target);
|
|
288
|
+
},
|
|
289
|
+
toggleHitObject: (hit) => {
|
|
290
|
+
for (let cur = hit; cur; cur = cur.parent) {
|
|
291
|
+
const rt = byObjectRef.current.get(cur);
|
|
292
|
+
if (rt && toggleDrivenJoint(rt)) return true;
|
|
293
|
+
}
|
|
294
|
+
return false;
|
|
295
|
+
},
|
|
296
|
+
toggleRolePrefix: (prefix) => {
|
|
297
|
+
const low = prefix.toLowerCase();
|
|
298
|
+
const matches = [...runtimeMapRef.current.values()].filter((rt) => !rt.detached && rt.drive?.stiffness !== void 0 && ((rt.role ?? "").toLowerCase().startsWith(low) || rt.nodeName.toLowerCase().startsWith(low)));
|
|
299
|
+
if (matches.length === 0) return;
|
|
300
|
+
const anyOpen = matches.some((rt) => isJointOpen(rt));
|
|
301
|
+
for (const rt of matches) {
|
|
302
|
+
const limits = rt.limits ?? [0, Math.PI / 2];
|
|
303
|
+
rt.applyTarget(anyOpen ? closedTargetForLimits(limits) : openTargetForLimits(limits));
|
|
304
|
+
}
|
|
305
|
+
},
|
|
306
|
+
shiftBodies: (dx, dy, dz) => {
|
|
307
|
+
for (const part of parts) {
|
|
308
|
+
const rb = bodyMapRef.current.get(part.spec.nodeName);
|
|
309
|
+
const rt = runtimeMapRef.current.get(part.spec.nodeName);
|
|
310
|
+
if (!rb || rt?.detached) continue;
|
|
311
|
+
const t = rb.translation();
|
|
312
|
+
rb.setTranslation({
|
|
313
|
+
x: t.x + dx,
|
|
314
|
+
y: t.y + dy,
|
|
315
|
+
z: t.z + dz
|
|
316
|
+
}, true);
|
|
317
|
+
}
|
|
318
|
+
},
|
|
319
|
+
syncBodiesToParent: () => {
|
|
320
|
+
const parent = vehicleRef.current?.body;
|
|
321
|
+
if (!parent) return;
|
|
322
|
+
const pt = parent.translation(), pr = parent.rotation(), lv = parent.linvel(), av = parent.angvel();
|
|
323
|
+
_pPos.set(pt.x, pt.y, pt.z);
|
|
324
|
+
_pQuat.set(pr.x, pr.y, pr.z, pr.w);
|
|
325
|
+
for (const part of parts) {
|
|
326
|
+
const rb = bodyMapRef.current.get(part.spec.nodeName);
|
|
327
|
+
const rt = runtimeMapRef.current.get(part.spec.nodeName);
|
|
328
|
+
if (!rb || rt?.detached) continue;
|
|
329
|
+
_anchor.fromArray(part.restRelChassisPos).applyQuaternion(_pQuat).add(_pPos);
|
|
330
|
+
rb.setTranslation({
|
|
331
|
+
x: _anchor.x,
|
|
332
|
+
y: _anchor.y,
|
|
333
|
+
z: _anchor.z
|
|
334
|
+
}, true);
|
|
335
|
+
rb.setRotation(pr, true);
|
|
336
|
+
rb.setLinvel(lv, true);
|
|
337
|
+
rb.setAngvel(av, true);
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
}), [parts, vehicleRef]);
|
|
341
|
+
if (parts.length === 0) return null;
|
|
342
|
+
return /* @__PURE__ */ jsx(Fragment, { children: parts.map((part) => /* @__PURE__ */ jsx(VehicleArticulatedPanel, {
|
|
343
|
+
part,
|
|
344
|
+
spawnPos,
|
|
345
|
+
spawnQuat,
|
|
346
|
+
linearVelocity,
|
|
347
|
+
angularVelocity,
|
|
348
|
+
gravityScale,
|
|
349
|
+
panelUserData,
|
|
350
|
+
parentBody: part.parent ? partBodies.get(part.parent.spec.nodeName) ?? null : chassisBody ?? vehicleRef.current?.body ?? null,
|
|
351
|
+
origin: origins?.get(part.spec.object),
|
|
352
|
+
onBody: handleBody,
|
|
353
|
+
onJointReady: handleJointReady
|
|
354
|
+
}, part.spec.nodeName)) });
|
|
355
|
+
}
|
|
9
356
|
// src/wheelLoads.ts
|
|
10
357
|
var STANDARD_GRAVITY = 9.81;
|
|
11
358
|
function projectToLowerBoundedSimplex(values, lowerBound, totalSum) {
|
|
@@ -99,7 +446,6 @@ function solveWheelLoads(wheels, totalMassKg, com, dampingRatio = .7, refGravity
|
|
|
99
446
|
}
|
|
100
447
|
// src/vehicle.tsx
|
|
101
448
|
var UNSPRUNG_CORNER_RATIO = .035;
|
|
102
|
-
var ATTACHED_PANEL_USER_DATA = { ecctrl: { excludeVehicleRay: true } };
|
|
103
449
|
var _samplePos = new THREE.Vector3();
|
|
104
450
|
function readPosTuple(pos) {
|
|
105
451
|
if (typeof pos === "number") return [
|
|
@@ -174,9 +520,44 @@ function readSpawnQuat(rotation, quaternion) {
|
|
|
174
520
|
function computeChassisMassProps(bodies) {
|
|
175
521
|
const dyn = bodies.filter((b) => !b.isKinematic && b.joint === null);
|
|
176
522
|
if (dyn.length === 0) throw new KhrValidationError("KhrVehicle: bundle has no dynamic rigid body for the chassis");
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
523
|
+
let mass = 0, mx = 0, my = 0, mz = 0;
|
|
524
|
+
let anyExplicitCom = false;
|
|
525
|
+
const rPos = new THREE.Vector3(), rQuat = new THREE.Quaternion();
|
|
526
|
+
for (const b of dyn) {
|
|
527
|
+
if (!(b.mass > 0)) continue;
|
|
528
|
+
mass += b.mass;
|
|
529
|
+
rPos.fromArray(b.centerOfMass).applyQuaternion(rQuat.fromArray(b.localQuaternion));
|
|
530
|
+
const cx = b.localPosition[0] + rPos.x;
|
|
531
|
+
const cy = b.localPosition[1] + rPos.y;
|
|
532
|
+
const cz = b.localPosition[2] + rPos.z;
|
|
533
|
+
mx += cx * b.mass;
|
|
534
|
+
my += cy * b.mass;
|
|
535
|
+
mz += cz * b.mass;
|
|
536
|
+
if (b.hasExplicitCom || Math.abs(cx) > 1e-9 || Math.abs(cy) > 1e-9 || Math.abs(cz) > 1e-9) anyExplicitCom = true;
|
|
537
|
+
}
|
|
538
|
+
if (!(mass > 0)) throw new KhrValidationError("KhrVehicle: chassis dynamic mass must be positive");
|
|
539
|
+
const com = [
|
|
540
|
+
mx / mass,
|
|
541
|
+
my / mass,
|
|
542
|
+
mz / mass
|
|
543
|
+
];
|
|
544
|
+
const massTarget = (dyn.length === 1 ? buildBodyMassTarget(dyn[0], dyn[0].localQuaternion, dyn[0].localPosition) : null) ?? {
|
|
545
|
+
mass,
|
|
546
|
+
com,
|
|
547
|
+
hasExplicitCom: anyExplicitCom,
|
|
548
|
+
inertiaDiagonal: [
|
|
549
|
+
0,
|
|
550
|
+
0,
|
|
551
|
+
0
|
|
552
|
+
],
|
|
553
|
+
inertiaFrame: [
|
|
554
|
+
0,
|
|
555
|
+
0,
|
|
556
|
+
0,
|
|
557
|
+
1
|
|
558
|
+
],
|
|
559
|
+
hasExplicitInertia: false
|
|
560
|
+
};
|
|
180
561
|
let gravityFactor;
|
|
181
562
|
if (dyn.some((b) => b.gravityFactor !== void 0)) {
|
|
182
563
|
const g0 = dyn[0].gravityFactor ?? 1;
|
|
@@ -197,9 +578,9 @@ function computeChassisMassProps(bodies) {
|
|
|
197
578
|
vz += lv[2] * b.mass;
|
|
198
579
|
}
|
|
199
580
|
linearVelocity = [
|
|
200
|
-
vx /
|
|
201
|
-
vy /
|
|
202
|
-
vz /
|
|
581
|
+
vx / mass,
|
|
582
|
+
vy / mass,
|
|
583
|
+
vz / mass
|
|
203
584
|
];
|
|
204
585
|
}
|
|
205
586
|
const rootDyn = dyn.find((b) => b.angularVelocity !== void 0);
|
|
@@ -209,21 +590,21 @@ function computeChassisMassProps(bodies) {
|
|
|
209
590
|
rootDyn.angularVelocity[2]
|
|
210
591
|
] : void 0;
|
|
211
592
|
return {
|
|
212
|
-
mass
|
|
213
|
-
com
|
|
214
|
-
|
|
215
|
-
inertiaFrame: lumped.inertiaFrame,
|
|
593
|
+
mass,
|
|
594
|
+
com,
|
|
595
|
+
massTarget,
|
|
216
596
|
...linearVelocity ? { linearVelocity } : {},
|
|
217
597
|
...angularVelocity ? { angularVelocity } : {},
|
|
218
598
|
...gravityFactor !== void 0 ? { gravityFactor } : {}
|
|
219
599
|
};
|
|
220
600
|
}
|
|
221
|
-
function buildChassisColliders(bodies,
|
|
601
|
+
function buildChassisColliders(bodies, massTarget) {
|
|
222
602
|
const out = [];
|
|
223
603
|
const one = new THREE.Vector3(1, 1, 1);
|
|
224
604
|
const pos = new THREE.Vector3();
|
|
225
605
|
const quat = new THREE.Quaternion();
|
|
226
606
|
const scl = new THREE.Vector3();
|
|
607
|
+
const density = massTarget.hasExplicitInertia ? 0 : 1;
|
|
227
608
|
for (const b of bodies) {
|
|
228
609
|
if (b.joint !== null || b.isKinematic && b.hasExplicitMotion) continue;
|
|
229
610
|
const bodyMat = bodyPoseMatrix(b);
|
|
@@ -232,8 +613,6 @@ function buildChassisColliders(bodies, chassis) {
|
|
|
232
613
|
const colMat = new THREE.Matrix4().compose(new THREE.Vector3().fromArray(c.localPosition), new THREE.Quaternion().fromArray(c.localQuaternion), one);
|
|
233
614
|
bodyMat.clone().multiply(colMat).decompose(pos, quat, scl);
|
|
234
615
|
quat.normalize();
|
|
235
|
-
const isFirst = out.length === 0;
|
|
236
|
-
const rigidColMat = isFirst ? new THREE.Matrix4().compose(pos, quat, one) : void 0;
|
|
237
616
|
out.push({
|
|
238
617
|
key: `${b.nodeName}:${idx}`,
|
|
239
618
|
spec: {
|
|
@@ -250,7 +629,7 @@ function buildChassisColliders(bodies, chassis) {
|
|
|
250
629
|
quat.w
|
|
251
630
|
]
|
|
252
631
|
},
|
|
253
|
-
|
|
632
|
+
density
|
|
254
633
|
});
|
|
255
634
|
}
|
|
256
635
|
}
|
|
@@ -302,408 +681,57 @@ function resetPropellerOutputs(handle) {
|
|
|
302
681
|
p.thrustDir.set(0, 0, 0);
|
|
303
682
|
p.torqueDir.set(0, 0, 0);
|
|
304
683
|
p.thrustImpulse.set(0, 0, 0);
|
|
305
|
-
p.torqueImpulse.set(0, 0, 0);
|
|
306
|
-
p.finalThrottle = 0;
|
|
307
|
-
p.throttle = 0;
|
|
308
|
-
p.setThrottle?.(0);
|
|
309
|
-
}
|
|
310
|
-
}
|
|
311
|
-
function resetDisabledRotors(handle, opts) {
|
|
312
|
-
if (opts.wheelsAllOff) resetWheelHits(handle);
|
|
313
|
-
else if (opts.frontOff || opts.rearOff) resetWheelHits(handle, (name) => name && opts.frontWheelNames.has(name) ? opts.frontOff : opts.rearOff);
|
|
314
|
-
if (opts.propsOff) resetPropellerOutputs(handle);
|
|
315
|
-
}
|
|
316
|
-
function RotorVisualSlot({ object, center, quaternion, scene, origin }) {
|
|
317
|
-
const slotRef = useRef(null);
|
|
318
|
-
useLayoutEffect(() => {
|
|
319
|
-
const slot = slotRef.current;
|
|
320
|
-
if (!slot?.parent) return;
|
|
321
|
-
const snap = origin ?? captureSubtreeOrigins([object], scene).get(object);
|
|
322
|
-
scene.updateMatrixWorld(true);
|
|
323
|
-
const localMat = computeRotorLocalMatrix(object, scene, center, quaternion);
|
|
324
|
-
if (snap.origParent) snap.origParent.remove(object);
|
|
325
|
-
const pos = new THREE.Vector3(), quat = new THREE.Quaternion(), scl = new THREE.Vector3();
|
|
326
|
-
localMat.decompose(pos, quat, scl);
|
|
327
|
-
object.position.copy(pos);
|
|
328
|
-
object.quaternion.copy(quat.normalize());
|
|
329
|
-
object.scale.copy(scl);
|
|
330
|
-
slot.add(object);
|
|
331
|
-
const untrack = registerMountedSubtree(snap.ownerScene, () => {
|
|
332
|
-
const p = object.position.clone(), q = object.quaternion.clone(), s = object.scale.clone();
|
|
333
|
-
if (object.parent === slot) slot.remove(object);
|
|
334
|
-
restoreSubtreeOrigin(object, snap);
|
|
335
|
-
return () => {
|
|
336
|
-
if (snap.origParent) snap.origParent.remove(object);
|
|
337
|
-
object.position.copy(p);
|
|
338
|
-
object.quaternion.copy(q);
|
|
339
|
-
object.scale.copy(s);
|
|
340
|
-
slot.add(object);
|
|
341
|
-
};
|
|
342
|
-
});
|
|
343
|
-
return () => {
|
|
344
|
-
untrack();
|
|
345
|
-
if (object.parent === slot) slot.remove(object);
|
|
346
|
-
restoreSubtreeOrigin(object, snap);
|
|
347
|
-
};
|
|
348
|
-
}, [
|
|
349
|
-
object,
|
|
350
|
-
center,
|
|
351
|
-
quaternion,
|
|
352
|
-
scene,
|
|
353
|
-
origin
|
|
354
|
-
]);
|
|
355
|
-
return /* @__PURE__ */ jsx("group", { ref: slotRef });
|
|
356
|
-
}
|
|
357
|
-
function planVehicleJointParts(bodies) {
|
|
358
|
-
const dyn = bodies.filter((b) => !b.isKinematic);
|
|
359
|
-
const byName = new Map(dyn.map((b) => [b.nodeName, b]));
|
|
360
|
-
const jointed = dyn.filter((b) => b.joint !== null);
|
|
361
|
-
if (jointed.length === 0) return [];
|
|
362
|
-
const depthMemo = /* @__PURE__ */ new Map();
|
|
363
|
-
const depthOf = (b, seen = /* @__PURE__ */ new Set()) => {
|
|
364
|
-
const hit = depthMemo.get(b.nodeName);
|
|
365
|
-
if (hit !== void 0) return hit;
|
|
366
|
-
if (!b.joint) {
|
|
367
|
-
depthMemo.set(b.nodeName, 0);
|
|
368
|
-
return 0;
|
|
369
|
-
}
|
|
370
|
-
if (seen.has(b.nodeName)) throw new KhrValidationError(`Joint cycle through "${b.nodeName}"`);
|
|
371
|
-
seen.add(b.nodeName);
|
|
372
|
-
const parent = byName.get(b.joint.connectedNodeName);
|
|
373
|
-
if (!parent) throw new KhrValidationError(`Vehicle joint on "${b.nodeName}" targets "${b.joint.connectedNodeName}", which is not a dynamic KHR body`);
|
|
374
|
-
const d = 1 + depthOf(parent, seen);
|
|
375
|
-
depthMemo.set(b.nodeName, d);
|
|
376
|
-
return d;
|
|
377
|
-
};
|
|
378
|
-
jointed.sort((a, b) => depthOf(a) - depthOf(b));
|
|
379
|
-
const one = new THREE.Vector3(1, 1, 1);
|
|
380
|
-
const rpPos = new THREE.Vector3(), rpQuat = new THREE.Quaternion();
|
|
381
|
-
const rcPos = new THREE.Vector3(), rcQuat = new THREE.Quaternion(), scl = new THREE.Vector3();
|
|
382
|
-
const partByName = /* @__PURE__ */ new Map();
|
|
383
|
-
return jointed.map((b) => {
|
|
384
|
-
const joint = b.joint;
|
|
385
|
-
const parent = partByName.get(joint.connectedNodeName) ?? null;
|
|
386
|
-
const bodyMat = bodyPoseMatrix(b);
|
|
387
|
-
(parent ? bodyPoseMatrix(parent.spec) : new THREE.Matrix4()).clone().invert().multiply(bodyMat).decompose(rpPos, rpQuat, scl);
|
|
388
|
-
bodyMat.decompose(rcPos, rcQuat, scl);
|
|
389
|
-
rcQuat.normalize();
|
|
390
|
-
const axChildVec = new THREE.Vector3().fromArray(joint.axis).normalize();
|
|
391
|
-
const axParentVec = axChildVec.clone().applyQuaternion(rcQuat).normalize();
|
|
392
|
-
const anchorParent = parent ? [
|
|
393
|
-
rcPos.x - parent.restRelChassisPos[0],
|
|
394
|
-
rcPos.y - parent.restRelChassisPos[1],
|
|
395
|
-
rcPos.z - parent.restRelChassisPos[2]
|
|
396
|
-
] : [
|
|
397
|
-
rpPos.x,
|
|
398
|
-
rpPos.y,
|
|
399
|
-
rpPos.z
|
|
400
|
-
];
|
|
401
|
-
const bodyAlignInv = new THREE.Matrix4().compose(new THREE.Vector3().fromArray(b.localPosition), new THREE.Quaternion(), one).invert();
|
|
402
|
-
const ownLumped = computeLumpedMassProps([b], bodyAlignInv);
|
|
403
|
-
const frameMat = new THREE.Matrix4().makeRotationFromQuaternion(rcQuat);
|
|
404
|
-
const cPos = new THREE.Vector3(), cQuat = new THREE.Quaternion();
|
|
405
|
-
const colliders = b.colliders.map((spec, idx) => {
|
|
406
|
-
const colQuat = new THREE.Quaternion().fromArray(spec.localQuaternion);
|
|
407
|
-
const local = new THREE.Matrix4().compose(new THREE.Vector3().fromArray(spec.localPosition), colQuat, one).premultiply(frameMat);
|
|
408
|
-
local.decompose(cPos, cQuat, scl);
|
|
409
|
-
cQuat.normalize();
|
|
410
|
-
const planned = {
|
|
411
|
-
spec: spec.collisionGroups === void 0 ? {
|
|
412
|
-
...spec,
|
|
413
|
-
collisionGroups: ATTACHED_PANEL_INTERACTION_GROUPS
|
|
414
|
-
} : spec,
|
|
415
|
-
position: [
|
|
416
|
-
cPos.x,
|
|
417
|
-
cPos.y,
|
|
418
|
-
cPos.z
|
|
419
|
-
],
|
|
420
|
-
quaternion: [
|
|
421
|
-
cQuat.x,
|
|
422
|
-
cQuat.y,
|
|
423
|
-
cQuat.z,
|
|
424
|
-
cQuat.w
|
|
425
|
-
]
|
|
426
|
-
};
|
|
427
|
-
if (idx === 0 && ownLumped) planned.massProperties = toColliderMass(ownLumped, local, cQuat);
|
|
428
|
-
return planned;
|
|
429
|
-
});
|
|
430
|
-
const plan = {
|
|
431
|
-
spec: b,
|
|
432
|
-
joint,
|
|
433
|
-
parent,
|
|
434
|
-
restRelParentPos: anchorParent,
|
|
435
|
-
restRelParentQuat: [
|
|
436
|
-
rcQuat.x,
|
|
437
|
-
rcQuat.y,
|
|
438
|
-
rcQuat.z,
|
|
439
|
-
rcQuat.w
|
|
440
|
-
],
|
|
441
|
-
restRelChassisPos: [
|
|
442
|
-
rcPos.x,
|
|
443
|
-
rcPos.y,
|
|
444
|
-
rcPos.z
|
|
445
|
-
],
|
|
446
|
-
restRelChassisQuat: [
|
|
447
|
-
rcQuat.x,
|
|
448
|
-
rcQuat.y,
|
|
449
|
-
rcQuat.z,
|
|
450
|
-
rcQuat.w
|
|
451
|
-
],
|
|
452
|
-
axisParent: [
|
|
453
|
-
axParentVec.x,
|
|
454
|
-
axParentVec.y,
|
|
455
|
-
axParentVec.z
|
|
456
|
-
],
|
|
457
|
-
axisChild: [
|
|
458
|
-
axChildVec.x,
|
|
459
|
-
axChildVec.y,
|
|
460
|
-
axChildVec.z
|
|
461
|
-
],
|
|
462
|
-
...ownLumped && colliders.length === 0 ? { partMass: toColliderMass(ownLumped) } : {},
|
|
463
|
-
colliders
|
|
464
|
-
};
|
|
465
|
-
partByName.set(b.nodeName, plan);
|
|
466
|
-
return plan;
|
|
467
|
-
});
|
|
684
|
+
p.torqueImpulse.set(0, 0, 0);
|
|
685
|
+
p.finalThrottle = 0;
|
|
686
|
+
p.throttle = 0;
|
|
687
|
+
p.setThrottle?.(0);
|
|
688
|
+
}
|
|
468
689
|
}
|
|
469
|
-
function
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
useEffect(() => {
|
|
501
|
-
const pm = part.partMass;
|
|
502
|
-
if (childBody && pm) {
|
|
503
|
-
childBody.setAdditionalMassProperties(pm.mass, pm.centerOfMass, pm.principalAngularInertia, pm.angularInertiaLocalFrame, true);
|
|
504
|
-
childBody.recomputeMassPropertiesFromColliders();
|
|
505
|
-
}
|
|
506
|
-
}, [childBody, part.partMass]);
|
|
507
|
-
useEffect(() => {
|
|
508
|
-
if (!parentBody || !childBody) return;
|
|
509
|
-
const imp = createRapierImpulseJoint(rapier, world, parentBody, childBody, part.joint, part.restRelParentPos, [
|
|
510
|
-
0,
|
|
511
|
-
0,
|
|
512
|
-
0
|
|
513
|
-
], part.axisParent, part.axisChild, part.restRelParentQuat);
|
|
514
|
-
const rt = buildJointRuntime({
|
|
515
|
-
rapier,
|
|
516
|
-
world,
|
|
517
|
-
nodeName: part.spec.nodeName,
|
|
518
|
-
role: part.joint.role,
|
|
519
|
-
kind: part.joint.kind,
|
|
520
|
-
object: part.spec.object,
|
|
521
|
-
axisParent: part.axisParent,
|
|
522
|
-
anchorParent: part.restRelParentPos,
|
|
523
|
-
limits: part.joint.limits,
|
|
524
|
-
drive: part.joint.drive,
|
|
525
|
-
breakaway: part.spec.breakaway,
|
|
526
|
-
rapierJoint: imp,
|
|
527
|
-
parentBody,
|
|
528
|
-
childBody
|
|
690
|
+
function resetDisabledRotors(handle, opts) {
|
|
691
|
+
if (opts.wheelsAllOff) resetWheelHits(handle);
|
|
692
|
+
else if (opts.frontOff || opts.rearOff) resetWheelHits(handle, (name) => name && opts.frontWheelNames.has(name) ? opts.frontOff : opts.rearOff);
|
|
693
|
+
if (opts.propsOff) resetPropellerOutputs(handle);
|
|
694
|
+
}
|
|
695
|
+
function RotorVisualSlot({ object, center, quaternion, scene, origin }) {
|
|
696
|
+
const slotRef = useRef(null);
|
|
697
|
+
useLayoutEffect(() => {
|
|
698
|
+
const slot = slotRef.current;
|
|
699
|
+
if (!slot?.parent) return;
|
|
700
|
+
const snap = origin ?? captureSubtreeOrigins([object], scene).get(object);
|
|
701
|
+
scene.updateMatrixWorld(true);
|
|
702
|
+
const localMat = computeRotorLocalMatrix(object, scene, center, quaternion);
|
|
703
|
+
if (snap.origParent) snap.origParent.remove(object);
|
|
704
|
+
const pos = new THREE.Vector3(), quat = new THREE.Quaternion(), scl = new THREE.Vector3();
|
|
705
|
+
localMat.decompose(pos, quat, scl);
|
|
706
|
+
object.position.copy(pos);
|
|
707
|
+
object.quaternion.copy(quat.normalize());
|
|
708
|
+
object.scale.copy(scl);
|
|
709
|
+
slot.add(object);
|
|
710
|
+
const untrack = registerMountedSubtree(snap.ownerScene, () => {
|
|
711
|
+
const p = object.position.clone(), q = object.quaternion.clone(), s = object.scale.clone();
|
|
712
|
+
if (object.parent === slot) slot.remove(object);
|
|
713
|
+
restoreSubtreeOrigin(object, snap);
|
|
714
|
+
return () => {
|
|
715
|
+
if (snap.origParent) snap.origParent.remove(object);
|
|
716
|
+
object.position.copy(p);
|
|
717
|
+
object.quaternion.copy(q);
|
|
718
|
+
object.scale.copy(s);
|
|
719
|
+
slot.add(object);
|
|
720
|
+
};
|
|
529
721
|
});
|
|
530
|
-
rtRef.current = rt;
|
|
531
|
-
registry?.add(rt);
|
|
532
|
-
onJointReady(rt.nodeName, part.spec.object, rt);
|
|
533
722
|
return () => {
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
if (rt.rapierJoint && world.getImpulseJoint(rt.rapierJoint.handle)) world.removeImpulseJoint(rt.rapierJoint, true);
|
|
723
|
+
untrack();
|
|
724
|
+
if (object.parent === slot) slot.remove(object);
|
|
725
|
+
restoreSubtreeOrigin(object, snap);
|
|
538
726
|
};
|
|
539
727
|
}, [
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
registry,
|
|
546
|
-
onJointReady
|
|
728
|
+
object,
|
|
729
|
+
center,
|
|
730
|
+
quaternion,
|
|
731
|
+
scene,
|
|
732
|
+
origin
|
|
547
733
|
]);
|
|
548
|
-
|
|
549
|
-
if (world.timestep > 0) rtRef.current?.applyImpactImpulse(p.totalForceMagnitude * world.timestep);
|
|
550
|
-
} : void 0, [part.spec.breakaway, world]);
|
|
551
|
-
const linVel = part.spec.linearVelocity ?? linearVelocity;
|
|
552
|
-
const angVel = part.spec.angularVelocity ?? angularVelocity;
|
|
553
|
-
const gravScale = part.spec.gravityFactor ?? gravityScale;
|
|
554
|
-
return /* @__PURE__ */ jsxs(RigidBody, {
|
|
555
|
-
ref: assignChildRef,
|
|
556
|
-
type: part.spec.isKinematic ? "kinematicPosition" : "dynamic",
|
|
557
|
-
position: worldPose.pos,
|
|
558
|
-
quaternion: worldPose.quat,
|
|
559
|
-
colliders: false,
|
|
560
|
-
userData: ATTACHED_PANEL_USER_DATA,
|
|
561
|
-
onContactForce: onForce,
|
|
562
|
-
...linVel ? { linearVelocity: linVel } : {},
|
|
563
|
-
...angVel ? { angularVelocity: angVel } : {},
|
|
564
|
-
...gravScale === void 0 ? {} : { gravityScale: gravScale },
|
|
565
|
-
children: [/* @__PURE__ */ jsx(MountSubtreeSlot, {
|
|
566
|
-
object: part.spec.object,
|
|
567
|
-
position: [
|
|
568
|
-
0,
|
|
569
|
-
0,
|
|
570
|
-
0
|
|
571
|
-
],
|
|
572
|
-
quaternion: part.restRelChassisQuat,
|
|
573
|
-
origin
|
|
574
|
-
}), part.colliders.map((col, cIdx) => /* @__PURE__ */ jsx(KhrCollider, {
|
|
575
|
-
c: col.spec,
|
|
576
|
-
position: col.position,
|
|
577
|
-
quaternion: col.quaternion,
|
|
578
|
-
massProperties: col.massProperties
|
|
579
|
-
}, `vj_${part.spec.nodeName}:${cIdx}`))]
|
|
580
|
-
});
|
|
581
|
-
}
|
|
582
|
-
var _pPos = new THREE.Vector3();
|
|
583
|
-
var _pQuat = new THREE.Quaternion();
|
|
584
|
-
var _rQuat = new THREE.Quaternion();
|
|
585
|
-
var _cQuat = new THREE.Quaternion();
|
|
586
|
-
var _anchor = new THREE.Vector3();
|
|
587
|
-
function KhrVehicleJoints({ bundleScene, bodies, vehicleRef, chassisBody, spawnPos, spawnQuat, enableCustomGravity = false, linearVelocity, angularVelocity, gravityScale, jointsRef }) {
|
|
588
|
-
const { world } = useRapier();
|
|
589
|
-
const applyGravityField = useCustomGravity((state) => state.applyGravityField);
|
|
590
|
-
const parts = useMemo(() => planVehicleJointParts(bodies), [bodies]);
|
|
591
|
-
const origins = useMemo(() => parts.length > 0 ? captureSubtreeOrigins(parts.map((p) => p.spec.object), bundleScene) : null, [parts, bundleScene]);
|
|
592
|
-
const bodyMapRef = useRef(/* @__PURE__ */ new Map());
|
|
593
|
-
const [partBodies, setPartBodies] = useState(() => /* @__PURE__ */ new Map());
|
|
594
|
-
const runtimeMapRef = useRef(/* @__PURE__ */ new Map());
|
|
595
|
-
const byObjectRef = useRef(/* @__PURE__ */ new Map());
|
|
596
|
-
useFrame(() => {
|
|
597
|
-
if (!enableCustomGravity || parts.length === 0 || world.timestep <= 0) return;
|
|
598
|
-
for (const rb of bodyMapRef.current.values()) if (rb.isDynamic()) applyGravityField(rb, world.timestep);
|
|
599
|
-
});
|
|
600
|
-
const handleBody = useCallback((nodeName, rb) => {
|
|
601
|
-
if (rb) bodyMapRef.current.set(nodeName, rb);
|
|
602
|
-
else bodyMapRef.current.delete(nodeName);
|
|
603
|
-
setPartBodies((prev) => {
|
|
604
|
-
if ((prev.get(nodeName) ?? null) === rb) return prev;
|
|
605
|
-
const next = new Map(prev);
|
|
606
|
-
if (rb) next.set(nodeName, rb);
|
|
607
|
-
else next.delete(nodeName);
|
|
608
|
-
return next;
|
|
609
|
-
});
|
|
610
|
-
}, []);
|
|
611
|
-
const handleJointReady = useCallback((nodeName, obj, rt) => {
|
|
612
|
-
if (rt) {
|
|
613
|
-
runtimeMapRef.current.set(nodeName, rt);
|
|
614
|
-
byObjectRef.current.set(obj, rt);
|
|
615
|
-
} else {
|
|
616
|
-
runtimeMapRef.current.delete(nodeName);
|
|
617
|
-
byObjectRef.current.delete(obj);
|
|
618
|
-
}
|
|
619
|
-
}, []);
|
|
620
|
-
useImperativeHandle(jointsRef, () => ({
|
|
621
|
-
get hasJoints() {
|
|
622
|
-
return parts.length > 0;
|
|
623
|
-
},
|
|
624
|
-
get jointNames() {
|
|
625
|
-
return parts.map((p) => p.spec.nodeName);
|
|
626
|
-
},
|
|
627
|
-
getBody: (nodeName) => bodyMapRef.current.get(nodeName) ?? null,
|
|
628
|
-
getJointRuntime: (nodeName) => runtimeMapRef.current.get(nodeName) ?? null,
|
|
629
|
-
getJointAngle: (nodeName) => runtimeMapRef.current.get(nodeName)?.getCoordinate() ?? null,
|
|
630
|
-
setJointTarget: (nodeName, target) => {
|
|
631
|
-
runtimeMapRef.current.get(nodeName)?.applyTarget(target);
|
|
632
|
-
},
|
|
633
|
-
setRoleTarget: (role, target) => {
|
|
634
|
-
for (const rt of runtimeMapRef.current.values()) if (rt.role === role) rt.applyTarget(target);
|
|
635
|
-
},
|
|
636
|
-
toggleHitObject: (hit) => {
|
|
637
|
-
for (let cur = hit; cur; cur = cur.parent) {
|
|
638
|
-
const rt = byObjectRef.current.get(cur);
|
|
639
|
-
if (rt && rt.limits && rt.drive?.stiffness !== void 0) {
|
|
640
|
-
rt.applyTarget(isJointOpen(rt) ? 0 : openTargetForLimits(rt.limits));
|
|
641
|
-
return true;
|
|
642
|
-
}
|
|
643
|
-
}
|
|
644
|
-
return false;
|
|
645
|
-
},
|
|
646
|
-
toggleRolePrefix: (prefix) => {
|
|
647
|
-
const low = prefix.toLowerCase();
|
|
648
|
-
const matches = [...runtimeMapRef.current.values()].filter((rt) => rt.limits !== null && rt.drive?.stiffness !== void 0 && ((rt.role ?? "").toLowerCase().startsWith(low) || rt.nodeName.toLowerCase().startsWith(low)));
|
|
649
|
-
if (matches.length === 0) return;
|
|
650
|
-
const anyOpen = matches.some((rt) => isJointOpen(rt));
|
|
651
|
-
for (const rt of matches) rt.applyTarget(anyOpen ? 0 : openTargetForLimits(rt.limits));
|
|
652
|
-
},
|
|
653
|
-
shiftBodies: (dx, dy, dz) => {
|
|
654
|
-
for (const part of parts) {
|
|
655
|
-
const rb = bodyMapRef.current.get(part.spec.nodeName);
|
|
656
|
-
const rt = runtimeMapRef.current.get(part.spec.nodeName);
|
|
657
|
-
if (!rb || rt?.detached) continue;
|
|
658
|
-
const t = rb.translation();
|
|
659
|
-
rb.setTranslation({
|
|
660
|
-
x: t.x + dx,
|
|
661
|
-
y: t.y + dy,
|
|
662
|
-
z: t.z + dz
|
|
663
|
-
}, true);
|
|
664
|
-
}
|
|
665
|
-
},
|
|
666
|
-
syncBodiesToParent: () => {
|
|
667
|
-
const parent = vehicleRef.current?.body;
|
|
668
|
-
if (!parent) return;
|
|
669
|
-
const pt = parent.translation(), pr = parent.rotation(), lv = parent.linvel(), av = parent.angvel();
|
|
670
|
-
_pPos.set(pt.x, pt.y, pt.z);
|
|
671
|
-
_pQuat.set(pr.x, pr.y, pr.z, pr.w);
|
|
672
|
-
for (const part of parts) {
|
|
673
|
-
const rb = bodyMapRef.current.get(part.spec.nodeName);
|
|
674
|
-
const rt = runtimeMapRef.current.get(part.spec.nodeName);
|
|
675
|
-
if (!rb || rt?.detached) continue;
|
|
676
|
-
_anchor.fromArray(part.restRelChassisPos).applyQuaternion(_pQuat).add(_pPos);
|
|
677
|
-
_cQuat.copy(_pQuat).multiply(_rQuat.fromArray(part.restRelChassisQuat)).normalize();
|
|
678
|
-
rb.setTranslation({
|
|
679
|
-
x: _anchor.x,
|
|
680
|
-
y: _anchor.y,
|
|
681
|
-
z: _anchor.z
|
|
682
|
-
}, true);
|
|
683
|
-
rb.setRotation({
|
|
684
|
-
x: _cQuat.x,
|
|
685
|
-
y: _cQuat.y,
|
|
686
|
-
z: _cQuat.z,
|
|
687
|
-
w: _cQuat.w
|
|
688
|
-
}, true);
|
|
689
|
-
rb.setLinvel(lv, true);
|
|
690
|
-
rb.setAngvel(av, true);
|
|
691
|
-
}
|
|
692
|
-
}
|
|
693
|
-
}), [parts, vehicleRef]);
|
|
694
|
-
if (parts.length === 0) return null;
|
|
695
|
-
return /* @__PURE__ */ jsx(Fragment, { children: parts.map((part) => /* @__PURE__ */ jsx(VehicleArticulatedPanel, {
|
|
696
|
-
part,
|
|
697
|
-
spawnPos,
|
|
698
|
-
spawnQuat,
|
|
699
|
-
linearVelocity,
|
|
700
|
-
angularVelocity,
|
|
701
|
-
gravityScale,
|
|
702
|
-
parentBody: part.parent ? partBodies.get(part.parent.spec.nodeName) ?? null : chassisBody ?? vehicleRef.current?.body ?? null,
|
|
703
|
-
origin: origins?.get(part.spec.object),
|
|
704
|
-
onBody: handleBody,
|
|
705
|
-
onJointReady: handleJointReady
|
|
706
|
-
}, part.spec.nodeName)) });
|
|
734
|
+
return /* @__PURE__ */ jsx("group", { ref: slotRef });
|
|
707
735
|
}
|
|
708
736
|
function hasActiveMovement(m) {
|
|
709
737
|
return Boolean(m.forward || m.backward || m.steerLeft || m.steerRight || m.brake || m.throttleUp || m.throttleDown || m.yawLeft || m.yawRight || m.pitchForward || m.pitchBackward || m.rollLeft || m.rollRight || m.joystickL && (Math.abs(m.joystickL.x) > 1e-4 || Math.abs(m.joystickL.y) > 1e-4) || m.joystickR && (Math.abs(m.joystickR.x) > 1e-4 || Math.abs(m.joystickR.y) > 1e-4));
|
|
@@ -721,11 +749,25 @@ function KhrVehicle({ scale, colliders, bundle, ...rest }) {
|
|
|
721
749
|
const zeroGRef = useRef(isZeroG);
|
|
722
750
|
const [chassisBody, setChassisBody] = useState(null);
|
|
723
751
|
const chassis = useMemo(() => computeChassisMassProps(bundle.bodies), [bundle]);
|
|
724
|
-
const collidersList = useMemo(() => buildChassisColliders(bundle.bodies, chassis), [bundle.bodies, chassis]);
|
|
725
|
-
const
|
|
752
|
+
const collidersList = useMemo(() => buildChassisColliders(bundle.bodies, chassis.massTarget), [bundle.bodies, chassis.massTarget]);
|
|
753
|
+
const [rapierCom, setRapierCom] = useState(null);
|
|
754
|
+
const effCom = rapierCom ?? bundle.centerOfMass;
|
|
755
|
+
useEffect(() => {
|
|
756
|
+
if (!chassisBody) return;
|
|
757
|
+
applyRapierBodyMass(chassisBody, chassis.massTarget);
|
|
758
|
+
if (!chassis.massTarget.hasExplicitCom) {
|
|
759
|
+
const lc = chassisBody.localCom();
|
|
760
|
+
setRapierCom((prev) => prev && Math.hypot(prev[0] - lc.x, prev[1] - lc.y, prev[2] - lc.z) < 1e-4 ? prev : [
|
|
761
|
+
lc.x,
|
|
762
|
+
lc.y,
|
|
763
|
+
lc.z
|
|
764
|
+
]);
|
|
765
|
+
} else setRapierCom(null);
|
|
766
|
+
}, [chassisBody, chassis.massTarget]);
|
|
767
|
+
const solvedWheels = useMemo(() => solveWheelLoads(bundle.wheels, bundle.totalDynamicMass, effCom, .7, refGrav), [
|
|
726
768
|
bundle.wheels,
|
|
727
769
|
bundle.totalDynamicMass,
|
|
728
|
-
|
|
770
|
+
effCom,
|
|
729
771
|
refGrav
|
|
730
772
|
]);
|
|
731
773
|
const rotorOrigins = useMemo(() => captureSubtreeOrigins([...bundle.wheels.map((w) => w.object), ...bundle.propellers.map((p) => p.object)], bundle.scene), [
|
|
@@ -736,11 +778,11 @@ function KhrVehicle({ scale, colliders, bundle, ...rest }) {
|
|
|
736
778
|
const forwardSign = useMemo(() => {
|
|
737
779
|
const steered = solvedWheels.filter((w) => w.tag.steer && !w.tag.steerInvert);
|
|
738
780
|
if (steered.length === 0) return 1;
|
|
739
|
-
return steered.reduce((s, w) => s + w.mountPos[2], 0) / steered.length <
|
|
740
|
-
}, [solvedWheels,
|
|
741
|
-
const frontWheelNames = useMemo(() => new Set(solvedWheels.filter((w) => (w.mountPos[2] -
|
|
781
|
+
return steered.reduce((s, w) => s + w.mountPos[2], 0) / steered.length < effCom[2] - .001 ? -1 : 1;
|
|
782
|
+
}, [solvedWheels, effCom]);
|
|
783
|
+
const frontWheelNames = useMemo(() => new Set(solvedWheels.filter((w) => (w.mountPos[2] - effCom[2]) * forwardSign >= 0).map((w) => w.tag.nodeName)), [
|
|
742
784
|
solvedWheels,
|
|
743
|
-
|
|
785
|
+
effCom,
|
|
744
786
|
forwardSign
|
|
745
787
|
]);
|
|
746
788
|
const wheelsAllOff = !enable || isZeroG || wheelConfig?.enable === false && frontWheelConfig?.enable !== true && rearWheelConfig?.enable !== true;
|
|
@@ -871,9 +913,9 @@ function KhrVehicle({ scale, colliders, bundle, ...rest }) {
|
|
|
871
913
|
owner: "KhrVehicle"
|
|
872
914
|
}),
|
|
873
915
|
children,
|
|
874
|
-
collidersList.map(({ key, spec,
|
|
916
|
+
collidersList.map(({ key, spec, density }) => /* @__PURE__ */ jsx(KhrCollider, {
|
|
875
917
|
c: spec,
|
|
876
|
-
|
|
918
|
+
density
|
|
877
919
|
}, key)),
|
|
878
920
|
solvedWheels.map(({ tag, cornerMassKg, springK, dampingC, mountPos }) => {
|
|
879
921
|
const axleCfg = frontWheelNames.has(tag.nodeName) ? frontWheelConfig : rearWheelConfig;
|
|
@@ -983,9 +1025,10 @@ function KhrVehicle({ scale, colliders, bundle, ...rest }) {
|
|
|
983
1025
|
linearVelocity: effLinVel,
|
|
984
1026
|
angularVelocity: effAngVel,
|
|
985
1027
|
gravityScale: effGravScale,
|
|
1028
|
+
userData: ecctrlRest.userData,
|
|
986
1029
|
jointsRef
|
|
987
1030
|
})] });
|
|
988
1031
|
}
|
|
989
|
-
export { KhrVehicle, solveWheelLoads };
|
|
1032
|
+
export { KhrVehicle, mergeVehiclePanelUserData, solveWheelLoads };
|
|
990
1033
|
|
|
991
1034
|
//# sourceMappingURL=vehicle.js.map
|