@khr3/core 0.2.4 → 0.2.6

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,4 +1,4 @@
1
- import { C as extractMeshColliderShape, E as KhrValidationError, S as computeRelativePose, T as readUnitQuat, _ as assertIdentityWorldScale, a as MountSubtreeSlot, b as computeAssemblyMassAndCom, c as planAssembly, f as KhrCollider, g as releaseBundleMount, h as disposeKhrBundle, i as BundleSceneHost, m as cloneKhrBundle, n as buildJointRuntime, o as captureSubtreeOrigins, p as claimBundleMount, r as createRapierImpulseJoint, t as JointRegistryContext, v as assertSceneIdentityWorldScale, w as readFiniteVec3, x as computeBodyInertia, y as assertUnitScale } from "./physicsEcs-B9XGoZtl.js";
1
+ import { C as computeRelativePose, D as KhrValidationError, E as readUnitQuat, S as computeBodyInertia, T as readFiniteVec3, _ as releaseBundleMount, a as MountSubtreeSlot, b as assertUnitScale, c as planAssembly, f as KhrCollider, h as disposeKhrBundle, i as BundleSceneHost, m as cloneKhrBundle, n as buildJointRuntime, o as captureSubtreeOrigins, p as claimBundleMount, r as createRapierImpulseJoint, t as JointRegistryContext, v as assertIdentityWorldScale, w as extractMeshColliderShape, x as computeAssemblyMassAndCom, y as assertSceneIdentityWorldScale } from "./physicsEcs-dBq4psbC.js";
2
2
  import { useCallback, useContext, useEffect, useImperativeHandle, useMemo, useRef } from "react";
3
3
  import { useFrame, useLoader } from "@react-three/fiber";
4
4
  import { Physics, RigidBody, useRapier } from "@react-three/rapier";
@@ -889,6 +889,12 @@ function findOwnerBodyIdx(idx, parentIdx, rawNodes) {
889
889
  for (let p = parentIdx[idx]; p !== -1; p = parentIdx[p]) if (rawNodes[p].extensions?.KHR_physics_rigid_bodies?.motion) return p;
890
890
  return -1;
891
891
  }
892
+ function resolveConnectedBodyIdx(cIdx, parentIdx, rawNodes) {
893
+ const phys = rawNodes[cIdx]?.extensions?.KHR_physics_rigid_bodies;
894
+ if (isPlainObject(phys) && (phys.motion !== void 0 || (phys.collider !== void 0 || phys.joint !== void 0) && findOwnerBodyIdx(cIdx, parentIdx, rawNodes) === -1)) return cIdx;
895
+ const owner = findOwnerBodyIdx(cIdx, parentIdx, rawNodes);
896
+ return owner !== -1 ? owner : cIdx;
897
+ }
892
898
  function isInsideRotor(idx, parentIdx, rawNodes) {
893
899
  for (let p = idx; p !== -1; p = parentIdx[p]) {
894
900
  const node = rawNodes[p];
@@ -1046,11 +1052,11 @@ function extractKhrBundleFromGltf(gltf, options) {
1046
1052
  if (typeof rawJ.joint !== "number" || !Number.isInteger(rawJ.joint)) throw new KhrValidationError(`Joint on "${name}" is missing numeric joint index`);
1047
1053
  if (typeof rawJ.connectedNode !== "number" || !Number.isInteger(rawJ.connectedNode)) throw new KhrValidationError(`Joint on "${name}" is missing numeric connectedNode index`);
1048
1054
  if (rawJ.enableCollision !== void 0 && typeof rawJ.enableCollision !== "boolean") throw new KhrValidationError(`Joint on "${name}" has non-boolean enableCollision`);
1049
- const cIdx = rawJ.connectedNode;
1055
+ const cIdx = resolveConnectedBodyIdx(rawJ.connectedNode, parentIdx, rawNodes);
1050
1056
  const cName = nodeNames[cIdx] ?? `node_${cIdx}`;
1051
1057
  const cObj = resolveObj(cIdx);
1052
- if (!cObj) throw new KhrValidationError(`Joint on "${name}" targets missing connectedNode "${cName}"`);
1053
- if (isInsideRotor(cIdx, parentIdx, rawNodes)) throw new KhrValidationError(`Joint on "${name}" cannot target detached rotor "${cName}"`);
1058
+ if (!cObj || !resolveObj(rawJ.connectedNode)) throw new KhrValidationError(`Joint on "${name}" targets missing connectedNode "${cName}"`);
1059
+ if (isInsideRotor(rawJ.connectedNode, parentIdx, rawNodes) || isInsideRotor(cIdx, parentIdx, rawNodes)) throw new KhrValidationError(`Joint on "${name}" cannot target detached rotor "${cName}"`);
1054
1060
  joint = decodeJoint(joints, rawJ.joint, name, cObj, obj, Boolean(rawJ.enableCollision), role, cName);
1055
1061
  }
1056
1062
  const wp = obj.getWorldPosition(new THREE.Vector3());
@@ -1579,15 +1585,14 @@ async function exportKhrGlb(root, specs, sceneExtras) {
1579
1585
  });
1580
1586
  }
1581
1587
  // src/AssemblyRootBody.tsx
