@khr3/core 0.2.8 → 0.2.10

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.
Files changed (40) hide show
  1. package/dist/KhrVehicle.d.ts +1 -1
  2. package/dist/curves.js +542 -1
  3. package/dist/curves.js.map +1 -0
  4. package/dist/index.d.ts +0 -1
  5. package/dist/index.js +1558 -4
  6. package/dist/index.js.map +1 -1
  7. package/dist/khrVehicleJointHelpers.d.ts +3 -4
  8. package/dist/khrVehicleJoints.d.ts +2 -0
  9. package/dist/{physicsEcs-C00N5zCh.js → physicsEcs-Bi0ikONC.js} +249 -7
  10. package/dist/physicsEcs-Bi0ikONC.js.map +1 -0
  11. package/dist/vehicle.js +91 -114
  12. package/dist/vehicle.js.map +1 -1
  13. package/package.json +5 -12
  14. package/AGENTS.md +0 -12
  15. package/CLAUDE.md +0 -8
  16. package/README.md +0 -178
  17. package/dist/curves-Cf8okB-M.js +0 -542
  18. package/dist/curves-Cf8okB-M.js.map +0 -1
  19. package/dist/khrInertia-DgCUCDTx.js +0 -247
  20. package/dist/khrInertia-DgCUCDTx.js.map +0 -1
  21. package/dist/physicsEcs-C00N5zCh.js.map +0 -1
  22. package/dist/sand3d/sand3dAssemblyHelpers.d.ts +0 -30
  23. package/dist/sand3d/sand3dCavityOps.d.ts +0 -15
  24. package/dist/sand3d/sand3dCompiler.d.ts +0 -121
  25. package/dist/sand3d/sand3dEvaluator.d.ts +0 -80
  26. package/dist/sand3d/sand3dFramesAndEdits.d.ts +0 -30
  27. package/dist/sand3d/sand3dHullAndSweep.d.ts +0 -27
  28. package/dist/sand3d/sand3dParser.d.ts +0 -6
  29. package/dist/sand3d/sand3dPrimitives.d.ts +0 -50
  30. package/dist/sand3d/sand3dProfiles.d.ts +0 -32
  31. package/dist/sand3d/sand3dRecipes.d.ts +0 -5
  32. package/dist/sand3d/sand3dShellOp.d.ts +0 -8
  33. package/dist/sand3d/sand3dSolids.d.ts +0 -32
  34. package/dist/sand3d/sand3dTokenizer.d.ts +0 -8
  35. package/dist/sand3d/sand3dTypes.d.ts +0 -157
  36. package/dist/sand3d-DQ8-OMlo.js +0 -5045
  37. package/dist/sand3d-DQ8-OMlo.js.map +0 -1
  38. package/dist/sand3d.d.ts +0 -23
  39. package/dist/sand3d.js +0 -1
  40. package/docs/api-reference.md +0 -379
package/dist/vehicle.js CHANGED
@@ -1,12 +1,11 @@
1
- import { a as MountSubtreeSlot, d as computeLumpedMassProps, f as KhrCollider, g as registerMountedSubtree, i as BundleSceneHost, l as toColliderMass, n as buildJointRuntime, o as captureSubtreeOrigins, r as createRapierImpulseJoint, s as restoreSubtreeOrigin, t as JointRegistryContext, u as bodyPoseMatrix } from "./physicsEcs-C00N5zCh.js";
2
- import { n as assertSceneIdentityWorldScale, r as assertUnitScale, u as KhrValidationError } from "./khrInertia-DgCUCDTx.js";
3
1
  import { useCallback, useContext, useEffect, useImperativeHandle, useLayoutEffect, useMemo, useRef, useState } from "react";
4
- import { useFrame } from "@react-three/fiber";
5
2
  import { RigidBody, useRapier } from "@react-three/rapier";
