@forgeax/engine-physics-rapier3d 0.0.0-dev.8d955ade1c79
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/LICENSE +202 -0
- package/dist/.tsbuildinfo +1 -0
- package/dist/__tests__/physics-rapier3d.unit.test.d.ts +2 -0
- package/dist/__tests__/physics-rapier3d.unit.test.d.ts.map +1 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.mjs +977 -0
- package/dist/index.mjs.map +1 -0
- package/dist/rapier-physics-world-3d.d.ts +296 -0
- package/dist/rapier-physics-world-3d.d.ts.map +1 -0
- package/dist/wasm-loader.d.ts +22 -0
- package/dist/wasm-loader.d.ts.map +1 -0
- package/package.json +64 -0
- package/src/__tests__/physics-rapier3d.unit.test.ts +2362 -0
- package/src/index.ts +13 -0
- package/src/rapier-physics-world-3d.ts +1499 -0
- package/src/wasm-loader.ts +66 -0
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
// @forgeax/engine-physics-rapier3d — WASM loader for Rapier 3D compat variant.
|
|
2
|
+
//
|
|
3
|
+
// Dynamic import of @dimforge/rapier3d-compat (plan-strategy D-4: compat variant,
|
|
4
|
+
// zero Vite configuration).
|
|
5
|
+
//
|
|
6
|
+
// Usage:
|
|
7
|
+
// const rapier = await loadRapier3D();
|
|
8
|
+
// if ('code' in rapier) { /* handle PhysicsError */ }
|
|
9
|
+
// const world = new rapier.World({ x: 0, y: -9.81, z: 0 });
|
|
10
|
+
|
|
11
|
+
import type { PhysicsErrorCode } from '@forgeax/engine-types';
|
|
12
|
+
import { PhysicsError } from '@forgeax/engine-types';
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* The RAPIER module namespace — all constructors, types, and helpers exposed
|
|
16
|
+
* by @dimforge/rapier3d-compat after init(). This is the shape of the default
|
|
17
|
+
* export of the compat package.
|
|
18
|
+
*/
|
|
19
|
+
// biome-ignore lint/suspicious/noExplicitAny: Rapier compat namespace type
|
|
20
|
+
export type Rapier3DModule = any;
|
|
21
|
+
|
|
22
|
+
/** Cached RAPIER instance — loaded once, reused across frames. */
|
|
23
|
+
let rapierInstance: Rapier3DModule | null = null;
|
|
24
|
+
|
|
25
|
+
/** Loading promise — ensures concurrent callers share one init. */
|
|
26
|
+
let loadingPromise: Promise<Rapier3DModule | PhysicsError> | null = null;
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Load the Rapier 3D WASM module and initialise it.
|
|
30
|
+
*
|
|
31
|
+
* Uses dynamic import of the compat variant (zero Vite configuration per
|
|
32
|
+
* plan-strategy D-4). The init() call is async but self-hosted — no external
|
|
33
|
+
* .wasm file needed (Base64-inlined JS).
|
|
34
|
+
*
|
|
35
|
+
* Concurrent callers share a single loading promise; after the first
|
|
36
|
+
* successful load the cached instance is returned synchronously.
|
|
37
|
+
*
|
|
38
|
+
* @returns the RAPIER module namespace on success, or a PhysicsError with
|
|
39
|
+
* code 'wasm-load-failed' if dynamic import or init() rejects.
|
|
40
|
+
*/
|
|
41
|
+
export async function loadRapier3D(): Promise<Rapier3DModule | PhysicsError> {
|
|
42
|
+
if (rapierInstance !== null) return rapierInstance;
|
|
43
|
+
if (loadingPromise !== null) return loadingPromise;
|
|
44
|
+
|
|
45
|
+
loadingPromise = _doLoad();
|
|
46
|
+
return loadingPromise;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
async function _doLoad(): Promise<Rapier3DModule | PhysicsError> {
|
|
50
|
+
try {
|
|
51
|
+
const RAPIER = await import('@dimforge/rapier3d-compat');
|
|
52
|
+
await RAPIER.default.init();
|
|
53
|
+
rapierInstance = RAPIER.default;
|
|
54
|
+
loadingPromise = null;
|
|
55
|
+
return RAPIER.default;
|
|
56
|
+
} catch (cause) {
|
|
57
|
+
const reason = cause instanceof Error ? cause.message : String(cause);
|
|
58
|
+
loadingPromise = null;
|
|
59
|
+
return new PhysicsError({
|
|
60
|
+
code: 'wasm-load-failed' as PhysicsErrorCode,
|
|
61
|
+
expected: 'successful dynamic import and init of @dimforge/rapier3d-compat',
|
|
62
|
+
hint: `dynamic import or init() failed: ${reason}. Check network, file path, and that @dimforge/rapier3d-compat is installed.`,
|
|
63
|
+
detail: { code: 'wasm-load-failed', reason },
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
}
|