1582
- function ArticulatedPartBody({ part, rootPos, rootQuat, parentBodyRef, origin, onBody, onJointReady }) {
1588
+ function ArticulatedPartBody({ part, rootSpec, rootPos, rootQuat, parentBodyRef, origin, onBody, onJointReady }) {
1583
1589
  const { rapier, world } = useRapier();
1584
1590
  const registry = useContext(JointRegistryContext);
1585
1591
  const childRef = useRef(null);
1586
1592
  const rtRef = useRef(null);
1587
1593
  const worldPose = useMemo(() => {
1588
- const rq = new THREE.Quaternion(rootQuat[0], rootQuat[1], rootQuat[2], rootQuat[3]);
1594
+ const rq = new THREE.Quaternion(rootQuat[0], rootQuat[1], rootQuat[2], rootQuat[3]).normalize();
1589
1595
  const pos = new THREE.Vector3().fromArray(part.restRelRootPos).applyQuaternion(rq).add(new THREE.Vector3(rootPos[0], rootPos[1], rootPos[2]));
1590
- const quat = rq.clone().multiply(new THREE.Quaternion().fromArray(part.restRelRootQuat)).normalize();
1591
1596
  return {
1592
1597
  pos: [
1593
1598
  pos.x,
@@ -1595,15 +1600,14 @@ function ArticulatedPartBody({ part, rootPos, rootQuat, parentBodyRef, origin, o
1595
1600
  pos.z
1596
1601
  ],
1597
1602
  quat: [
1598
- quat.x,
1599
- quat.y,
1600
- quat.z,
1601
- quat.w
1603
+ rq.x,
1604
+ rq.y,
1605
+ rq.z,
1606
+ rq.w
1602
1607
  ]
1603
1608
  };
1604
1609
  }, [
1605
1610
  part.restRelRootPos,
1606
- part.restRelRootQuat,
1607
1611
  rootPos,
1608
1612
  rootQuat
1609
1613
  ]);
@@ -1637,7 +1641,12 @@ function ArticulatedPartBody({ part, rootPos, rootQuat, parentBodyRef, origin, o
1637
1641
  object: part.spec.object,
1638
1642
  axisParent: part.axisParent,
1639
1643
  anchorParent: part.restRelParentPos,
1640
- restRelQuat: part.restRelParentQuat,
1644
+ restRelQuat: [
1645
+ 0,
1646
+ 0,
1647
+ 0,
1648
+ 1
1649
+ ],
1641
1650
  limits: part.joint.limits,
1642
1651
  drive: part.joint.drive,
1643
1652
  breakaway: part.spec.breakaway,
@@ -1665,6 +1674,9 @@ function ArticulatedPartBody({ part, rootPos, rootQuat, parentBodyRef, origin, o
1665
1674
  const onForce = part.spec.breakaway ? (p) => {
1666
1675
  rtRef.current?.applyImpactImpulse(p.totalForceMagnitude * Math.max(world.timestep, 1 / 240));
1667
1676
  } : void 0;
1677
+ const linVel = part.spec.linearVelocity ?? rootSpec.linearVelocity;
1678
+ const angVel = part.spec.angularVelocity ?? rootSpec.angularVelocity;
1679
+ const gravScale = part.spec.gravityFactor ?? rootSpec.gravityFactor;
1668
1680
  return /* @__PURE__ */ jsxs(RigidBody, {
1669
1681
  ref: childRef,
1670
1682
  type: part.spec.isKinematic ? "kinematicPosition" : "dynamic",
@@ -1672,6 +1684,9 @@ function ArticulatedPartBody({ part, rootPos, rootQuat, parentBodyRef, origin, o
1672
1684
  quaternion: worldPose.quat,
1673
1685
  colliders: false,
1674
1686
  onContactForce: onForce,
1687
+ ...linVel ? { linearVelocity: linVel } : {},
1688
+ ...angVel ? { angularVelocity: angVel } : {},
1689
+ ...gravScale === void 0 ? {} : { gravityScale: gravScale },
1675
1690
  children: [/* @__PURE__ */ jsx(MountSubtreeSlot, {
1676
1691
  object: part.spec.object,
1677
1692
  position: [
@@ -1679,12 +1694,7 @@ function ArticulatedPartBody({ part, rootPos, rootQuat, parentBodyRef, origin, o
1679
1694
  0,
1680
1695
  0
1681
1696
  ],
1682
- quaternion: [
1683
- 0,
1684
- 0,
1685
- 0,
1686
- 1
1687
- ],
1697
+ quaternion: part.restRelRootQuat,
1688
1698
  origin
1689
1699
  }), part.colliders.map((col, cIdx) => /* @__PURE__ */ jsx(KhrCollider, {
1690
1700
  c: col.spec,
@@ -1748,6 +1758,7 @@ function AssemblyRootBody({ plan, position, quaternion, fixedRoot, rigidBodyProp
1748
1758
  }, `r${idx}`))]
1749
1759
  }), plan.parts.map((part) => /* @__PURE__ */ jsx(ArticulatedPartBody, {
1750
1760
  part,
1761
+ rootSpec,
1751
1762
  rootPos: position,
1752
1763
  rootQuat: quaternion,
1753
1764
  parentBodyRef: part.parent ? partBodyRefs.get(part.parent.spec.nodeName) : rbRef,