6
- import { Fragment, jsx, jsxs } from "react/jsx-runtime";
7
- import * as THREE from "three";
8
3
  import { EcctrlVehicle, ShapeCastWheel, ThrustPropeller } from "@pinkaphant/ecctrl/vehicle";
4
+ import { D as KhrValidationError, a as MountSubtreeSlot, b as assertUnitScale, d as computeLumpedMassProps, f as KhrCollider, g as registerMountedSubtree, i as BundleSceneHost, l as toColliderMass, n as buildJointRuntime, o as captureSubtreeOrigins, r as createRapierImpulseJoint, s as restoreSubtreeOrigin, t as JointRegistryContext, u as bodyPoseMatrix, y as assertSceneIdentityWorldScale } from "./physicsEcs-Bi0ikONC.js";
5
+ import * as THREE from "three";
6
+ import { useFrame } from "@react-three/fiber";
9
7
  import { useCustomGravity } from "@pinkaphant/ecctrl/gravity";
8
+ import { Fragment, jsx, jsxs } from "react/jsx-runtime";
10
9
  // src/khrVehicleChassis.ts
11
10
  function assertVehicleProps(scale, colliders) {
12
11
  if (colliders !== void 0) throw new KhrValidationError("KhrVehicle: colliders prop cannot be overridden; edit colliders in the .glb asset");
@@ -185,7 +184,7 @@ function computeRotorLocalMatrix(node, scene, center, quaternion) {
185
184
  // src/khrVehicleJointHelpers.ts
186
185
  /** Membership group 1 (0x0002), filter ~0x0002 (0xfffd): collides with world/actors, never with sibling attached vehicle panels. */
187
186
  var ATTACHED_PANEL_COLLISION_GROUPS = 196605;
188
- var DETACHED_PANEL_COLLISION_GROUPS = 4294967295;
187
+ var DETACHED_PANEL_COLLISION_GROUPS = 0xffffffff;
189
188
  function planVehicleJointParts(bodies) {
190
189
  const dyn = bodies.filter((b) => !b.isKinematic);
191
190
  const byName = new Map(dyn.map((b) => [b.nodeName, b]));
@@ -201,11 +200,8 @@ function planVehicleJointParts(bodies) {
201
200
  };
202
201
  jointed.sort((a, b) => depthOf(a) - depthOf(b));
203
202
  const one = new THREE.Vector3(1, 1, 1);
204
- const rpPos = new THREE.Vector3();
205
- const rpQuat = new THREE.Quaternion();
206
- const rcPos = new THREE.Vector3();
207
- const rcQuat = new THREE.Quaternion();
208
- const scl = new THREE.Vector3();
203
+ const rpPos = new THREE.Vector3(), rpQuat = new THREE.Quaternion();
204
+ const rcPos = new THREE.Vector3(), rcQuat = new THREE.Quaternion(), scl = new THREE.Vector3();
209
205
  const partByName = /* @__PURE__ */ new Map();
210
206
  return jointed.map((b) => {
211
207
  const joint = b.joint;
@@ -308,66 +304,61 @@ var _pQuat = new THREE.Quaternion();
308
304
  var _rQuat = new THREE.Quaternion();
309
305
  var _cQuat = new THREE.Quaternion();
310
306
  var _anchor = new THREE.Vector3();
311
- var _gravSample = new THREE.Vector3();
307
+ function shiftAttachedPanels(parts, bodyMap, runtimeMap, dx, dy, dz) {
308
+ for (const part of parts) {
309
+ const rb = bodyMap.get(part.spec.nodeName);
310
+ const rt = runtimeMap.get(part.spec.nodeName);
311
+ if (!rb || rt?.detached) continue;
312
+ const t = rb.translation();
313
+ rb.setTranslation({
314
+ x: t.x + dx,
315
+ y: t.y + dy,
316
+ z: t.z + dz
317
+ }, true);
318
+ }
319
+ }
320
+ function syncAttachedPanelsToParent(parts, bodyMap, runtimeMap, parentBody) {
321
+ if (!parentBody) return;
322
+ const pt = parentBody.translation(), pr = parentBody.rotation(), lv = parentBody.linvel(), av = parentBody.angvel();
323
+ _pPos.set(pt.x, pt.y, pt.z);
324
+ _pQuat.set(pr.x, pr.y, pr.z, pr.w);
325
+ for (const part of parts) {
326
+ const rb = bodyMap.get(part.spec.nodeName), rt = runtimeMap.get(part.spec.nodeName);
327
+ if (!rb || rt?.detached) continue;
328
+ _anchor.fromArray(part.restRelChassisPos).applyQuaternion(_pQuat).add(_pPos);
329
+ _cQuat.copy(_pQuat).multiply(_rQuat.fromArray(part.restRelChassisQuat)).normalize();
330
+ rb.setTranslation({
331
+ x: _anchor.x,
332
+ y: _anchor.y,
333
+ z: _anchor.z
334
+ }, true);
335
+ rb.setRotation({
336
+ x: _cQuat.x,
337
+ y: _cQuat.y,
338
+ z: _cQuat.z,
339
+ w: _cQuat.w
340
+ }, true);
341
+ rb.setLinvel(lv, true);
342
+ rb.setAngvel(av, true);
343
+ }
344
+ }
312
345
  function stepVehicleArticulatedPanels(params) {
313
- const { parts, bodyMap, runtimeMap, detachedCleared, parentBody, enableCustomGravity, gravityScale, timestep, gravityField } = params;
346
+ const { parts, bodyMap, runtimeMap, detachedCleared, enableCustomGravity, timestep, applyGravityField } = params;
314
347
  for (const part of parts) {
315
348
  const rb = bodyMap.get(part.spec.nodeName);
316
349
  if (!rb) continue;
317
- if (runtimeMap.get(part.spec.nodeName)?.detached) {
318
- if (!detachedCleared.has(rb)) {
319
- detachedCleared.add(rb);
320
- rb.userData = {};
321
- const n = rb.numColliders();
322
- for (let i = 0; i < n; i++) if (part.spec.colliders[i]?.collisionGroups === void 0) rb.collider(i).setCollisionGroups(DETACHED_PANEL_COLLISION_GROUPS);
323
- }
324
- } else if (!part.spec.isKinematic) {
325
- const pRb = part.parent ? bodyMap.get(part.parent.spec.nodeName) ?? null : parentBody;
326
- if (pRb) {
327
- const pt = pRb.translation();
328
- const pr = pRb.rotation();
329
- const ct = rb.translation();
330
- _pQuat.set(pr.x, pr.y, pr.z, pr.w);
331
- _anchor.set(part.restRelParentPos[0], part.restRelParentPos[1], part.restRelParentPos[2]).applyQuaternion(_pQuat).add(_pPos.set(pt.x, pt.y, pt.z));
332
- const dx = ct.x - _anchor.x, dy = ct.y - _anchor.y, dz = ct.z - _anchor.z;
333
- const maxDrift = 1.5 + (part.joint.kind === "prismatic" && part.joint.limits ? Math.abs(part.joint.limits[0]) + Math.abs(part.joint.limits[1]) : 0);
334
- if (dx * dx + dy * dy + dz * dz > maxDrift * maxDrift) {
335
- const rrq = part.restRelParentQuat;
336
- _cQuat.copy(_pQuat).multiply(_rQuat.set(rrq[0], rrq[1], rrq[2], rrq[3])).normalize();
337
- rb.setTranslation({
338
- x: _anchor.x,
339
- y: _anchor.y,
340
- z: _anchor.z
341
- }, true);
342
- rb.setRotation({
343
- x: _cQuat.x,
344
- y: _cQuat.y,
345
- z: _cQuat.z,
346
- w: _cQuat.w
347
- }, true);
348
- rb.setLinvel(pRb.linvel(), true);
349
- rb.setAngvel(pRb.angvel(), true);
350
- }
351
- }
352
- }
353
- if (enableCustomGravity && timestep > 0 && gravityField && rb.isDynamic() && !rb.isSleeping()) {
354
- const effScale = part.spec.gravityFactor ?? gravityScale ?? 1;
355
- if (effScale !== 0) {
356
- const ct = rb.translation();
357
- const g = gravityField(_gravSample.set(ct.x, ct.y, ct.z));
358
- const s = rb.mass() * effScale * timestep;
359
- rb.applyImpulse({
360
- x: g.x * s,
361
- y: g.y * s,
362
- z: g.z * s
363
- }, false);
364
- }
350
+ if (runtimeMap.get(part.spec.nodeName)?.detached && !detachedCleared.has(rb)) {
351
+ detachedCleared.add(rb);
352
+ rb.userData = {};
353
+ const n = rb.numColliders();
354
+ for (let i = 0; i < n; i++) if (part.spec.colliders[i]?.collisionGroups === void 0) rb.collider(i).setCollisionGroups(DETACHED_PANEL_COLLISION_GROUPS);
365
355
  }
356
+ if (enableCustomGravity && timestep > 0 && applyGravityField && rb.isDynamic()) applyGravityField(rb, timestep);
366
357
  }
367
358
  }
368
359
  // src/khrVehicleJoints.tsx
369
360
  var ATTACHED_PANEL_USER_DATA = { ecctrl: { excludeVehicleRay: true } };
370
- function VehicleArticulatedPanel({ part, spawnPos, spawnQuat, enableCustomGravity, linearVelocity, angularVelocity, gravityScale, parentBodyGetter, origin, onBody, onJointReady }) {
361
+ function VehicleArticulatedPanel({ part, spawnPos, spawnQuat, linearVelocity, angularVelocity, gravityScale, parentBodyGetter, origin, onBody, onJointReady }) {
371
362
  const { rapier, world } = useRapier();
372
363
  const registry = useContext(JointRegistryContext);
373
364
  const childRef = useRef(null);
@@ -400,16 +391,7 @@ function VehicleArticulatedPanel({ part, spawnPos, spawnQuat, enableCustomGravit
400
391
  return () => onBody(part.spec.nodeName, null);
401
392
  }, [part.spec.nodeName, onBody]);
402
393
  useEffect(() => {
403
- const rb = childRef.current;
404
- const pm = part.partMass;
405
- if (rb && pm) {
406
- rb.setAdditionalMassProperties(pm.mass, pm.centerOfMass, pm.principalAngularInertia, pm.angularInertiaLocalFrame, true);
407
- rb.recomputeMassPropertiesFromColliders();
408
- }
409
- }, [part.partMass]);
410
- useEffect(() => {
411
- const parentBody = parentBodyGetterRef.current();
412
- const childBody = childRef.current;
394
+ const parentBody = parentBodyGetterRef.current(), childBody = childRef.current;
413
395
  if (!parentBody || !childBody) return;
414
396
  const imp = createRapierImpulseJoint(rapier, world, parentBody, childBody, part.joint, part.restRelParentPos, [
415
397
  0,
@@ -459,7 +441,7 @@ function VehicleArticulatedPanel({ part, spawnPos, spawnQuat, enableCustomGravit
459
441
  } : void 0;
460
442
  const linVel = part.spec.linearVelocity ?? linearVelocity;
461
443
  const angVel = part.spec.angularVelocity ?? angularVelocity;
462
- const gravScale = enableCustomGravity ? 0 : part.spec.gravityFactor ?? gravityScale;
444
+ const gravScale = part.spec.gravityFactor ?? gravityScale;
463
445
  return /* @__PURE__ */ jsxs(RigidBody, {
464
446
  ref: childRef,
465
447
  type: part.spec.isKinematic ? "kinematicPosition" : "dynamic",
@@ -490,6 +472,7 @@ function VehicleArticulatedPanel({ part, spawnPos, spawnQuat, enableCustomGravit
490
472
  }
491
473
  function KhrVehicleJoints({ bodies, vehicleRef, spawnPos, spawnQuat, enableCustomGravity = false, linearVelocity, angularVelocity, gravityScale, jointsRef }) {
492
474
  const { world } = useRapier();
475
+ const applyGravityField = useCustomGravity((state) => state.applyGravityField);
493
476
  const parts = useMemo(() => planVehicleJointParts(bodies), [bodies]);
494
477
  const origins = useMemo(() => parts.length > 0 ? captureSubtreeOrigins(parts.map((p) => p.spec.object)) : null, [parts]);
495
478
  const bodyMapRef = useRef(/* @__PURE__ */ new Map());
@@ -503,11 +486,9 @@ function KhrVehicleJoints({ bodies, vehicleRef, spawnPos, spawnQuat, enableCusto
503
486
  bodyMap: bodyMapRef.current,
504
487
  runtimeMap: runtimeMapRef.current,
505
488
  detachedCleared: detachedClearedRef.current,
506
- parentBody: vehicleRef.current?.body ?? null,
507
489
  enableCustomGravity,
508
- gravityScale,
509
490
  timestep: world.timestep,
510
- gravityField: enableCustomGravity ? useCustomGravity.getState().gravityField : void 0
491
+ applyGravityField: enableCustomGravity ? applyGravityField : void 0
511
492
  });
512
493
  });
513
494
  const handleBody = useCallback((nodeName, rb) => {
@@ -555,14 +536,15 @@ function KhrVehicleJoints({ bodies, vehicleRef, spawnPos, spawnQuat, enableCusto
555
536
  if (matches.length === 0) return;
556
537
  const anyOpen = matches.some((rt) => isVehicleJointOpen(rt));
557
538
  for (const rt of matches) rt.applyTarget(anyOpen ? 0 : openTargetForLimits(rt.limits));
558
- }
559
- }), [parts]);
539
+ },
540
+ shiftBodies: (dx, dy, dz) => shiftAttachedPanels(parts, bodyMapRef.current, runtimeMapRef.current, dx, dy, dz),
541
+ syncBodiesToParent: () => syncAttachedPanelsToParent(parts, bodyMapRef.current, runtimeMapRef.current, vehicleRef.current?.body ?? null)
542
+ }), [parts, vehicleRef]);
560
543
  if (parts.length === 0) return null;
561
544
  return /* @__PURE__ */ jsx(Fragment, { children: parts.map((part) => /* @__PURE__ */ jsx(VehicleArticulatedPanel, {
562
545
  part,
563
546
  spawnPos,
564
547
  spawnQuat,
565
- enableCustomGravity,
566
548
  linearVelocity,
567
549
  angularVelocity,
568
550
  gravityScale,
@@ -924,7 +906,6 @@ function KhrVehicle({ scale, colliders, bundle, ...rest }) {
924
906
  assertSceneIdentityWorldScale(bundle.scene);
925
907
  const { ref, vehicleRef, jointsRef, position, rotation, quaternion, enable = true, enableCustomGravity = false, carConfig, droneConfig, wheelConfig, frontWheelConfig, rearWheelConfig, propellerConfig, linearVelocity, angularVelocity, gravityScale, children, ...ecctrlRest } = rest;
926
908
  const { world } = useRapier();
927
- const [rotorsMounted, setRotorsMounted] = useState(false);
928
909
  const [isZeroG, setIsZeroG] = useState(() => {
929
910
  if (enableCustomGravity) return true;
930
911
  const g = world.gravity;
@@ -933,9 +914,6 @@ function KhrVehicle({ scale, colliders, bundle, ...rest }) {
933
914
  const [wheelEpoch, setWheelEpoch] = useState(0);
934
915
  const zeroGRef = useRef(isZeroG);
935
916
  const poisonedRef = useRef(false);
936
- useLayoutEffect(() => {
937
- setRotorsMounted(true);
938
- }, []);
939
917
  const handleZeroGChange = useCallback((nextZeroG) => {
940
918
  if (nextZeroG) {
941
919
  poisonedRef.current = true;
@@ -1015,7 +993,8 @@ function KhrVehicle({ scale, colliders, bundle, ...rest }) {
1015
993
  };
1016
994
  }
1017
995
  innerRef.current = handle;
1018
- if (vehicleRef) vehicleRef.current = handle;
996
+ if (typeof vehicleRef === "function") vehicleRef(handle);
997
+ else if (vehicleRef && typeof vehicleRef === "object") vehicleRef.current = handle;
1019
998
  if (typeof ref === "function") ref(handle);
1020
999
  else if (ref && typeof ref === "object") ref.current = handle;
1021
1000
  }, [ref, vehicleRef]);
@@ -1058,37 +1037,35 @@ function KhrVehicle({ scale, colliders, bundle, ...rest }) {
1058
1037
  c: spec,
1059
1038
  massProperties
1060
1039
  }, key)),
1061
- rotorsMounted && /* @__PURE__ */ jsxs(Fragment, { children: [
1062
- /* @__PURE__ */ jsx(ZeroGWheelGate, {
1063
- vehicleRef: innerRef,
1064
- enable,
1065
- enableCustomGravity,
1066
- spawnPos: bodyPos,
1067
- zeroGRef,
1068
- onZeroGChange: handleZeroGChange
1069
- }),
1070
- /* @__PURE__ */ jsx(KhrVehicleRotors, {
1071
- solvedWheels,
1072
- propellers: bundle.propellers,
1073
- scene: bundle.scene,
1074
- comZ: chassis.com[2],
1075
- rotorOrigins,
1076
- enable,
1077
- wheelEnable: enable && !isZeroG,
1078
- wheelEpoch,
1079
- wheelConfig,
1080
- frontWheelConfig,
1081
- rearWheelConfig,
1082
- propellerConfig
1083
- }),
1084
- /* @__PURE__ */ jsx(ZeroGWheelPostClear, {
1085
- vehicleRef: innerRef,
1086
- zeroGRef,
1087
- disabledRotorsRef
1088
- }, `post:${wheelEpoch}`)
1089
- ] })
1040
+ /* @__PURE__ */ jsx(ZeroGWheelGate, {
1041
+ vehicleRef: innerRef,
1042
+ enable,
1043
+ enableCustomGravity,
1044
+ spawnPos: bodyPos,
1045
+ zeroGRef,
1046
+ onZeroGChange: handleZeroGChange
1047
+ }),
1048
+ /* @__PURE__ */ jsx(KhrVehicleRotors, {
1049
+ solvedWheels,
1050
+ propellers: bundle.propellers,
1051
+ scene: bundle.scene,
1052
+ comZ: chassis.com[2],
1053
+ rotorOrigins,
1054
+ enable,
1055
+ wheelEnable: enable && !isZeroG,
1056
+ wheelEpoch,
1057
+ wheelConfig,
1058
+ frontWheelConfig,
1059
+ rearWheelConfig,
1060
+ propellerConfig
1061
+ }),
1062
+ /* @__PURE__ */ jsx(ZeroGWheelPostClear, {
1063
+ vehicleRef: innerRef,
1064
+ zeroGRef,
1065
+ disabledRotorsRef
1066
+ }, `post:${wheelEpoch}`)
1090
1067
  ]
1091
- }), rotorsMounted && /* @__PURE__ */ jsx(KhrVehicleJoints, {
1068
+ }), /* @__PURE__ */ jsx(KhrVehicleJoints, {
1092
1069
  bodies: bundle.bodies,
1093
1070
  vehicleRef: innerRef,
1094
1071
  spawnPos: bodyPos,