@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/index.js CHANGED
@@ -1,13 +1,12 @@
1
- import React, { useCallback, useContext, useEffect, useImperativeHandle, useMemo, useRef } from "react";
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 computeRelativePose, B as assignRef, C as applyRapierBodyMass, D as bodyPoseMatrix, E as assertUnitScale, F as readUnitQuat, G as optFiniteNum, H as isNonNegNum, I as resolveBodyMassFromColliders, L as COMBINE_MODES, M as extractMeshColliderShape, N as jacobiPrincipalInertia, O as computeAssemblyMassAndCom, P as readFiniteVec3, R as JointRegistryContext, S as snapshotSceneWithMountedSubtrees, T as assertSceneIdentityWorldScale, U as isObj, V as isFiniteNum, W as isPosNum, _ as cloneKhrBundle, a as configureJointDrive, b as releaseBundleMount, c as measureJointCoordinate, d as KHR_TRIMESH_FLAGS, f as KhrCollider, g as claimBundleMount, h as captureSubtreeOrigins, i as closedTargetForLimits, j as computeShapeInertia, k as computeLumpedMassProps, l as openTargetForLimits, m as MountSubtreeSlot, n as DETACHED_PANEL_INTERACTION_GROUPS, o as createRapierImpulseJoint, p as BundleSceneHost, r as buildJointRuntime, s as isJointOpen, t as ATTACHED_PANEL_INTERACTION_GROUPS, u as toggleDrivenJoint, v as disposeKhrBundle, w as assertIdentityWorldScale, z as KhrValidationError } from "./joints-H3uae8pm.js";
6
- import { Fragment, jsx, jsxs } from "react/jsx-runtime";
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 }) {
@@ -1829,579 +1828,6 @@ function KhrHeightfield({ ref, colliderRef, heights, segments, size, layout = "r
1829
1828
  children
1830
1829
  });
1831
1830
  }
