@khr3/core 0.2.1 → 0.2.2

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 CHANGED
@@ -1,16 +1,16 @@
1
1
  # `@khr3/core` (`khr3`)
2
2
 
3
- `KHR_physics_rigid_bodies` + `KHR_implicit_shapes` runtime loader, binary `.glb` exporter, `@react-three/rapier` multi-body assembly synchronizer, and `@pinkaphant/ecctrl` vehicle rig + monotone Pacejka curve presets for React Three Fiber.
3
+ `KHR_physics_rigid_bodies` + `KHR_implicit_shapes` + `OMI_vehicle_wheel` runtime loader, binary `.glb` exporter, `@react-three/rapier` multi-body `ImpulseJoint` assembly & contact-impulse breakaway synchronizer ([src/KhrAssembly.tsx](./src/KhrAssembly.tsx), [src/KhrCollider.tsx](./src/KhrCollider.tsx), [src/PhysicsWorld.tsx](./src/PhysicsWorld.tsx)), and `@pinkaphant/ecctrl` vehicle rig ([src/KhrVehicle.tsx](./src/KhrVehicle.tsx)) + monotone Pacejka curve presets ([src/pacejka.ts](./src/pacejka.ts)) for React Three Fiber.
4
4
 
5
5
  - Full API & configuration reference: [docs/api-reference.md](./docs/api-reference.md)
6
6
  - Runnable example: [example/App.tsx](./example/App.tsx) & [example/headless.ts](./example/headless.ts)
7
7
 
8
8
  ## Subpath Exports
9
9
 
10
- | Subpath | Exports |
11
- | --- | --- |
12
- | `@khr3/core` | `PhysicsWorld`, `createKhrGltfPlugin`, `useKhrGlb`, `loadKhrGlb`, `cloneKhrBundle`, `disposeKhrBundle`, `claimBundleMount`, `releaseBundleMount`, `exportKhrGlb`, `patchGlbJsonChunk`, `KhrCollider`, `KhrAssembly`, `JointRegistryContext`, `assertUnitScale`, `assertIdentityWorldScale`, `assertSceneIdentityWorldScale`, `KhrValidationError`, KHR & rotor tag types |
13
- | `@khr3/core/vehicle` | `KhrVehicle`, `solveWheelLoads`, `KhrPropellerConfig`, `KhrVehicleJointsHandle`, `KhrVehicleProps`, `KhrWheelConfig`, `SolvedWheel` |
14
- | `@khr3/core/curves` | `compilePacejkaCurve`, `tireGripForSurface`, `engineHorsepowerForPeakTorque`, `TIRE_ARCHETYPES`, `TIRE_SLIP_CURVES`, `ENGINE_TORQUE_CURVES`, `STEER_SPEED_CURVES`, `TireArchetypePreset`, `CurveData`, `CurvePoint` |
10
+ | Subpath | Source | Exports |
11
+ | --- | --- | --- |
12
+ | `@khr3/core` | [src/index.ts](./src/index.ts), [src/PhysicsWorld.tsx](./src/PhysicsWorld.tsx), [src/KhrAssembly.tsx](./src/KhrAssembly.tsx), [src/KhrCollider.tsx](./src/KhrCollider.tsx) | `PhysicsWorld`, `createKhrGltfPlugin`, `useKhrGlb`, `loadKhrGlb`, `cloneKhrBundle`, `disposeKhrBundle`, `claimBundleMount`, `releaseBundleMount`, `exportKhrGlb`, `patchGlbJsonChunk`, `KhrCollider`, `KhrAssembly`, `JointRegistryContext`, `assertUnitScale`, `assertIdentityWorldScale`, `assertSceneIdentityWorldScale`, `KhrValidationError`, KHR, `KhrBreakawaySpec` & rotor tag types |
13
+ | `@khr3/core/vehicle` | [src/KhrVehicle.tsx](./src/KhrVehicle.tsx), [src/khrVehicleJoints.tsx](./src/khrVehicleJoints.tsx), [src/wheelLoads.ts](./src/wheelLoads.ts) | `KhrVehicle`, `solveWheelLoads`, `KhrPropellerConfig`, `KhrVehicleJointsHandle`, `KhrVehicleProps`, `KhrWheelConfig`, `SolvedWheel` |
14
+ | `@khr3/core/curves` | [src/curves.ts](./src/curves.ts), [src/pacejka.ts](./src/pacejka.ts) | `compilePacejkaCurve`, `tireGripForSurface`, `engineHorsepowerForPeakTorque`, `TIRE_ARCHETYPES`, `TIRE_SLIP_CURVES`, `ENGINE_TORQUE_CURVES`, `STEER_SPEED_CURVES`, `TireArchetypePreset`, `CurveData`, `CurvePoint` |
15
15
 
