@khr3/core 0.2.13 → 0.2.14
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/{joints-H3uae8pm.js → KhrAssembly-BOY_g5we.js} +653 -24
- package/dist/KhrAssembly-BOY_g5we.js.map +1 -0
- package/dist/KhrAssembly.d.ts +28 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.js +4 -578
- package/dist/index.js.map +1 -1
- package/dist/vehicle.js +15 -120
- package/dist/vehicle.js.map +1 -1
- package/dist/vehicleJoints.d.ts +2 -0
- package/package.json +1 -1
- package/dist/joints-H3uae8pm.js.map +0 -1
|
@@ -1,8 +1,10 @@
|
|
|
1
|
-
import React, { 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";
|
|
1
|
+
import React, { createContext, useCallback, useContext, useEffect, useImperativeHandle, useLayoutEffect, useMemo, useRef } from "react";
|
|
4
2
|
import * as THREE from "three";
|
|
3
|
+
import { useFrame } from "@react-three/fiber";
|
|
4
|
+
import { BallCollider, CapsuleCollider, CoefficientCombineRule, ConvexHullCollider, CuboidCollider, CylinderCollider, RigidBody, TrimeshCollider, useRapier } from "@react-three/rapier";
|
|
5
|
+
import { useCustomGravity } from "@pinkaphant/ecctrl/gravity";
|
|
5
6
|
import { ConvexGeometry } from "three/examples/jsm/geometries/ConvexGeometry.js";
|
|
7
|
+
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
|
|
6
8
|
// src/types.ts
|
|
7
9
|
var KhrValidationError = class extends Error {
|
|
8
10
|
constructor(message) {
|
|
@@ -357,9 +359,10 @@ function integrateSurfaceShellProps(vertices, indices, label) {
|
|
|
357
359
|
const cxy = sxy24 / (24 * area) - comRelX * comRelY;
|
|
358
360
|
const cxz = sxz24 / (24 * area) - comRelX * comRelZ;
|
|
359
361
|
const cyz = syz24 / (24 * area) - comRelY * comRelZ;
|
|
360
|
-
const ixx =
|
|
361
|
-
const iyy =
|
|
362
|
-
const izz =
|
|
362
|
+
const ixx = cyy + czz;
|
|
363
|
+
const iyy = cxx + czz;
|
|
364
|
+
const izz = cxx + cyy;
|
|
365
|
+
if (!(ixx > 1e-12 && iyy > 1e-12 && izz > 1e-12)) throw new KhrValidationError(`Collider mesh "${label}" is degenerate (1D collinear strip); cannot compute positive 3D inertia`);
|
|
363
366
|
return {
|
|
364
367
|
measure: area,
|
|
365
368
|
isVolumetric: false,
|
|
@@ -387,6 +390,75 @@ function deduplicateQuantizedPoints(vertices) {
|
|
|
387
390
|
}
|
|
388
391
|
return pts;
|
|
389
392
|
}
|
|
393
|
+
/**
|
|
394
|
+
* Exact 2D Andrew Monotone Chain convex hull in the best-fit plane of coplanar 3D points `pts`,
|
|
395
|
+
* returning CCW-ordered perimeter vertices so triangle fan integration never self-intersects.
|
|
396
|
+
*/
|
|
397
|
+
function buildCoplanarConvexPolygon(pts, label) {
|
|
398
|
+
const p0 = pts[0];
|
|
399
|
+
let bestNormal = new THREE.Vector3();
|
|
400
|
+
let bestNormSq = 0;
|
|
401
|
+
const e1 = new THREE.Vector3();
|
|
402
|
+
const e2 = new THREE.Vector3();
|
|
403
|
+
const nCand = new THREE.Vector3();
|
|
404
|
+
for (let i = 1; i < pts.length; i++) {
|
|
405
|
+
e1.subVectors(pts[i], p0);
|
|
406
|
+
for (let j = i + 1; j < pts.length; j++) {
|
|
407
|
+
e2.subVectors(pts[j], p0);
|
|
408
|
+
nCand.crossVectors(e1, e2);
|
|
409
|
+
const sq = nCand.lengthSq();
|
|
410
|
+
if (sq > bestNormSq) {
|
|
411
|
+
bestNormSq = sq;
|
|
412
|
+
bestNormal.copy(nCand);
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
if (!(bestNormSq > 1e-20)) throw new KhrValidationError(`ConvexHull collider "${label}" has collinear vertices (zero 2D hull area)`);
|
|
417
|
+
bestNormal.normalize();
|
|
418
|
+
const axisU = new THREE.Vector3();
|
|
419
|
+
if (Math.abs(bestNormal.x) < .9) axisU.set(1, 0, 0).cross(bestNormal).normalize();
|
|
420
|
+
else axisU.set(0, 1, 0).cross(bestNormal).normalize();
|
|
421
|
+
const axisV = new THREE.Vector3().crossVectors(bestNormal, axisU).normalize();
|
|
422
|
+
const projected = pts.map((p) => {
|
|
423
|
+
const d = e1.subVectors(p, p0);
|
|
424
|
+
return {
|
|
425
|
+
u: d.dot(axisU),
|
|
426
|
+
v: d.dot(axisV),
|
|
427
|
+
p
|
|
428
|
+
};
|
|
429
|
+
});
|
|
430
|
+
projected.sort((a, b) => a.u !== b.u ? a.u - b.u : a.v - b.v);
|
|
431
|
+
const cross2 = (o, a, b) => (a.u - o.u) * (b.v - o.v) - (a.v - o.v) * (b.u - o.u);
|
|
432
|
+
const lower = [];
|
|
433
|
+
for (const pt of projected) {
|
|
434
|
+
while (lower.length >= 2 && cross2(lower[lower.length - 2], lower[lower.length - 1], pt) <= 1e-12) lower.pop();
|
|
435
|
+
lower.push(pt);
|
|
436
|
+
}
|
|
437
|
+
const upper = [];
|
|
438
|
+
for (let i = projected.length - 1; i >= 0; i--) {
|
|
439
|
+
const pt = projected[i];
|
|
440
|
+
while (upper.length >= 2 && cross2(upper[upper.length - 2], upper[upper.length - 1], pt) <= 1e-12) upper.pop();
|
|
441
|
+
upper.push(pt);
|
|
442
|
+
}
|
|
443
|
+
lower.pop();
|
|
444
|
+
upper.pop();
|
|
445
|
+
const hull = [...lower, ...upper];
|
|
446
|
+
if (hull.length < 3) throw new KhrValidationError(`ConvexHull collider "${label}" has fewer than 3 non-collinear hull vertices`);
|
|
447
|
+
const fanVerts = new Float32Array((hull.length - 2) * 9);
|
|
448
|
+
let o = 0;
|
|
449
|
+
for (let i = 1; i + 1 < hull.length; i++) {
|
|
450
|
+
fanVerts[o++] = hull[0].p.x;
|
|
451
|
+
fanVerts[o++] = hull[0].p.y;
|
|
452
|
+
fanVerts[o++] = hull[0].p.z;
|
|
453
|
+
fanVerts[o++] = hull[i].p.x;
|
|
454
|
+
fanVerts[o++] = hull[i].p.y;
|
|
455
|
+
fanVerts[o++] = hull[i].p.z;
|
|
456
|
+
fanVerts[o++] = hull[i + 1].p.x;
|
|
457
|
+
fanVerts[o++] = hull[i + 1].p.y;
|
|
458
|
+
fanVerts[o++] = hull[i + 1].p.z;
|
|
459
|
+
}
|
|
460
|
+
return fanVerts;
|
|
461
|
+
}
|
|
390
462
|
function unitInertiaForShape(shape) {
|
|
391
463
|
if (shape.type === "box") {
|
|
392
464
|
const [x, y, z] = shape.size;
|
|
@@ -431,22 +503,7 @@ function unitInertiaForShape(shape) {
|
|
|
431
503
|
hullGeo.dispose();
|
|
432
504
|
if (closed) return closed;
|
|
433
505
|
} catch {}
|
|
434
|
-
if (pts.length >= 3)
|
|
435
|
-
const fanVerts = new Float32Array((pts.length - 2) * 9);
|
|
436
|
-
let o = 0;
|
|
437
|
-
for (let i = 1; i + 1 < pts.length; i++) {
|
|
438
|
-
fanVerts[o++] = pts[0].x;
|
|
439
|
-
fanVerts[o++] = pts[0].y;
|
|
440
|
-
fanVerts[o++] = pts[0].z;
|
|
441
|
-
fanVerts[o++] = pts[i].x;
|
|
442
|
-
fanVerts[o++] = pts[i].y;
|
|
443
|
-
fanVerts[o++] = pts[i].z;
|
|
444
|
-
fanVerts[o++] = pts[i + 1].x;
|
|
445
|
-
fanVerts[o++] = pts[i + 1].y;
|
|
446
|
-
fanVerts[o++] = pts[i + 1].z;
|
|
447
|
-
}
|
|
448
|
-
return integrateSurfaceShellProps(fanVerts, null, shape.meshNodeName);
|
|
449
|
-
}
|
|
506
|
+
if (pts.length >= 3) return integrateSurfaceShellProps(buildCoplanarConvexPolygon(pts, shape.meshNodeName), null, shape.meshNodeName);
|
|
450
507
|
throw new KhrValidationError(`ConvexHull collider "${shape.meshNodeName}" has fewer than 3 distinct vertices`);
|
|
451
508
|
}
|
|
452
509
|
const r = shape.radiusTop;
|
|
@@ -1334,6 +1391,578 @@ function toggleDrivenJoint(rt) {
|
|
|
1334
1391
|
rt.applyTarget(nextTarget);
|
|
1335
1392
|
return true;
|
|
1336
1393
|
}
|
|
1337
|
-
|
|
1394
|
+
// src/KhrAssembly.tsx
|
|
1395
|
+
function matrixToRoot(node, sceneRoot) {
|
|
1396
|
+
const out = new THREE.Matrix4();
|
|
1397
|
+
for (let cur = node; cur && cur !== sceneRoot; cur = cur.parent) {
|
|
1398
|
+
if (!cur.parent) throw new KhrValidationError(`Node "${node.name}" is detached from bundle.scene`);
|
|
1399
|
+
cur.updateMatrix();
|
|
1400
|
+
out.premultiply(cur.matrix);
|
|
1401
|
+
}
|
|
1402
|
+
return out;
|
|
1403
|
+
}
|
|
1404
|
+
function isSceneDescendant(node, ancestor) {
|
|
1405
|
+
for (let cur = node; cur; cur = cur.parent) if (cur === ancestor) return true;
|
|
1406
|
+
return false;
|
|
1407
|
+
}
|
|
1408
|
+
function lowestCommonAncestor(nodes, sceneRoot) {
|
|
1409
|
+
let candidate = nodes[0];
|
|
1410
|
+
while (candidate && candidate !== sceneRoot) {
|
|
1411
|
+
if (nodes.every((n) => isSceneDescendant(n, candidate))) return candidate;
|
|
1412
|
+
candidate = candidate.parent ?? sceneRoot;
|
|
1413
|
+
}
|
|
1414
|
+
return sceneRoot;
|
|
1415
|
+
}
|
|
1416
|
+
function planOwnColliders(body, frameQuat) {
|
|
1417
|
+
const one = new THREE.Vector3(1, 1, 1);
|
|
1418
|
+
const frameMat = frameQuat ? new THREE.Matrix4().makeRotationFromQuaternion(frameQuat) : null;
|
|
1419
|
+
const pos = new THREE.Vector3(), quat = new THREE.Quaternion(), scl = new THREE.Vector3();
|
|
1420
|
+
return body.colliders.map((spec) => {
|
|
1421
|
+
const colQuat = new THREE.Quaternion().fromArray(spec.localQuaternion);
|
|
1422
|
+
const local = new THREE.Matrix4().compose(new THREE.Vector3().fromArray(spec.localPosition), colQuat, one);
|
|
1423
|
+
if (frameMat) local.premultiply(frameMat);
|
|
1424
|
+
local.decompose(pos, quat, scl);
|
|
1425
|
+
quat.normalize();
|
|
1426
|
+
return {
|
|
1427
|
+
spec,
|
|
1428
|
+
position: [
|
|
1429
|
+
pos.x,
|
|
1430
|
+
pos.y,
|
|
1431
|
+
pos.z
|
|
1432
|
+
],
|
|
1433
|
+
quaternion: [
|
|
1434
|
+
quat.x,
|
|
1435
|
+
quat.y,
|
|
1436
|
+
quat.z,
|
|
1437
|
+
quat.w
|
|
1438
|
+
]
|
|
1439
|
+
};
|
|
1440
|
+
});
|
|
1441
|
+
}
|
|
1442
|
+
function planAssembly(bundle) {
|
|
1443
|
+
const byName = new Map(bundle.bodies.map((b) => [b.nodeName, b]));
|
|
1444
|
+
const rootMemo = /* @__PURE__ */ new Map();
|
|
1445
|
+
const depthMemo = /* @__PURE__ */ new Map();
|
|
1446
|
+
const resolveRootAndDepth = (b, visiting = /* @__PURE__ */ new Set()) => {
|
|
1447
|
+
const cachedRoot = rootMemo.get(b.nodeName);
|
|
1448
|
+
const cachedDepth = depthMemo.get(b.nodeName);
|
|
1449
|
+
if (cachedRoot && cachedDepth !== void 0) return {
|
|
1450
|
+
root: cachedRoot,
|
|
1451
|
+
depth: cachedDepth
|
|
1452
|
+
};
|
|
1453
|
+
if (!b.joint) {
|
|
1454
|
+
rootMemo.set(b.nodeName, b);
|
|
1455
|
+
depthMemo.set(b.nodeName, 0);
|
|
1456
|
+
return {
|
|
1457
|
+
root: b,
|
|
1458
|
+
depth: 0
|
|
1459
|
+
};
|
|
1460
|
+
}
|
|
1461
|
+
if (visiting.has(b.nodeName)) throw new KhrValidationError(`Joint cycle through "${b.nodeName}"`);
|
|
1462
|
+
visiting.add(b.nodeName);
|
|
1463
|
+
const parent = byName.get(b.joint.connectedNodeName);
|
|
1464
|
+
if (!parent) throw new KhrValidationError(`Joint on "${b.nodeName}" targets "${b.joint.connectedNodeName}", which is not a KHR body`);
|
|
1465
|
+
const up = resolveRootAndDepth(parent, visiting);
|
|
1466
|
+
rootMemo.set(b.nodeName, up.root);
|
|
1467
|
+
depthMemo.set(b.nodeName, up.depth + 1);
|
|
1468
|
+
return {
|
|
1469
|
+
root: up.root,
|
|
1470
|
+
depth: up.depth + 1
|
|
1471
|
+
};
|
|
1472
|
+
};
|
|
1473
|
+
for (const b of bundle.bodies) resolveRootAndDepth(b);
|
|
1474
|
+
const plans = bundle.bodies.filter((b) => !b.joint).map((root) => {
|
|
1475
|
+
const invRoot = bodyPoseMatrix(root).clone().invert();
|
|
1476
|
+
const descendants = bundle.bodies.filter((b) => b.joint && rootMemo.get(b.nodeName) === root).sort((a, b) => (depthMemo.get(a.nodeName) ?? 0) - (depthMemo.get(b.nodeName) ?? 0));
|
|
1477
|
+
const partByName = /* @__PURE__ */ new Map();
|
|
1478
|
+
const parts = descendants.map((b) => {
|
|
1479
|
+
const joint = b.joint;
|
|
1480
|
+
const parent = partByName.get(joint.connectedNodeName) ?? null;
|
|
1481
|
+
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}"`);
|
|
1482
|
+
const relParentMat = bodyPoseMatrix(parent ? parent.spec : root).clone().invert().multiply(bodyPoseMatrix(b));
|
|
1483
|
+
const relRootMat = invRoot.clone().multiply(bodyPoseMatrix(b));
|
|
1484
|
+
const rpPos = new THREE.Vector3(), tmpQ = new THREE.Quaternion(), rpScl = new THREE.Vector3();
|
|
1485
|
+
relParentMat.decompose(rpPos, tmpQ, rpScl);
|
|
1486
|
+
const rrPos = new THREE.Vector3(), rrQuat = new THREE.Quaternion();
|
|
1487
|
+
relRootMat.decompose(rrPos, rrQuat, rpScl);
|
|
1488
|
+
rrQuat.normalize();
|
|
1489
|
+
const axChildVec = new THREE.Vector3().fromArray(joint.axis).normalize();
|
|
1490
|
+
const axParentVec = axChildVec.clone().applyQuaternion(rrQuat).normalize();
|
|
1491
|
+
const anchorParent = parent ? [
|
|
1492
|
+
rrPos.x - parent.restRelRootPos[0],
|
|
1493
|
+
rrPos.y - parent.restRelRootPos[1],
|
|
1494
|
+
rrPos.z - parent.restRelRootPos[2]
|
|
1495
|
+
] : [
|
|
1496
|
+
rpPos.x,
|
|
1497
|
+
rpPos.y,
|
|
1498
|
+
rpPos.z
|
|
1499
|
+
];
|
|
1500
|
+
const rrQuatTuple = [
|
|
1501
|
+
rrQuat.x,
|
|
1502
|
+
rrQuat.y,
|
|
1503
|
+
rrQuat.z,
|
|
1504
|
+
rrQuat.w
|
|
1505
|
+
];
|
|
1506
|
+
const invPartFrame = new THREE.Matrix4().compose(new THREE.Vector3().fromArray(b.localPosition), new THREE.Quaternion().fromArray(root.localQuaternion), new THREE.Vector3(1, 1, 1)).invert();
|
|
1507
|
+
const lumpedMass = computeLumpedMassProps([b], invPartFrame);
|
|
1508
|
+
const colliders = planOwnColliders(b, rrQuat);
|
|
1509
|
+
const plan = {
|
|
1510
|
+
spec: b,
|
|
1511
|
+
joint,
|
|
1512
|
+
parent,
|
|
1513
|
+
restRelParentPos: anchorParent,
|
|
1514
|
+
restRelParentQuat: rrQuatTuple,
|
|
1515
|
+
restRelRootPos: [
|
|
1516
|
+
rrPos.x,
|
|
1517
|
+
rrPos.y,
|
|
1518
|
+
rrPos.z
|
|
1519
|
+
],
|
|
1520
|
+
restRelRootQuat: rrQuatTuple,
|
|
1521
|
+
axisParent: [
|
|
1522
|
+
axParentVec.x,
|
|
1523
|
+
axParentVec.y,
|
|
1524
|
+
axParentVec.z
|
|
1525
|
+
],
|
|
1526
|
+
axisChild: [
|
|
1527
|
+
axChildVec.x,
|
|
1528
|
+
axChildVec.y,
|
|
1529
|
+
axChildVec.z
|
|
1530
|
+
],
|
|
1531
|
+
lumpedMass,
|
|
1532
|
+
colliders
|
|
1533
|
+
};
|
|
1534
|
+
partByName.set(b.nodeName, plan);
|
|
1535
|
+
return plan;
|
|
1536
|
+
});
|
|
1537
|
+
const lumpedMass = computeLumpedMassProps([root], invRoot);
|
|
1538
|
+
const colliders = planOwnColliders(root);
|
|
1539
|
+
const mountNode = lowestCommonAncestor([root.object, ...descendants.map((d) => d.object)], bundle.scene);
|
|
1540
|
+
const mountInRoot = invRoot.clone().multiply(matrixToRoot(mountNode, bundle.scene));
|
|
1541
|
+
const scenePos = new THREE.Vector3(), sceneQuat = new THREE.Quaternion(), sceneScl = new THREE.Vector3();
|
|
1542
|
+
invRoot.decompose(scenePos, sceneQuat, sceneScl);
|
|
1543
|
+
sceneQuat.normalize();
|
|
1544
|
+
const mountPos = new THREE.Vector3(), mountQuat = new THREE.Quaternion();
|
|
1545
|
+
mountInRoot.decompose(mountPos, mountQuat, sceneScl);
|
|
1546
|
+
mountQuat.normalize();
|
|
1547
|
+
return {
|
|
1548
|
+
spec: root,
|
|
1549
|
+
sceneInRootPos: [
|
|
1550
|
+
scenePos.x,
|
|
1551
|
+
scenePos.y,
|
|
1552
|
+
scenePos.z
|
|
1553
|
+
],
|
|
1554
|
+
sceneInRootQuat: [
|
|
1555
|
+
sceneQuat.x,
|
|
1556
|
+
sceneQuat.y,
|
|
1557
|
+
sceneQuat.z,
|
|
1558
|
+
sceneQuat.w
|
|
1559
|
+
],
|
|
1560
|
+
mountNode,
|
|
1561
|
+
mountPosition: [
|
|
1562
|
+
mountPos.x,
|
|
1563
|
+
mountPos.y,
|
|
1564
|
+
mountPos.z
|
|
1565
|
+
],
|
|
1566
|
+
mountQuaternion: [
|
|
1567
|
+
mountQuat.x,
|
|
1568
|
+
mountQuat.y,
|
|
1569
|
+
mountQuat.z,
|
|
1570
|
+
mountQuat.w
|
|
1571
|
+
],
|
|
1572
|
+
lumpedMass,
|
|
1573
|
+
colliders,
|
|
1574
|
+
parts
|
|
1575
|
+
};
|
|
1576
|
+
});
|
|
1577
|
+
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`);
|
|
1578
|
+
return plans;
|
|
1579
|
+
}
|
|
1580
|
+
function ArticulatedPartBody({ part, rootPos, rootQuat, linearVelocity, angularVelocity, gravityScale, parentBody, userData, origin, onBody, onJointReady }) {
|
|
1581
|
+
const { rapier, world } = useRapier();
|
|
1582
|
+
const registry = useContext(JointRegistryContext);
|
|
1583
|
+
const rtRef = useRef(null);
|
|
1584
|
+
const [childBody, setChildBody] = React.useState(null);
|
|
1585
|
+
const worldPose = useMemo(() => {
|
|
1586
|
+
const rq = new THREE.Quaternion(rootQuat[0], rootQuat[1], rootQuat[2], rootQuat[3]).normalize();
|
|
1587
|
+
const pos = new THREE.Vector3().fromArray(part.restRelRootPos).applyQuaternion(rq).add(new THREE.Vector3(rootPos[0], rootPos[1], rootPos[2]));
|
|
1588
|
+
return {
|
|
1589
|
+
pos: [
|
|
1590
|
+
pos.x,
|
|
1591
|
+
pos.y,
|
|
1592
|
+
pos.z
|
|
1593
|
+
],
|
|
1594
|
+
quat: [
|
|
1595
|
+
rq.x,
|
|
1596
|
+
rq.y,
|
|
1597
|
+
rq.z,
|
|
1598
|
+
rq.w
|
|
1599
|
+
]
|
|
1600
|
+
};
|
|
1601
|
+
}, [
|
|
1602
|
+
part.restRelRootPos,
|
|
1603
|
+
rootPos,
|
|
1604
|
+
rootQuat
|
|
1605
|
+
]);
|
|
1606
|
+
const assignChildRef = useCallback((rb) => {
|
|
1607
|
+
setChildBody(rb);
|
|
1608
|
+
onBody(part.spec.nodeName, rb);
|
|
1609
|
+
}, [part.spec.nodeName, onBody]);
|
|
1610
|
+
useEffect(() => () => onBody(part.spec.nodeName, null), [part.spec.nodeName, onBody]);
|
|
1611
|
+
useEffect(() => {
|
|
1612
|
+
if (childBody && part.lumpedMass) applyRapierBodyMass(childBody, part.lumpedMass);
|
|
1613
|
+
}, [childBody, part.lumpedMass]);
|
|
1614
|
+
useEffect(() => {
|
|
1615
|
+
if (!parentBody || !childBody) return;
|
|
1616
|
+
const imp = createRapierImpulseJoint(rapier, world, parentBody, childBody, part.joint, part.restRelParentPos, [
|
|
1617
|
+
0,
|
|
1618
|
+
0,
|
|
1619
|
+
0
|
|
1620
|
+
], part.axisParent, part.axisChild, part.restRelParentQuat);
|
|
1621
|
+
const rt = buildJointRuntime({
|
|
1622
|
+
rapier,
|
|
1623
|
+
world,
|
|
1624
|
+
nodeName: part.spec.nodeName,
|
|
1625
|
+
role: part.joint.role,
|
|
1626
|
+
kind: part.joint.kind,
|
|
1627
|
+
object: part.spec.object,
|
|
1628
|
+
axisParent: part.axisParent,
|
|
1629
|
+
anchorParent: part.restRelParentPos,
|
|
1630
|
+
limits: part.joint.limits,
|
|
1631
|
+
drive: part.joint.drive,
|
|
1632
|
+
breakaway: part.spec.breakaway,
|
|
1633
|
+
rapierJoint: imp,
|
|
1634
|
+
parentBody,
|
|
1635
|
+
childBody
|
|
1636
|
+
});
|
|
1637
|
+
rtRef.current = rt;
|
|
1638
|
+
registry?.add(rt);
|
|
1639
|
+
onJointReady(rt.nodeName, part.spec.object, rt);
|
|
1640
|
+
return () => {
|
|
1641
|
+
rtRef.current = null;
|
|
1642
|
+
registry?.delete(rt);
|
|
1643
|
+
onJointReady(rt.nodeName, part.spec.object, null);
|
|
1644
|
+
if (rt.rapierJoint && world.getImpulseJoint(rt.rapierJoint.handle)) world.removeImpulseJoint(rt.rapierJoint, true);
|
|
1645
|
+
};
|
|
1646
|
+
}, [
|
|
1647
|
+
rapier,
|
|
1648
|
+
world,
|
|
1649
|
+
part,
|
|
1650
|
+
parentBody,
|
|
1651
|
+
childBody,
|
|
1652
|
+
registry,
|
|
1653
|
+
onJointReady
|
|
1654
|
+
]);
|
|
1655
|
+
const onForce = useMemo(() => part.spec.breakaway ? (p) => {
|
|
1656
|
+
if (world.timestep > 0) rtRef.current?.applyImpactImpulse(p.totalForceMagnitude * world.timestep);
|
|
1657
|
+
} : void 0, [part.spec.breakaway, world]);
|
|
1658
|
+
const linVel = part.spec.linearVelocity ?? linearVelocity;
|
|
1659
|
+
const angVel = part.spec.angularVelocity ?? angularVelocity;
|
|
1660
|
+
const gravScale = part.spec.gravityFactor ?? gravityScale;
|
|
1661
|
+
return /* @__PURE__ */ jsxs(RigidBody, {
|
|
1662
|
+
ref: assignChildRef,
|
|
1663
|
+
type: part.spec.isKinematic ? "kinematicPosition" : "dynamic",
|
|
1664
|
+
position: worldPose.pos,
|
|
1665
|
+
quaternion: worldPose.quat,
|
|
1666
|
+
colliders: false,
|
|
1667
|
+
onContactForce: onForce,
|
|
1668
|
+
...userData === void 0 ? {} : { userData },
|
|
1669
|
+
...linVel ? { linearVelocity: linVel } : {},
|
|
1670
|
+
...angVel ? { angularVelocity: angVel } : {},
|
|
1671
|
+
...gravScale === void 0 ? {} : { gravityScale: gravScale },
|
|
1672
|
+
children: [/* @__PURE__ */ jsx(MountSubtreeSlot, {
|
|
1673
|
+
object: part.spec.object,
|
|
1674
|
+
position: [
|
|
1675
|
+
0,
|
|
1676
|
+
0,
|
|
1677
|
+
0
|
|
1678
|
+
],
|
|
1679
|
+
quaternion: part.restRelRootQuat,
|
|
1680
|
+
origin
|
|
1681
|
+
}), part.colliders.map((col, cIdx) => /* @__PURE__ */ jsx(KhrCollider, {
|
|
1682
|
+
c: col.spec,
|
|
1683
|
+
position: col.position,
|
|
1684
|
+
quaternion: col.quaternion
|
|
1685
|
+
}, `${part.spec.nodeName}:${cIdx}`))]
|
|
1686
|
+
});
|
|
1687
|
+
}
|
|
1688
|
+
function AssemblyRootBody({ plan, bundleScene, position, quaternion, fixedRoot, rigidBodyProps, onBody, onJointReady, children }) {
|
|
1689
|
+
const [rootBody, setRootBody] = React.useState(null);
|
|
1690
|
+
const [partBodies, setPartBodies] = React.useState(() => /* @__PURE__ */ new Map());
|
|
1691
|
+
const partOrigins = useMemo(() => plan.parts.length > 0 ? captureSubtreeOrigins(plan.parts.map((p) => p.spec.object), bundleScene) : null, [plan.parts, bundleScene]);
|
|
1692
|
+
const rootSpec = plan.spec;
|
|
1693
|
+
const assignRootRef = useCallback((rb) => {
|
|
1694
|
+
setRootBody(rb);
|
|
1695
|
+
onBody(rootSpec.nodeName, rb);
|
|
1696
|
+
}, [rootSpec.nodeName, onBody]);
|
|
1697
|
+
useEffect(() => () => onBody(rootSpec.nodeName, null), [rootSpec.nodeName, onBody]);
|
|
1698
|
+
useEffect(() => {
|
|
1699
|
+
if (rootBody && plan.lumpedMass) applyRapierBodyMass(rootBody, plan.lumpedMass);
|
|
1700
|
+
}, [rootBody, plan.lumpedMass]);
|
|
1701
|
+
const handlePartBody = useCallback((nodeName, rb) => {
|
|
1702
|
+
setPartBodies((prev) => {
|
|
1703
|
+
if ((prev.get(nodeName) ?? null) === rb) return prev;
|
|
1704
|
+
const next = new Map(prev);
|
|
1705
|
+
if (rb) next.set(nodeName, rb);
|
|
1706
|
+
else next.delete(nodeName);
|
|
1707
|
+
return next;
|
|
1708
|
+
});
|
|
1709
|
+
onBody(nodeName, rb);
|
|
1710
|
+
}, [onBody]);
|
|
1711
|
+
const bodyType = rootSpec.isKinematic ? fixedRoot ? "fixed" : "kinematicPosition" : "dynamic";
|
|
1712
|
+
return /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsxs(RigidBody, {
|
|
1713
|
+
...rigidBodyProps,
|
|
1714
|
+
ref: assignRootRef,
|
|
1715
|
+
type: bodyType,
|
|
1716
|
+
position,
|
|
1717
|
+
quaternion,
|
|
1718
|
+
colliders: false,
|
|
1719
|
+
...rootSpec.linearVelocity ? { linearVelocity: rootSpec.linearVelocity } : {},
|
|
1720
|
+
...rootSpec.angularVelocity ? { angularVelocity: rootSpec.angularVelocity } : {},
|
|
1721
|
+
...rootSpec.gravityFactor === void 0 ? {} : { gravityScale: rootSpec.gravityFactor },
|
|
1722
|
+
children: [children, plan.colliders.map((col, idx) => /* @__PURE__ */ jsx(KhrCollider, {
|
|
1723
|
+
c: col.spec,
|
|
1724
|
+
position: col.position,
|
|
1725
|
+
quaternion: col.quaternion
|
|
1726
|
+
}, `r${idx}`))]
|
|
1727
|
+
}), plan.parts.map((part) => /* @__PURE__ */ jsx(ArticulatedPartBody, {
|
|
1728
|
+
part,
|
|
1729
|
+
rootPos: position,
|
|
1730
|
+
rootQuat: quaternion,
|
|
1731
|
+
linearVelocity: rootSpec.linearVelocity,
|
|
1732
|
+
angularVelocity: rootSpec.angularVelocity,
|
|
1733
|
+
gravityScale: rootSpec.gravityFactor,
|
|
1734
|
+
parentBody: part.parent ? partBodies.get(part.parent.spec.nodeName) ?? null : rootBody,
|
|
1735
|
+
userData: rigidBodyProps?.userData,
|
|
1736
|
+
origin: partOrigins?.get(part.spec.object),
|
|
1737
|
+
onBody: handlePartBody,
|
|
1738
|
+
onJointReady
|
|
1739
|
+
}, part.spec.nodeName))] });
|
|
1740
|
+
}
|
|
1741
|
+
function KhrAssembly({ ref, bundle, position = [
|
|
1742
|
+
0,
|
|
1743
|
+
0,
|
|
1744
|
+
0
|
|
1745
|
+
], rotation, quaternion, scale, fixedRoot = false, enableCustomGravity = false, rigidBodyProps, children, ...groupProps }) {
|
|
1746
|
+
assertUnitScale(scale, "KhrAssembly");
|
|
1747
|
+
useMemo(() => assertSceneIdentityWorldScale(bundle.scene), [bundle]);
|
|
1748
|
+
const { world } = useRapier();
|
|
1749
|
+
const applyGravityField = useCustomGravity((s) => s.applyGravityField);
|
|
1750
|
+
const plans = useMemo(() => planAssembly(bundle), [bundle]);
|
|
1751
|
+
const isStaticMultiRootCompound = useMemo(() => fixedRoot && plans.length > 1 && plans.every((p) => p.spec.isKinematic && p.parts.length === 0), [fixedRoot, plans]);
|
|
1752
|
+
const compoundColliders = useMemo(() => {
|
|
1753
|
+
if (!isStaticMultiRootCompound) return null;
|
|
1754
|
+
const one = new THREE.Vector3(1, 1, 1);
|
|
1755
|
+
const pos = new THREE.Vector3(), quat = new THREE.Quaternion(), scl = new THREE.Vector3();
|
|
1756
|
+
const out = [];
|
|
1757
|
+
for (const p of plans) {
|
|
1758
|
+
const bMat = bodyPoseMatrix(p.spec);
|
|
1759
|
+
for (let idx = 0; idx < p.spec.colliders.length; idx++) {
|
|
1760
|
+
const c = p.spec.colliders[idx];
|
|
1761
|
+
const cMat = new THREE.Matrix4().compose(new THREE.Vector3().fromArray(c.localPosition), new THREE.Quaternion().fromArray(c.localQuaternion), one);
|
|
1762
|
+
bMat.clone().multiply(cMat).decompose(pos, quat, scl);
|
|
1763
|
+
quat.normalize();
|
|
1764
|
+
out.push({
|
|
1765
|
+
key: `${p.spec.nodeName}:${idx}`,
|
|
1766
|
+
spec: c,
|
|
1767
|
+
position: [
|
|
1768
|
+
pos.x,
|
|
1769
|
+
pos.y,
|
|
1770
|
+
pos.z
|
|
1771
|
+
],
|
|
1772
|
+
quaternion: [
|
|
1773
|
+
quat.x,
|
|
1774
|
+
quat.y,
|
|
1775
|
+
quat.z,
|
|
1776
|
+
quat.w
|
|
1777
|
+
]
|
|
1778
|
+
});
|
|
1779
|
+
}
|
|
1780
|
+
}
|
|
1781
|
+
return out;
|
|
1782
|
+
}, [isStaticMultiRootCompound, plans]);
|
|
1783
|
+
const origins = useMemo(() => !isStaticMultiRootCompound && plans.length > 1 ? captureSubtreeOrigins(plans.map((p) => p.mountNode), bundle.scene) : null, [
|
|
1784
|
+
isStaticMultiRootCompound,
|
|
1785
|
+
plans,
|
|
1786
|
+
bundle.scene
|
|
1787
|
+
]);
|
|
1788
|
+
const [px, py, pz] = position;
|
|
1789
|
+
const [rx, ry, rz] = rotation ?? [
|
|
1790
|
+
0,
|
|
1791
|
+
0,
|
|
1792
|
+
0
|
|
1793
|
+
];
|
|
1794
|
+
const hasQuat = quaternion !== void 0;
|
|
1795
|
+
const qx = quaternion?.[0] ?? 0;
|
|
1796
|
+
const qy = quaternion?.[1] ?? 0;
|
|
1797
|
+
const qz = quaternion?.[2] ?? 0;
|
|
1798
|
+
const qw = quaternion?.[3] ?? 1;
|
|
1799
|
+
const spawnQuat = useMemo(() => hasQuat ? new THREE.Quaternion(qx, qy, qz, qw).normalize() : new THREE.Quaternion().setFromEuler(new THREE.Euler(rx, ry, rz)), [
|
|
1800
|
+
hasQuat,
|
|
1801
|
+
qx,
|
|
1802
|
+
qy,
|
|
1803
|
+
qz,
|
|
1804
|
+
qw,
|
|
1805
|
+
rx,
|
|
1806
|
+
ry,
|
|
1807
|
+
rz
|
|
1808
|
+
]);
|
|
1809
|
+
const rootPoses = useMemo(() => plans.map((p) => {
|
|
1810
|
+
const pos = new THREE.Vector3().fromArray(p.spec.localPosition).applyQuaternion(spawnQuat).add(new THREE.Vector3(px, py, pz));
|
|
1811
|
+
const quat = spawnQuat.clone().multiply(new THREE.Quaternion().fromArray(p.spec.localQuaternion));
|
|
1812
|
+
return {
|
|
1813
|
+
position: [
|
|
1814
|
+
pos.x,
|
|
1815
|
+
pos.y,
|
|
1816
|
+
pos.z
|
|
1817
|
+
],
|
|
1818
|
+
quaternion: [
|
|
1819
|
+
quat.x,
|
|
1820
|
+
quat.y,
|
|
1821
|
+
quat.z,
|
|
1822
|
+
quat.w
|
|
1823
|
+
]
|
|
1824
|
+
};
|
|
1825
|
+
}), [
|
|
1826
|
+
plans,
|
|
1827
|
+
spawnQuat,
|
|
1828
|
+
px,
|
|
1829
|
+
py,
|
|
1830
|
+
pz
|
|
1831
|
+
]);
|
|
1832
|
+
const bodiesRef = useRef(/* @__PURE__ */ new Map());
|
|
1833
|
+
const jointsRef = useRef(/* @__PURE__ */ new Map());
|
|
1834
|
+
const byObjectRef = useRef(/* @__PURE__ */ new Map());
|
|
1835
|
+
const handleBody = useCallback((nodeName, rb) => {
|
|
1836
|
+
if (rb) bodiesRef.current.set(nodeName, rb);
|
|
1837
|
+
else bodiesRef.current.delete(nodeName);
|
|
1838
|
+
}, []);
|
|
1839
|
+
const handleJointReady = useCallback((nodeName, obj, rt) => {
|
|
1840
|
+
if (rt) {
|
|
1841
|
+
jointsRef.current.set(nodeName, rt);
|
|
1842
|
+
byObjectRef.current.set(obj, rt);
|
|
1843
|
+
} else {
|
|
1844
|
+
jointsRef.current.delete(nodeName);
|
|
1845
|
+
byObjectRef.current.delete(obj);
|
|
1846
|
+
}
|
|
1847
|
+
}, []);
|
|
1848
|
+
useFrame(() => {
|
|
1849
|
+
if (!enableCustomGravity || world.timestep <= 0) return;
|
|
1850
|
+
for (const rb of bodiesRef.current.values()) if (rb.isDynamic()) applyGravityField(rb, world.timestep);
|
|
1851
|
+
});
|
|
1852
|
+
useImperativeHandle(ref, () => ({
|
|
1853
|
+
get jointNames() {
|
|
1854
|
+
return [...jointsRef.current.keys()];
|
|
1855
|
+
},
|
|
1856
|
+
getBody: (nodeName) => bodiesRef.current.get(nodeName) ?? null,
|
|
1857
|
+
getJointRuntime: (nodeName) => jointsRef.current.get(nodeName) ?? null,
|
|
1858
|
+
setJointTarget: (nodeName, target) => {
|
|
1859
|
+
jointsRef.current.get(nodeName)?.applyTarget(target);
|
|
1860
|
+
},
|
|
1861
|
+
setRoleTarget: (role, target) => {
|
|
1862
|
+
for (const rt of jointsRef.current.values()) if (rt.role === role) rt.applyTarget(target);
|
|
1863
|
+
},
|
|
1864
|
+
toggleHitObject: (hit) => {
|
|
1865
|
+
for (let cur = hit; cur; cur = cur.parent) {
|
|
1866
|
+
const rt = byObjectRef.current.get(cur);
|
|
1867
|
+
if (rt && toggleDrivenJoint(rt)) return true;
|
|
1868
|
+
}
|
|
1869
|
+
return false;
|
|
1870
|
+
}
|
|
1871
|
+
}), []);
|
|
1872
|
+
const assignCompoundRef = useCallback((rb) => {
|
|
1873
|
+
for (const p of plans) if (rb) bodiesRef.current.set(p.spec.nodeName, rb);
|
|
1874
|
+
else bodiesRef.current.delete(p.spec.nodeName);
|
|
1875
|
+
}, [plans]);
|
|
1876
|
+
useEffect(() => {
|
|
1877
|
+
if (!isStaticMultiRootCompound) return;
|
|
1878
|
+
return () => {
|
|
1879
|
+
for (const p of plans) bodiesRef.current.delete(p.spec.nodeName);
|
|
1880
|
+
};
|
|
1881
|
+
}, [isStaticMultiRootCompound, plans]);
|
|
1882
|
+
if (isStaticMultiRootCompound && compoundColliders) return /* @__PURE__ */ jsx("group", {
|
|
1883
|
+
...groupProps,
|
|
1884
|
+
children: /* @__PURE__ */ jsxs(RigidBody, {
|
|
1885
|
+
...rigidBodyProps,
|
|
1886
|
+
ref: assignCompoundRef,
|
|
1887
|
+
type: "fixed",
|
|
1888
|
+
position,
|
|
1889
|
+
quaternion: [
|
|
1890
|
+
spawnQuat.x,
|
|
1891
|
+
spawnQuat.y,
|
|
1892
|
+
spawnQuat.z,
|
|
1893
|
+
spawnQuat.w
|
|
1894
|
+
],
|
|
1895
|
+
colliders: false,
|
|
1896
|
+
children: [
|
|
1897
|
+
/* @__PURE__ */ jsx(BundleSceneHost, {
|
|
1898
|
+
bundle,
|
|
1899
|
+
owner: "KhrAssembly"
|
|
1900
|
+
}),
|
|
1901
|
+
children,
|
|
1902
|
+
compoundColliders.map((col) => /* @__PURE__ */ jsx(KhrCollider, {
|
|
1903
|
+
c: col.spec,
|
|
1904
|
+
position: col.position,
|
|
1905
|
+
quaternion: col.quaternion
|
|
1906
|
+
}, col.key))
|
|
1907
|
+
]
|
|
1908
|
+
})
|
|
1909
|
+
});
|
|
1910
|
+
if (plans.length === 1) {
|
|
1911
|
+
const plan = plans[0];
|
|
1912
|
+
return /* @__PURE__ */ jsx("group", {
|
|
1913
|
+
...groupProps,
|
|
1914
|
+
children: /* @__PURE__ */ jsx(AssemblyRootBody, {
|
|
1915
|
+
plan,
|
|
1916
|
+
bundleScene: bundle.scene,
|
|
1917
|
+
position: rootPoses[0].position,
|
|
1918
|
+
quaternion: rootPoses[0].quaternion,
|
|
1919
|
+
fixedRoot,
|
|
1920
|
+
rigidBodyProps,
|
|
1921
|
+
onBody: handleBody,
|
|
1922
|
+
onJointReady: handleJointReady,
|
|
1923
|
+
children: /* @__PURE__ */ jsxs("group", {
|
|
1924
|
+
position: plan.sceneInRootPos,
|
|
1925
|
+
quaternion: plan.sceneInRootQuat,
|
|
1926
|
+
children: [/* @__PURE__ */ jsx(BundleSceneHost, {
|
|
1927
|
+
bundle,
|
|
1928
|
+
owner: "KhrAssembly"
|
|
1929
|
+
}), children]
|
|
1930
|
+
})
|
|
1931
|
+
}, `${bundle.scene.uuid}:${plan.spec.nodeName}`)
|
|
1932
|
+
});
|
|
1933
|
+
}
|
|
1934
|
+
return /* @__PURE__ */ jsxs("group", {
|
|
1935
|
+
...groupProps,
|
|
1936
|
+
children: [/* @__PURE__ */ jsxs("group", {
|
|
1937
|
+
position,
|
|
1938
|
+
quaternion: [
|
|
1939
|
+
spawnQuat.x,
|
|
1940
|
+
spawnQuat.y,
|
|
1941
|
+
spawnQuat.z,
|
|
1942
|
+
spawnQuat.w
|
|
1943
|
+
],
|
|
1944
|
+
children: [/* @__PURE__ */ jsx(BundleSceneHost, {
|
|
1945
|
+
bundle,
|
|
1946
|
+
owner: "KhrAssembly"
|
|
1947
|
+
}), children]
|
|
1948
|
+
}), plans.map((plan, idx) => /* @__PURE__ */ jsx(AssemblyRootBody, {
|
|
1949
|
+
plan,
|
|
1950
|
+
bundleScene: bundle.scene,
|
|
1951
|
+
position: rootPoses[idx].position,
|
|
1952
|
+
quaternion: rootPoses[idx].quaternion,
|
|
1953
|
+
fixedRoot,
|
|
1954
|
+
rigidBodyProps,
|
|
1955
|
+
onBody: handleBody,
|
|
1956
|
+
onJointReady: handleJointReady,
|
|
1957
|
+
children: /* @__PURE__ */ jsx(MountSubtreeSlot, {
|
|
1958
|
+
object: plan.mountNode,
|
|
1959
|
+
position: plan.mountPosition,
|
|
1960
|
+
quaternion: plan.mountQuaternion,
|
|
1961
|
+
origin: origins?.get(plan.mountNode)
|
|
1962
|
+
})
|
|
1963
|
+
}, `${bundle.scene.uuid}:${plan.spec.nodeName}`))]
|
|
1964
|
+
});
|
|
1965
|
+
}
|
|
1966
|
+
export { computeAssemblyMassAndCom as A, JointRegistryContext as B, restoreSubtreeOrigin as C, assertSceneIdentityWorldScale as D, assertIdentityWorldScale as E, jacobiPrincipalInertia as F, isObj as G, assignRef as H, readFiniteVec3 as I, isPosNum as K, readUnitQuat as L, computeRelativePose as M, computeShapeInertia as N, assertUnitScale as O, extractMeshColliderShape as P, resolveBodyMassFromColliders as R, releaseBundleMount as S, applyRapierBodyMass as T, isFiniteNum as U, KhrValidationError as V, isNonNegNum as W, captureSubtreeOrigins as _, DETACHED_PANEL_INTERACTION_GROUPS as a, disposeKhrBundle as b, configureJointDrive as c, measureJointCoordinate as d, openTargetForLimits as f, BundleSceneHost as g, KhrCollider as h, ATTACHED_PANEL_INTERACTION_GROUPS as i, computeLumpedMassProps as j, bodyPoseMatrix as k, createRapierImpulseJoint as l, KHR_TRIMESH_FLAGS as m, KhrAssembly as n, buildJointRuntime as o, toggleDrivenJoint as p, optFiniteNum as q, planAssembly as r, closedTargetForLimits as s, ArticulatedPartBody as t, isJointOpen as u, claimBundleMount as v, snapshotSceneWithMountedSubtrees as w, registerMountedSubtree as x, cloneKhrBundle as y, COMBINE_MODES as z };
|
|
1338
1967
|
|
|
1339
|
-
//# sourceMappingURL=
|
|
1968
|
+
//# sourceMappingURL=KhrAssembly-BOY_g5we.js.map
|