@khr3/core 0.2.12 → 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/README.md +54 -3
- package/dist/KhrAssembly-BOY_g5we.js +1968 -0
- package/dist/KhrAssembly-BOY_g5we.js.map +1 -0
- package/dist/KhrAssembly.d.ts +31 -4
- package/dist/decoders.d.ts +5 -2
- package/dist/heightfield.d.ts +45 -0
- package/dist/index.d.ts +4 -3
- package/dist/index.js +255 -496
- package/dist/index.js.map +1 -1
- package/dist/inertia.d.ts +29 -17
- package/dist/types.d.ts +12 -0
- package/dist/vehicle.js +37 -198
- package/dist/vehicle.js.map +1 -1
- package/dist/vehicleJoints.d.ts +4 -2
- package/docs/api-reference.md +160 -0
- package/package.json +4 -3
- package/dist/joints-5IgWDD2w.js +0 -865
- package/dist/joints-5IgWDD2w.js.map +0 -1
package/dist/index.js
CHANGED
|
@@ -1,13 +1,12 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { useEffect, useMemo, useRef } from "react";
|
|
2
2
|
import { useFrame, useLoader } from "@react-three/fiber";
|
|
3
3
|
import { Physics, RigidBody, useRapier } from "@react-three/rapier";
|
|
4
4
|
import { TimeControl } from "@pinkaphant/ecctrl/time";
|
|
5
|
-
import { A as
|
|
6
|
-
import {
|
|
5
|
+
import { A as computeAssemblyMassAndCom, B as JointRegistryContext, D as assertSceneIdentityWorldScale, E as assertIdentityWorldScale, F as jacobiPrincipalInertia, G as isObj, H as assignRef, I as readFiniteVec3, K as isPosNum, L as readUnitQuat, M as computeRelativePose, N as computeShapeInertia, O as assertUnitScale, P as extractMeshColliderShape, R as resolveBodyMassFromColliders, S as releaseBundleMount, T as applyRapierBodyMass, U as isFiniteNum, V as KhrValidationError, W as isNonNegNum, a as DETACHED_PANEL_INTERACTION_GROUPS, b as disposeKhrBundle, c as configureJointDrive, d as measureJointCoordinate, f as openTargetForLimits, h as KhrCollider, i as ATTACHED_PANEL_INTERACTION_GROUPS, j as computeLumpedMassProps, l as createRapierImpulseJoint, m as KHR_TRIMESH_FLAGS, n as KhrAssembly, o as buildJointRuntime, p as toggleDrivenJoint, q as optFiniteNum, r as planAssembly, s as closedTargetForLimits, u as isJointOpen, v as claimBundleMount, w as snapshotSceneWithMountedSubtrees, y as cloneKhrBundle, z as COMBINE_MODES } from "./KhrAssembly-BOY_g5we.js";
|
|
6
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
7
7
|
import * as THREE from "three";
|
|
8
8
|
import { GLTFLoader } from "three/examples/jsm/loaders/GLTFLoader.js";
|
|
9
9
|
import { GLTFExporter } from "three/examples/jsm/exporters/GLTFExporter.js";
|
|
10
|
-
import { useCustomGravity } from "@pinkaphant/ecctrl/gravity";
|
|
11
10
|
// src/PhysicsWorld.tsx
|
|
12
11
|
var readVal = (v) => typeof v === "number" ? v : v.current;
|
|
13
12
|
function TimestepPresync({ paused, timeScale, maxDelta }) {
|
|
@@ -175,7 +174,8 @@ function decodeMotion(motion, hasMotion, name) {
|
|
|
175
174
|
0,
|
|
176
175
|
0
|
|
177
176
|
] : readFiniteVec3(motion.centerOfMass, `${name}.motion.centerOfMass`);
|
|
178
|
-
const
|
|
177
|
+
const hasExplicitInertiaOrientation = motion.inertiaOrientation !== void 0;
|
|
178
|
+
const inertiaOrientation = !hasExplicitInertiaOrientation ? [
|
|
179
179
|
0,
|
|
180
180
|
0,
|
|
181
181
|
0,
|
|
@@ -199,6 +199,7 @@ function decodeMotion(motion, hasMotion, name) {
|
|
|
199
199
|
hasExplicitMotion: hasMotion,
|
|
200
200
|
hasExplicitCom,
|
|
201
201
|
hasExplicitInertia,
|
|
202
|
+
hasExplicitInertiaOrientation,
|
|
202
203
|
mass,
|
|
203
204
|
centerOfMass,
|
|
204
205
|
inertiaDiagonal,
|
|
@@ -548,7 +549,49 @@ function decodeBreakawaySpec(raw) {
|
|
|
548
549
|
breakImpulseNs: raw.breakImpulseNs
|
|
549
550
|
};
|
|
550
551
|
}
|
|
551
|
-
|
|
552
|
+
var SOCKET_PREFIX_RE = /^(?:socket|ik)_(.+)$/i;
|
|
553
|
+
var SEAT_NODE_RE = /^seat(?:_[lr])?$/i;
|
|
554
|
+
var _seatBox = new THREE.Box3();
|
|
555
|
+
var _seatCenter = new THREE.Vector3();
|
|
556
|
+
function resolveNodePivotOrMeshCenter(obj) {
|
|
557
|
+
obj.getWorldPosition(_wpA);
|
|
558
|
+
if (_wpA.lengthSq() > 1e-8) return [
|
|
559
|
+
_wpA.x,
|
|
560
|
+
_wpA.y,
|
|
561
|
+
_wpA.z
|
|
562
|
+
];
|
|
563
|
+
_seatBox.makeEmpty();
|
|
564
|
+
obj.traverse((child) => {
|
|
565
|
+
const mesh = child;
|
|
566
|
+
if (mesh.isMesh && mesh.geometry) _seatBox.expandByObject(mesh);
|
|
567
|
+
});
|
|
568
|
+
if (!_seatBox.isEmpty()) {
|
|
569
|
+
_seatBox.getCenter(_seatCenter);
|
|
570
|
+
return [
|
|
571
|
+
_seatCenter.x,
|
|
572
|
+
_seatCenter.y,
|
|
573
|
+
_seatCenter.z
|
|
574
|
+
];
|
|
575
|
+
}
|
|
576
|
+
return [
|
|
577
|
+
_wpA.x,
|
|
578
|
+
_wpA.y,
|
|
579
|
+
_wpA.z
|
|
580
|
+
];
|
|
581
|
+
}
|
|
582
|
+
function extractDefaultBodyRole(extras) {
|
|
583
|
+
for (const key of [
|
|
584
|
+
"role",
|
|
585
|
+
"vehicleRole",
|
|
586
|
+
"buildingRole",
|
|
587
|
+
"weaponRole"
|
|
588
|
+
]) {
|
|
589
|
+
const val = extras[key];
|
|
590
|
+
if (typeof val === "string" && val.length > 0) return val;
|
|
591
|
+
}
|
|
592
|
+
return null;
|
|
593
|
+
}
|
|
594
|
+
function collectNodeSockets(obj, name, extras, sockets, emitters, scene, rawExtensions, isSocketNode) {
|
|
552
595
|
if (extras.socketId !== void 0 && (typeof extras.socketId !== "string" || extras.socketId.length === 0)) throw new KhrValidationError(`Node "${name}" has invalid extras.socketId`);
|
|
553
596
|
obj.getWorldPosition(_wpA);
|
|
554
597
|
const worldPosition = [
|
|
@@ -556,10 +599,51 @@ function collectNodeSockets(obj, name, extras, sockets, isSocketNode) {
|
|
|
556
599
|
_wpA.y,
|
|
557
600
|
_wpA.z
|
|
558
601
|
];
|
|
602
|
+
let emitterSocket;
|
|
603
|
+
const rawEmitter = rawExtensions?.KOLMO_emitter;
|
|
604
|
+
if (rawEmitter !== void 0) {
|
|
605
|
+
if (!isObj(rawEmitter)) throw new KhrValidationError(`Node "${name}" has invalid extensions.KOLMO_emitter`);
|
|
606
|
+
if (rawEmitter.socket !== void 0) {
|
|
607
|
+
if (typeof rawEmitter.socket !== "string" || rawEmitter.socket.length === 0) throw new KhrValidationError(`Node "${name}" has invalid KOLMO_emitter.socket`);
|
|
608
|
+
emitterSocket = rawEmitter.socket;
|
|
609
|
+
sockets[emitterSocket] = [...worldPosition];
|
|
610
|
+
sockets[name] = [...worldPosition];
|
|
611
|
+
}
|
|
612
|
+
if (rawEmitter.kind !== void 0) {
|
|
613
|
+
if (typeof rawEmitter.kind !== "string" || rawEmitter.kind.length === 0) throw new KhrValidationError(`Node "${name}" has invalid KOLMO_emitter.kind`);
|
|
614
|
+
if (rawEmitter.drive !== void 0 && (typeof rawEmitter.drive !== "string" || rawEmitter.drive.length === 0)) throw new KhrValidationError(`Node "${name}" has invalid KOLMO_emitter.drive`);
|
|
615
|
+
let color;
|
|
616
|
+
if (rawEmitter.color !== void 0) color = readFiniteVec3(rawEmitter.color, `${name}.KOLMO_emitter.color`);
|
|
617
|
+
const size = optFiniteNum(rawEmitter.size, `${name}.KOLMO_emitter.size`, true);
|
|
618
|
+
emitters.push({
|
|
619
|
+
nodeName: name,
|
|
620
|
+
kind: rawEmitter.kind,
|
|
621
|
+
...typeof rawEmitter.drive === "string" ? { drive: rawEmitter.drive } : {},
|
|
622
|
+
...color ? { color } : {},
|
|
623
|
+
...size === void 0 ? {} : { size },
|
|
624
|
+
position: [...worldPosition],
|
|
625
|
+
quaternion: getRelativeQuatArray(obj, scene),
|
|
626
|
+
object: obj
|
|
627
|
+
});
|
|
628
|
+
}
|
|
629
|
+
}
|
|
630
|
+
let inferredSocket;
|
|
559
631
|
const custom = isSocketNode?.(name, extras);
|
|
560
632
|
if (custom || extras.isIkAnchor === true || typeof extras.socketId === "string") {
|
|
561
633
|
const key = typeof custom === "string" && custom.length > 0 ? custom : typeof extras.socketId === "string" ? extras.socketId : name;
|
|
562
634
|
sockets[key] = [...worldPosition];
|
|
635
|
+
} else if (!emitterSocket) {
|
|
636
|
+
const prefixMatch = SOCKET_PREFIX_RE.exec(name);
|
|
637
|
+
if (prefixMatch) {
|
|
638
|
+
inferredSocket = prefixMatch[1].toLowerCase();
|
|
639
|
+
sockets[inferredSocket] ??= [...worldPosition];
|
|
640
|
+
sockets[name] ??= [...worldPosition];
|
|
641
|
+
} else if (SEAT_NODE_RE.test(name)) {
|
|
642
|
+
const seatPos = resolveNodePivotOrMeshCenter(obj);
|
|
643
|
+
sockets[name] = seatPos;
|
|
644
|
+
if (/^seat(?:_l)?$/i.test(name)) sockets.seat ??= [...seatPos];
|
|
645
|
+
inferredSocket = name;
|
|
646
|
+
}
|
|
563
647
|
}
|
|
564
648
|
let contactDots;
|
|
565
649
|
if (extras.contactDots !== void 0) {
|
|
@@ -582,6 +666,8 @@ function collectNodeSockets(obj, name, extras, sockets, isSocketNode) {
|
|
|
582
666
|
nodeName: name,
|
|
583
667
|
extras,
|
|
584
668
|
worldPosition,
|
|
669
|
+
...emitterSocket ? { emitterSocket } : {},
|
|
670
|
+
...inferredSocket ? { inferredSocket } : {},
|
|
585
671
|
...contactDots ? { contactDots } : {}
|
|
586
672
|
};
|
|
587
673
|
}
|
|
@@ -771,12 +857,13 @@ function applyKhrCustomExtractors(clonedBundle, templateBundle, extractRole, isS
|
|
|
771
857
|
const rawBody = meta.bodies[i];
|
|
772
858
|
const dstBody = clonedBundle.bodies[i];
|
|
773
859
|
if (!rawBody || !dstBody) continue;
|
|
774
|
-
const role = extractRole(rawBody.nodeName, rawBody.extras) ?? (
|
|
860
|
+
const role = extractRole(rawBody.nodeName, rawBody.extras) ?? extractDefaultBodyRole(rawBody.extras);
|
|
775
861
|
dstBody.role = role;
|
|
776
862
|
if (dstBody.joint) dstBody.joint.role = role;
|
|
777
863
|
}
|
|
778
864
|
if (isSocketNode) {
|
|
779
865
|
const sockets = Object.create(null);
|
|
866
|
+
for (const [k, v] of Object.entries(templateBundle.sockets)) sockets[k] = [...v];
|
|
780
867
|
for (const node of meta.nodes) {
|
|
781
868
|
const custom = isSocketNode(node.nodeName, node.extras);
|
|
782
869
|
if (custom || node.extras.isIkAnchor === true || typeof node.extras.socketId === "string") {
|
|
@@ -927,13 +1014,14 @@ function extractKhrBundleFromGltf(gltf, options) {
|
|
|
927
1014
|
const { parentIdx, nodeNames, resolveObj } = buildNodeGraphLookup(gltf, rawNodes, json);
|
|
928
1015
|
const nodeMetas = [];
|
|
929
1016
|
const sockets = Object.create(null);
|
|
1017
|
+
const emitters = [];
|
|
930
1018
|
for (let i = 0; i < rawNodes.length; i++) {
|
|
931
1019
|
const obj = resolveObj(i);
|
|
932
1020
|
if (!obj) continue;
|
|
933
1021
|
const rawExt = rawNodes[i].extensions;
|
|
934
1022
|
if (rawExt !== void 0 && !isObj(rawExt)) throw new KhrValidationError(`Node "${nodeNames[i]}" has invalid extensions`);
|
|
935
1023
|
const extras = isObj(rawNodes[i].extras) ? rawNodes[i].extras : {};
|
|
936
|
-
nodeMetas.push(collectNodeSockets(obj, nodeNames[i], extras, sockets, options?.isSocketNode));
|
|
1024
|
+
nodeMetas.push(collectNodeSockets(obj, nodeNames[i], extras, sockets, emitters, gltf.scene, rawExt, options?.isSocketNode));
|
|
937
1025
|
}
|
|
938
1026
|
const bodies = [];
|
|
939
1027
|
const bodyMetas = [];
|
|
@@ -973,7 +1061,7 @@ function extractKhrBundleFromGltf(gltf, options) {
|
|
|
973
1061
|
}
|
|
974
1062
|
if (phys.motion !== void 0 && !isObj(phys.motion)) throw new KhrValidationError(`Node "${name}" has invalid motion`);
|
|
975
1063
|
const motion = decodeMotion(phys.motion ?? {}, phys.motion !== void 0, name);
|
|
976
|
-
const role = options?.extractRole?.(name, extras) ?? (
|
|
1064
|
+
const role = options?.extractRole?.(name, extras) ?? extractDefaultBodyRole(extras);
|
|
977
1065
|
let joint = null;
|
|
978
1066
|
if (phys.joint !== void 0) {
|
|
979
1067
|
if (!isObj(phys.joint)) throw new KhrValidationError(`Node "${name}" has invalid joint`);
|
|
@@ -1056,7 +1144,12 @@ function extractKhrBundleFromGltf(gltf, options) {
|
|
|
1056
1144
|
...colGroups === void 0 ? {} : { collisionGroups: colGroups }
|
|
1057
1145
|
});
|
|
1058
1146
|
}
|
|
1059
|
-
for (const body of bodies) if (!body.isKinematic && !body.hasExplicitInertia
|
|
1147
|
+
for (const body of bodies) if (!body.isKinematic && !body.hasExplicitInertia) {
|
|
1148
|
+
const solved = resolveBodyMassFromColliders(body.colliders, body.mass, body.hasExplicitCom ? body.centerOfMass : null, body.hasExplicitInertiaOrientation ? body.inertiaOrientation : null, body.nodeName);
|
|
1149
|
+
body.centerOfMass = solved.centerOfMass;
|
|
1150
|
+
body.inertiaDiagonal = solved.inertiaDiagonal;
|
|
1151
|
+
body.inertiaOrientation = solved.inertiaOrientation;
|
|
1152
|
+
}
|
|
1060
1153
|
const { totalDynamicMass, centerOfMass } = computeAssemblyMassAndCom(bodies);
|
|
1061
1154
|
const ikSockets = [];
|
|
1062
1155
|
const sceneExtras = json.scenes?.[json.scene ?? 0]?.extras ?? json.extras;
|
|
@@ -1080,6 +1173,7 @@ function extractKhrBundleFromGltf(gltf, options) {
|
|
|
1080
1173
|
bodies,
|
|
1081
1174
|
wheels,
|
|
1082
1175
|
propellers,
|
|
1176
|
+
emitters,
|
|
1083
1177
|
sockets,
|
|
1084
1178
|
totalDynamicMass,
|
|
1085
1179
|
centerOfMass,
|
|
@@ -1567,506 +1661,171 @@ async function exportKhrGlb(root, specs, sceneExtras) {
|
|
|
1567
1661
|
if (used.size > 0) gltf.extensionsUsed = [...used];
|
|
1568
1662
|
});
|
|
1569
1663
|
}
|
|
1570
|
-
// src/
|
|
1571
|
-
|
|
1572
|
-
|
|
1573
|
-
|
|
1574
|
-
|
|
1575
|
-
|
|
1576
|
-
|
|
1664
|
+
// src/heightfield.tsx
|
|
1665
|
+
/** `RAPIER.HeightFieldFlags.FIX_INTERNAL_EDGES` (1) — eliminates internal grid-edge ghost contact normals. */
|
|
1666
|
+
var KHR_HEIGHTFIELD_FLAGS = 1;
|
|
1667
|
+
/**
|
|
1668
|
+
* Transposes a row-major `(nrows + 1) * (ncols + 1)` height grid (`heights[zRow * (ncols + 1) + xCol]`,
|
|
1669
|
+
* as returned by `@khr3/roads` `sampleHeightfield` and Three.js `PlaneGeometry`) into Rapier WASM's
|
|
1670
|
+
* column-major layout (`out[xCol * (nrows + 1) + zRow]`).
|
|
1671
|
+
*/
|
|
1672
|
+
function transposeHeightfieldForRapier(rowMajor, nrows, ncols = nrows, out) {
|
|
1673
|
+
if (!Number.isInteger(nrows) || nrows < 1 || !Number.isInteger(ncols) || ncols < 1) throw new KhrValidationError(`transposeHeightfieldForRapier: nrows (${nrows}) and ncols (${ncols}) must be integers >= 1`);
|
|
1674
|
+
const strideX = ncols + 1;
|
|
1675
|
+
const strideZ = nrows + 1;
|
|
1676
|
+
const expected = strideZ * strideX;
|
|
1677
|
+
if (rowMajor.length !== expected) throw new KhrValidationError(`transposeHeightfieldForRapier: expected (${nrows} + 1) * (${ncols} + 1) = ${expected} samples, got ${rowMajor.length}`);
|
|
1678
|
+
const dst = out && out.length === expected ? out : new Float32Array(expected);
|
|
1679
|
+
for (let zRow = 0; zRow <= nrows; zRow++) {
|
|
1680
|
+
const rowBase = zRow * strideX;
|
|
1681
|
+
for (let xCol = 0; xCol <= ncols; xCol++) dst[xCol * strideZ + zRow] = rowMajor[rowBase + xCol];
|
|
1577
1682
|
}
|
|
1578
|
-
return
|
|
1579
|
-
}
|
|
1580
|
-
function isSceneDescendant(node, ancestor) {
|
|
1581
|
-
for (let cur = node; cur; cur = cur.parent) if (cur === ancestor) return true;
|
|
1582
|
-
return false;
|
|
1683
|
+
return dst;
|
|
1583
1684
|
}
|
|
1584
|
-
|
|
1585
|
-
|
|
1586
|
-
|
|
1587
|
-
|
|
1588
|
-
|
|
1685
|
+
/**
|
|
1686
|
+
* Samples `heightFn(x, z)` directly in Rapier WASM's column-major `(nrows + 1) * (ncols + 1)` layout
|
|
1687
|
+
* over `[originX, originX + sizeX] x [originZ, originZ + sizeZ]`.
|
|
1688
|
+
*/
|
|
1689
|
+
function sampleRapierHeightfield(heightFn, originX, originZ, sizeX, sizeZ, nrows, ncols = nrows, out) {
|
|
1690
|
+
if (!Number.isInteger(nrows) || nrows < 1 || !Number.isInteger(ncols) || ncols < 1) throw new KhrValidationError(`sampleRapierHeightfield: nrows (${nrows}) and ncols (${ncols}) must be integers >= 1`);
|
|
1691
|
+
if (!(sizeX > 0 && sizeZ > 0 && Number.isFinite(sizeX) && Number.isFinite(sizeZ))) throw new KhrValidationError(`sampleRapierHeightfield: sizeX (${sizeX}) and sizeZ (${sizeZ}) must be finite > 0`);
|
|
1692
|
+
const strideZ = nrows + 1;
|
|
1693
|
+
const total = strideZ * (ncols + 1);
|
|
1694
|
+
const dst = out && out.length === total ? out : new Float32Array(total);
|
|
1695
|
+
for (let xCol = 0; xCol <= ncols; xCol++) {
|
|
1696
|
+
const x = originX + xCol / ncols * sizeX;
|
|
1697
|
+
const colBase = xCol * strideZ;
|
|
1698
|
+
for (let zRow = 0; zRow <= nrows; zRow++) {
|
|
1699
|
+
const z = originZ + zRow / nrows * sizeZ;
|
|
1700
|
+
dst[colBase + zRow] = heightFn(x, z);
|
|
1701
|
+
}
|
|
1589
1702
|
}
|
|
1590
|
-
return
|
|
1591
|
-
}
|
|
1592
|
-
function planOwnColliders(body, massTarget, frameQuat) {
|
|
1593
|
-
const one = new THREE.Vector3(1, 1, 1);
|
|
1594
|
-
const frameMat = frameQuat ? new THREE.Matrix4().makeRotationFromQuaternion(frameQuat) : null;
|
|
1595
|
-
const pos = new THREE.Vector3(), quat = new THREE.Quaternion(), scl = new THREE.Vector3();
|
|
1596
|
-
const density = massTarget && !massTarget.hasExplicitInertia ? 1 : 0;
|
|
1597
|
-
return body.colliders.map((spec) => {
|
|
1598
|
-
const colQuat = new THREE.Quaternion().fromArray(spec.localQuaternion);
|
|
1599
|
-
const local = new THREE.Matrix4().compose(new THREE.Vector3().fromArray(spec.localPosition), colQuat, one);
|
|
1600
|
-
if (frameMat) local.premultiply(frameMat);
|
|
1601
|
-
local.decompose(pos, quat, scl);
|
|
1602
|
-
quat.normalize();
|
|
1603
|
-
return {
|
|
1604
|
-
spec,
|
|
1605
|
-
position: [
|
|
1606
|
-
pos.x,
|
|
1607
|
-
pos.y,
|
|
1608
|
-
pos.z
|
|
1609
|
-
],
|
|
1610
|
-
quaternion: [
|
|
1611
|
-
quat.x,
|
|
1612
|
-
quat.y,
|
|
1613
|
-
quat.z,
|
|
1614
|
-
quat.w
|
|
1615
|
-
],
|
|
1616
|
-
density
|
|
1617
|
-
};
|
|
1618
|
-
});
|
|
1703
|
+
return dst;
|
|
1619
1704
|
}
|
|
1620
|
-
|
|
1621
|
-
|
|
1622
|
-
|
|
1623
|
-
|
|
1624
|
-
|
|
1625
|
-
|
|
1626
|
-
|
|
1627
|
-
|
|
1628
|
-
|
|
1629
|
-
|
|
1630
|
-
|
|
1631
|
-
|
|
1632
|
-
|
|
1633
|
-
|
|
1634
|
-
|
|
1635
|
-
|
|
1636
|
-
|
|
1637
|
-
|
|
1705
|
+
/**
|
|
1706
|
+
* Builds an indexed Three.js `BufferGeometry` centered at `(0, 0, 0)` spanning
|
|
1707
|
+
* `[-sizeX/2, +sizeX/2] x [-sizeZ/2, +sizeZ/2]` whose vertex positions, cell triangulation,
|
|
1708
|
+
* and finite-difference normals match Rapier's `HeightField` collider 1:1.
|
|
1709
|
+
*/
|
|
1710
|
+
function createHeightfieldGeometry(heights, sizeX, sizeZ, nrows, ncols = nrows, layout = "rapier-column-major") {
|
|
1711
|
+
const strideZ = nrows + 1;
|
|
1712
|
+
const strideX = ncols + 1;
|
|
1713
|
+
const expected = strideZ * strideX;
|
|
1714
|
+
if (heights.length !== expected) throw new KhrValidationError(`createHeightfieldGeometry: expected ${expected} height samples for ${nrows}x${ncols} cells, got ${heights.length}`);
|
|
1715
|
+
const readH = layout === "rapier-column-major" ? (zRow, xCol) => heights[xCol * strideZ + zRow] : (zRow, xCol) => heights[zRow * strideX + xCol];
|
|
1716
|
+
const positions = new Float32Array(expected * 3);
|
|
1717
|
+
const normals = new Float32Array(expected * 3);
|
|
1718
|
+
const uvs = new Float32Array(expected * 2);
|
|
1719
|
+
const dx = sizeX / ncols;
|
|
1720
|
+
const dz = sizeZ / nrows;
|
|
1721
|
+
const halfX = sizeX * .5;
|
|
1722
|
+
const halfZ = sizeZ * .5;
|
|
1723
|
+
let vIdx = 0;
|
|
1724
|
+
for (let zRow = 0; zRow <= nrows; zRow++) {
|
|
1725
|
+
const z = -halfZ + zRow * dz;
|
|
1726
|
+
const v = zRow / nrows;
|
|
1727
|
+
for (let xCol = 0; xCol <= ncols; xCol++) {
|
|
1728
|
+
const x = -halfX + xCol * dx;
|
|
1729
|
+
const y = readH(zRow, xCol);
|
|
1730
|
+
positions[vIdx * 3] = x;
|
|
1731
|
+
positions[vIdx * 3 + 1] = y;
|
|
1732
|
+
positions[vIdx * 3 + 2] = z;
|
|
1733
|
+
const hL = readH(zRow, Math.max(0, xCol - 1));
|
|
1734
|
+
const hR = readH(zRow, Math.min(ncols, xCol + 1));
|
|
1735
|
+
const hD = readH(Math.max(0, zRow - 1), xCol);
|
|
1736
|
+
const hU = readH(Math.min(nrows, zRow + 1), xCol);
|
|
1737
|
+
const spanX = (Math.min(ncols, xCol + 1) - Math.max(0, xCol - 1)) * dx;
|
|
1738
|
+
const spanZ = (Math.min(nrows, zRow + 1) - Math.max(0, zRow - 1)) * dz;
|
|
1739
|
+
const dhdx = spanX > 0 ? (hR - hL) / spanX : 0;
|
|
1740
|
+
const dhdz = spanZ > 0 ? (hU - hD) / spanZ : 0;
|
|
1741
|
+
const invLen = 1 / Math.hypot(dhdx, 1, dhdz);
|
|
1742
|
+
normals[vIdx * 3] = -dhdx * invLen;
|
|
1743
|
+
normals[vIdx * 3 + 1] = invLen;
|
|
1744
|
+
normals[vIdx * 3 + 2] = -dhdz * invLen;
|
|
1745
|
+
uvs[vIdx * 2] = xCol / ncols;
|
|
1746
|
+
uvs[vIdx * 2 + 1] = v;
|
|
1747
|
+
vIdx++;
|
|
1638
1748
|
}
|
|
1639
|
-
|
|
1640
|
-
|
|
1641
|
-
|
|
1642
|
-
|
|
1643
|
-
const
|
|
1644
|
-
|
|
1645
|
-
|
|
1646
|
-
|
|
1647
|
-
|
|
1648
|
-
|
|
1649
|
-
|
|
1650
|
-
|
|
1651
|
-
|
|
1652
|
-
|
|
1653
|
-
|
|
1654
|
-
|
|
1655
|
-
|
|
1656
|
-
|
|
1657
|
-
|
|
1658
|
-
|
|
1659
|
-
|
|
1660
|
-
|
|
1661
|
-
const relParentMat = bodyPoseMatrix(parentSpec).clone().invert().multiply(bodyPoseMatrix(b));
|
|
1662
|
-
const relRootMat = invRoot.clone().multiply(bodyPoseMatrix(b));
|
|
1663
|
-
const rpPos = new THREE.Vector3(), tmpQ = new THREE.Quaternion(), rpScl = new THREE.Vector3();
|
|
1664
|
-
relParentMat.decompose(rpPos, tmpQ, rpScl);
|
|
1665
|
-
const rrPos = new THREE.Vector3(), rrQuat = new THREE.Quaternion();
|
|
1666
|
-
relRootMat.decompose(rrPos, rrQuat, rpScl);
|
|
1667
|
-
rrQuat.normalize();
|
|
1668
|
-
const axChildVec = new THREE.Vector3().fromArray(joint.axis).normalize();
|
|
1669
|
-
const axParentVec = axChildVec.clone().applyQuaternion(rrQuat).normalize();
|
|
1670
|
-
const anchorParent = parent ? [
|
|
1671
|
-
rrPos.x - parent.restRelRootPos[0],
|
|
1672
|
-
rrPos.y - parent.restRelRootPos[1],
|
|
1673
|
-
rrPos.z - parent.restRelRootPos[2]
|
|
1674
|
-
] : [
|
|
1675
|
-
rpPos.x,
|
|
1676
|
-
rpPos.y,
|
|
1677
|
-
rpPos.z
|
|
1678
|
-
];
|
|
1679
|
-
const rrQuatTuple = [
|
|
1680
|
-
rrQuat.x,
|
|
1681
|
-
rrQuat.y,
|
|
1682
|
-
rrQuat.z,
|
|
1683
|
-
rrQuat.w
|
|
1684
|
-
];
|
|
1685
|
-
const massTarget = buildBodyMassTarget(b, rrQuatTuple);
|
|
1686
|
-
const colliders = planOwnColliders(b, massTarget, rrQuat);
|
|
1687
|
-
const plan = {
|
|
1688
|
-
spec: b,
|
|
1689
|
-
joint,
|
|
1690
|
-
parent,
|
|
1691
|
-
restRelParentPos: anchorParent,
|
|
1692
|
-
restRelParentQuat: rrQuatTuple,
|
|
1693
|
-
restRelRootPos: [
|
|
1694
|
-
rrPos.x,
|
|
1695
|
-
rrPos.y,
|
|
1696
|
-
rrPos.z
|
|
1697
|
-
],
|
|
1698
|
-
restRelRootQuat: rrQuatTuple,
|
|
1699
|
-
axisParent: [
|
|
1700
|
-
axParentVec.x,
|
|
1701
|
-
axParentVec.y,
|
|
1702
|
-
axParentVec.z
|
|
1703
|
-
],
|
|
1704
|
-
axisChild: [
|
|
1705
|
-
axChildVec.x,
|
|
1706
|
-
axChildVec.y,
|
|
1707
|
-
axChildVec.z
|
|
1708
|
-
],
|
|
1709
|
-
massTarget,
|
|
1710
|
-
colliders
|
|
1711
|
-
};
|
|
1712
|
-
partByName.set(b.nodeName, plan);
|
|
1713
|
-
return plan;
|
|
1714
|
-
});
|
|
1715
|
-
const massTarget = buildBodyMassTarget(root);
|
|
1716
|
-
const colliders = planOwnColliders(root, massTarget);
|
|
1717
|
-
const mountNode = lowestCommonAncestor([root.object, ...descendants.map((d) => d.object)], bundle.scene);
|
|
1718
|
-
const mountInRoot = invRoot.clone().multiply(matrixToRoot(mountNode, bundle.scene));
|
|
1719
|
-
const scenePos = new THREE.Vector3(), sceneQuat = new THREE.Quaternion(), sceneScl = new THREE.Vector3();
|
|
1720
|
-
invRoot.decompose(scenePos, sceneQuat, sceneScl);
|
|
1721
|
-
sceneQuat.normalize();
|
|
1722
|
-
const mountPos = new THREE.Vector3(), mountQuat = new THREE.Quaternion();
|
|
1723
|
-
mountInRoot.decompose(mountPos, mountQuat, sceneScl);
|
|
1724
|
-
mountQuat.normalize();
|
|
1725
|
-
return {
|
|
1726
|
-
spec: root,
|
|
1727
|
-
sceneInRootPos: [
|
|
1728
|
-
scenePos.x,
|
|
1729
|
-
scenePos.y,
|
|
1730
|
-
scenePos.z
|
|
1731
|
-
],
|
|
1732
|
-
sceneInRootQuat: [
|
|
1733
|
-
sceneQuat.x,
|
|
1734
|
-
sceneQuat.y,
|
|
1735
|
-
sceneQuat.z,
|
|
1736
|
-
sceneQuat.w
|
|
1737
|
-
],
|
|
1738
|
-
mountNode,
|
|
1739
|
-
mountPosition: [
|
|
1740
|
-
mountPos.x,
|
|
1741
|
-
mountPos.y,
|
|
1742
|
-
mountPos.z
|
|
1743
|
-
],
|
|
1744
|
-
mountQuaternion: [
|
|
1745
|
-
mountQuat.x,
|
|
1746
|
-
mountQuat.y,
|
|
1747
|
-
mountQuat.z,
|
|
1748
|
-
mountQuat.w
|
|
1749
|
-
],
|
|
1750
|
-
massTarget,
|
|
1751
|
-
colliders,
|
|
1752
|
-
parts
|
|
1753
|
-
};
|
|
1754
|
-
});
|
|
1755
|
-
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`);
|
|
1756
|
-
return plans;
|
|
1749
|
+
}
|
|
1750
|
+
const triIndices = new Uint32Array(nrows * ncols * 6);
|
|
1751
|
+
let iPtr = 0;
|
|
1752
|
+
for (let zRow = 0; zRow < nrows; zRow++) for (let xCol = 0; xCol < ncols; xCol++) {
|
|
1753
|
+
const i00 = zRow * strideX + xCol;
|
|
1754
|
+
const i10 = i00 + 1;
|
|
1755
|
+
const i01 = i00 + strideX;
|
|
1756
|
+
const i11 = i01 + 1;
|
|
1757
|
+
triIndices[iPtr++] = i00;
|
|
1758
|
+
triIndices[iPtr++] = i01;
|
|
1759
|
+
triIndices[iPtr++] = i10;
|
|
1760
|
+
triIndices[iPtr++] = i10;
|
|
1761
|
+
triIndices[iPtr++] = i01;
|
|
1762
|
+
triIndices[iPtr++] = i11;
|
|
1763
|
+
}
|
|
1764
|
+
const geo = new THREE.BufferGeometry();
|
|
1765
|
+
geo.setAttribute("position", new THREE.BufferAttribute(positions, 3));
|
|
1766
|
+
geo.setAttribute("normal", new THREE.BufferAttribute(normals, 3));
|
|
1767
|
+
geo.setAttribute("uv", new THREE.BufferAttribute(uvs, 2));
|
|
1768
|
+
geo.setIndex(new THREE.BufferAttribute(triIndices, 1));
|
|
1769
|
+
geo.computeBoundingSphere();
|
|
1770
|
+
return geo;
|
|
1757
1771
|
}
|
|
1758
|
-
|
|
1772
|
+
/**
|
|
1773
|
+
* Direct Rapier WASM heightfield body & collider (`HeightFieldFlags.FIX_INTERNAL_EDGES`),
|
|
1774
|
+
* bypassing `@react-three/rapier`'s broken `<HeightfieldCollider>` `scaleColliderArgs` mutation.
|
|
1775
|
+
*/
|
|
1776
|
+
function KhrHeightfield({ ref, colliderRef, heights, segments, size, layout = "rapier-column-major", friction = .85, restitution = .05, collisionGroups, solverGroups, version = 0, children, ...rbProps }) {
|
|
1759
1777
|
const { rapier, world } = useRapier();
|
|
1760
|
-
const
|
|
1761
|
-
const
|
|
1762
|
-
const
|
|
1763
|
-
const [
|
|
1764
|
-
const
|
|
1765
|
-
|
|
1766
|
-
|
|
1767
|
-
|
|
1768
|
-
pos: [
|
|
1769
|
-
pos.x,
|
|
1770
|
-
pos.y,
|
|
1771
|
-
pos.z
|
|
1772
|
-
],
|
|
1773
|
-
quat: [
|
|
1774
|
-
rq.x,
|
|
1775
|
-
rq.y,
|
|
1776
|
-
rq.z,
|
|
1777
|
-
rq.w
|
|
1778
|
-
]
|
|
1779
|
-
};
|
|
1780
|
-
}, [
|
|
1781
|
-
part.restRelRootPos,
|
|
1782
|
-
rootPos,
|
|
1783
|
-
rootQuat
|
|
1784
|
-
]);
|
|
1785
|
-
const assignChildRef = useCallback((rb) => {
|
|
1786
|
-
childRef.current = rb;
|
|
1787
|
-
setChildBody(rb);
|
|
1788
|
-
onBody(part.spec.nodeName, rb);
|
|
1789
|
-
}, [part.spec.nodeName, onBody]);
|
|
1790
|
-
useEffect(() => () => onBody(part.spec.nodeName, null), [part.spec.nodeName, onBody]);
|
|
1791
|
-
useEffect(() => {
|
|
1792
|
-
if (childBody && part.massTarget) applyRapierBodyMass(childBody, part.massTarget);
|
|
1793
|
-
}, [childBody, part.massTarget]);
|
|
1778
|
+
const rbRef = useRef(null);
|
|
1779
|
+
const nrows = typeof segments === "number" ? segments : segments[0];
|
|
1780
|
+
const ncols = typeof segments === "number" ? segments : segments[1];
|
|
1781
|
+
const [sizeX, sizeZ] = size;
|
|
1782
|
+
const assignRb = (rb) => {
|
|
1783
|
+
rbRef.current = rb;
|
|
1784
|
+
assignRef(ref, rb);
|
|
1785
|
+
};
|
|
1794
1786
|
useEffect(() => {
|
|
1795
|
-
|
|
1796
|
-
|
|
1797
|
-
|
|
1798
|
-
|
|
1799
|
-
|
|
1800
|
-
|
|
1801
|
-
|
|
1802
|
-
|
|
1803
|
-
|
|
1804
|
-
|
|
1805
|
-
|
|
1806
|
-
|
|
1807
|
-
|
|
1808
|
-
|
|
1809
|
-
|
|
1810
|
-
|
|
1811
|
-
drive: part.joint.drive,
|
|
1812
|
-
breakaway: part.spec.breakaway,
|
|
1813
|
-
rapierJoint: imp,
|
|
1814
|
-
parentBody,
|
|
1815
|
-
childBody
|
|
1816
|
-
});
|
|
1817
|
-
rtRef.current = rt;
|
|
1818
|
-
registry?.add(rt);
|
|
1819
|
-
onJointReady(rt.nodeName, part.spec.object, rt);
|
|
1787
|
+
const rb = rbRef.current;
|
|
1788
|
+
if (!rb || !world.getRigidBody(rb.handle)) return;
|
|
1789
|
+
const colHeights = layout === "row-major" ? transposeHeightfieldForRapier(heights, nrows, ncols) : heights;
|
|
1790
|
+
const expected = (nrows + 1) * (ncols + 1);
|
|
1791
|
+
if (colHeights.length !== expected) throw new KhrValidationError(`KhrHeightfield: expected ${expected} samples for ${nrows}x${ncols} cells, got ${colHeights.length}`);
|
|
1792
|
+
const desc = rapier.ColliderDesc.heightfield(nrows, ncols, colHeights, {
|
|
1793
|
+
x: sizeX,
|
|
1794
|
+
y: 1,
|
|
1795
|
+
z: sizeZ
|
|
1796
|
+
}, 1);
|
|
1797
|
+
desc.setFriction(friction);
|
|
1798
|
+
desc.setRestitution(restitution);
|
|
1799
|
+
if (collisionGroups !== void 0) desc.setCollisionGroups(collisionGroups);
|
|
1800
|
+
if (solverGroups !== void 0 || collisionGroups !== void 0) desc.setSolverGroups(solverGroups ?? collisionGroups);
|
|
1801
|
+
const col = world.createCollider(desc, rb);
|
|
1802
|
+
assignRef(colliderRef, col);
|
|
1820
1803
|
return () => {
|
|
1821
|
-
|
|
1822
|
-
|
|
1823
|
-
onJointReady(rt.nodeName, part.spec.object, null);
|
|
1824
|
-
if (rt.rapierJoint && world.getImpulseJoint(rt.rapierJoint.handle)) world.removeImpulseJoint(rt.rapierJoint, true);
|
|
1804
|
+
assignRef(colliderRef, null);
|
|
1805
|
+
if (world.getCollider(col.handle)) world.removeCollider(col, false);
|
|
1825
1806
|
};
|
|
1826
1807
|
}, [
|
|
1827
1808
|
rapier,
|
|
1828
1809
|
world,
|
|
1829
|
-
|
|
1830
|
-
|
|
1831
|
-
|
|
1832
|
-
|
|
1833
|
-
|
|
1810
|
+
heights,
|
|
1811
|
+
nrows,
|
|
1812
|
+
ncols,
|
|
1813
|
+
sizeX,
|
|
1814
|
+
sizeZ,
|
|
1815
|
+
layout,
|
|
1816
|
+
friction,
|
|
1817
|
+
restitution,
|
|
1818
|
+
collisionGroups,
|
|
1819
|
+
solverGroups,
|
|
1820
|
+
version,
|
|
1821
|
+
colliderRef
|
|
1834
1822
|
]);
|
|
1835
|
-
|
|
1836
|
-
|
|
1837
|
-
|
|
1838
|
-
|
|
1839
|
-
const angVel = part.spec.angularVelocity ?? rootSpec.angularVelocity;
|
|
1840
|
-
const gravScale = part.spec.gravityFactor ?? rootSpec.gravityFactor;
|
|
1841
|
-
return /* @__PURE__ */ jsxs(RigidBody, {
|
|
1842
|
-
ref: assignChildRef,
|
|
1843
|
-
type: part.spec.isKinematic ? "kinematicPosition" : "dynamic",
|
|
1844
|
-
position: worldPose.pos,
|
|
1845
|
-
quaternion: worldPose.quat,
|
|
1823
|
+
return /* @__PURE__ */ jsx(RigidBody, {
|
|
1824
|
+
...rbProps,
|
|
1825
|
+
ref: assignRb,
|
|
1826
|
+
type: "fixed",
|
|
1846
1827
|
colliders: false,
|
|
1847
|
-
|
|
1848
|
-
...userData === void 0 ? {} : { userData },
|
|
1849
|
-
...linVel ? { linearVelocity: linVel } : {},
|
|
1850
|
-
...angVel ? { angularVelocity: angVel } : {},
|
|
1851
|
-
...gravScale === void 0 ? {} : { gravityScale: gravScale },
|
|
1852
|
-
children: [/* @__PURE__ */ jsx(MountSubtreeSlot, {
|
|
1853
|
-
object: part.spec.object,
|
|
1854
|
-
position: [
|
|
1855
|
-
0,
|
|
1856
|
-
0,
|
|
1857
|
-
0
|
|
1858
|
-
],
|
|
1859
|
-
quaternion: part.restRelRootQuat,
|
|
1860
|
-
origin
|
|
1861
|
-
}), part.colliders.map((col, cIdx) => /* @__PURE__ */ jsx(KhrCollider, {
|
|
1862
|
-
c: col.spec,
|
|
1863
|
-
position: col.position,
|
|
1864
|
-
quaternion: col.quaternion,
|
|
1865
|
-
density: col.density
|
|
1866
|
-
}, `${part.spec.nodeName}:${cIdx}`))]
|
|
1867
|
-
});
|
|
1868
|
-
}
|
|
1869
|
-
function AssemblyRootBody({ plan, bundleScene, position, quaternion, fixedRoot, rigidBodyProps, onBody, onJointReady, children }) {
|
|
1870
|
-
const [rootBody, setRootBody] = React.useState(null);
|
|
1871
|
-
const [partBodies, setPartBodies] = React.useState(() => /* @__PURE__ */ new Map());
|
|
1872
|
-
const partOrigins = useMemo(() => plan.parts.length > 0 ? captureSubtreeOrigins(plan.parts.map((p) => p.spec.object), bundleScene) : null, [plan.parts, bundleScene]);
|
|
1873
|
-
const rootSpec = plan.spec;
|
|
1874
|
-
const assignRootRef = useCallback((rb) => {
|
|
1875
|
-
setRootBody(rb);
|
|
1876
|
-
onBody(rootSpec.nodeName, rb);
|
|
1877
|
-
}, [rootSpec.nodeName, onBody]);
|
|
1878
|
-
useEffect(() => () => onBody(rootSpec.nodeName, null), [rootSpec.nodeName, onBody]);
|
|
1879
|
-
useEffect(() => {
|
|
1880
|
-
if (rootBody && plan.massTarget) applyRapierBodyMass(rootBody, plan.massTarget);
|
|
1881
|
-
}, [rootBody, plan.massTarget]);
|
|
1882
|
-
const handlePartBody = useCallback((nodeName, rb) => {
|
|
1883
|
-
setPartBodies((prev) => {
|
|
1884
|
-
if ((prev.get(nodeName) ?? null) === rb) return prev;
|
|
1885
|
-
const next = new Map(prev);
|
|
1886
|
-
if (rb) next.set(nodeName, rb);
|
|
1887
|
-
else next.delete(nodeName);
|
|
1888
|
-
return next;
|
|
1889
|
-
});
|
|
1890
|
-
onBody(nodeName, rb);
|
|
1891
|
-
}, [onBody]);
|
|
1892
|
-
const bodyType = rootSpec.isKinematic ? fixedRoot ? "fixed" : "kinematicPosition" : "dynamic";
|
|
1893
|
-
return /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsxs(RigidBody, {
|
|
1894
|
-
...rigidBodyProps,
|
|
1895
|
-
ref: assignRootRef,
|
|
1896
|
-
type: bodyType,
|
|
1897
|
-
position,
|
|
1898
|
-
quaternion,
|
|
1899
|
-
colliders: false,
|
|
1900
|
-
...rootSpec.linearVelocity ? { linearVelocity: rootSpec.linearVelocity } : {},
|
|
1901
|
-
...rootSpec.angularVelocity ? { angularVelocity: rootSpec.angularVelocity } : {},
|
|
1902
|
-
...rootSpec.gravityFactor === void 0 ? {} : { gravityScale: rootSpec.gravityFactor },
|
|
1903
|
-
children: [children, plan.colliders.map((col, idx) => /* @__PURE__ */ jsx(KhrCollider, {
|
|
1904
|
-
c: col.spec,
|
|
1905
|
-
position: col.position,
|
|
1906
|
-
quaternion: col.quaternion,
|
|
1907
|
-
density: col.density
|
|
1908
|
-
}, `r${idx}`))]
|
|
1909
|
-
}), plan.parts.map((part) => /* @__PURE__ */ jsx(ArticulatedPartBody, {
|
|
1910
|
-
part,
|
|
1911
|
-
rootSpec,
|
|
1912
|
-
rootPos: position,
|
|
1913
|
-
rootQuat: quaternion,
|
|
1914
|
-
parentBody: part.parent ? partBodies.get(part.parent.spec.nodeName) ?? null : rootBody,
|
|
1915
|
-
userData: rigidBodyProps?.userData,
|
|
1916
|
-
origin: partOrigins?.get(part.spec.object),
|
|
1917
|
-
onBody: handlePartBody,
|
|
1918
|
-
onJointReady
|
|
1919
|
-
}, part.spec.nodeName))] });
|
|
1920
|
-
}
|
|
1921
|
-
function KhrAssembly({ ref, bundle, position = [
|
|
1922
|
-
0,
|
|
1923
|
-
0,
|
|
1924
|
-
0
|
|
1925
|
-
], rotation, quaternion, scale, fixedRoot = false, enableCustomGravity = false, rigidBodyProps, children, ...groupProps }) {
|
|
1926
|
-
assertUnitScale(scale, "KhrAssembly");
|
|
1927
|
-
useMemo(() => assertSceneIdentityWorldScale(bundle.scene), [bundle]);
|
|
1928
|
-
const { world } = useRapier();
|
|
1929
|
-
const applyGravityField = useCustomGravity((s) => s.applyGravityField);
|
|
1930
|
-
const plans = useMemo(() => planAssembly(bundle), [bundle]);
|
|
1931
|
-
const origins = useMemo(() => plans.length > 1 ? captureSubtreeOrigins(plans.map((p) => p.mountNode), bundle.scene) : null, [plans, bundle.scene]);
|
|
1932
|
-
const [px, py, pz] = position;
|
|
1933
|
-
const [rx, ry, rz] = rotation ?? [
|
|
1934
|
-
0,
|
|
1935
|
-
0,
|
|
1936
|
-
0
|
|
1937
|
-
];
|
|
1938
|
-
const hasQuat = quaternion !== void 0;
|
|
1939
|
-
const qx = quaternion?.[0] ?? 0;
|
|
1940
|
-
const qy = quaternion?.[1] ?? 0;
|
|
1941
|
-
const qz = quaternion?.[2] ?? 0;
|
|
1942
|
-
const qw = quaternion?.[3] ?? 1;
|
|
1943
|
-
const spawnQuat = useMemo(() => hasQuat ? new THREE.Quaternion(qx, qy, qz, qw).normalize() : new THREE.Quaternion().setFromEuler(new THREE.Euler(rx, ry, rz)), [
|
|
1944
|
-
hasQuat,
|
|
1945
|
-
qx,
|
|
1946
|
-
qy,
|
|
1947
|
-
qz,
|
|
1948
|
-
qw,
|
|
1949
|
-
rx,
|
|
1950
|
-
ry,
|
|
1951
|
-
rz
|
|
1952
|
-
]);
|
|
1953
|
-
const rootPoses = useMemo(() => plans.map((p) => {
|
|
1954
|
-
const pos = new THREE.Vector3().fromArray(p.spec.localPosition).applyQuaternion(spawnQuat).add(new THREE.Vector3(px, py, pz));
|
|
1955
|
-
const quat = spawnQuat.clone().multiply(new THREE.Quaternion().fromArray(p.spec.localQuaternion));
|
|
1956
|
-
return {
|
|
1957
|
-
position: [
|
|
1958
|
-
pos.x,
|
|
1959
|
-
pos.y,
|
|
1960
|
-
pos.z
|
|
1961
|
-
],
|
|
1962
|
-
quaternion: [
|
|
1963
|
-
quat.x,
|
|
1964
|
-
quat.y,
|
|
1965
|
-
quat.z,
|
|
1966
|
-
quat.w
|
|
1967
|
-
]
|
|
1968
|
-
};
|
|
1969
|
-
}), [
|
|
1970
|
-
plans,
|
|
1971
|
-
spawnQuat,
|
|
1972
|
-
px,
|
|
1973
|
-
py,
|
|
1974
|
-
pz
|
|
1975
|
-
]);
|
|
1976
|
-
const bodiesRef = useRef(/* @__PURE__ */ new Map());
|
|
1977
|
-
const jointsRef = useRef(/* @__PURE__ */ new Map());
|
|
1978
|
-
const byObjectRef = useRef(/* @__PURE__ */ new Map());
|
|
1979
|
-
const handleBody = useCallback((nodeName, rb) => {
|
|
1980
|
-
if (rb) bodiesRef.current.set(nodeName, rb);
|
|
1981
|
-
else bodiesRef.current.delete(nodeName);
|
|
1982
|
-
}, []);
|
|
1983
|
-
const handleJointReady = useCallback((nodeName, obj, rt) => {
|
|
1984
|
-
if (rt) {
|
|
1985
|
-
jointsRef.current.set(nodeName, rt);
|
|
1986
|
-
byObjectRef.current.set(obj, rt);
|
|
1987
|
-
} else {
|
|
1988
|
-
jointsRef.current.delete(nodeName);
|
|
1989
|
-
byObjectRef.current.delete(obj);
|
|
1990
|
-
}
|
|
1991
|
-
}, []);
|
|
1992
|
-
useFrame(() => {
|
|
1993
|
-
if (!enableCustomGravity || world.timestep <= 0) return;
|
|
1994
|
-
for (const rb of bodiesRef.current.values()) if (rb.isDynamic()) applyGravityField(rb, world.timestep);
|
|
1995
|
-
});
|
|
1996
|
-
useImperativeHandle(ref, () => ({
|
|
1997
|
-
get jointNames() {
|
|
1998
|
-
return [...jointsRef.current.keys()];
|
|
1999
|
-
},
|
|
2000
|
-
getBody: (nodeName) => bodiesRef.current.get(nodeName) ?? null,
|
|
2001
|
-
getJointRuntime: (nodeName) => jointsRef.current.get(nodeName) ?? null,
|
|
2002
|
-
setJointTarget: (nodeName, target) => {
|
|
2003
|
-
jointsRef.current.get(nodeName)?.applyTarget(target);
|
|
2004
|
-
},
|
|
2005
|
-
setRoleTarget: (role, target) => {
|
|
2006
|
-
for (const rt of jointsRef.current.values()) if (rt.role === role) rt.applyTarget(target);
|
|
2007
|
-
},
|
|
2008
|
-
toggleHitObject: (hit) => {
|
|
2009
|
-
for (let cur = hit; cur; cur = cur.parent) {
|
|
2010
|
-
const rt = byObjectRef.current.get(cur);
|
|
2011
|
-
if (rt && toggleDrivenJoint(rt)) return true;
|
|
2012
|
-
}
|
|
2013
|
-
return false;
|
|
2014
|
-
}
|
|
2015
|
-
}), []);
|
|
2016
|
-
if (plans.length === 1) {
|
|
2017
|
-
const plan = plans[0];
|
|
2018
|
-
return /* @__PURE__ */ jsx("group", {
|
|
2019
|
-
...groupProps,
|
|
2020
|
-
children: /* @__PURE__ */ jsx(AssemblyRootBody, {
|
|
2021
|
-
plan,
|
|
2022
|
-
bundleScene: bundle.scene,
|
|
2023
|
-
position: rootPoses[0].position,
|
|
2024
|
-
quaternion: rootPoses[0].quaternion,
|
|
2025
|
-
fixedRoot,
|
|
2026
|
-
rigidBodyProps,
|
|
2027
|
-
onBody: handleBody,
|
|
2028
|
-
onJointReady: handleJointReady,
|
|
2029
|
-
children: /* @__PURE__ */ jsxs("group", {
|
|
2030
|
-
position: plan.sceneInRootPos,
|
|
2031
|
-
quaternion: plan.sceneInRootQuat,
|
|
2032
|
-
children: [/* @__PURE__ */ jsx(BundleSceneHost, {
|
|
2033
|
-
bundle,
|
|
2034
|
-
owner: "KhrAssembly"
|
|
2035
|
-
}), children]
|
|
2036
|
-
})
|
|
2037
|
-
}, `${bundle.scene.uuid}:${plan.spec.nodeName}`)
|
|
2038
|
-
});
|
|
2039
|
-
}
|
|
2040
|
-
return /* @__PURE__ */ jsxs("group", {
|
|
2041
|
-
...groupProps,
|
|
2042
|
-
children: [/* @__PURE__ */ jsxs("group", {
|
|
2043
|
-
position,
|
|
2044
|
-
quaternion: [
|
|
2045
|
-
spawnQuat.x,
|
|
2046
|
-
spawnQuat.y,
|
|
2047
|
-
spawnQuat.z,
|
|
2048
|
-
spawnQuat.w
|
|
2049
|
-
],
|
|
2050
|
-
children: [/* @__PURE__ */ jsx(BundleSceneHost, {
|
|
2051
|
-
bundle,
|
|
2052
|
-
owner: "KhrAssembly"
|
|
2053
|
-
}), children]
|
|
2054
|
-
}), plans.map((plan, idx) => /* @__PURE__ */ jsx(AssemblyRootBody, {
|
|
2055
|
-
plan,
|
|
2056
|
-
bundleScene: bundle.scene,
|
|
2057
|
-
position: rootPoses[idx].position,
|
|
2058
|
-
quaternion: rootPoses[idx].quaternion,
|
|
2059
|
-
fixedRoot,
|
|
2060
|
-
rigidBodyProps,
|
|
2061
|
-
onBody: handleBody,
|
|
2062
|
-
onJointReady: handleJointReady,
|
|
2063
|
-
children: /* @__PURE__ */ jsx(MountSubtreeSlot, {
|
|
2064
|
-
object: plan.mountNode,
|
|
2065
|
-
position: plan.mountPosition,
|
|
2066
|
-
quaternion: plan.mountQuaternion,
|
|
2067
|
-
origin: origins?.get(plan.mountNode)
|
|
2068
|
-
})
|
|
2069
|
-
}, `${bundle.scene.uuid}:${plan.spec.nodeName}`))]
|
|
1828
|
+
children
|
|
2070
1829
|
});
|
|
2071
1830
|
}
|
|
2072
1831
|
// src/useKhrGlb.ts
|
|
@@ -2181,6 +1940,6 @@ function useKhrGlb(url, options) {
|
|
|
2181
1940
|
}
|
|
2182
1941
|
};
|
|
2183
1942
|
})(useKhrGlb || (useKhrGlb = {}));
|
|
2184
|
-
export { ATTACHED_PANEL_INTERACTION_GROUPS, DETACHED_PANEL_INTERACTION_GROUPS, JointRegistryContext, KHR_TRIMESH_FLAGS, KhrAssembly, KhrCollider, KhrGLTFLoader, KhrValidationError, PhysicsWorld, applyRapierBodyMass, assertIdentityWorldScale, assertSceneIdentityWorldScale, assertUnitScale, assignRef,
|
|
1943
|
+
export { ATTACHED_PANEL_INTERACTION_GROUPS, DETACHED_PANEL_INTERACTION_GROUPS, JointRegistryContext, KHR_HEIGHTFIELD_FLAGS, KHR_TRIMESH_FLAGS, KhrAssembly, KhrCollider, KhrGLTFLoader, KhrHeightfield, KhrValidationError, PhysicsWorld, applyRapierBodyMass, assertIdentityWorldScale, assertSceneIdentityWorldScale, assertUnitScale, assignRef, buildJointRuntime, claimBundleMount, cloneKhrBundle, closedTargetForLimits, computeLumpedMassProps, computeShapeInertia, configureJointDrive, createHeightfieldGeometry, createKhrGltfPlugin, createRapierImpulseJoint, createScopedGltfLoaderClass, disposeKhrBundle, ensureHeadlessFileReader, exportKhrGlb, isJointOpen, jacobiPrincipalInertia, loadKhrGlb, measureJointCoordinate, openTargetForLimits, patchGlbJsonChunk, planAssembly, releaseBundleMount, resolveBodyMassFromColliders, sampleRapierHeightfield, snapshotSceneWithMountedSubtrees, toggleDrivenJoint, transposeHeightfieldForRapier, useKhrGlb };
|
|
2185
1944
|
|
|
2186
1945
|
//# sourceMappingURL=index.js.map
|