16
- See [docs/api-reference.md](./docs/api-reference.md) for the complete API, `.glb` KHR + `extras` schema, custom gravity, transmission, and curve reference.
16
+ See [docs/api-reference.md](./docs/api-reference.md) for the complete API, `.glb` KHR + `OMI_vehicle_wheel` + `extras` schema, custom gravity, transmission, and curve reference.
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  export { PhysicsWorld, type PhysicsWorldProps, type TimeControlValue } from "./PhysicsWorld.js";
2
- export { KhrValidationError, createKhrGltfPlugin, loadKhrGlb, type EcctrlPropellerTag, type EcctrlWheelTag, type KhrAssetBundle, type KhrBodySpec, type KhrCarConfig, type KhrColliderShape, type KhrColliderSpec, type KhrCombineMode, type KhrDroneConfig, type KhrExportNodeSpec, type KhrImplicitShape, type KhrJointDriveSpec, type KhrJointSpec, type KhrPluginOptions, type LoadKhrGlbOptions, type OmiVehicleWheelSpec, type UseKhrGlbOptions, } from "./khrLoader.js";
2
+ export { KhrValidationError, createKhrGltfPlugin, loadKhrGlb, type EcctrlPropellerTag, type EcctrlWheelTag, type KhrAssetBundle, type KhrBodySpec, type KhrBreakawaySpec, type KhrCarConfig, type KhrColliderShape, type KhrColliderSpec, type KhrCombineMode, type KhrDroneConfig, type KhrExportNodeSpec, type KhrImplicitShape, type KhrJointDriveSpec, type KhrJointSpec, type KhrPluginOptions, type LoadKhrGlbOptions, type OmiVehicleWheelSpec, type UseKhrGlbOptions, } from "./khrLoader.js";
3
3
  export { exportKhrGlb, patchGlbJsonChunk } from "./khrExporter.js";
4
4
  export type { MutableGltfJson, RawGltfJson } from "./khrGlb.js";
5
5
  export { claimBundleMount, cloneKhrBundle, disposeKhrBundle, releaseBundleMount } from "./khrClone.js";
package/dist/index.js CHANGED
@@ -1,13 +1,23 @@
1
- import { C as extractMeshColliderShape, E as KhrValidationError, S as computeRelativePose, T as readUnitQuat, _ as assertIdentityWorldScale, a as MountSubtreeSlot, b as computeAssemblyMassAndCom, c as planAssembly, f as KhrCollider, g as releaseBundleMount, h as disposeKhrBundle, i as BundleSceneHost, m as cloneKhrBundle, n as buildJointRuntime, o as captureSubtreeOrigins, p as claimBundleMount, r as createRapierImpulseJoint, t as JointRegistryContext, v as assertSceneIdentityWorldScale, w as readFiniteVec3, x as computeBodyInertia, y as assertUnitScale } from "./physicsEcs-9H-Do7eX.js";
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";
2
2
  import { useCallback, useContext, useEffect, useImperativeHandle, useMemo, useRef } from "react";
3
+ import { useFrame, useLoader } from "@react-three/fiber";
3
4
  import { Physics, RigidBody, useRapier } from "@react-three/rapier";
4
5
  import { TimeControl } from "@pinkaphant/ecctrl/time";
5
6
  import { Fragment, jsx, jsxs } from "react/jsx-runtime";
6
7
  import { GLTFLoader } from "three/examples/jsm/loaders/GLTFLoader.js";
7
8
  import * as THREE from "three";
8
9
  import { GLTFExporter } from "three/examples/jsm/exporters/GLTFExporter.js";
9
- import { useLoader } from "@react-three/fiber";
10
10
  // src/PhysicsWorld.tsx