1832
- // src/KhrAssembly.tsx
1833
- function matrixToRoot(node, sceneRoot) {
1834
- const out = new THREE.Matrix4();
1835
- for (let cur = node; cur && cur !== sceneRoot; cur = cur.parent) {
1836
- if (!cur.parent) throw new KhrValidationError(`Node "${node.name}" is detached from bundle.scene`);
1837
- cur.updateMatrix();
1838
- out.premultiply(cur.matrix);
1839
- }
1840
- return out;
1841
- }
1842
- function isSceneDescendant(node, ancestor) {
1843
- for (let cur = node; cur; cur = cur.parent) if (cur === ancestor) return true;
1844
- return false;
1845
- }
1846
- function lowestCommonAncestor(nodes, sceneRoot) {
1847
- let candidate = nodes[0];
1848
- while (candidate && candidate !== sceneRoot) {
1849
- if (nodes.every((n) => isSceneDescendant(n, candidate))) return candidate;
1850
- candidate = candidate.parent ?? sceneRoot;
1851
- }
1852
- return sceneRoot;
1853
- }
1854
- function planOwnColliders(body, frameQuat) {
1855
- const one = new THREE.Vector3(1, 1, 1);
1856
- const frameMat = frameQuat ? new THREE.Matrix4().makeRotationFromQuaternion(frameQuat) : null;
1857
- const pos = new THREE.Vector3(), quat = new THREE.Quaternion(), scl = new THREE.Vector3();
1858
- return body.colliders.map((spec) => {
1859
- const colQuat = new THREE.Quaternion().fromArray(spec.localQuaternion);
1860
- const local = new THREE.Matrix4().compose(new THREE.Vector3().fromArray(spec.localPosition), colQuat, one);
1861
- if (frameMat) local.premultiply(frameMat);
1862
- local.decompose(pos, quat, scl);
1863
- quat.normalize();
1864
- return {
1865
- spec,
1866
- position: [
1867
- pos.x,
1868
- pos.y,
1869
- pos.z
1870
- ],
1871
- quaternion: [
1872
- quat.x,
1873
- quat.y,
1874
- quat.z,
1875
- quat.w
1876
- ]
1877
- };
1878
- });
1879
- }
1880
- function planAssembly(bundle) {
1881
- const byName = new Map(bundle.bodies.map((b) => [b.nodeName, b]));
1882
- const rootMemo = /* @__PURE__ */ new Map();
1883
- const depthMemo = /* @__PURE__ */ new Map();
1884
- const resolveRootAndDepth = (b, visiting = /* @__PURE__ */ new Set()) => {
1885
- const cachedRoot = rootMemo.get(b.nodeName);
1886
- const cachedDepth = depthMemo.get(b.nodeName);
1887
- if (cachedRoot && cachedDepth !== void 0) return {
1888
- root: cachedRoot,
1889
- depth: cachedDepth
1890
- };
1891
- if (!b.joint) {
1892
- rootMemo.set(b.nodeName, b);
1893
- depthMemo.set(b.nodeName, 0);
1894
- return {
1895
- root: b,
1896
- depth: 0
1897
- };
1898
- }
1899
- if (visiting.has(b.nodeName)) throw new KhrValidationError(`Joint cycle through "${b.nodeName}"`);
1900
- visiting.add(b.nodeName);
1901
- const parent = byName.get(b.joint.connectedNodeName);
1902
- if (!parent) throw new KhrValidationError(`Joint on "${b.nodeName}" targets "${b.joint.connectedNodeName}", which is not a KHR body`);
1903
- const up = resolveRootAndDepth(parent, visiting);
1904
- rootMemo.set(b.nodeName, up.root);
1905
- depthMemo.set(b.nodeName, up.depth + 1);
1906
- return {
1907
- root: up.root,
1908
- depth: up.depth + 1
1909
- };
1910
- };
1911
- for (const b of bundle.bodies) resolveRootAndDepth(b);
1912
- const plans = bundle.bodies.filter((b) => !b.joint).map((root) => {
1913
- const invRoot = bodyPoseMatrix(root).clone().invert();
1914
- 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));
1915
- const partByName = /* @__PURE__ */ new Map();
1916
- const parts = descendants.map((b) => {
1917
- const joint = b.joint;
1918
- const parent = partByName.get(joint.connectedNodeName) ?? null;
1919
- 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}"`);
1920
- const parentSpec = parent ? parent.spec : root;
1921
- const relParentMat = bodyPoseMatrix(parentSpec).clone().invert().multiply(bodyPoseMatrix(b));
1922
- const relRootMat = invRoot.clone().multiply(bodyPoseMatrix(b));
1923
- const rpPos = new THREE.Vector3(), tmpQ = new THREE.Quaternion(), rpScl = new THREE.Vector3();
1924
- relParentMat.decompose(rpPos, tmpQ, rpScl);
1925
- const rrPos = new THREE.Vector3(), rrQuat = new THREE.Quaternion();
1926
- relRootMat.decompose(rrPos, rrQuat, rpScl);
1927
- rrQuat.normalize();
1928
- const axChildVec = new THREE.Vector3().fromArray(joint.axis).normalize();
1929
- const axParentVec = axChildVec.clone().applyQuaternion(rrQuat).normalize();
1930
- const anchorParent = parent ? [
1931
- rrPos.x - parent.restRelRootPos[0],
1932
- rrPos.y - parent.restRelRootPos[1],
1933
- rrPos.z - parent.restRelRootPos[2]
1934
- ] : [
1935
- rpPos.x,
1936
- rpPos.y,
1937
- rpPos.z
1938
- ];
1939
- const rrQuatTuple = [
1940
- rrQuat.x,
1941
- rrQuat.y,
1942
- rrQuat.z,
1943
- rrQuat.w
1944
- ];
1945
- 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();
1946
- const lumpedMass = computeLumpedMassProps([b], invPartFrame);
1947
- const colliders = planOwnColliders(b, rrQuat);
1948
- const plan = {
1949
- spec: b,
1950
- joint,
1951
- parent,
1952
- restRelParentPos: anchorParent,
1953
- restRelParentQuat: rrQuatTuple,
1954
- restRelRootPos: [
1955
- rrPos.x,
1956
- rrPos.y,
1957
- rrPos.z
1958
- ],
1959
- restRelRootQuat: rrQuatTuple,
1960
- axisParent: [
1961
- axParentVec.x,
1962
- axParentVec.y,
1963
- axParentVec.z
1964
- ],
1965
- axisChild: [
1966
- axChildVec.x,
1967
- axChildVec.y,
1968
- axChildVec.z
1969
- ],
1970
- lumpedMass,
1971
- colliders
1972
- };
1973
- partByName.set(b.nodeName, plan);
1974
- return plan;
1975
- });
1976
- const lumpedMass = computeLumpedMassProps([root], invRoot);
1977
- const colliders = planOwnColliders(root);
1978
- const mountNode = lowestCommonAncestor([root.object, ...descendants.map((d) => d.object)], bundle.scene);
1979
- const mountInRoot = invRoot.clone().multiply(matrixToRoot(mountNode, bundle.scene));
1980
- const scenePos = new THREE.Vector3(), sceneQuat = new THREE.Quaternion(), sceneScl = new THREE.Vector3();
1981
- invRoot.decompose(scenePos, sceneQuat, sceneScl);
1982
- sceneQuat.normalize();
1983
- const mountPos = new THREE.Vector3(), mountQuat = new THREE.Quaternion();
1984
- mountInRoot.decompose(mountPos, mountQuat, sceneScl);
1985
- mountQuat.normalize();
1986
- return {
1987
- spec: root,
1988
- sceneInRootPos: [
1989
- scenePos.x,
1990
- scenePos.y,
1991
- scenePos.z
1992
- ],
1993
- sceneInRootQuat: [
1994
- sceneQuat.x,
1995
- sceneQuat.y,
1996
- sceneQuat.z,
1997
- sceneQuat.w
1998
- ],
1999
- mountNode,
2000
- mountPosition: [
2001
- mountPos.x,
2002
- mountPos.y,
2003
- mountPos.z
2004
- ],
2005
- mountQuaternion: [
2006
- mountQuat.x,
2007
- mountQuat.y,
2008
- mountQuat.z,
2009
- mountQuat.w
2010
- ],
2011
- lumpedMass,
2012
- colliders,
2013
- parts
2014
- };
2015
- });
2016
- 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`);
2017
- return plans;
2018
- }
2019
- function ArticulatedPartBody({ part, rootSpec, rootPos, rootQuat, parentBody, userData, origin, onBody, onJointReady }) {
2020
- const { rapier, world } = useRapier();
2021
- const registry = useContext(JointRegistryContext);
2022
- const childRef = useRef(null);
2023
- const rtRef = useRef(null);
2024
- const [childBody, setChildBody] = React.useState(null);
2025
- const worldPose = useMemo(() => {
2026
- const rq = new THREE.Quaternion(rootQuat[0], rootQuat[1], rootQuat[2], rootQuat[3]).normalize();
2027
- const pos = new THREE.Vector3().fromArray(part.restRelRootPos).applyQuaternion(rq).add(new THREE.Vector3(rootPos[0], rootPos[1], rootPos[2]));
2028
- return {
2029
- pos: [
2030
- pos.x,
2031
- pos.y,
2032
- pos.z
2033
- ],
2034
- quat: [
2035
- rq.x,
2036
- rq.y,
2037
- rq.z,
2038
- rq.w
2039
- ]
2040
- };
2041
- }, [
2042
- part.restRelRootPos,
2043
- rootPos,
2044
- rootQuat
2045
- ]);
2046
- const assignChildRef = useCallback((rb) => {
2047
- childRef.current = rb;
2048
- setChildBody(rb);
2049
- onBody(part.spec.nodeName, rb);
2050
- }, [part.spec.nodeName, onBody]);
2051
- useEffect(() => () => onBody(part.spec.nodeName, null), [part.spec.nodeName, onBody]);
2052
- useEffect(() => {
2053
- if (childBody && part.lumpedMass) applyRapierBodyMass(childBody, part.lumpedMass);
2054
- }, [childBody, part.lumpedMass]);
2055
- useEffect(() => {
2056
- if (!parentBody || !childBody) return;
2057
- const imp = createRapierImpulseJoint(rapier, world, parentBody, childBody, part.joint, part.restRelParentPos, [
2058
- 0,
2059
- 0,
2060
- 0
2061
- ], part.axisParent, part.axisChild, part.restRelParentQuat);
2062
- const rt = buildJointRuntime({
2063
- rapier,
2064
- world,
2065
- nodeName: part.spec.nodeName,
2066
- role: part.joint.role,
2067
- kind: part.joint.kind,
2068
- object: part.spec.object,
2069
- axisParent: part.axisParent,
2070
- anchorParent: part.restRelParentPos,
2071
- limits: part.joint.limits,
2072
- drive: part.joint.drive,
2073
- breakaway: part.spec.breakaway,
2074
- rapierJoint: imp,
2075
- parentBody,
2076
- childBody
2077
- });
2078
- rtRef.current = rt;
2079
- registry?.add(rt);
2080
- onJointReady(rt.nodeName, part.spec.object, rt);
2081
- return () => {
2082
- rtRef.current = null;
2083
- registry?.delete(rt);
2084
- onJointReady(rt.nodeName, part.spec.object, null);
2085
- if (rt.rapierJoint && world.getImpulseJoint(rt.rapierJoint.handle)) world.removeImpulseJoint(rt.rapierJoint, true);
2086
- };
2087
- }, [
2088
- rapier,
2089
- world,
2090
- part,
2091
- parentBody,
2092
- childBody,
2093
- registry,
2094
- onJointReady
2095
- ]);
2096
- const onForce = useMemo(() => part.spec.breakaway ? (p) => {
2097
- if (world.timestep > 0) rtRef.current?.applyImpactImpulse(p.totalForceMagnitude * world.timestep);
2098
- } : void 0, [part.spec.breakaway, world]);
2099
- const linVel = part.spec.linearVelocity ?? rootSpec.linearVelocity;
2100
- const angVel = part.spec.angularVelocity ?? rootSpec.angularVelocity;
2101
- const gravScale = part.spec.gravityFactor ?? rootSpec.gravityFactor;
2102
- return /* @__PURE__ */ jsxs(RigidBody, {
2103
- ref: assignChildRef,
2104
- type: part.spec.isKinematic ? "kinematicPosition" : "dynamic",
2105
- position: worldPose.pos,
2106
- quaternion: worldPose.quat,
2107
- colliders: false,
2108
- onContactForce: onForce,
2109
- ...userData === void 0 ? {} : { userData },
2110
- ...linVel ? { linearVelocity: linVel } : {},
2111
- ...angVel ? { angularVelocity: angVel } : {},
2112
- ...gravScale === void 0 ? {} : { gravityScale: gravScale },
2113
- children: [/* @__PURE__ */ jsx(MountSubtreeSlot, {
2114
- object: part.spec.object,
2115
- position: [
2116
- 0,
2117
- 0,
2118
- 0
2119
- ],
2120
- quaternion: part.restRelRootQuat,
2121
- origin
2122
- }), part.colliders.map((col, cIdx) => /* @__PURE__ */ jsx(KhrCollider, {
2123
- c: col.spec,
2124
- position: col.position,
2125
- quaternion: col.quaternion
2126
- }, `${part.spec.nodeName}:${cIdx}`))]
2127
- });
2128
- }
2129
- function AssemblyRootBody({ plan, bundleScene, position, quaternion, fixedRoot, rigidBodyProps, onBody, onJointReady, children }) {
2130
- const [rootBody, setRootBody] = React.useState(null);
2131
- const [partBodies, setPartBodies] = React.useState(() => /* @__PURE__ */ new Map());
2132
- const partOrigins = useMemo(() => plan.parts.length > 0 ? captureSubtreeOrigins(plan.parts.map((p) => p.spec.object), bundleScene) : null, [plan.parts, bundleScene]);
2133
- const rootSpec = plan.spec;
2134
- const assignRootRef = useCallback((rb) => {
2135
- setRootBody(rb);
2136
- onBody(rootSpec.nodeName, rb);
2137
- }, [rootSpec.nodeName, onBody]);
2138
- useEffect(() => () => onBody(rootSpec.nodeName, null), [rootSpec.nodeName, onBody]);
2139
- useEffect(() => {
2140
- if (rootBody && plan.lumpedMass) applyRapierBodyMass(rootBody, plan.lumpedMass);
2141
- }, [rootBody, plan.lumpedMass]);
2142
- const handlePartBody = useCallback((nodeName, rb) => {
2143
- setPartBodies((prev) => {
2144
- if ((prev.get(nodeName) ?? null) === rb) return prev;
2145
- const next = new Map(prev);
2146
- if (rb) next.set(nodeName, rb);
2147
- else next.delete(nodeName);
2148
- return next;
2149
- });
2150
- onBody(nodeName, rb);
2151
- }, [onBody]);
2152
- const bodyType = rootSpec.isKinematic ? fixedRoot ? "fixed" : "kinematicPosition" : "dynamic";
2153
- return /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsxs(RigidBody, {
2154
- ...rigidBodyProps,
2155
- ref: assignRootRef,
2156
- type: bodyType,
2157
- position,
2158
- quaternion,
2159
- colliders: false,
2160
- ...rootSpec.linearVelocity ? { linearVelocity: rootSpec.linearVelocity } : {},
2161
- ...rootSpec.angularVelocity ? { angularVelocity: rootSpec.angularVelocity } : {},
2162
- ...rootSpec.gravityFactor === void 0 ? {} : { gravityScale: rootSpec.gravityFactor },
2163
- children: [children, plan.colliders.map((col, idx) => /* @__PURE__ */ jsx(KhrCollider, {
2164
- c: col.spec,
2165
- position: col.position,
2166
- quaternion: col.quaternion
2167
- }, `r${idx}`))]
2168
- }), plan.parts.map((part) => /* @__PURE__ */ jsx(ArticulatedPartBody, {
2169
- part,
2170
- rootSpec,
2171
- rootPos: position,
2172
- rootQuat: quaternion,
2173
- parentBody: part.parent ? partBodies.get(part.parent.spec.nodeName) ?? null : rootBody,
2174
- userData: rigidBodyProps?.userData,
2175
- origin: partOrigins?.get(part.spec.object),
2176
- onBody: handlePartBody,
2177
- onJointReady
2178
- }, part.spec.nodeName))] });
2179
- }
2180
- function KhrAssembly({ ref, bundle, position = [
2181
- 0,
2182
- 0,
2183
- 0
2184
- ], rotation, quaternion, scale, fixedRoot = false, enableCustomGravity = false, rigidBodyProps, children, ...groupProps }) {
2185
- assertUnitScale(scale, "KhrAssembly");
2186
- useMemo(() => assertSceneIdentityWorldScale(bundle.scene), [bundle]);
2187
- const { world } = useRapier();
2188
- const applyGravityField = useCustomGravity((s) => s.applyGravityField);
2189
- const plans = useMemo(() => planAssembly(bundle), [bundle]);
2190
- const isStaticMultiRootCompound = useMemo(() => fixedRoot && plans.length > 1 && plans.every((p) => p.spec.isKinematic && p.parts.length === 0), [fixedRoot, plans]);
2191
- const compoundColliders = useMemo(() => {
2192
- if (!isStaticMultiRootCompound) return null;
2193
- const one = new THREE.Vector3(1, 1, 1);
2194
- const pos = new THREE.Vector3(), quat = new THREE.Quaternion(), scl = new THREE.Vector3();
2195
- const out = [];
2196
- for (const p of plans) {
2197
- const bMat = bodyPoseMatrix(p.spec);
2198
- for (let idx = 0; idx < p.spec.colliders.length; idx++) {
2199
- const c = p.spec.colliders[idx];
2200
- const cMat = new THREE.Matrix4().compose(new THREE.Vector3().fromArray(c.localPosition), new THREE.Quaternion().fromArray(c.localQuaternion), one);
2201
- bMat.clone().multiply(cMat).decompose(pos, quat, scl);
2202
- quat.normalize();
2203
- out.push({
2204
- key: `${p.spec.nodeName}:${idx}`,
2205
- spec: c,
2206
- position: [
2207
- pos.x,
2208
- pos.y,
2209
- pos.z
2210
- ],
2211
- quaternion: [
2212
- quat.x,
2213
- quat.y,
2214
- quat.z,
2215
- quat.w
2216
- ]
2217
- });
2218
- }
2219
- }
2220
- return out;
2221
- }, [isStaticMultiRootCompound, plans]);
2222
- const origins = useMemo(() => !isStaticMultiRootCompound && plans.length > 1 ? captureSubtreeOrigins(plans.map((p) => p.mountNode), bundle.scene) : null, [
2223
- isStaticMultiRootCompound,
2224
- plans,
2225
- bundle.scene
2226
- ]);
2227
- const [px, py, pz] = position;
2228
- const [rx, ry, rz] = rotation ?? [
2229
- 0,
2230
- 0,
2231
- 0
2232
- ];
2233
- const hasQuat = quaternion !== void 0;
2234
- const qx = quaternion?.[0] ?? 0;
2235
- const qy = quaternion?.[1] ?? 0;
2236
- const qz = quaternion?.[2] ?? 0;
2237
- const qw = quaternion?.[3] ?? 1;
2238
- const spawnQuat = useMemo(() => hasQuat ? new THREE.Quaternion(qx, qy, qz, qw).normalize() : new THREE.Quaternion().setFromEuler(new THREE.Euler(rx, ry, rz)), [
2239
- hasQuat,
2240
- qx,
2241
- qy,
2242
- qz,
2243
- qw,
2244
- rx,
2245
- ry,
2246
- rz
2247
- ]);
2248
- const rootPoses = useMemo(() => plans.map((p) => {
2249
- const pos = new THREE.Vector3().fromArray(p.spec.localPosition).applyQuaternion(spawnQuat).add(new THREE.Vector3(px, py, pz));
2250
- const quat = spawnQuat.clone().multiply(new THREE.Quaternion().fromArray(p.spec.localQuaternion));
2251
- return {
2252
- position: [
2253
- pos.x,
2254
- pos.y,
2255
- pos.z
2256
- ],
2257
- quaternion: [
2258
- quat.x,
2259
- quat.y,
2260
- quat.z,
2261
- quat.w
2262
- ]
2263
- };
2264
- }), [
2265
- plans,
2266
- spawnQuat,
2267
- px,
2268
- py,
2269
- pz
2270
- ]);
2271
- const bodiesRef = useRef(/* @__PURE__ */ new Map());
2272
- const jointsRef = useRef(/* @__PURE__ */ new Map());
2273
- const byObjectRef = useRef(/* @__PURE__ */ new Map());
2274
- const handleBody = useCallback((nodeName, rb) => {
2275
- if (rb) bodiesRef.current.set(nodeName, rb);
2276
- else bodiesRef.current.delete(nodeName);
2277
- }, []);
2278
- const handleJointReady = useCallback((nodeName, obj, rt) => {
2279
- if (rt) {
2280
- jointsRef.current.set(nodeName, rt);
2281
- byObjectRef.current.set(obj, rt);
2282
- } else {
2283
- jointsRef.current.delete(nodeName);
2284
- byObjectRef.current.delete(obj);
2285
- }
2286
- }, []);
2287
- useFrame(() => {
2288
- if (!enableCustomGravity || world.timestep <= 0) return;
2289
- for (const rb of bodiesRef.current.values()) if (rb.isDynamic()) applyGravityField(rb, world.timestep);
2290
- });
2291
- useImperativeHandle(ref, () => ({
2292
- get jointNames() {
2293
- return [...jointsRef.current.keys()];
2294
- },
2295
- getBody: (nodeName) => bodiesRef.current.get(nodeName) ?? null,
2296
- getJointRuntime: (nodeName) => jointsRef.current.get(nodeName) ?? null,
2297
- setJointTarget: (nodeName, target) => {
2298
- jointsRef.current.get(nodeName)?.applyTarget(target);
2299
- },
2300
- setRoleTarget: (role, target) => {
2301
- for (const rt of jointsRef.current.values()) if (rt.role === role) rt.applyTarget(target);
2302
- },
2303
- toggleHitObject: (hit) => {
2304
- for (let cur = hit; cur; cur = cur.parent) {
2305
- const rt = byObjectRef.current.get(cur);
2306
- if (rt && toggleDrivenJoint(rt)) return true;
2307
- }
2308
- return false;
2309
- }
2310
- }), []);
2311
- const assignCompoundRef = useCallback((rb) => {
2312
- for (const p of plans) if (rb) bodiesRef.current.set(p.spec.nodeName, rb);
2313
- else bodiesRef.current.delete(p.spec.nodeName);
2314
- }, [plans]);
2315
- useEffect(() => {
2316
- if (!isStaticMultiRootCompound) return;
2317
- return () => {
2318
- for (const p of plans) bodiesRef.current.delete(p.spec.nodeName);
2319
- };
2320
- }, [isStaticMultiRootCompound, plans]);
2321
- if (isStaticMultiRootCompound && compoundColliders) return /* @__PURE__ */ jsx("group", {
2322
- ...groupProps,
2323
- children: /* @__PURE__ */ jsxs(RigidBody, {
2324
- ...rigidBodyProps,
2325
- ref: assignCompoundRef,
2326
- type: "fixed",
2327
- position,
2328
- quaternion: [
2329
- spawnQuat.x,
2330
- spawnQuat.y,
2331
- spawnQuat.z,
2332
- spawnQuat.w
2333
- ],
2334
- colliders: false,
2335
- children: [
2336
- /* @__PURE__ */ jsx(BundleSceneHost, {
2337
- bundle,
2338
- owner: "KhrAssembly"
2339
- }),
2340
- children,
2341
- compoundColliders.map((col) => /* @__PURE__ */ jsx(KhrCollider, {
2342
- c: col.spec,
2343
- position: col.position,
2344
- quaternion: col.quaternion
2345
- }, col.key))
2346
- ]
2347
- })
2348
- });
2349
- if (plans.length === 1) {
2350
- const plan = plans[0];
2351
- return /* @__PURE__ */ jsx("group", {
2352
- ...groupProps,
2353
- children: /* @__PURE__ */ jsx(AssemblyRootBody, {
2354
- plan,
2355
- bundleScene: bundle.scene,
2356
- position: rootPoses[0].position,
2357
- quaternion: rootPoses[0].quaternion,
2358
- fixedRoot,
2359
- rigidBodyProps,
2360
- onBody: handleBody,
2361
- onJointReady: handleJointReady,
2362
- children: /* @__PURE__ */ jsxs("group", {
2363
- position: plan.sceneInRootPos,
2364
- quaternion: plan.sceneInRootQuat,
2365
- children: [/* @__PURE__ */ jsx(BundleSceneHost, {
2366
- bundle,
2367
- owner: "KhrAssembly"
2368
- }), children]
2369
- })
2370
- }, `${bundle.scene.uuid}:${plan.spec.nodeName}`)
2371
- });
2372
- }
2373
- return /* @__PURE__ */ jsxs("group", {
2374
- ...groupProps,
2375
- children: [/* @__PURE__ */ jsxs("group", {
2376
- position,
2377
- quaternion: [
2378
- spawnQuat.x,
2379
- spawnQuat.y,
2380
- spawnQuat.z,
2381
- spawnQuat.w
2382
- ],
2383
- children: [/* @__PURE__ */ jsx(BundleSceneHost, {
2384
- bundle,
2385
- owner: "KhrAssembly"
2386
- }), children]
2387
- }), plans.map((plan, idx) => /* @__PURE__ */ jsx(AssemblyRootBody, {
2388
- plan,
2389
- bundleScene: bundle.scene,
2390
- position: rootPoses[idx].position,
2391
- quaternion: rootPoses[idx].quaternion,
2392
- fixedRoot,
2393
- rigidBodyProps,
2394
- onBody: handleBody,
2395
- onJointReady: handleJointReady,
2396
- children: /* @__PURE__ */ jsx(MountSubtreeSlot, {
2397
- object: plan.mountNode,
2398
- position: plan.mountPosition,
2399
- quaternion: plan.mountQuaternion,
2400
- origin: origins?.get(plan.mountNode)
2401
- })
2402
- }, `${bundle.scene.uuid}:${plan.spec.nodeName}`))]
2403
- });
2404
- }
2405
1831
  // src/useKhrGlb.ts
2406
1832
  var KhrGLTFLoader = class extends GLTFLoader {
2407
1833
  static khrLoaderScope = Symbol("KhrGLTFLoader");
@@ -2514,6 +1940,6 @@ function useKhrGlb(url, options) {
2514
1940
  }
2515
1941
  };
2516
1942
  })(useKhrGlb || (useKhrGlb = {}));
2517
- 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, exportKhrGlb, isJointOpen, jacobiPrincipalInertia, loadKhrGlb, measureJointCoordinate, openTargetForLimits, patchGlbJsonChunk, planAssembly, releaseBundleMount, resolveBodyMassFromColliders, sampleRapierHeightfield, snapshotSceneWithMountedSubtrees, toggleDrivenJoint, transposeHeightfieldForRapier, useKhrGlb };
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 };
2518
1944
 
2519
1945
  //# sourceMappingURL=index.js.map