@khr3/core 0.2.12 → 0.2.13
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.d.ts +3 -4
- package/dist/decoders.d.ts +5 -2
- package/dist/heightfield.d.ts +45 -0
- package/dist/index.d.ts +3 -2
- package/dist/index.js +360 -27
- package/dist/index.js.map +1 -1
- package/dist/inertia.d.ts +29 -17
- package/dist/{joints-5IgWDD2w.js → joints-H3uae8pm.js} +600 -126
- package/dist/joints-H3uae8pm.js.map +1 -0
- package/dist/types.d.ts +12 -0
- package/dist/vehicle.js +26 -82
- package/dist/vehicle.js.map +1 -1
- package/dist/vehicleJoints.d.ts +2 -2
- package/docs/api-reference.md +160 -0
- package/package.json +4 -3
- package/dist/joints-5IgWDD2w.js.map +0 -1
package/README.md
CHANGED
|
@@ -1,8 +1,59 @@
|
|
|
1
1
|
# `@khr3/core`
|
|
2
2
|
|
|
3
|
-
Khronos `KHR_physics_rigid_bodies` + `KHR_implicit_shapes` + `OMI_vehicle_wheel` loader, exporter, Rapier WASM
|
|
3
|
+
Khronos `KHR_physics_rigid_bodies` + `KHR_implicit_shapes` + `OMI_vehicle_wheel` + `KOLMO_emitter` loader, binary `.glb` exporter, exact solid/surface inertia solver, column-major Rapier WASM heightfields, articulated multi-body assemblies, and `@pinkaphant/ecctrl` vehicle rigs for React Three Fiber.
|
|
4
|
+
|
|
5
|
+
- **SI Right-Handed Frame**: `1.0 unit === 1.0 metre`, `+Y` up, `+Z` forward. Every node must have baked unit scale `[1, 1, 1]`.
|
|
6
|
+
- **Zero Silent Fallbacks**: Malformed `.glb` schemas, non-unit scales, or degenerate zero-area colliders throw `KhrValidationError`.
|
|
7
|
+
- **Full API Reference**: [`docs/api-reference.md`](./docs/api-reference.md)
|
|
8
|
+
|
|
9
|
+
## Quick Start
|
|
10
|
+
|
|
11
|
+
```tsx
|
|
12
|
+
import { Suspense } from "react";
|
|
13
|
+
import { Canvas } from "@react-three/fiber";
|
|
14
|
+
import {
|
|
15
|
+
PhysicsWorld,
|
|
16
|
+
KhrAssembly,
|
|
17
|
+
KhrHeightfield,
|
|
18
|
+
createHeightfieldGeometry,
|
|
19
|
+
sampleRapierHeightfield,
|
|
20
|
+
useKhrGlb,
|
|
21
|
+
} from "@khr3/core";
|
|
22
|
+
import { KhrVehicle } from "@khr3/core/vehicle";
|
|
23
|
+
|
|
24
|
+
const VAULT = "https://khr3.optimuspi.workers.dev/assets";
|
|
25
|
+
const heights = sampleRapierHeightfield((x, z) => 2 * Math.sin(x * 0.04) * Math.cos(z * 0.04), -100, -100, 200, 200, 128);
|
|
26
|
+
const terrainGeo = createHeightfieldGeometry(heights, 200, 200, 128);
|
|
27
|
+
|
|
28
|
+
function Scene() {
|
|
29
|
+
const car = useKhrGlb(`${VAULT}/cars/nissan_skyline_r34.glb`);
|
|
30
|
+
const door = useKhrGlb(`${VAULT}/city/door_hinged_single_900x2100.glb`);
|
|
31
|
+
|
|
32
|
+
return (
|
|
33
|
+
<PhysicsWorld gravity={[0, -9.81, 0]}>
|
|
34
|
+
<KhrHeightfield heights={heights} segments={128} size={[200, 200]}>
|
|
35
|
+
<mesh geometry={terrainGeo} receiveShadow>
|
|
36
|
+
<meshStandardMaterial color="#3c4a3e" roughness={0.92} />
|
|
37
|
+
</mesh>
|
|
38
|
+
</KhrHeightfield>
|
|
39
|
+
<KhrVehicle bundle={car} position={[0, 2.5, 0]} />
|
|
40
|
+
<KhrAssembly bundle={door} position={[4, 0, 4]} fixedRoot />
|
|
41
|
+
</PhysicsWorld>
|
|
42
|
+
);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export default function App() {
|
|
46
|
+
return (
|
|
47
|
+
<Canvas shadows camera={{ position: [0, 6, -12], fov: 55 }}>
|
|
48
|
+
<Suspense fallback={null}>
|
|
49
|
+
<Scene />
|
|
50
|
+
</Suspense>
|
|
51
|
+
</Canvas>
|
|
52
|
+
);
|
|
53
|
+
}
|
|
54
|
+
```
|
|
4
55
|
|
|
5
56
|
## Entrypoints
|
|
6
57
|
|
|
7
|
-
- `@khr3/core` — `loadKhrGlb`, `useKhrGlb`, `exportKhrGlb`, `cloneKhrBundle`, `disposeKhrBundle`, `
|
|
8
|
-
- `@khr3/core/vehicle` — `KhrVehicle`, `solveWheelLoads`
|
|
58
|
+
- `@khr3/core` — `PhysicsWorld`, `loadKhrGlb`, `useKhrGlb`, `exportKhrGlb`, `cloneKhrBundle`, `disposeKhrBundle`, `KhrAssembly`, `KhrCollider`, `KhrHeightfield`, `sampleRapierHeightfield`, `transposeHeightfieldForRapier`, `createHeightfieldGeometry`, `computeShapeInertia`, `resolveBodyMassFromColliders`, `jacobiPrincipalInertia`, `computeLumpedMassProps`, `applyRapierBodyMass`, `JointRegistryContext`
|
|
59
|
+
- `@khr3/core/vehicle` — `KhrVehicle`, `solveWheelLoads`, `mergeVehiclePanelUserData`
|
package/dist/KhrAssembly.d.ts
CHANGED
|
@@ -2,7 +2,7 @@ import React from "react";
|
|
|
2
2
|
import * as THREE from "three";
|
|
3
3
|
import { type ThreeElements } from "@react-three/fiber";
|
|
4
4
|
import { type RapierRigidBody, type RigidBodyProps } from "@react-three/rapier";
|
|
5
|
-
import { type
|
|
5
|
+
import { type LumpedMassProps } from "./inertia.js";
|
|
6
6
|
import { type JointRuntime, type KhrAssetBundle, type KhrBodySpec, type KhrColliderSpec, type KhrJointSpec } from "./types.js";
|
|
7
7
|
type Vec3 = [number, number, number];
|
|
8
8
|
type Quat = [number, number, number, number];
|
|
@@ -10,7 +10,6 @@ export interface PlannedCollider {
|
|
|
10
10
|
spec: KhrColliderSpec;
|
|
11
11
|
position: Vec3;
|
|
12
12
|
quaternion: Quat;
|
|
13
|
-
density?: number;
|
|
14
13
|
}
|
|
15
14
|
export interface PartPlan {
|
|
16
15
|
spec: KhrBodySpec;
|
|
@@ -22,7 +21,7 @@ export interface PartPlan {
|
|
|
22
21
|
restRelRootQuat: Quat;
|
|
23
22
|
axisParent: Vec3;
|
|
24
23
|
axisChild: Vec3;
|
|
25
|
-
|
|
24
|
+
lumpedMass: LumpedMassProps | null;
|
|
26
25
|
colliders: PlannedCollider[];
|
|
27
26
|
}
|
|
28
27
|
export interface RootPlan {
|
|
@@ -32,7 +31,7 @@ export interface RootPlan {
|
|
|
32
31
|
mountNode: THREE.Object3D;
|
|
33
32
|
mountPosition: Vec3;
|
|
34
33
|
mountQuaternion: Quat;
|
|
35
|
-
|
|
34
|
+
lumpedMass: LumpedMassProps | null;
|
|
36
35
|
colliders: PlannedCollider[];
|
|
37
36
|
parts: PartPlan[];
|
|
38
37
|
}
|
package/dist/decoders.d.ts
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import * as THREE from "three";
|
|
2
|
-
import { type EcctrlPropellerTag, type EcctrlWheelTag, type KhrBodySpec, type KhrBreakawaySpec, type KhrCombineMode, type KhrImplicitShape, type KhrJointSpec, type KhrPluginOptions } from "./types.js";
|
|
2
|
+
import { type EcctrlPropellerTag, type EcctrlWheelTag, type KhrBodySpec, type KhrBreakawaySpec, type KhrCombineMode, type KhrEmitterSpec, type KhrImplicitShape, type KhrJointSpec, type KhrPluginOptions } from "./types.js";
|
|
3
3
|
export interface KhrNodeMeta {
|
|
4
4
|
nodeName: string;
|
|
5
5
|
extras: Record<string, unknown>;
|
|
6
6
|
worldPosition: [number, number, number];
|
|
7
|
+
emitterSocket?: string;
|
|
8
|
+
inferredSocket?: string;
|
|
7
9
|
contactDots?: Array<[string, [number, number, number]]>;
|
|
8
10
|
}
|
|
9
11
|
export interface KhrTemplateMeta {
|
|
@@ -27,4 +29,5 @@ export declare function decodeJoint(rawJoints: Array<Record<string, unknown>>, j
|
|
|
27
29
|
export declare function decodeWheelTag(obj: THREE.Object3D, name: string, rawWheel: unknown, scene?: THREE.Object3D, rawOmiWheel?: unknown): EcctrlWheelTag;
|
|
28
30
|
export declare function decodePropellerTag(obj: THREE.Object3D, name: string, rawPropeller: unknown, scene?: THREE.Object3D): EcctrlPropellerTag;
|
|
29
31
|
export declare function decodeBreakawaySpec(raw: unknown): KhrBreakawaySpec | undefined;
|
|
30
|
-
export declare function
|
|
32
|
+
export declare function extractDefaultBodyRole(extras: Record<string, unknown>): string | null;
|
|
33
|
+
export declare function collectNodeSockets(obj: THREE.Object3D, name: string, extras: Record<string, unknown>, sockets: Record<string, [number, number, number]>, emitters: KhrEmitterSpec[], scene?: THREE.Object3D, rawExtensions?: Record<string, unknown>, isSocketNode?: KhrPluginOptions["isSocketNode"]): KhrNodeMeta;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
import * as THREE from "three";
|
|
3
|
+
import { type RapierCollider, type RapierRigidBody, type RigidBodyProps } from "@react-three/rapier";
|
|
4
|
+
/** `RAPIER.HeightFieldFlags.FIX_INTERNAL_EDGES` (1) — eliminates internal grid-edge ghost contact normals. */
|
|
5
|
+
export declare const KHR_HEIGHTFIELD_FLAGS = 1;
|
|
6
|
+
export type HeightfieldLayout = "rapier-column-major" | "row-major";
|
|
7
|
+
/**
|
|
8
|
+
* Transposes a row-major `(nrows + 1) * (ncols + 1)` height grid (`heights[zRow * (ncols + 1) + xCol]`,
|
|
9
|
+
* as returned by `@khr3/roads` `sampleHeightfield` and Three.js `PlaneGeometry`) into Rapier WASM's
|
|
10
|
+
* column-major layout (`out[xCol * (nrows + 1) + zRow]`).
|
|
11
|
+
*/
|
|
12
|
+
export declare function transposeHeightfieldForRapier(rowMajor: ArrayLike<number>, nrows: number, ncols?: number, out?: Float32Array): Float32Array;
|
|
13
|
+
/**
|
|
14
|
+
* Samples `heightFn(x, z)` directly in Rapier WASM's column-major `(nrows + 1) * (ncols + 1)` layout
|
|
15
|
+
* over `[originX, originX + sizeX] x [originZ, originZ + sizeZ]`.
|
|
16
|
+
*/
|
|
17
|
+
export declare function sampleRapierHeightfield(heightFn: (x: number, z: number) => number, originX: number, originZ: number, sizeX: number, sizeZ: number, nrows: number, ncols?: number, out?: Float32Array): Float32Array;
|
|
18
|
+
/**
|
|
19
|
+
* Builds an indexed Three.js `BufferGeometry` centered at `(0, 0, 0)` spanning
|
|
20
|
+
* `[-sizeX/2, +sizeX/2] x [-sizeZ/2, +sizeZ/2]` whose vertex positions, cell triangulation,
|
|
21
|
+
* and finite-difference normals match Rapier's `HeightField` collider 1:1.
|
|
22
|
+
*/
|
|
23
|
+
export declare function createHeightfieldGeometry(heights: ArrayLike<number>, sizeX: number, sizeZ: number, nrows: number, ncols?: number, layout?: HeightfieldLayout): THREE.BufferGeometry;
|
|
24
|
+
export interface KhrHeightfieldProps extends Omit<RigidBodyProps, "type" | "colliders" | "ref"> {
|
|
25
|
+
readonly ref?: React.Ref<RapierRigidBody>;
|
|
26
|
+
readonly colliderRef?: React.Ref<RapierCollider>;
|
|
27
|
+
readonly heights: Float32Array;
|
|
28
|
+
/** Cell subdivisions `[nrows (Z), ncols (X)]` or uniform `n`. `heights.length` must equal `(nrows + 1) * (ncols + 1)`. */
|
|
29
|
+
readonly segments: number | readonly [nrows: number, ncols: number];
|
|
30
|
+
/** World footprint `[sizeX, sizeZ]` in metres, centered on `position`. */
|
|
31
|
+
readonly size: readonly [sizeX: number, sizeZ: number];
|
|
32
|
+
/** Input layout of `heights` (`"rapier-column-major"` default, or `"row-major"` to auto-transpose). */
|
|
33
|
+
readonly layout?: HeightfieldLayout;
|
|
34
|
+
readonly friction?: number;
|
|
35
|
+
readonly restitution?: number;
|
|
36
|
+
readonly collisionGroups?: number;
|
|
37
|
+
readonly solverGroups?: number;
|
|
38
|
+
/** Increment when mutating `heights` in-place to rebuild the Rapier WASM heightfield collider. */
|
|
39
|
+
readonly version?: number;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Direct Rapier WASM heightfield body & collider (`HeightFieldFlags.FIX_INTERNAL_EDGES`),
|
|
43
|
+
* bypassing `@react-three/rapier`'s broken `<HeightfieldCollider>` `scaleColliderArgs` mutation.
|
|
44
|
+
*/
|
|
45
|
+
export declare function KhrHeightfield({ ref, colliderRef, heights, segments, size, layout, friction, restitution, collisionGroups, solverGroups, version, children, ...rbProps }: KhrHeightfieldProps): React.JSX.Element;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
export { PhysicsWorld, type PhysicsWorldProps, type TimeControlValue } from "./PhysicsWorld.js";
|
|
2
|
-
export { JointRegistryContext, KhrValidationError, assignRef, type CurveData, type CurvePoint, type EcctrlPropellerTag, type EcctrlWheelTag, type JointRegistry, type JointRuntime, type KhrAssetBundle, type KhrBodySpec, type KhrBreakawaySpec, type KhrCarConfig, type KhrColliderShape, type KhrColliderSpec, type KhrCombineMode, type KhrCurveData, type KhrCurvePoint, type KhrDroneConfig, type KhrExportNodeSpec, type KhrImplicitShape, type KhrJointDriveSpec, type KhrJointSpec, type KhrPluginOptions, type LoadKhrGlbOptions, type OmiVehicleWheelSpec, type UseKhrGlbOptions, } from "./types.js";
|
|
2
|
+
export { JointRegistryContext, KhrValidationError, assignRef, type CurveData, type CurvePoint, type EcctrlPropellerTag, type EcctrlWheelTag, type JointRegistry, type JointRuntime, type KhrAssetBundle, type KhrBodySpec, type KhrBreakawaySpec, type KhrCarConfig, type KhrColliderShape, type KhrColliderSpec, type KhrCombineMode, type KhrCurveData, type KhrCurvePoint, type KhrDroneConfig, type KhrEmitterSpec, type KhrExportNodeSpec, type KhrImplicitShape, type KhrJointDriveSpec, type KhrJointSpec, type KhrPluginOptions, type LoadKhrGlbOptions, type OmiVehicleWheelSpec, type UseKhrGlbOptions, } from "./types.js";
|
|
3
3
|
export { createKhrGltfPlugin, loadKhrGlb } from "./loader.js";
|
|
4
4
|
export { exportKhrGlb, patchGlbJsonChunk } from "./exporter.js";
|
|
5
5
|
export type { MutableGltfJson, RawGltfJson } from "./glb.js";
|
|
6
6
|
export { claimBundleMount, cloneKhrBundle, disposeKhrBundle, releaseBundleMount, snapshotSceneWithMountedSubtrees } from "./clone.js";
|
|
7
|
-
export { applyRapierBodyMass, assertIdentityWorldScale, assertSceneIdentityWorldScale, assertUnitScale,
|
|
7
|
+
export { applyRapierBodyMass, assertIdentityWorldScale, assertSceneIdentityWorldScale, assertUnitScale, computeLumpedMassProps, computeShapeInertia, jacobiPrincipalInertia, resolveBodyMassFromColliders, type LumpedMassProps, } from "./inertia.js";
|
|
8
|
+
export { KHR_HEIGHTFIELD_FLAGS, KhrHeightfield, createHeightfieldGeometry, sampleRapierHeightfield, transposeHeightfieldForRapier, type HeightfieldLayout, type KhrHeightfieldProps, } from "./heightfield.js";
|
|
8
9
|
export { KHR_TRIMESH_FLAGS, KhrCollider, type ColliderMass, type KhrColliderProps } from "./KhrCollider.js";
|
|
9
10
|
export { KhrAssembly, planAssembly, type KhrAssemblyHandle, type KhrAssemblyProps } from "./KhrAssembly.js";
|
|
10
11
|
export { KhrGLTFLoader, createScopedGltfLoaderClass, useKhrGlb } from "./useKhrGlb.js";
|