@khr3/core 0.2.0 → 0.2.1
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/AssemblyRootBody.d.ts +2 -3
- package/dist/KhrAssembly.d.ts +3 -5
- package/dist/assemblyPlan.d.ts +8 -9
- package/dist/index.d.ts +1 -1
- package/dist/index.js +235 -210
- package/dist/index.js.map +1 -1
- package/dist/jointDynamics.d.ts +21 -17
- package/dist/khrGlb.d.ts +0 -5
- package/dist/khrVehicleJointHelpers.d.ts +13 -15
- package/dist/khrVehicleJoints.d.ts +5 -1
- package/dist/khrVehicleSlots.d.ts +7 -9
- package/dist/{BundleSceneHost-DUqqPtJF.js → physicsEcs-9H-Do7eX.js} +382 -358
- package/dist/physicsEcs-9H-Do7eX.js.map +1 -0
- package/dist/physicsEcs.d.ts +9 -40
- package/dist/vehicle.js +270 -209
- package/dist/vehicle.js.map +1 -1
- package/docs/api-reference.md +14 -15
- package/package.json +1 -1
- package/dist/BundleSceneHost-DUqqPtJF.js.map +0 -1
|
@@ -5,13 +5,12 @@ import { type JointRuntime } from "./physicsEcs.js";
|
|
|
5
5
|
export type AssemblyRigidBodyProps = Omit<RigidBodyProps, "type" | "position" | "rotation" | "quaternion" | "colliders" | "children" | "ref">;
|
|
6
6
|
export interface AssemblyRootBodyProps {
|
|
7
7
|
plan: RootPlan;
|
|
8
|
-
assemblyId: number;
|
|
9
8
|
position: [number, number, number];
|
|
10
9
|
quaternion: [number, number, number, number];
|
|
11
10
|
fixedRoot: boolean;
|
|
12
11
|
rigidBodyProps?: AssemblyRigidBodyProps;
|
|
13
|
-
onBody: (
|
|
12
|
+
onBody: (nodeName: string, rb: React.RefObject<RapierRigidBody | null> | null) => void;
|
|
14
13
|
onJoints: (rootName: string, joints: Map<string, JointRuntime> | null) => void;
|
|
15
14
|
children?: React.ReactNode;
|
|
16
15
|
}
|
|
17
|
-
export declare function AssemblyRootBody({ plan,
|
|
16
|
+
export declare function AssemblyRootBody({ plan, position, quaternion, fixedRoot, rigidBodyProps, onBody, onJoints, children, }: AssemblyRootBodyProps): React.JSX.Element;
|
package/dist/KhrAssembly.d.ts
CHANGED
|
@@ -4,11 +4,11 @@ import type { RapierRigidBody } from "@react-three/rapier";
|
|
|
4
4
|
import { type AssemblyRigidBodyProps } from "./AssemblyRootBody.js";
|
|
5
5
|
import { type KhrAssetBundle } from "./khrTypes.js";
|
|
6
6
|
export interface KhrAssemblyHandle {
|
|
7
|
-
/** RigidBody
|
|
7
|
+
/** RigidBody owning `nodeName`, or null if no KHR body has that name. */
|
|
8
8
|
getBody: (nodeName: string) => RapierRigidBody | null;
|
|
9
|
-
/** Sets the authored
|
|
9
|
+
/** Sets the authored Rapier motor position target on one articulated node. */
|
|
10
10
|
setJointTarget: (nodeName: string, target: number) => void;
|
|
11
|
-
/** Sets the position target on every articulated node whose `extras.role` matches. */
|
|
11
|
+
/** Sets the motor position target on every articulated node whose `extras.role` matches. */
|
|
12
12
|
setRoleTarget: (role: string, target: number) => void;
|
|
13
13
|
}
|
|
14
14
|
type GroupProps = Omit<ThreeElements["group"], "ref" | "position" | "rotation" | "quaternion" | "scale" | "children">;
|
|
@@ -18,9 +18,7 @@ export interface KhrAssemblyProps extends GroupProps {
|
|
|
18
18
|
position?: [number, number, number];
|
|
19
19
|
rotation?: [number, number, number];
|
|
20
20
|
quaternion?: [number, number, number, number];
|
|
21
|
-
/** Must be 1: assets are baked to identity scale by the importer gate. */
|
|
22
21
|
scale?: ThreeElements["group"]["scale"];
|
|
23
|
-
/** Kinematic roots become `fixed` bodies instead of `kinematicPosition`. */
|
|
24
22
|
fixedRoot?: boolean;
|
|
25
23
|
rigidBodyProps?: AssemblyRigidBodyProps;
|
|
26
24
|
children?: React.ReactNode;
|
package/dist/assemblyPlan.d.ts
CHANGED
|
@@ -24,39 +24,38 @@ export interface ColliderMass {
|
|
|
24
24
|
}
|
|
25
25
|
export interface PlannedCollider {
|
|
26
26
|
spec: KhrColliderSpec;
|
|
27
|
-
/** Pose in the owning body's local frame. */
|
|
28
27
|
local: THREE.Matrix4;
|
|
29
|
-
/** Rest pose in the root body frame. */
|
|
30
28
|
position: Vec3;
|
|
31
29
|
quaternion: Quat;
|
|
32
|
-
/** Composite mass properties carried by the stationary root's first collider; moving child colliders stay massless. */
|
|
33
30
|
massProperties?: ColliderMass;
|
|
34
31
|
}
|
|
35
32
|
export interface PartPlan {
|
|
36
33
|
spec: KhrBodySpec;
|
|
37
34
|
joint: KhrJointSpec;
|
|
38
35
|
parent: PartPlan | null;
|
|
39
|
-
|
|
36
|
+
restRelParentPos: Vec3;
|
|
37
|
+
restRelParentQuat: Quat;
|
|
38
|
+
restRelRootPos: Vec3;
|
|
39
|
+
restRelRootQuat: Quat;
|
|
40
|
+
axisParent: Vec3;
|
|
41
|
+
axisChild: Vec3;
|
|
40
42
|
inertia: number;
|
|
43
|
+
partMass?: ColliderMass;
|
|
41
44
|
colliders: PlannedCollider[];
|
|
42
45
|
}
|
|
43
46
|
export interface RootPlan {
|
|
44
47
|
spec: KhrBodySpec;
|
|
45
|
-
/** Pose of `bundle.scene` in this root body's local frame (used when `plans.length === 1`). */
|
|
46
48
|
sceneInRootPos: Vec3;
|
|
47
49
|
sceneInRootQuat: Quat;
|
|
48
|
-
/** Disjoint glTF subtree root that rides on this body when `plans.length > 1`. */
|
|
49
50
|
mountNode: THREE.Object3D;
|
|
50
51
|
mountInRoot: THREE.Matrix4;
|
|
51
52
|
mountPosition: Vec3;
|
|
52
53
|
mountQuaternion: Quat;
|
|
53
|
-
/** Composite mass properties in the root RigidBody frame when `colliders` is empty on a dynamic root. */
|
|
54
54
|
rootMass?: ColliderMass;
|
|
55
55
|
colliders: PlannedCollider[];
|
|
56
|
-
/** Parent-first order. */
|
|
57
56
|
parts: PartPlan[];
|
|
58
57
|
}
|
|
59
58
|
export declare function toColliderMass(lumped: LumpedMassProps, colMat?: THREE.Matrix4, colQuat?: THREE.Quaternion): ColliderMass;
|
|
60
|
-
/**
|
|
59
|
+
/** Plans independent Rapier bodies for roots and jointed dynamic children connected by Rapier ImpulseJoints. */
|
|
61
60
|
export declare function planAssembly(bundle: KhrAssetBundle): RootPlan[];
|
|
62
61
|
export {};
|
package/dist/index.d.ts
CHANGED
|
@@ -7,4 +7,4 @@ export { assertIdentityWorldScale, assertSceneIdentityWorldScale, assertUnitScal
|
|
|
7
7
|
export { KhrCollider } from "./KhrCollider.js";
|
|
8
8
|
export { KhrAssembly, type KhrAssemblyHandle, type KhrAssemblyProps } from "./KhrAssembly.js";
|
|
9
9
|
export { useKhrGlb } from "./useKhrGlb.js";
|
|
10
|
-
export { JointRegistryContext, type
|
|
10
|
+
export { JointRegistryContext, type JointRegistry, type JointRuntime } from "./physicsEcs.js";
|
package/dist/index.js
CHANGED
|
@@ -1,80 +1,28 @@
|
|
|
1
|
-
import { E as KhrValidationError, S as
|
|
2
|
-
import { useCallback, useContext, useEffect, useImperativeHandle,
|
|
3
|
-
import { useFrame, useLoader } from "@react-three/fiber";
|
|
1
|
+
import { C as extractMeshColliderShape, E as KhrValidationError, S as computeRelativePose, T as readUnitQuat, _ as assertIdentityWorldScale, a as MountSubtreeSlot, b as computeAssemblyMassAndCom, c as planAssembly, f as KhrCollider, g as releaseBundleMount, h as disposeKhrBundle, i as BundleSceneHost, m as cloneKhrBundle, n as buildJointRuntime, o as captureSubtreeOrigins, p as claimBundleMount, r as createRapierImpulseJoint, t as JointRegistryContext, v as assertSceneIdentityWorldScale, w as readFiniteVec3, x as computeBodyInertia, y as assertUnitScale } from "./physicsEcs-9H-Do7eX.js";
|
|
2
|
+
import { useCallback, useContext, useEffect, useImperativeHandle, useMemo, useRef } from "react";
|
|
4
3
|
import { Physics, RigidBody, useRapier } from "@react-three/rapier";
|
|
5
|
-
import
|
|
6
|
-
import { jsx, jsxs } from "react/jsx-runtime";
|
|
4
|
+
import { TimeControl } from "@pinkaphant/ecctrl/time";
|
|
5
|
+
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
|
|
7
6
|
import { GLTFLoader } from "three/examples/jsm/loaders/GLTFLoader.js";
|
|
7
|
+
import * as THREE from "three";
|
|
8
8
|
import { GLTFExporter } from "three/examples/jsm/exporters/GLTFExporter.js";
|
|
9
|
+
import { useLoader } from "@react-three/fiber";
|
|
9
10
|
// src/PhysicsWorld.tsx
|
|
10
|
-
var readVal = (v) => typeof v === "number" ? v : v.current;
|
|
11
|
-
function StepDriver({ paused, timeScale, maxDelta, joints, dtRef }) {
|
|
12
|
-
const { world } = useRapier();
|
|
13
|
-
const frameRef = useRef(0);
|
|
14
|
-
useFrame((_, rawDelta) => {
|
|
15
|
-
const ts = readVal(timeScale);
|
|
16
|
-
const md = Math.max(0, readVal(maxDelta));
|
|
17
|
-
const dt = !paused && ts > 0 && md > 0 && rawDelta > 0 ? Math.min(Math.min(rawDelta, md) * ts, .5) : 0;
|
|
18
|
-
if (!(dt > 0)) {
|
|
19
|
-
dtRef.current = 0;
|
|
20
|
-
return;
|
|
21
|
-
}
|
|
22
|
-
dtRef.current = dt;
|
|
23
|
-
world.timestep = dt;
|
|
24
|
-
if (joints.size > 0) {
|
|
25
|
-
const frame = ++frameRef.current;
|
|
26
|
-
for (const j of joints) stepJoint(j, dt);
|
|
27
|
-
for (const j of joints) writeJointPose(j, frame);
|
|
28
|
-
}
|
|
29
|
-
});
|
|
30
|
-
return null;
|
|
31
|
-
}
|
|
32
|
-
function StepCommit({ dtRef }) {
|
|
33
|
-
const { step } = useRapier();
|
|
34
|
-
useFrame(() => {
|
|
35
|
-
const dt = dtRef.current;
|
|
36
|
-
if (dt > 0) step(dt);
|
|
37
|
-
});
|
|
38
|
-
return null;
|
|
39
|
-
}
|
|
40
11
|
function PhysicsWorld({ paused = false, timeScale = 1, maxDelta = 1 / 30, gravity = [
|
|
41
12
|
0,
|
|
42
13
|
-9.81,
|
|
43
14
|
0
|
|
44
15
|
], children, ...physicsProps }) {
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
gravity
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
]);
|
|
56
|
-
return /* @__PURE__ */ jsx(JointRegistryContext.Provider, {
|
|
57
|
-
value: joints,
|
|
58
|
-
children: /* @__PURE__ */ jsx(WorldGravityContext.Provider, {
|
|
59
|
-
value: gravityMemo,
|
|
60
|
-
children: /* @__PURE__ */ jsxs(Physics, {
|
|
61
|
-
...physicsProps,
|
|
62
|
-
paused: true,
|
|
63
|
-
timeStep: "vary",
|
|
64
|
-
gravity,
|
|
65
|
-
children: [
|
|
66
|
-
/* @__PURE__ */ jsx(StepDriver, {
|
|
67
|
-
paused,
|
|
68
|
-
timeScale,
|
|
69
|
-
maxDelta,
|
|
70
|
-
joints,
|
|
71
|
-
dtRef
|
|
72
|
-
}),
|
|
73
|
-
children,
|
|
74
|
-
/* @__PURE__ */ jsx(StepCommit, { dtRef })
|
|
75
|
-
]
|
|
76
|
-
})
|
|
77
|
-
})
|
|
16
|
+
return /* @__PURE__ */ jsxs(Physics, {
|
|
17
|
+
...physicsProps,
|
|
18
|
+
paused: true,
|
|
19
|
+
timeStep: "vary",
|
|
20
|
+
gravity,
|
|
21
|
+
children: [children, /* @__PURE__ */ jsx(TimeControl, {
|
|
22
|
+
paused,
|
|
23
|
+
timeScale,
|
|
24
|
+
maxDelta
|
|
25
|
+
})]
|
|
78
26
|
});
|
|
79
27
|
}
|
|
80
28
|
// src/khrGlb.ts
|
|
@@ -544,6 +492,15 @@ function decodeJoint(rawJoints, jointIdx, nodeName, connectedNode, childNode, en
|
|
|
544
492
|
gain = linGain[freeLin[0]];
|
|
545
493
|
} else throw new KhrValidationError(`physicsJoints[${jointIdx}] unsupported DOF (${freeLin.length} lin, ${freeAng.length} ang) on "${nodeName}"`);
|
|
546
494
|
const drives = decodeJointDrives(rawDrives, jointIdx, nodeName);
|
|
495
|
+
if (isObj$2(raw.extras) && Array.isArray(raw.extras.jointAxis) && raw.extras.jointAxis.length === 3 && raw.extras.jointAxis.every((n) => typeof n === "number" && Number.isFinite(n))) {
|
|
496
|
+
const [jx, jy, jz] = raw.extras.jointAxis;
|
|
497
|
+
const jLen = Math.hypot(jx, jy, jz);
|
|
498
|
+
if (jLen > 1e-8) axis = [
|
|
499
|
+
jx / jLen,
|
|
500
|
+
jy / jLen,
|
|
501
|
+
jz / jLen
|
|
502
|
+
];
|
|
503
|
+
}
|
|
547
504
|
const anchor1Vec = childNode.getWorldPosition(new THREE.Vector3()).sub(connectedNode.getWorldPosition(new THREE.Vector3())).applyQuaternion(connectedNode.getWorldQuaternion(new THREE.Quaternion()).invert());
|
|
548
505
|
return {
|
|
549
506
|
index: jointIdx,
|
|
@@ -1009,7 +966,6 @@ function extractKhrBundleFromGltf(gltf, options) {
|
|
|
1009
966
|
if (rawExt !== void 0 && !isPlainObject(rawExt)) throw new KhrValidationError(`Node "${nodeNames[i]}" has invalid extensions`);
|
|
1010
967
|
if (isPlainObject(rawExt)) {
|
|
1011
968
|
if (Object.hasOwn(rawExt, "KOLMO_emitter") && rawExt.KOLMO_emitter !== void 0 && !Object.hasOwn(obj.userData, "KOLMO_emitter")) obj.userData.KOLMO_emitter = structuredClone(rawExt.KOLMO_emitter);
|
|
1012
|
-
if (Object.hasOwn(rawExt, "KHR3_vfx_emitter") && rawExt.KHR3_vfx_emitter !== void 0 && !Object.hasOwn(obj.userData, "KHR3_vfx_emitter")) obj.userData.KHR3_vfx_emitter = structuredClone(rawExt.KHR3_vfx_emitter);
|
|
1013
969
|
}
|
|
1014
970
|
const extras = isPlainObject(rawNodes[i].extras) ? rawNodes[i].extras : {};
|
|
1015
971
|
nodeMetas.push(collectNodeSockets(obj, nodeNames[i], extras, sockets, options?.isSocketNode));
|
|
@@ -1205,11 +1161,22 @@ function encodeShape(shape, nodeName) {
|
|
|
1205
1161
|
}
|
|
1206
1162
|
};
|
|
1207
1163
|
}
|
|
1208
|
-
function
|
|
1164
|
+
function normalizeJointAxis(axis, kind, nodeName) {
|
|
1209
1165
|
if (!Array.isArray(axis) || axis.length !== 3 || !axis.every((n) => typeof n === "number" && Number.isFinite(n))) throw new KhrValidationError(`exportKhrGlb: ${kind} joint on "${nodeName}" requires a finite axis`);
|
|
1210
|
-
const
|
|
1211
|
-
if (
|
|
1212
|
-
|
|
1166
|
+
const len = Math.hypot(axis[0], axis[1], axis[2]);
|
|
1167
|
+
if (len <= 1e-8) throw new KhrValidationError(`exportKhrGlb: ${kind} joint on "${nodeName}" requires non-zero axis`);
|
|
1168
|
+
const ux = axis[0] / len, uy = axis[1] / len, uz = axis[2] / len;
|
|
1169
|
+
const ax = Math.abs(ux), ay = Math.abs(uy), az = Math.abs(uz);
|
|
1170
|
+
const primary = ax >= ay && ax >= az ? 0 : ay >= az ? 1 : 2;
|
|
1171
|
+
return {
|
|
1172
|
+
primary,
|
|
1173
|
+
unit: [
|
|
1174
|
+
ux,
|
|
1175
|
+
uy,
|
|
1176
|
+
uz
|
|
1177
|
+
],
|
|
1178
|
+
isCardinal: (primary === 0 ? ax : primary === 1 ? ay : az) >= 1 - 1e-6
|
|
1179
|
+
};
|
|
1213
1180
|
}
|
|
1214
1181
|
function encodeJointDrive(drive, defaultType, defaultAxis, nodeName) {
|
|
1215
1182
|
if (!isObj(drive)) throw new KhrValidationError(`exportKhrGlb: joint drive on "${nodeName}" must be an object`);
|
|
@@ -1255,7 +1222,7 @@ function encodeJoint(j, nodeName) {
|
|
|
1255
1222
|
max: 0
|
|
1256
1223
|
}] };
|
|
1257
1224
|
}
|
|
1258
|
-
const ax =
|
|
1225
|
+
const { primary: ax, unit, isCardinal } = normalizeJointAxis(j.axis, j.kind, nodeName);
|
|
1259
1226
|
const lockedAxes = [
|
|
1260
1227
|
0,
|
|
1261
1228
|
1,
|
|
@@ -1274,39 +1241,42 @@ function encodeJoint(j, nodeName) {
|
|
|
1274
1241
|
...stiffness === void 0 ? {} : { stiffness },
|
|
1275
1242
|
...damping === void 0 ? {} : { damping }
|
|
1276
1243
|
}] : [];
|
|
1277
|
-
const out = {
|
|
1278
|
-
|
|
1279
|
-
|
|
1280
|
-
|
|
1281
|
-
|
|
1282
|
-
|
|
1283
|
-
|
|
1284
|
-
|
|
1285
|
-
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
|
|
1293
|
-
|
|
1294
|
-
|
|
1295
|
-
|
|
1296
|
-
|
|
1297
|
-
|
|
1298
|
-
|
|
1299
|
-
|
|
1300
|
-
|
|
1301
|
-
|
|
1302
|
-
|
|
1303
|
-
|
|
1304
|
-
|
|
1305
|
-
|
|
1306
|
-
|
|
1307
|
-
|
|
1308
|
-
|
|
1309
|
-
|
|
1244
|
+
const out = {
|
|
1245
|
+
limits: j.kind === "revolute" ? [
|
|
1246
|
+
{
|
|
1247
|
+
linearAxes: [
|
|
1248
|
+
0,
|
|
1249
|
+
1,
|
|
1250
|
+
2
|
|
1251
|
+
],
|
|
1252
|
+
min: 0,
|
|
1253
|
+
max: 0
|
|
1254
|
+
},
|
|
1255
|
+
{
|
|
1256
|
+
angularAxes: lockedAxes,
|
|
1257
|
+
min: 0,
|
|
1258
|
+
max: 0
|
|
1259
|
+
},
|
|
1260
|
+
...freeLimit
|
|
1261
|
+
] : [
|
|
1262
|
+
{
|
|
1263
|
+
angularAxes: [
|
|
1264
|
+
0,
|
|
1265
|
+
1,
|
|
1266
|
+
2
|
|
1267
|
+
],
|
|
1268
|
+
min: 0,
|
|
1269
|
+
max: 0
|
|
1270
|
+
},
|
|
1271
|
+
{
|
|
1272
|
+
linearAxes: lockedAxes,
|
|
1273
|
+
min: 0,
|
|
1274
|
+
max: 0
|
|
1275
|
+
},
|
|
1276
|
+
...freeLimit
|
|
1277
|
+
],
|
|
1278
|
+
...!isCardinal || unit[ax] < 0 ? { extras: { jointAxis: unit } } : {}
|
|
1279
|
+
};
|
|
1310
1280
|
const drives = j.drives ?? (j.drive ? [j.drive] : []);
|
|
1311
1281
|
if (drives.length > 0) out.drives = drives.map((d) => encodeJointDrive(d, j.kind === "revolute" ? "angular" : "linear", ax, nodeName));
|
|
1312
1282
|
return out;
|
|
@@ -1579,15 +1549,130 @@ async function exportKhrGlb(root, specs, sceneExtras) {
|
|
|
1579
1549
|
});
|
|
1580
1550
|
}
|
|
1581
1551
|
// src/AssemblyRootBody.tsx
|
|
1582
|
-
function
|
|
1583
|
-
const
|
|
1552
|
+
function ArticulatedPartBody({ part, rootPos, rootQuat, parentBodyRef, origin, onBody, onJointReady }) {
|
|
1553
|
+
const { rapier, world } = useRapier();
|
|
1554
|
+
const registry = useContext(JointRegistryContext);
|
|
1555
|
+
const childRef = useRef(null);
|
|
1556
|
+
const worldPose = useMemo(() => {
|
|
1557
|
+
const rq = new THREE.Quaternion(rootQuat[0], rootQuat[1], rootQuat[2], rootQuat[3]);
|
|
1558
|
+
const pos = new THREE.Vector3().fromArray(part.restRelRootPos).applyQuaternion(rq).add(new THREE.Vector3(rootPos[0], rootPos[1], rootPos[2]));
|
|
1559
|
+
const quat = rq.clone().multiply(new THREE.Quaternion().fromArray(part.restRelRootQuat)).normalize();
|
|
1560
|
+
return {
|
|
1561
|
+
pos: [
|
|
1562
|
+
pos.x,
|
|
1563
|
+
pos.y,
|
|
1564
|
+
pos.z
|
|
1565
|
+
],
|
|
1566
|
+
quat: [
|
|
1567
|
+
quat.x,
|
|
1568
|
+
quat.y,
|
|
1569
|
+
quat.z,
|
|
1570
|
+
quat.w
|
|
1571
|
+
]
|
|
1572
|
+
};
|
|
1573
|
+
}, [
|
|
1574
|
+
part.restRelRootPos,
|
|
1575
|
+
part.restRelRootQuat,
|
|
1576
|
+
rootPos,
|
|
1577
|
+
rootQuat
|
|
1578
|
+
]);
|
|
1579
|
+
useEffect(() => {
|
|
1580
|
+
onBody(part.spec.nodeName, childRef);
|
|
1581
|
+
return () => onBody(part.spec.nodeName, null);
|
|
1582
|
+
}, [part.spec.nodeName, onBody]);
|
|
1583
|
+
useEffect(() => {
|
|
1584
|
+
const rb = childRef.current;
|
|
1585
|
+
const pm = part.partMass;
|
|
1586
|
+
if (rb && pm) {
|
|
1587
|
+
rb.setAdditionalMassProperties(pm.mass, pm.centerOfMass, pm.principalAngularInertia, pm.angularInertiaLocalFrame, true);
|
|
1588
|
+
rb.recomputeMassPropertiesFromColliders();
|
|
1589
|
+
}
|
|
1590
|
+
}, [part.partMass]);
|
|
1591
|
+
useEffect(() => {
|
|
1592
|
+
const parentBody = parentBodyRef.current;
|
|
1593
|
+
const childBody = childRef.current;
|
|
1594
|
+
if (!parentBody || !childBody) return;
|
|
1595
|
+
const imp = createRapierImpulseJoint(rapier, world, parentBody, childBody, part.joint, part.restRelParentPos, [
|
|
1596
|
+
0,
|
|
1597
|
+
0,
|
|
1598
|
+
0
|
|
1599
|
+
], part.axisParent, part.axisChild, part.restRelParentQuat);
|
|
1600
|
+
const rt = buildJointRuntime({
|
|
1601
|
+
rapier,
|
|
1602
|
+
nodeName: part.spec.nodeName,
|
|
1603
|
+
role: part.joint.role,
|
|
1604
|
+
kind: part.joint.kind,
|
|
1605
|
+
object: part.spec.object,
|
|
1606
|
+
axisParent: part.axisParent,
|
|
1607
|
+
anchorParent: part.restRelParentPos,
|
|
1608
|
+
restRelQuat: part.restRelParentQuat,
|
|
1609
|
+
limits: part.joint.limits,
|
|
1610
|
+
drive: part.joint.drive,
|
|
1611
|
+
rapierJoint: imp,
|
|
1612
|
+
parentBody,
|
|
1613
|
+
childBody
|
|
1614
|
+
});
|
|
1615
|
+
registry?.add(rt);
|
|
1616
|
+
onJointReady(rt.nodeName, rt);
|
|
1617
|
+
return () => {
|
|
1618
|
+
registry?.delete(rt);
|
|
1619
|
+
onJointReady(rt.nodeName, null);
|
|
1620
|
+
if (world.getImpulseJoint(imp.handle)) world.removeImpulseJoint(imp, true);
|
|
1621
|
+
};
|
|
1622
|
+
}, [
|
|
1623
|
+
rapier,
|
|
1624
|
+
world,
|
|
1625
|
+
part,
|
|
1626
|
+
parentBodyRef,
|
|
1627
|
+
registry,
|
|
1628
|
+
onJointReady
|
|
1629
|
+
]);
|
|
1630
|
+
return /* @__PURE__ */ jsxs(RigidBody, {
|
|
1631
|
+
ref: childRef,
|
|
1632
|
+
type: part.spec.isKinematic ? "kinematicPosition" : "dynamic",
|
|
1633
|
+
position: worldPose.pos,
|
|
1634
|
+
quaternion: worldPose.quat,
|
|
1635
|
+
colliders: false,
|
|
1636
|
+
children: [/* @__PURE__ */ jsx(MountSubtreeSlot, {
|
|
1637
|
+
object: part.spec.object,
|
|
1638
|
+
position: [
|
|
1639
|
+
0,
|
|
1640
|
+
0,
|
|
1641
|
+
0
|
|
1642
|
+
],
|
|
1643
|
+
quaternion: [
|
|
1644
|
+
0,
|
|
1645
|
+
0,
|
|
1646
|
+
0,
|
|
1647
|
+
1
|
|
1648
|
+
],
|
|
1649
|
+
origin
|
|
1650
|
+
}), part.colliders.map((col, cIdx) => /* @__PURE__ */ jsx(KhrCollider, {
|
|
1651
|
+
c: col.spec,
|
|
1652
|
+
position: col.position,
|
|
1653
|
+
quaternion: col.quaternion,
|
|
1654
|
+
massProperties: col.massProperties
|
|
1655
|
+
}, `${part.spec.nodeName}:${cIdx}`))]
|
|
1656
|
+
});
|
|
1657
|
+
}
|
|
1658
|
+
function AssemblyRootBody({ plan, position, quaternion, fixedRoot, rigidBodyProps, onBody, onJoints, children }) {
|
|
1584
1659
|
const rbRef = useRef(null);
|
|
1585
|
-
const
|
|
1660
|
+
const partBodyRefs = useMemo(() => new Map(plan.parts.map((p) => [p.spec.nodeName, { current: null }])), [plan]);
|
|
1661
|
+
const partOrigins = useMemo(() => plan.parts.length > 0 ? captureSubtreeOrigins(plan.parts.map((p) => p.spec.object)) : null, [plan.parts]);
|
|
1662
|
+
const jointsMapRef = useRef(/* @__PURE__ */ new Map());
|
|
1586
1663
|
const rootSpec = plan.spec;
|
|
1587
1664
|
useEffect(() => {
|
|
1588
1665
|
onBody(rootSpec.nodeName, rbRef);
|
|
1589
|
-
|
|
1590
|
-
|
|
1666
|
+
onJoints(rootSpec.nodeName, jointsMapRef.current);
|
|
1667
|
+
return () => {
|
|
1668
|
+
onBody(rootSpec.nodeName, null);
|
|
1669
|
+
onJoints(rootSpec.nodeName, null);
|
|
1670
|
+
};
|
|
1671
|
+
}, [
|
|
1672
|
+
rootSpec.nodeName,
|
|
1673
|
+
onBody,
|
|
1674
|
+
onJoints
|
|
1675
|
+
]);
|
|
1591
1676
|
useEffect(() => {
|
|
1592
1677
|
const rb = rbRef.current;
|
|
1593
1678
|
const rm = plan.rootMass;
|
|
@@ -1596,60 +1681,17 @@ function AssemblyRootBody({ plan, assemblyId, position, quaternion, fixedRoot, r
|
|
|
1596
1681
|
rb.recomputeMassPropertiesFromColliders();
|
|
1597
1682
|
}
|
|
1598
1683
|
}, [plan.rootMass]);
|
|
1599
|
-
|
|
1600
|
-
const
|
|
1601
|
-
|
|
1602
|
-
|
|
1603
|
-
|
|
1604
|
-
|
|
1605
|
-
|
|
1606
|
-
|
|
1607
|
-
|
|
1608
|
-
kind: part.joint.kind,
|
|
1609
|
-
object: part.spec.object,
|
|
1610
|
-
axis: new THREE.Vector3().fromArray(part.joint.axis).normalize(),
|
|
1611
|
-
restPosition: part.spec.object.position.clone(),
|
|
1612
|
-
restQuaternion: part.spec.object.quaternion.clone(),
|
|
1613
|
-
q: 0,
|
|
1614
|
-
v: 0,
|
|
1615
|
-
positionTarget: drive?.positionTarget ?? null,
|
|
1616
|
-
velocityTarget: drive?.velocityTarget ?? 0,
|
|
1617
|
-
drive,
|
|
1618
|
-
limits: part.joint.limits,
|
|
1619
|
-
limitStiffness: part.joint.limitStiffness ?? null,
|
|
1620
|
-
limitDamping: part.joint.limitDamping ?? 0,
|
|
1621
|
-
inertia: part.inertia,
|
|
1622
|
-
parent: part.parent ? map.get(part.parent.spec.nodeName) ?? null : null,
|
|
1623
|
-
restRelParent: part.restRelParent,
|
|
1624
|
-
current: part.restRelParent.clone(),
|
|
1625
|
-
stamp: -1,
|
|
1626
|
-
colliders: part.colliders.map((c, idx) => ({
|
|
1627
|
-
ref: partColliderRefs[i][idx],
|
|
1628
|
-
local: c.local
|
|
1629
|
-
}))
|
|
1630
|
-
};
|
|
1631
|
-
map.set(rt.nodeName, rt);
|
|
1632
|
-
joints?.add(rt);
|
|
1633
|
-
}
|
|
1634
|
-
onJoints(rootSpec.nodeName, map);
|
|
1635
|
-
return () => {
|
|
1636
|
-
onJoints(rootSpec.nodeName, null);
|
|
1637
|
-
for (const rt of map.values()) {
|
|
1638
|
-
joints?.delete(rt);
|
|
1639
|
-
rt.object.position.copy(rt.restPosition);
|
|
1640
|
-
rt.object.quaternion.copy(rt.restQuaternion);
|
|
1641
|
-
}
|
|
1642
|
-
};
|
|
1643
|
-
}, [
|
|
1644
|
-
plan,
|
|
1645
|
-
joints,
|
|
1646
|
-
assemblyId,
|
|
1647
|
-
partColliderRefs,
|
|
1648
|
-
rootSpec,
|
|
1649
|
-
onJoints
|
|
1650
|
-
]);
|
|
1684
|
+
const handlePartBody = useCallback((nodeName, refObj) => {
|
|
1685
|
+
const slot = partBodyRefs.get(nodeName);
|
|
1686
|
+
if (slot) slot.current = refObj?.current ?? null;
|
|
1687
|
+
onBody(nodeName, refObj);
|
|
1688
|
+
}, [partBodyRefs, onBody]);
|
|
1689
|
+
const handleJointReady = useCallback((nodeName, rt) => {
|
|
1690
|
+
if (rt) jointsMapRef.current.set(nodeName, rt);
|
|
1691
|
+
else jointsMapRef.current.delete(nodeName);
|
|
1692
|
+
}, []);
|
|
1651
1693
|
const bodyType = rootSpec.isKinematic ? fixedRoot ? "fixed" : "kinematicPosition" : "dynamic";
|
|
1652
|
-
return /* @__PURE__ */ jsxs(RigidBody, {
|
|
1694
|
+
return /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsxs(RigidBody, {
|
|
1653
1695
|
...rigidBodyProps,
|
|
1654
1696
|
ref: rbRef,
|
|
1655
1697
|
type: bodyType,
|
|
@@ -1659,26 +1701,23 @@ function AssemblyRootBody({ plan, assemblyId, position, quaternion, fixedRoot, r
|
|
|
1659
1701
|
...rootSpec.linearVelocity ? { linearVelocity: rootSpec.linearVelocity } : {},
|
|
1660
1702
|
...rootSpec.angularVelocity ? { angularVelocity: rootSpec.angularVelocity } : {},
|
|
1661
1703
|
...rootSpec.gravityFactor === void 0 ? {} : { gravityScale: rootSpec.gravityFactor },
|
|
1662
|
-
children: [
|
|
1663
|
-
|
|
1664
|
-
|
|
1665
|
-
|
|
1666
|
-
|
|
1667
|
-
|
|
1668
|
-
|
|
1669
|
-
|
|
1670
|
-
|
|
1671
|
-
|
|
1672
|
-
|
|
1673
|
-
|
|
1674
|
-
|
|
1675
|
-
|
|
1676
|
-
|
|
1677
|
-
]
|
|
1678
|
-
});
|
|
1704
|
+
children: [children, plan.colliders.map((col, idx) => /* @__PURE__ */ jsx(KhrCollider, {
|
|
1705
|
+
c: col.spec,
|
|
1706
|
+
position: col.position,
|
|
1707
|
+
quaternion: col.quaternion,
|
|
1708
|
+
massProperties: col.massProperties
|
|
1709
|
+
}, `r${idx}`))]
|
|
1710
|
+
}), plan.parts.map((part) => /* @__PURE__ */ jsx(ArticulatedPartBody, {
|
|
1711
|
+
part,
|
|
1712
|
+
rootPos: position,
|
|
1713
|
+
rootQuat: quaternion,
|
|
1714
|
+
parentBodyRef: part.parent ? partBodyRefs.get(part.parent.spec.nodeName) : rbRef,
|
|
1715
|
+
origin: partOrigins?.get(part.spec.object),
|
|
1716
|
+
onBody: handlePartBody,
|
|
1717
|
+
onJointReady: handleJointReady
|
|
1718
|
+
}, part.spec.nodeName))] });
|
|
1679
1719
|
}
|
|
1680
1720
|
// src/KhrAssembly.tsx
|
|
1681
|
-
var nextAssemblyId = 0;
|
|
1682
1721
|
function KhrAssembly({ ref, bundle, position = [
|
|
1683
1722
|
0,
|
|
1684
1723
|
0,
|
|
@@ -1687,7 +1726,6 @@ function KhrAssembly({ ref, bundle, position = [
|
|
|
1687
1726
|
assertUnitScale(scale, "KhrAssembly");
|
|
1688
1727
|
bundle.scene.updateMatrixWorld(true);
|
|
1689
1728
|
assertSceneIdentityWorldScale(bundle.scene);
|
|
1690
|
-
const assemblyId = useMemo(() => nextAssemblyId++, []);
|
|
1691
1729
|
const plans = useMemo(() => planAssembly(bundle), [bundle]);
|
|
1692
1730
|
const origins = useMemo(() => plans.length > 1 ? captureSubtreeOrigins(plans.map((p) => p.mountNode)) : null, [plans]);
|
|
1693
1731
|
const [px, py, pz] = position;
|
|
@@ -1744,42 +1782,30 @@ function KhrAssembly({ ref, bundle, position = [
|
|
|
1744
1782
|
}
|
|
1745
1783
|
return m;
|
|
1746
1784
|
}, [plans]);
|
|
1747
|
-
const handleBody = useCallback((
|
|
1748
|
-
if (rb) bodiesRef.current.set(
|
|
1749
|
-
else bodiesRef.current.delete(
|
|
1785
|
+
const handleBody = useCallback((nodeName, rb) => {
|
|
1786
|
+
if (rb) bodiesRef.current.set(nodeName, rb);
|
|
1787
|
+
else bodiesRef.current.delete(nodeName);
|
|
1750
1788
|
}, []);
|
|
1751
1789
|
const handleJoints = useCallback((rootName, map) => {
|
|
1752
1790
|
if (map) jointsRef.current.set(rootName, map);
|
|
1753
1791
|
else jointsRef.current.delete(rootName);
|
|
1754
1792
|
}, []);
|
|
1755
|
-
useImperativeHandle(ref, () => {
|
|
1756
|
-
|
|
1757
|
-
|
|
1758
|
-
rt
|
|
1759
|
-
|
|
1760
|
-
|
|
1761
|
-
|
|
1762
|
-
|
|
1763
|
-
|
|
1764
|
-
|
|
1765
|
-
return rootName ? bodiesRef.current.get(rootName)?.current ?? null : null;
|
|
1766
|
-
},
|
|
1767
|
-
setJointTarget: (nodeName, target) => {
|
|
1768
|
-
const rt = jointsRef.current.get(nodeToRoot.get(nodeName) ?? "")?.get(nodeName);
|
|
1769
|
-
if (rt) setTarget(rt, target);
|
|
1770
|
-
},
|
|
1771
|
-
setRoleTarget: (role, target) => {
|
|
1772
|
-
for (const map of jointsRef.current.values()) for (const rt of map.values()) if (rt.role === role) setTarget(rt, target);
|
|
1773
|
-
}
|
|
1774
|
-
};
|
|
1775
|
-
}, [nodeToRoot]);
|
|
1793
|
+
useImperativeHandle(ref, () => ({
|
|
1794
|
+
getBody: (nodeName) => bodiesRef.current.get(nodeName)?.current ?? null,
|
|
1795
|
+
setJointTarget: (nodeName, target) => {
|
|
1796
|
+
const rt = jointsRef.current.get(nodeToRoot.get(nodeName) ?? "")?.get(nodeName);
|
|
1797
|
+
if (rt) rt.applyTarget(target);
|
|
1798
|
+
},
|
|
1799
|
+
setRoleTarget: (role, target) => {
|
|
1800
|
+
for (const map of jointsRef.current.values()) for (const rt of map.values()) if (rt.role === role) rt.applyTarget(target);
|
|
1801
|
+
}
|
|
1802
|
+
}), [nodeToRoot]);
|
|
1776
1803
|
if (plans.length === 1) {
|
|
1777
1804
|
const plan = plans[0];
|
|
1778
1805
|
return /* @__PURE__ */ jsx("group", {
|
|
1779
1806
|
...groupProps,
|
|
1780
1807
|
children: /* @__PURE__ */ jsx(AssemblyRootBody, {
|
|
1781
1808
|
plan,
|
|
1782
|
-
assemblyId,
|
|
1783
1809
|
position: rootPoses[0].position,
|
|
1784
1810
|
quaternion: rootPoses[0].quaternion,
|
|
1785
1811
|
fixedRoot,
|
|
@@ -1813,7 +1839,6 @@ function KhrAssembly({ ref, bundle, position = [
|
|
|
1813
1839
|
}), children]
|
|
1814
1840
|
}), plans.map((plan, idx) => /* @__PURE__ */ jsx(AssemblyRootBody, {
|
|
1815
1841
|
plan,
|
|
1816
|
-
assemblyId,
|
|
1817
1842
|
position: rootPoses[idx].position,
|
|
1818
1843
|
quaternion: rootPoses[idx].quaternion,
|
|
1819
1844
|
fixedRoot,
|