11
+ var readVal = (v) => typeof v === "number" ? v : v.current;
12
+ function TimestepPresync({ paused, timeScale, maxDelta }) {
13
+ const { world } = useRapier();
14
+ useFrame((_, delta) => {
15
+ const md = Math.max(0, readVal(maxDelta));
16
+ const ts = readVal(timeScale);
17
+ world.timestep = !paused && ts > 0 && md > 0 && delta > 0 ? Math.min(delta, md) * ts : 0;
18
+ });
19
+ return null;
20
+ }
11
21
  function PhysicsWorld({ paused = false, timeScale = 1, maxDelta = 1 / 30, gravity = [
12
22
  0,
13
23
  -9.81,
@@ -18,11 +28,19 @@ function PhysicsWorld({ paused = false, timeScale = 1, maxDelta = 1 / 30, gravit
18
28
  paused: true,
19
29
  timeStep: "vary",
20
30
  gravity,
21
- children: [children, /* @__PURE__ */ jsx(TimeControl, {
22
- paused,
23
- timeScale,
24
- maxDelta
25
- })]
31
+ children: [
32
+ /* @__PURE__ */ jsx(TimestepPresync, {
33
+ paused,
34
+ timeScale,
35
+ maxDelta
36
+ }),
37
+ children,
38
+ /* @__PURE__ */ jsx(TimeControl, {
39
+ paused,
40
+ timeScale,
41
+ maxDelta
42
+ })
43
+ ]
26
44
  });
27
45
  }
28
46
  // src/khrGlb.ts
@@ -286,6 +304,16 @@ function decodePropellerTag(obj, name, rawPropeller, scene) {
286
304
  };
287
305
  }
288
306
  // src/khrExtras.ts
307
+ function decodeBreakawaySpec(raw) {
308
+ if (!raw || typeof raw !== "object" || Array.isArray(raw)) return void 0;
309
+ const rec = raw;
310
+ if (typeof rec.breakImpulseNs !== "number" || !Number.isFinite(rec.breakImpulseNs) || rec.breakImpulseNs <= 0) return void 0;
311
+ const latch = typeof rec.latchImpulseNs === "number" && Number.isFinite(rec.latchImpulseNs) && rec.latchImpulseNs > 0 ? rec.latchImpulseNs : void 0;
312
+ return {
313
+ ...latch === void 0 ? {} : { latchImpulseNs: latch },
314
+ breakImpulseNs: rec.breakImpulseNs
315
+ };
316
+ }
289
317
  var templateMetaMap = /* @__PURE__ */ new WeakMap();
290
318
  function registerKhrTemplateMeta(scene, meta) {
291
319
  templateMetaMap.set(scene, meta);
@@ -1027,6 +1055,7 @@ function extractKhrBundleFromGltf(gltf, options) {
1027
1055
  }
1028
1056
  const wp = obj.getWorldPosition(new THREE.Vector3());
1029
1057
  const wq = obj.getWorldQuaternion(new THREE.Quaternion());
1058
+ const breakaway = decodeBreakawaySpec(extras.breakaway);
1030
1059
  const spec = {
1031
1060
  nodeName: name,
1032
1061
  ...motion,
@@ -1043,6 +1072,7 @@ function extractKhrBundleFromGltf(gltf, options) {
1043
1072
  ],
1044
1073
  colliders: [],
1045
1074
  joint,
1075
+ ...breakaway ? { breakaway } : {},
1046
1076
  object: obj,
1047
1077
  role
1048
1078
  };
@@ -1553,6 +1583,7 @@ function ArticulatedPartBody({ part, rootPos, rootQuat, parentBodyRef, origin, o
1553
1583
  const { rapier, world } = useRapier();
1554
1584
  const registry = useContext(JointRegistryContext);
1555
1585
  const childRef = useRef(null);
1586
+ const rtRef = useRef(null);
1556
1587
  const worldPose = useMemo(() => {
1557
1588
  const rq = new THREE.Quaternion(rootQuat[0], rootQuat[1], rootQuat[2], rootQuat[3]);
1558
1589
  const pos = new THREE.Vector3().fromArray(part.restRelRootPos).applyQuaternion(rq).add(new THREE.Vector3(rootPos[0], rootPos[1], rootPos[2]));
@@ -1599,6 +1630,7 @@ function ArticulatedPartBody({ part, rootPos, rootQuat, parentBodyRef, origin, o
1599
1630
  ], part.axisParent, part.axisChild, part.restRelParentQuat);
1600
1631
  const rt = buildJointRuntime({
1601
1632
  rapier,
1633
+ world,
1602
1634
  nodeName: part.spec.nodeName,
1603
1635
  role: part.joint.role,
1604
1636
  kind: part.joint.kind,
@@ -1608,16 +1640,19 @@ function ArticulatedPartBody({ part, rootPos, rootQuat, parentBodyRef, origin, o
1608
1640
  restRelQuat: part.restRelParentQuat,
1609
1641
  limits: part.joint.limits,
1610
1642
  drive: part.joint.drive,
1643
+ breakaway: part.spec.breakaway,
1611
1644
  rapierJoint: imp,
1612
1645
  parentBody,
1613
1646
  childBody
1614
1647
  });
1648
+ rtRef.current = rt;
1615
1649
  registry?.add(rt);
1616
1650
  onJointReady(rt.nodeName, rt);
1617
1651
  return () => {
1652
+ rtRef.current = null;
1618
1653
  registry?.delete(rt);
1619
1654
  onJointReady(rt.nodeName, null);
1620
- if (world.getImpulseJoint(imp.handle)) world.removeImpulseJoint(imp, true);
1655
+ if (rt.rapierJoint && world.getImpulseJoint(rt.rapierJoint.handle)) world.removeImpulseJoint(rt.rapierJoint, true);
1621
1656
  };
1622
1657
  }, [
1623
1658
  rapier,
@@ -1627,12 +1662,16 @@ function ArticulatedPartBody({ part, rootPos, rootQuat, parentBodyRef, origin, o
1627
1662
  registry,
1628
1663
  onJointReady
1629
1664
  ]);
1665
+ const onForce = part.spec.breakaway ? (p) => {
1666
+ rtRef.current?.applyImpactImpulse(p.totalForceMagnitude * Math.max(world.timestep, 1 / 240));
1667
+ } : void 0;
1630
1668
  return /* @__PURE__ */ jsxs(RigidBody, {
1631
1669
  ref: childRef,
1632
1670
  type: part.spec.isKinematic ? "kinematicPosition" : "dynamic",
1633
1671
  position: worldPose.pos,
1634
1672
  quaternion: worldPose.quat,
1635
1673
  colliders: false,
1674
+ onContactForce: onForce,
1636
1675
  children: [/* @__PURE__ */ jsx(MountSubtreeSlot, {
1637
1676
  object: part.spec.object,
1638
1677
  position: [