@threenative/physics 0.3.15 → 0.3.16
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/{Area3D-BI0ynKEY.d.ts → Area3D-BqLJBY6B.d.ts} +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.js +1567 -78
- package/dist/native/index.d.ts +194 -1
- package/dist/native/index.js +23 -21
- package/dist/navigation/index.d.ts +1 -1
- package/package.json +4 -3
- package/wasm/checksums.json +6 -0
- package/wasm/threenative_native_physics.wasm +0 -0
package/dist/index.js
CHANGED
|
@@ -2,14 +2,7 @@ import { Matrix4, Vector3, Quaternion, Object3D, Mesh, InstancedMesh, Matrix3 }
|
|
|
2
2
|
import { isStreamedGeometry } from '@threenative/core';
|
|
3
3
|
import { snowDiscFootprint } from '@threenative/core/world';
|
|
4
4
|
|
|
5
|
-
// src/
|
|
6
|
-
function interactionGroups(layer, mask) {
|
|
7
|
-
if (!Number.isInteger(layer) || layer < 0 || layer > 65535)
|
|
8
|
-
throw new Error("interactionGroups: layer must be an integer in 0..0xffff.");
|
|
9
|
-
if (!Number.isInteger(mask) || mask < 0 || mask > 65535)
|
|
10
|
-
throw new Error("interactionGroups: mask must be an integer in 0..0xffff.");
|
|
11
|
-
return (layer << 16 | mask) >>> 0;
|
|
12
|
-
}
|
|
5
|
+
// src/web.ts
|
|
13
6
|
|
|
14
7
|
// src/handles.ts
|
|
15
8
|
function physicsHandle(raw) {
|
|
@@ -25,6 +18,15 @@ function physicsColliderHandle(id, raw) {
|
|
|
25
18
|
return { id, raw };
|
|
26
19
|
}
|
|
27
20
|
|
|
21
|
+
// src/collision.ts
|
|
22
|
+
function interactionGroups(layer, mask) {
|
|
23
|
+
if (!Number.isInteger(layer) || layer < 0 || layer > 65535)
|
|
24
|
+
throw new Error("interactionGroups: layer must be an integer in 0..0xffff.");
|
|
25
|
+
if (!Number.isInteger(mask) || mask < 0 || mask > 65535)
|
|
26
|
+
throw new Error("interactionGroups: mask must be an integer in 0..0xffff.");
|
|
27
|
+
return (layer << 16 | mask) >>> 0;
|
|
28
|
+
}
|
|
29
|
+
|
|
28
30
|
// src/simulation.ts
|
|
29
31
|
function countEngineCost(name, count) {
|
|
30
32
|
const host = globalThis;
|
|
@@ -1172,44 +1174,1534 @@ function requireWheelFriction(wheel, frictionSlip, wheels) {
|
|
|
1172
1174
|
throw new Error("TN_PHYSICS_NON_FINITE: wheel friction slip must be a finite number >= 0.");
|
|
1173
1175
|
}
|
|
1174
1176
|
|
|
1177
|
+
// src/native/host.ts
|
|
1178
|
+
function nativePhysicsHost() {
|
|
1179
|
+
const host = globalThis.__THREENATIVE_NATIVE__?.physics;
|
|
1180
|
+
if (host === void 0 || typeof host.version !== "string" || typeof host.createSimulation !== "function") {
|
|
1181
|
+
throw new Error("TN_NATIVE_PHYSICS_MISSING: runtime did not install the physics ABI");
|
|
1182
|
+
}
|
|
1183
|
+
return host;
|
|
1184
|
+
}
|
|
1185
|
+
function nativeSimulation(value) {
|
|
1186
|
+
if (typeof value !== "object" || value === null || !("configureCharacter" in value) || typeof value.configureCharacter !== "function" || !("createBody" in value) || typeof value.createBody !== "function" || !("dispose" in value) || typeof value.dispose !== "function" || !("drainCollisionEvents" in value) || typeof value.drainCollisionEvents !== "function" || !("removeBody" in value) || typeof value.removeBody !== "function" || !("readVisibleTransforms" in value) || typeof value.readVisibleTransforms !== "function" || !("readBodySleepStates" in value) || typeof value.readBodySleepStates !== "function" || !("readCharacterStates" in value) || typeof value.readCharacterStates !== "function" || !("readAreaIntersections" in value) || typeof value.readAreaIntersections !== "function" || !("intersectRay" in value) || typeof value.intersectRay !== "function" || !("intersectShape" in value) || typeof value.intersectShape !== "function" || !("intersectPoint" in value) || typeof value.intersectPoint !== "function" || !("step" in value) || typeof value.step !== "function") {
|
|
1187
|
+
throw new Error("TN_NATIVE_PHYSICS_INVALID: physics world is not a native simulation");
|
|
1188
|
+
}
|
|
1189
|
+
return value;
|
|
1190
|
+
}
|
|
1191
|
+
function primitiveShape(shape) {
|
|
1192
|
+
if (shape.kind !== "box" && shape.kind !== "sphere" && shape.kind !== "capsule")
|
|
1193
|
+
throw new Error(`TN_NATIVE_PHYSICS_SHAPE_UNSUPPORTED: ${shape.kind} remains OPEN on native`);
|
|
1194
|
+
return shape;
|
|
1195
|
+
}
|
|
1196
|
+
function nativeBodyShape(shape) {
|
|
1197
|
+
if (shape.kind === "heightfield") {
|
|
1198
|
+
const { columns, heights, rows, scale } = shape;
|
|
1199
|
+
if (rows === void 0 || columns === void 0 || heights === void 0 || scale === void 0 || heights.length !== rows * columns)
|
|
1200
|
+
throw new Error(
|
|
1201
|
+
"TN_NATIVE_PHYSICS_SHAPE_INVALID: heightfield requires rows x columns heights"
|
|
1202
|
+
);
|
|
1203
|
+
return {
|
|
1204
|
+
collisionLayer: shape.collisionLayer,
|
|
1205
|
+
collisionMask: shape.collisionMask,
|
|
1206
|
+
columns,
|
|
1207
|
+
heights,
|
|
1208
|
+
kind: "heightfield",
|
|
1209
|
+
rows,
|
|
1210
|
+
scale,
|
|
1211
|
+
sensor: shape.sensor,
|
|
1212
|
+
x: 0,
|
|
1213
|
+
y: 0,
|
|
1214
|
+
z: 0
|
|
1215
|
+
};
|
|
1216
|
+
}
|
|
1217
|
+
if (shape.kind === "trimesh") {
|
|
1218
|
+
if (shape.vertices === void 0 || shape.indices === void 0)
|
|
1219
|
+
throw new Error("TN_NATIVE_PHYSICS_SHAPE_INVALID: trimesh requires vertices and indices");
|
|
1220
|
+
return {
|
|
1221
|
+
collisionLayer: shape.collisionLayer,
|
|
1222
|
+
collisionMask: shape.collisionMask,
|
|
1223
|
+
indices: shape.indices,
|
|
1224
|
+
kind: shape.kind,
|
|
1225
|
+
sensor: shape.sensor,
|
|
1226
|
+
vertices: shape.vertices,
|
|
1227
|
+
x: shape.x,
|
|
1228
|
+
y: shape.y,
|
|
1229
|
+
z: shape.z
|
|
1230
|
+
};
|
|
1231
|
+
}
|
|
1232
|
+
return primitiveShape(shape);
|
|
1233
|
+
}
|
|
1234
|
+
function opaqueNativeShape(shape) {
|
|
1235
|
+
return Object.freeze({ backend: "native", kind: shape.kind });
|
|
1236
|
+
}
|
|
1237
|
+
function isSmallBufferError(error) {
|
|
1238
|
+
return error instanceof Error && /buffer is too small/i.test(error.message);
|
|
1239
|
+
}
|
|
1240
|
+
function growUint32(buffer, minimum) {
|
|
1241
|
+
let length = Math.max(16, buffer.length);
|
|
1242
|
+
while (length < minimum) length *= 2;
|
|
1243
|
+
return length === buffer.length ? buffer : new Uint32Array(length);
|
|
1244
|
+
}
|
|
1245
|
+
var NATIVE_CHARACTER_STATE_STRIDE = 6;
|
|
1246
|
+
var RAPIER_DEFAULT_MAX_SUSPENSION_TRAVEL = 5;
|
|
1247
|
+
var NATIVE_VEHICLE_WHEEL_WIDTH = 12;
|
|
1248
|
+
function flattenVehicleWheels(options) {
|
|
1249
|
+
const wheels = new Float32Array(options.wheels.length * NATIVE_VEHICLE_WHEEL_WIDTH);
|
|
1250
|
+
options.wheels.forEach((wheel, index) => {
|
|
1251
|
+
wheels.set(
|
|
1252
|
+
[
|
|
1253
|
+
wheel.position.x,
|
|
1254
|
+
wheel.position.y,
|
|
1255
|
+
wheel.position.z,
|
|
1256
|
+
wheel.wheelRadius,
|
|
1257
|
+
wheel.suspensionRestLength,
|
|
1258
|
+
wheel.suspensionStiffness,
|
|
1259
|
+
wheel.dampingCompression,
|
|
1260
|
+
wheel.dampingRelaxation,
|
|
1261
|
+
wheel.wheelFrictionSlip,
|
|
1262
|
+
wheel.maxSuspensionTravel ?? RAPIER_DEFAULT_MAX_SUSPENSION_TRAVEL,
|
|
1263
|
+
wheel.useAsSteering ? 1 : 0,
|
|
1264
|
+
wheel.useAsTraction ? 1 : 0
|
|
1265
|
+
],
|
|
1266
|
+
index * NATIVE_VEHICLE_WHEEL_WIDTH
|
|
1267
|
+
);
|
|
1268
|
+
});
|
|
1269
|
+
return wheels;
|
|
1270
|
+
}
|
|
1271
|
+
function createNativePhysicsSimulation(raw, version) {
|
|
1272
|
+
const bodyIds = /* @__PURE__ */ new Set();
|
|
1273
|
+
const bodyHandles = /* @__PURE__ */ new Map();
|
|
1274
|
+
const jointBodies = /* @__PURE__ */ new Map();
|
|
1275
|
+
const characterIds = /* @__PURE__ */ new Set();
|
|
1276
|
+
const characterState2 = /* @__PURE__ */ new Map();
|
|
1277
|
+
const characterStatePresent = /* @__PURE__ */ new Set();
|
|
1278
|
+
const vehicles = /* @__PURE__ */ new Map();
|
|
1279
|
+
const vehicleBodies = /* @__PURE__ */ new Map();
|
|
1280
|
+
const dynamicBodies = /* @__PURE__ */ new Set();
|
|
1281
|
+
let characterStates = new Float32Array(48);
|
|
1282
|
+
const rayOutput = new Float32Array(8);
|
|
1283
|
+
let areaPairs = new Uint32Array(32);
|
|
1284
|
+
const areaIntersections = /* @__PURE__ */ new Map();
|
|
1285
|
+
const emptyAreaIntersections = /* @__PURE__ */ new Set();
|
|
1286
|
+
let bodyTransforms = new Float32Array(PHYSICS_TRANSFORM_STRIDE * 8);
|
|
1287
|
+
const bodyTransformRows = /* @__PURE__ */ new Map();
|
|
1288
|
+
let characterStateDirty = true;
|
|
1289
|
+
let areaIntersectionsDirty = true;
|
|
1290
|
+
let bodyTransformsDirty = true;
|
|
1291
|
+
let disposed = false;
|
|
1292
|
+
const requireLive = () => {
|
|
1293
|
+
if (disposed) throw new Error("Physics simulation is disposed.");
|
|
1294
|
+
};
|
|
1295
|
+
const invalidateObservations = () => {
|
|
1296
|
+
characterStateDirty = true;
|
|
1297
|
+
areaIntersectionsDirty = true;
|
|
1298
|
+
bodyTransformsDirty = true;
|
|
1299
|
+
};
|
|
1300
|
+
const bodyHandle = (id) => {
|
|
1301
|
+
const handle = bodyHandles.get(id);
|
|
1302
|
+
if (handle === void 0)
|
|
1303
|
+
throw new Error("TN_NATIVE_PHYSICS_INVALID: query returned an unknown body");
|
|
1304
|
+
return handle;
|
|
1305
|
+
};
|
|
1306
|
+
const requireVehicle = (id, operation) => {
|
|
1307
|
+
const record = vehicles.get(id);
|
|
1308
|
+
if (record === void 0)
|
|
1309
|
+
throw new Error(`TN_VEHICLE_UNKNOWN: ${operation} references an unknown vehicle ${id}.`);
|
|
1310
|
+
return record;
|
|
1311
|
+
};
|
|
1312
|
+
const finiteQueryVector = (value, label) => {
|
|
1313
|
+
if (!Number.isFinite(value.x) || !Number.isFinite(value.y) || !Number.isFinite(value.z))
|
|
1314
|
+
throw new Error(`TN_NATIVE_PHYSICS_INVALID: ${label} contains a non-finite vector`);
|
|
1315
|
+
return { x: value.x, y: value.y, z: value.z };
|
|
1316
|
+
};
|
|
1317
|
+
const nativeHit = (hit) => {
|
|
1318
|
+
if (!Number.isSafeInteger(hit.bodyId) || hit.bodyId < 0)
|
|
1319
|
+
throw new Error("TN_NATIVE_PHYSICS_INVALID: query returned an invalid body id");
|
|
1320
|
+
const body = bodyHandle(hit.bodyId);
|
|
1321
|
+
return {
|
|
1322
|
+
body,
|
|
1323
|
+
entity: body.entity,
|
|
1324
|
+
position: finiteQueryVector(hit.position, "query hit position")
|
|
1325
|
+
};
|
|
1326
|
+
};
|
|
1327
|
+
const refreshBodyTransforms = () => {
|
|
1328
|
+
if (!bodyTransformsDirty) return;
|
|
1329
|
+
const required = Math.max(1, bodyIds.size) * PHYSICS_TRANSFORM_STRIDE;
|
|
1330
|
+
if (bodyTransforms.length < required) bodyTransforms = new Float32Array(required);
|
|
1331
|
+
const count = raw.readVisibleTransforms(bodyTransforms);
|
|
1332
|
+
bodyTransformRows.clear();
|
|
1333
|
+
for (let index = 0; index < count; index += 1) {
|
|
1334
|
+
const offset = index * PHYSICS_TRANSFORM_STRIDE;
|
|
1335
|
+
const id = bodyTransforms[offset];
|
|
1336
|
+
if (id === void 0 || !Number.isInteger(id) || id < 0)
|
|
1337
|
+
throw new Error("TN_NATIVE_PHYSICS_INVALID: malformed visible transform record");
|
|
1338
|
+
bodyTransformRows.set(id, offset);
|
|
1339
|
+
}
|
|
1340
|
+
bodyTransformsDirty = false;
|
|
1341
|
+
};
|
|
1342
|
+
const scalarAt = (offset) => {
|
|
1343
|
+
const value = bodyTransforms[offset];
|
|
1344
|
+
if (value === void 0 || !Number.isFinite(value))
|
|
1345
|
+
throw new Error("TN_NATIVE_PHYSICS_INVALID: visible transform contains a non-finite scalar");
|
|
1346
|
+
return value;
|
|
1347
|
+
};
|
|
1348
|
+
const refreshCharacterState = () => {
|
|
1349
|
+
if (!characterStateDirty) return;
|
|
1350
|
+
const required = Math.max(1, characterIds.size) * NATIVE_CHARACTER_STATE_STRIDE;
|
|
1351
|
+
if (characterStates.length < required) characterStates = new Float32Array(required);
|
|
1352
|
+
characterStates.fill(Number.NaN, 0, required);
|
|
1353
|
+
const stateCount = raw.readCharacterStates(characterStates);
|
|
1354
|
+
characterStatePresent.clear();
|
|
1355
|
+
for (let index = 0; index < stateCount; index += 1) {
|
|
1356
|
+
const offset = index * NATIVE_CHARACTER_STATE_STRIDE;
|
|
1357
|
+
const id = characterStates[offset];
|
|
1358
|
+
const grounded = characterStates[offset + 1];
|
|
1359
|
+
const groundCollider = characterStates[offset + 2];
|
|
1360
|
+
const normalX = characterStates[offset + 3];
|
|
1361
|
+
const normalY = characterStates[offset + 4];
|
|
1362
|
+
const normalZ = characterStates[offset + 5];
|
|
1363
|
+
if (id === void 0 || grounded === void 0 || groundCollider === void 0 || normalX === void 0 || normalY === void 0 || normalZ === void 0 || !Number.isInteger(id) || id < 0 || grounded !== 0 && grounded !== 1 || !Number.isFinite(groundCollider) || !Number.isFinite(normalX) || !Number.isFinite(normalY) || !Number.isFinite(normalZ))
|
|
1364
|
+
throw new Error("TN_NATIVE_PHYSICS_INVALID: malformed six-float character state");
|
|
1365
|
+
let state = characterState2.get(id);
|
|
1366
|
+
if (state === void 0) {
|
|
1367
|
+
state = { grounded: false, groundNormal: { x: 0, y: 1, z: 0 } };
|
|
1368
|
+
characterState2.set(id, state);
|
|
1369
|
+
}
|
|
1370
|
+
state.grounded = grounded === 1;
|
|
1371
|
+
state.groundCollider = groundCollider < 0 ? void 0 : groundCollider;
|
|
1372
|
+
state.groundBody = groundCollider < 0 ? void 0 : bodyHandles.get(groundCollider);
|
|
1373
|
+
state.groundNormal.x = normalX;
|
|
1374
|
+
state.groundNormal.y = normalY;
|
|
1375
|
+
state.groundNormal.z = normalZ;
|
|
1376
|
+
characterStatePresent.add(id);
|
|
1377
|
+
}
|
|
1378
|
+
characterStateDirty = false;
|
|
1379
|
+
};
|
|
1380
|
+
const refreshAreaIntersections = () => {
|
|
1381
|
+
if (!areaIntersectionsDirty) return;
|
|
1382
|
+
areaPairs = growUint32(areaPairs, bodyIds.size * 2);
|
|
1383
|
+
const maximum = Math.max(16, bodyIds.size * bodyIds.size * 2);
|
|
1384
|
+
let pairCount;
|
|
1385
|
+
for (; ; ) {
|
|
1386
|
+
try {
|
|
1387
|
+
pairCount = raw.readAreaIntersections(areaPairs);
|
|
1388
|
+
break;
|
|
1389
|
+
} catch (error) {
|
|
1390
|
+
if (!isSmallBufferError(error) || areaPairs.length >= maximum) throw error;
|
|
1391
|
+
areaPairs = growUint32(areaPairs, Math.min(maximum, areaPairs.length * 2));
|
|
1392
|
+
}
|
|
1393
|
+
}
|
|
1394
|
+
for (const members of areaIntersections.values()) members.clear();
|
|
1395
|
+
for (let index = 0; index < pairCount; index += 1) {
|
|
1396
|
+
const offset = index * 2;
|
|
1397
|
+
const areaId = areaPairs[offset];
|
|
1398
|
+
const bodyId3 = areaPairs[offset + 1];
|
|
1399
|
+
if (areaId === void 0 || bodyId3 === void 0)
|
|
1400
|
+
throw new Error("TN_NATIVE_PHYSICS_INVALID: malformed area intersection");
|
|
1401
|
+
areaIntersections.get(areaId)?.add(bodyId3);
|
|
1402
|
+
}
|
|
1403
|
+
areaIntersectionsDirty = false;
|
|
1404
|
+
};
|
|
1405
|
+
const simulation = {
|
|
1406
|
+
version,
|
|
1407
|
+
rawEventQueue: raw,
|
|
1408
|
+
rawWorld: raw,
|
|
1409
|
+
createBody: (options) => {
|
|
1410
|
+
requireLive();
|
|
1411
|
+
if (!Number.isFinite(options.mass) || options.mass < 0)
|
|
1412
|
+
throw new Error("Physics body mass must be a finite non-negative number.");
|
|
1413
|
+
if (options.continuousCollision !== void 0 && typeof options.continuousCollision !== "boolean")
|
|
1414
|
+
throw new Error("Physics body continuousCollision must be a boolean.");
|
|
1415
|
+
requireFiniteVector(options.position, "body position");
|
|
1416
|
+
requireFiniteRotation(options.rotation, "body rotation");
|
|
1417
|
+
const shape = nativeBodyShape(options.shape);
|
|
1418
|
+
const sensor = requirePhysicsBodySensor(options);
|
|
1419
|
+
const id = raw.createBody({
|
|
1420
|
+
collisionLayer: shape.collisionLayer,
|
|
1421
|
+
collisionMask: shape.collisionMask,
|
|
1422
|
+
mass: options.mass,
|
|
1423
|
+
position: options.position,
|
|
1424
|
+
rotation: options.rotation,
|
|
1425
|
+
sensor,
|
|
1426
|
+
shape,
|
|
1427
|
+
type: options.type,
|
|
1428
|
+
continuousCollision: effectiveContinuousCollision(
|
|
1429
|
+
options.type,
|
|
1430
|
+
options.continuousCollision
|
|
1431
|
+
)
|
|
1432
|
+
});
|
|
1433
|
+
if (!Number.isInteger(id) || id < 0)
|
|
1434
|
+
throw new Error("TN_NATIVE_PHYSICS_INVALID: runtime returned an invalid body id");
|
|
1435
|
+
const rawHandle = { backend: "native", id };
|
|
1436
|
+
const bodyHandleValue = physicsBodyHandle(id, rawHandle, options.entity);
|
|
1437
|
+
bodyIds.add(id);
|
|
1438
|
+
bodyHandles.set(id, bodyHandleValue);
|
|
1439
|
+
if (options.type === "dynamic") dynamicBodies.add(id);
|
|
1440
|
+
if (sensor) {
|
|
1441
|
+
areaIntersections.set(id, /* @__PURE__ */ new Set());
|
|
1442
|
+
}
|
|
1443
|
+
if (options.type === "character") {
|
|
1444
|
+
characterIds.add(id);
|
|
1445
|
+
characterState2.set(id, { grounded: false, groundNormal: { x: 0, y: 1, z: 0 } });
|
|
1446
|
+
}
|
|
1447
|
+
invalidateObservations();
|
|
1448
|
+
return {
|
|
1449
|
+
body: bodyHandleValue,
|
|
1450
|
+
collider: physicsColliderHandle(id, rawHandle),
|
|
1451
|
+
controller: options.type === "character" ? physicsHandle(rawHandle) : void 0,
|
|
1452
|
+
rawShape: opaqueNativeShape(shape)
|
|
1453
|
+
};
|
|
1454
|
+
},
|
|
1455
|
+
createJoint: (jointOptions) => {
|
|
1456
|
+
requireLive();
|
|
1457
|
+
const normalized = requirePhysicsJointCreateOptions(jointOptions);
|
|
1458
|
+
if (!bodyIds.has(normalized.bodyA) || !bodyIds.has(normalized.bodyB))
|
|
1459
|
+
throw new Error("TN_PHYSICS_UNKNOWN_BODY: joint references an unknown body.");
|
|
1460
|
+
if (raw.createJoint === void 0)
|
|
1461
|
+
throw new Error("TN_NATIVE_PHYSICS_JOINTS_MISSING: runtime ABI is too old");
|
|
1462
|
+
const id = raw.createJoint(normalized);
|
|
1463
|
+
if (!Number.isSafeInteger(id) || id < 0)
|
|
1464
|
+
throw new Error("TN_NATIVE_PHYSICS_INVALID: runtime returned an invalid joint id");
|
|
1465
|
+
if (jointBodies.has(id))
|
|
1466
|
+
throw new Error("TN_NATIVE_PHYSICS_INVALID: runtime returned a duplicate joint id");
|
|
1467
|
+
jointBodies.set(id, [normalized.bodyA, normalized.bodyB]);
|
|
1468
|
+
return id;
|
|
1469
|
+
},
|
|
1470
|
+
configureCharacter: (id, options) => {
|
|
1471
|
+
requireLive();
|
|
1472
|
+
raw.configureCharacter(id, options);
|
|
1473
|
+
invalidateObservations();
|
|
1474
|
+
},
|
|
1475
|
+
removeBody: (id) => {
|
|
1476
|
+
requireLive();
|
|
1477
|
+
raw.removeBody(id);
|
|
1478
|
+
bodyIds.delete(id);
|
|
1479
|
+
bodyHandles.delete(id);
|
|
1480
|
+
dynamicBodies.delete(id);
|
|
1481
|
+
for (const [jointId, bodies] of jointBodies) {
|
|
1482
|
+
if (bodies[0] === id || bodies[1] === id) jointBodies.delete(jointId);
|
|
1483
|
+
}
|
|
1484
|
+
areaIntersections.delete(id);
|
|
1485
|
+
characterIds.delete(id);
|
|
1486
|
+
characterState2.delete(id);
|
|
1487
|
+
characterStatePresent.delete(id);
|
|
1488
|
+
for (const [vehicleId, bodyId3] of vehicleBodies) {
|
|
1489
|
+
if (bodyId3 !== id) continue;
|
|
1490
|
+
vehicleBodies.delete(vehicleId);
|
|
1491
|
+
vehicles.delete(vehicleId);
|
|
1492
|
+
}
|
|
1493
|
+
invalidateObservations();
|
|
1494
|
+
},
|
|
1495
|
+
removeJoint: (id) => {
|
|
1496
|
+
if (disposed || !jointBodies.has(id)) return;
|
|
1497
|
+
if (raw.removeJoint === void 0)
|
|
1498
|
+
throw new Error("TN_NATIVE_PHYSICS_JOINTS_MISSING: runtime ABI is too old");
|
|
1499
|
+
raw.removeJoint(id);
|
|
1500
|
+
jointBodies.delete(id);
|
|
1501
|
+
},
|
|
1502
|
+
setBodyTransform: (id, position) => {
|
|
1503
|
+
requireLive();
|
|
1504
|
+
if (raw.setBodyTransform === void 0)
|
|
1505
|
+
throw new Error("TN_NATIVE_PHYSICS_SET_TRANSFORM_MISSING: runtime ABI is too old");
|
|
1506
|
+
raw.setBodyTransform(id, position);
|
|
1507
|
+
},
|
|
1508
|
+
// Keep the optional raw members: an older runtime must throw rather than accept a call and
|
|
1509
|
+
// drop the actuation, which would make the same game diverge by platform.
|
|
1510
|
+
applyBodyImpulse: (id, impulse) => {
|
|
1511
|
+
requireLive();
|
|
1512
|
+
requireFiniteVector(impulse, "impulse");
|
|
1513
|
+
if (raw.applyBodyImpulse === void 0)
|
|
1514
|
+
throw new Error("TN_NATIVE_PHYSICS_ACTUATION_MISSING: runtime ABI is too old");
|
|
1515
|
+
raw.applyBodyImpulse(id, impulse);
|
|
1516
|
+
},
|
|
1517
|
+
applyBodyForce: (id, force) => {
|
|
1518
|
+
requireLive();
|
|
1519
|
+
requireFiniteVector(force, "force");
|
|
1520
|
+
if (raw.applyBodyForce === void 0)
|
|
1521
|
+
throw new Error("TN_NATIVE_PHYSICS_ACTUATION_MISSING: runtime ABI is too old");
|
|
1522
|
+
raw.applyBodyForce(id, force);
|
|
1523
|
+
},
|
|
1524
|
+
applyBodyForceAtPoint: (id, force, point) => {
|
|
1525
|
+
requireLive();
|
|
1526
|
+
requireFiniteVector(force, "force");
|
|
1527
|
+
requireFiniteVector(point, "force point");
|
|
1528
|
+
if (raw.applyBodyForceAtPoint === void 0)
|
|
1529
|
+
throw new Error("TN_NATIVE_PHYSICS_ACTUATION_MISSING: runtime ABI is too old");
|
|
1530
|
+
raw.applyBodyForceAtPoint(id, force, point);
|
|
1531
|
+
},
|
|
1532
|
+
setBodyLinearVelocity: (id, velocity) => {
|
|
1533
|
+
requireLive();
|
|
1534
|
+
requireFiniteVector(velocity, "linearVelocity");
|
|
1535
|
+
if (raw.setBodyLinearVelocity === void 0)
|
|
1536
|
+
throw new Error("TN_NATIVE_PHYSICS_ACTUATION_MISSING: runtime ABI is too old");
|
|
1537
|
+
raw.setBodyLinearVelocity(id, velocity);
|
|
1538
|
+
},
|
|
1539
|
+
readBodyLinearVelocity: (id) => {
|
|
1540
|
+
requireLive();
|
|
1541
|
+
if (raw.readBodyLinearVelocity === void 0)
|
|
1542
|
+
throw new Error("TN_NATIVE_PHYSICS_ACTUATION_MISSING: runtime ABI is too old");
|
|
1543
|
+
const velocity = raw.readBodyLinearVelocity(id);
|
|
1544
|
+
requireFiniteVector(velocity, "native linear velocity");
|
|
1545
|
+
return velocity;
|
|
1546
|
+
},
|
|
1547
|
+
step: (deltaTime, inputSnapshot) => {
|
|
1548
|
+
requireLive();
|
|
1549
|
+
requirePhysicsStepInput(deltaTime, inputSnapshot, (id) => bodyIds.has(id));
|
|
1550
|
+
raw.step(deltaTime, inputSnapshot);
|
|
1551
|
+
invalidateObservations();
|
|
1552
|
+
},
|
|
1553
|
+
readVisibleTransforms: (buffer) => {
|
|
1554
|
+
requireLive();
|
|
1555
|
+
requirePhysicsRenderBuffer(buffer, bodyIds.size);
|
|
1556
|
+
return raw.readVisibleTransforms(buffer);
|
|
1557
|
+
},
|
|
1558
|
+
readBodySleepStates: (buffer) => {
|
|
1559
|
+
if (disposed) throw new Error("Physics simulation is disposed.");
|
|
1560
|
+
requirePhysicsSleepStateBuffer(buffer, bodyIds.size);
|
|
1561
|
+
return raw.readBodySleepStates(buffer);
|
|
1562
|
+
},
|
|
1563
|
+
readContacts: (target, colliders, buffer) => {
|
|
1564
|
+
requireLive();
|
|
1565
|
+
if (raw.readContacts === void 0)
|
|
1566
|
+
throw new Error("TN_NATIVE_PHYSICS_CONTACTS_MISSING: runtime ABI is too old");
|
|
1567
|
+
if (!(buffer instanceof Float32Array) || !(colliders instanceof Uint32Array))
|
|
1568
|
+
throw new Error(
|
|
1569
|
+
"IPhysicsSimulation.readContacts requires Uint32Array ids and a Float32Array."
|
|
1570
|
+
);
|
|
1571
|
+
if (!bodyIds.has(target.id))
|
|
1572
|
+
throw new Error(
|
|
1573
|
+
`IPhysicsSimulation contact target ${String(target.id)} is not a live collider.`
|
|
1574
|
+
);
|
|
1575
|
+
const count = raw.readContacts(target.id, colliders, buffer);
|
|
1576
|
+
if (!Number.isInteger(count) || count < 0)
|
|
1577
|
+
throw new Error("TN_NATIVE_PHYSICS_INVALID: contact read returned an invalid count");
|
|
1578
|
+
return count;
|
|
1579
|
+
},
|
|
1580
|
+
setColliderShape: (collider, shape) => {
|
|
1581
|
+
requireLive();
|
|
1582
|
+
if (raw.setColliderShape === void 0)
|
|
1583
|
+
throw new Error("TN_NATIVE_PHYSICS_SHAPE_REFRESH_MISSING: runtime ABI is too old");
|
|
1584
|
+
if (!bodyIds.has(collider.id))
|
|
1585
|
+
throw new Error(
|
|
1586
|
+
`IPhysicsSimulation shape target ${String(collider.id)} is not a live collider.`
|
|
1587
|
+
);
|
|
1588
|
+
if (shape.kind !== "heightfield")
|
|
1589
|
+
throw new Error(
|
|
1590
|
+
`TN_NATIVE_PHYSICS_SHAPE_UNSUPPORTED: native setColliderShape takes a heightfield, not ${shape.kind}`
|
|
1591
|
+
);
|
|
1592
|
+
raw.setColliderShape(collider.id, nativeBodyShape(shape));
|
|
1593
|
+
invalidateObservations();
|
|
1594
|
+
},
|
|
1595
|
+
intersectRay: (value) => {
|
|
1596
|
+
requireLive();
|
|
1597
|
+
const query = requirePhysicsRayQuery(value);
|
|
1598
|
+
const hit = raw.intersectRay(query, rayOutput);
|
|
1599
|
+
if (typeof hit === "number") {
|
|
1600
|
+
if (hit === 0) return void 0;
|
|
1601
|
+
if (hit !== 1)
|
|
1602
|
+
throw new Error("TN_NATIVE_PHYSICS_INVALID: ray query returned an invalid status");
|
|
1603
|
+
const bodyId3 = rayOutput[0];
|
|
1604
|
+
const position = {
|
|
1605
|
+
x: rayOutput[1],
|
|
1606
|
+
y: rayOutput[2],
|
|
1607
|
+
z: rayOutput[3]
|
|
1608
|
+
};
|
|
1609
|
+
const normal = {
|
|
1610
|
+
x: rayOutput[4],
|
|
1611
|
+
y: rayOutput[5],
|
|
1612
|
+
z: rayOutput[6]
|
|
1613
|
+
};
|
|
1614
|
+
const distance = rayOutput[7];
|
|
1615
|
+
if (bodyId3 === void 0 || !Number.isSafeInteger(bodyId3) || bodyId3 < 0 || distance === void 0)
|
|
1616
|
+
throw new Error("TN_NATIVE_PHYSICS_INVALID: ray output is malformed");
|
|
1617
|
+
const base2 = nativeHit({ bodyId: bodyId3, position });
|
|
1618
|
+
if (!Number.isFinite(distance) || distance < 0)
|
|
1619
|
+
throw new Error("TN_NATIVE_PHYSICS_INVALID: ray output distance is invalid");
|
|
1620
|
+
return { ...base2, distance, normal: finiteQueryVector(normal, "ray output normal") };
|
|
1621
|
+
}
|
|
1622
|
+
if (hit === void 0 || hit === null) return void 0;
|
|
1623
|
+
if (!Number.isFinite(hit.distance) || hit.distance < 0)
|
|
1624
|
+
throw new Error("TN_NATIVE_PHYSICS_INVALID: ray query returned an invalid distance");
|
|
1625
|
+
const base = nativeHit({ bodyId: hit.bodyId, position: hit.position });
|
|
1626
|
+
return {
|
|
1627
|
+
...base,
|
|
1628
|
+
distance: hit.distance,
|
|
1629
|
+
normal: finiteQueryVector(hit.normal, "ray hit normal")
|
|
1630
|
+
};
|
|
1631
|
+
},
|
|
1632
|
+
intersectShape: (value) => {
|
|
1633
|
+
requireLive();
|
|
1634
|
+
const query = requirePhysicsShapeQuery(value);
|
|
1635
|
+
const hits = raw.intersectShape({
|
|
1636
|
+
collisionMask: query.collisionMask,
|
|
1637
|
+
maxResults: query.maxResults,
|
|
1638
|
+
position: query.position,
|
|
1639
|
+
rotation: query.rotation,
|
|
1640
|
+
shape: primitiveShape(query.shape)
|
|
1641
|
+
});
|
|
1642
|
+
if (!Array.isArray(hits) || hits.length > query.maxResults)
|
|
1643
|
+
throw new Error("TN_NATIVE_PHYSICS_INVALID: malformed shape query result");
|
|
1644
|
+
return hits.map(nativeHit);
|
|
1645
|
+
},
|
|
1646
|
+
intersectPoint: (value) => {
|
|
1647
|
+
requireLive();
|
|
1648
|
+
const query = requirePhysicsPointQuery(value);
|
|
1649
|
+
const hits = raw.intersectPoint(query);
|
|
1650
|
+
if (!Array.isArray(hits) || hits.length > query.maxResults)
|
|
1651
|
+
throw new Error("TN_NATIVE_PHYSICS_INVALID: malformed point query result");
|
|
1652
|
+
return hits.map(nativeHit);
|
|
1653
|
+
},
|
|
1654
|
+
readBodyTransform: (id) => {
|
|
1655
|
+
requireLive();
|
|
1656
|
+
refreshBodyTransforms();
|
|
1657
|
+
const offset = bodyTransformRows.get(id);
|
|
1658
|
+
if (offset === void 0) return void 0;
|
|
1659
|
+
return {
|
|
1660
|
+
position: {
|
|
1661
|
+
x: scalarAt(offset + 1),
|
|
1662
|
+
y: scalarAt(offset + 2),
|
|
1663
|
+
z: scalarAt(offset + 3)
|
|
1664
|
+
},
|
|
1665
|
+
rotation: {
|
|
1666
|
+
x: scalarAt(offset + 4),
|
|
1667
|
+
y: scalarAt(offset + 5),
|
|
1668
|
+
z: scalarAt(offset + 6),
|
|
1669
|
+
w: scalarAt(offset + 7)
|
|
1670
|
+
}
|
|
1671
|
+
};
|
|
1672
|
+
},
|
|
1673
|
+
readCharacterState: (id) => {
|
|
1674
|
+
requireLive();
|
|
1675
|
+
refreshCharacterState();
|
|
1676
|
+
return characterStatePresent.has(id) ? characterState2.get(id) : void 0;
|
|
1677
|
+
},
|
|
1678
|
+
areaIntersections: (id) => {
|
|
1679
|
+
requireLive();
|
|
1680
|
+
refreshAreaIntersections();
|
|
1681
|
+
return areaIntersections.get(id) ?? emptyAreaIntersections;
|
|
1682
|
+
},
|
|
1683
|
+
drainCollisionEvents: (buffer) => {
|
|
1684
|
+
requireLive();
|
|
1685
|
+
requirePhysicsEventBuffer(buffer);
|
|
1686
|
+
return raw.drainCollisionEvents(buffer);
|
|
1687
|
+
},
|
|
1688
|
+
createVehicle: (options) => {
|
|
1689
|
+
requireLive();
|
|
1690
|
+
const requested = requirePhysicsVehicleCreateOptions(options);
|
|
1691
|
+
if (!bodyIds.has(requested.bodyId))
|
|
1692
|
+
throw new Error(
|
|
1693
|
+
`TN_PHYSICS_UNKNOWN_BODY: createVehicle references an unknown body ${requested.bodyId}.`
|
|
1694
|
+
);
|
|
1695
|
+
if (!dynamicBodies.has(requested.bodyId))
|
|
1696
|
+
throw new Error(
|
|
1697
|
+
`TN_PHYSICS_NOT_DYNAMIC: createVehicle needs a dynamic body; body ${requested.bodyId} is not dynamic.`
|
|
1698
|
+
);
|
|
1699
|
+
if (raw.createVehicle === void 0)
|
|
1700
|
+
throw new Error("TN_NATIVE_PHYSICS_VEHICLE_MISSING: runtime ABI is too old");
|
|
1701
|
+
const wheelCount = requested.wheels.length;
|
|
1702
|
+
const id = raw.createVehicle({
|
|
1703
|
+
axle: requested.axle,
|
|
1704
|
+
bodyId: requested.bodyId,
|
|
1705
|
+
forwardAxis: requested.forwardAxis,
|
|
1706
|
+
wheels: flattenVehicleWheels(requested)
|
|
1707
|
+
});
|
|
1708
|
+
if (!Number.isSafeInteger(id) || id < 0)
|
|
1709
|
+
throw new Error("TN_NATIVE_PHYSICS_INVALID: runtime returned an invalid vehicle id");
|
|
1710
|
+
const buffer = new Float32Array(1 + wheelCount * PHYSICS_VEHICLE_WHEEL_STRIDE);
|
|
1711
|
+
vehicles.set(id, {
|
|
1712
|
+
buffer,
|
|
1713
|
+
state: { speed: 0, wheels: buffer.subarray(1) }
|
|
1714
|
+
});
|
|
1715
|
+
vehicleBodies.set(id, requested.bodyId);
|
|
1716
|
+
return id;
|
|
1717
|
+
},
|
|
1718
|
+
setVehicleInput: (id, input) => {
|
|
1719
|
+
requireLive();
|
|
1720
|
+
if (raw.setVehicleInput === void 0)
|
|
1721
|
+
throw new Error("TN_NATIVE_PHYSICS_VEHICLE_MISSING: runtime ABI is too old");
|
|
1722
|
+
if (typeof input?.engineForce !== "number" || typeof input.brake !== "number" || typeof input.steering !== "number" || !Number.isFinite(input.engineForce) || !Number.isFinite(input.brake) || !Number.isFinite(input.steering))
|
|
1723
|
+
throw new Error(
|
|
1724
|
+
"TN_PHYSICS_NON_FINITE: vehicle input needs a finite engineForce, brake and steering."
|
|
1725
|
+
);
|
|
1726
|
+
requireVehicle(id, "setVehicleInput");
|
|
1727
|
+
raw.setVehicleInput(id, input);
|
|
1728
|
+
},
|
|
1729
|
+
setVehicleWheelFriction: (id, wheel, frictionSlip) => {
|
|
1730
|
+
requireLive();
|
|
1731
|
+
if (raw.setVehicleWheelFriction === void 0)
|
|
1732
|
+
throw new Error("TN_NATIVE_PHYSICS_VEHICLE_MISSING: runtime ABI is too old");
|
|
1733
|
+
const record = requireVehicle(id, "setVehicleWheelFriction");
|
|
1734
|
+
requireWheelFriction(
|
|
1735
|
+
wheel,
|
|
1736
|
+
frictionSlip,
|
|
1737
|
+
(record.buffer.length - 1) / PHYSICS_VEHICLE_WHEEL_STRIDE
|
|
1738
|
+
);
|
|
1739
|
+
raw.setVehicleWheelFriction(id, wheel, frictionSlip);
|
|
1740
|
+
},
|
|
1741
|
+
readVehicleState: (id) => {
|
|
1742
|
+
requireLive();
|
|
1743
|
+
if (raw.readVehicleState === void 0)
|
|
1744
|
+
throw new Error("TN_NATIVE_PHYSICS_VEHICLE_MISSING: runtime ABI is too old");
|
|
1745
|
+
const record = requireVehicle(id, "readVehicleState");
|
|
1746
|
+
const count = raw.readVehicleState(id, record.buffer);
|
|
1747
|
+
if (!Number.isSafeInteger(count) || count < 1)
|
|
1748
|
+
throw new Error("TN_VEHICLE_UNKNOWN: the vehicle left the backend.");
|
|
1749
|
+
if (count !== record.buffer.length)
|
|
1750
|
+
throw new Error(
|
|
1751
|
+
`TN_VEHICLE_STATE_SHORT: readVehicleState wrote ${count} of ${record.buffer.length} floats for vehicle ${id}.`
|
|
1752
|
+
);
|
|
1753
|
+
record.state.speed = record.buffer[0] ?? 0;
|
|
1754
|
+
return record.state;
|
|
1755
|
+
},
|
|
1756
|
+
resetVehicle: (id, position, yaw) => {
|
|
1757
|
+
requireLive();
|
|
1758
|
+
if (raw.resetVehicle === void 0)
|
|
1759
|
+
throw new Error("TN_NATIVE_PHYSICS_VEHICLE_MISSING: runtime ABI is too old");
|
|
1760
|
+
requireVehicle(id, "resetVehicle");
|
|
1761
|
+
requireFiniteVector(position, "vehicle position");
|
|
1762
|
+
if (typeof yaw !== "number" || !Number.isFinite(yaw))
|
|
1763
|
+
throw new Error("TN_PHYSICS_NON_FINITE: vehicle yaw must be a finite number of radians.");
|
|
1764
|
+
raw.resetVehicle(id, position, yaw);
|
|
1765
|
+
bodyTransformsDirty = true;
|
|
1766
|
+
},
|
|
1767
|
+
dispose: () => {
|
|
1768
|
+
if (disposed) return;
|
|
1769
|
+
disposed = true;
|
|
1770
|
+
raw.dispose();
|
|
1771
|
+
bodyIds.clear();
|
|
1772
|
+
bodyTransformRows.clear();
|
|
1773
|
+
bodyHandles.clear();
|
|
1774
|
+
jointBodies.clear();
|
|
1775
|
+
vehicles.clear();
|
|
1776
|
+
vehicleBodies.clear();
|
|
1777
|
+
dynamicBodies.clear();
|
|
1778
|
+
characterIds.clear();
|
|
1779
|
+
characterState2.clear();
|
|
1780
|
+
characterStatePresent.clear();
|
|
1781
|
+
areaIntersections.clear();
|
|
1782
|
+
characterStateDirty = false;
|
|
1783
|
+
areaIntersectionsDirty = false;
|
|
1784
|
+
}
|
|
1785
|
+
};
|
|
1786
|
+
return simulation;
|
|
1787
|
+
}
|
|
1788
|
+
|
|
1789
|
+
// src/native.ts
|
|
1790
|
+
function createNativePhysicsBackend(hostOf, initialize = async () => void 0) {
|
|
1791
|
+
const simulations = /* @__PURE__ */ new WeakMap();
|
|
1792
|
+
const wrap = (raw, version) => {
|
|
1793
|
+
const simulation = createNativePhysicsSimulation(raw, version);
|
|
1794
|
+
simulations.set(raw, simulation);
|
|
1795
|
+
return simulation;
|
|
1796
|
+
};
|
|
1797
|
+
return {
|
|
1798
|
+
initialize,
|
|
1799
|
+
createSimulation: (options = {}) => {
|
|
1800
|
+
const host = hostOf();
|
|
1801
|
+
return wrap(nativeSimulation(host.createSimulation(options)), host.version);
|
|
1802
|
+
},
|
|
1803
|
+
createShape: (shape) => Object.freeze({ backend: "native", kind: shape.kind }),
|
|
1804
|
+
simulationForWorld: (world) => {
|
|
1805
|
+
if (typeof world !== "object" || world === null)
|
|
1806
|
+
throw new Error("TN_NATIVE_PHYSICS_INVALID: native world must be an object");
|
|
1807
|
+
const existing = simulations.get(world);
|
|
1808
|
+
if (existing !== void 0) return existing;
|
|
1809
|
+
return wrap(nativeSimulation(world), hostOf().version);
|
|
1810
|
+
}
|
|
1811
|
+
};
|
|
1812
|
+
}
|
|
1813
|
+
installPhysicsSimulationBackend(createNativePhysicsBackend(nativePhysicsHost));
|
|
1814
|
+
|
|
1815
|
+
// src/native/wasm-host.ts
|
|
1816
|
+
var EXPORTS = [
|
|
1817
|
+
"tn_physics_add_body",
|
|
1818
|
+
"tn_physics_add_heightfield_body",
|
|
1819
|
+
"tn_physics_add_trimesh_body",
|
|
1820
|
+
"tn_physics_alloc",
|
|
1821
|
+
"tn_physics_apply_body_force",
|
|
1822
|
+
"tn_physics_apply_body_force_at_point",
|
|
1823
|
+
"tn_physics_apply_body_impulse",
|
|
1824
|
+
"tn_physics_configure_character",
|
|
1825
|
+
"tn_physics_create",
|
|
1826
|
+
"tn_physics_create_joint",
|
|
1827
|
+
"tn_physics_create_vehicle",
|
|
1828
|
+
"tn_physics_destroy",
|
|
1829
|
+
"tn_physics_drain_collision_events",
|
|
1830
|
+
"tn_physics_free",
|
|
1831
|
+
"tn_physics_intersect_point",
|
|
1832
|
+
"tn_physics_intersect_ray",
|
|
1833
|
+
"tn_physics_intersect_shape",
|
|
1834
|
+
"tn_physics_read_area_intersections",
|
|
1835
|
+
"tn_physics_read_body_linear_velocity",
|
|
1836
|
+
"tn_physics_read_body_sleep_states",
|
|
1837
|
+
"tn_physics_read_character_states",
|
|
1838
|
+
"tn_physics_read_contacts",
|
|
1839
|
+
"tn_physics_read_vehicle_state",
|
|
1840
|
+
"tn_physics_read_visible_transforms",
|
|
1841
|
+
"tn_physics_remove_body",
|
|
1842
|
+
"tn_physics_remove_joint",
|
|
1843
|
+
"tn_physics_reset_vehicle",
|
|
1844
|
+
"tn_physics_set_body_linear_velocity",
|
|
1845
|
+
"tn_physics_set_body_transform",
|
|
1846
|
+
"tn_physics_set_heightfield_shape",
|
|
1847
|
+
"tn_physics_set_vehicle_input",
|
|
1848
|
+
"tn_physics_set_vehicle_wheel_friction",
|
|
1849
|
+
"tn_physics_step",
|
|
1850
|
+
"tn_physics_version"
|
|
1851
|
+
];
|
|
1852
|
+
var MAX_EXACT_FLOAT_ID = 16777215;
|
|
1853
|
+
var F32_MAX = 34028234663852886e22;
|
|
1854
|
+
var ACTUATION_OK = 1;
|
|
1855
|
+
var RECORD_BYTES = 128;
|
|
1856
|
+
var RAY_HIT = 32;
|
|
1857
|
+
var QUERY_HIT_BYTES = 16;
|
|
1858
|
+
var WHEEL_WIDTH = 12;
|
|
1859
|
+
var INVALID = /* @__PURE__ */ Symbol("invalid");
|
|
1860
|
+
function fields(value) {
|
|
1861
|
+
if (typeof value !== "object" || value === null) throw INVALID;
|
|
1862
|
+
return value;
|
|
1863
|
+
}
|
|
1864
|
+
function finite(object, name) {
|
|
1865
|
+
const value = object[name];
|
|
1866
|
+
if (typeof value !== "number" || !(value >= -F32_MAX && value <= F32_MAX)) throw INVALID;
|
|
1867
|
+
return value;
|
|
1868
|
+
}
|
|
1869
|
+
function optionalFinite(object, name, fallback) {
|
|
1870
|
+
return object[name] === void 0 ? fallback : finite(object, name);
|
|
1871
|
+
}
|
|
1872
|
+
function group(object, name, fallback) {
|
|
1873
|
+
const value = object[name];
|
|
1874
|
+
if (value === void 0) return fallback;
|
|
1875
|
+
if (typeof value !== "number" || !Number.isInteger(value) || value < 0 || value > 4294967295)
|
|
1876
|
+
throw INVALID;
|
|
1877
|
+
return value;
|
|
1878
|
+
}
|
|
1879
|
+
function flag(object, name, fallback) {
|
|
1880
|
+
const value = object[name];
|
|
1881
|
+
if (value === void 0) return fallback;
|
|
1882
|
+
if (typeof value !== "boolean") throw INVALID;
|
|
1883
|
+
return value;
|
|
1884
|
+
}
|
|
1885
|
+
function bodyId(value) {
|
|
1886
|
+
if (typeof value !== "number" || !Number.isInteger(value) || value < 0 || value > MAX_EXACT_FLOAT_ID)
|
|
1887
|
+
throw INVALID;
|
|
1888
|
+
return value;
|
|
1889
|
+
}
|
|
1890
|
+
function maxResults(object) {
|
|
1891
|
+
const value = object.maxResults;
|
|
1892
|
+
if (typeof value !== "number" || !Number.isInteger(value) || value < 1 || value > 4294967295)
|
|
1893
|
+
throw INVALID;
|
|
1894
|
+
return value;
|
|
1895
|
+
}
|
|
1896
|
+
function gridCount(object, name) {
|
|
1897
|
+
const value = Math.fround(finite(object, name));
|
|
1898
|
+
if (value < 2 || value > 65536 || !Number.isInteger(value)) throw INVALID;
|
|
1899
|
+
return value;
|
|
1900
|
+
}
|
|
1901
|
+
function fail(error, message) {
|
|
1902
|
+
return error === INVALID ? new Error(message) : error;
|
|
1903
|
+
}
|
|
1904
|
+
function actuationError(status) {
|
|
1905
|
+
if (status === 0)
|
|
1906
|
+
return new Error("TN_PHYSICS_UNKNOWN_BODY: actuation references an unknown body.");
|
|
1907
|
+
if (status === -1) return new Error("TN_PHYSICS_NOT_DYNAMIC: actuation needs a dynamic body.");
|
|
1908
|
+
if (status === -2) return new Error("TN_PHYSICS_NON_FINITE: actuation vector must be finite.");
|
|
1909
|
+
return new Error("TN_NATIVE_PHYSICS_INVALID: actuation was rejected.");
|
|
1910
|
+
}
|
|
1911
|
+
function createWasmPhysicsHost(exports) {
|
|
1912
|
+
if (!(exports.memory instanceof WebAssembly.Memory) || EXPORTS.some((name) => typeof exports[name] !== "function"))
|
|
1913
|
+
throw new Error("TN_NATIVE_PHYSICS_MISSING: Wasm module does not export the physics ABI");
|
|
1914
|
+
const wasm = exports;
|
|
1915
|
+
let buffer;
|
|
1916
|
+
let f32 = new Float32Array(0);
|
|
1917
|
+
let u32 = new Uint32Array(0);
|
|
1918
|
+
let u8 = new Uint8Array(0);
|
|
1919
|
+
const views = () => {
|
|
1920
|
+
if (wasm.memory.buffer === buffer) return;
|
|
1921
|
+
buffer = wasm.memory.buffer;
|
|
1922
|
+
f32 = new Float32Array(buffer);
|
|
1923
|
+
u32 = new Uint32Array(buffer);
|
|
1924
|
+
u8 = new Uint8Array(buffer);
|
|
1925
|
+
};
|
|
1926
|
+
const alloc = (bytes) => {
|
|
1927
|
+
const pointer = wasm.tn_physics_alloc(bytes) >>> 0;
|
|
1928
|
+
if (pointer === 0) throw new Error("TN_NATIVE_PHYSICS_INVALID: Wasm physics allocation failed");
|
|
1929
|
+
return pointer;
|
|
1930
|
+
};
|
|
1931
|
+
const versionPointer = wasm.tn_physics_version() >>> 0;
|
|
1932
|
+
views();
|
|
1933
|
+
const version = new TextDecoder().decode(
|
|
1934
|
+
u8.subarray(versionPointer, u8.indexOf(0, versionPointer))
|
|
1935
|
+
);
|
|
1936
|
+
const writeVector = (object, name, index) => {
|
|
1937
|
+
const vector = fields(object[name]);
|
|
1938
|
+
f32[index] = finite(vector, "x");
|
|
1939
|
+
f32[index + 1] = finite(vector, "y");
|
|
1940
|
+
f32[index + 2] = finite(vector, "z");
|
|
1941
|
+
};
|
|
1942
|
+
const writeQuaternion = (object, name, index) => {
|
|
1943
|
+
const quaternion = fields(object[name]);
|
|
1944
|
+
f32[index] = finite(quaternion, "x");
|
|
1945
|
+
f32[index + 1] = finite(quaternion, "y");
|
|
1946
|
+
f32[index + 2] = finite(quaternion, "z");
|
|
1947
|
+
f32[index + 3] = finite(quaternion, "w");
|
|
1948
|
+
};
|
|
1949
|
+
const readGravity = (options, index) => {
|
|
1950
|
+
const gravity = options.gravity;
|
|
1951
|
+
if (gravity === void 0) return;
|
|
1952
|
+
if (typeof gravity === "number") {
|
|
1953
|
+
if (!Number.isFinite(gravity)) throw INVALID;
|
|
1954
|
+
f32[index + 1] = gravity;
|
|
1955
|
+
return;
|
|
1956
|
+
}
|
|
1957
|
+
if (Array.isArray(gravity)) {
|
|
1958
|
+
for (let axis = 0; axis < 3; axis += 1) {
|
|
1959
|
+
const value = gravity[axis];
|
|
1960
|
+
if (typeof value !== "number" || !Number.isFinite(Math.fround(value))) throw INVALID;
|
|
1961
|
+
f32[index + axis] = value;
|
|
1962
|
+
}
|
|
1963
|
+
return;
|
|
1964
|
+
}
|
|
1965
|
+
const vector = fields(gravity);
|
|
1966
|
+
f32[index] = finite(vector, "x");
|
|
1967
|
+
f32[index + 1] = finite(vector, "y");
|
|
1968
|
+
f32[index + 2] = finite(vector, "z");
|
|
1969
|
+
};
|
|
1970
|
+
const createSimulation = (options) => {
|
|
1971
|
+
const record = alloc(RECORD_BYTES);
|
|
1972
|
+
views();
|
|
1973
|
+
const r = record >>> 2;
|
|
1974
|
+
f32[r] = 0;
|
|
1975
|
+
f32[r + 1] = -9.81;
|
|
1976
|
+
f32[r + 2] = 0;
|
|
1977
|
+
try {
|
|
1978
|
+
if (options !== void 0) readGravity(fields(options), r);
|
|
1979
|
+
} catch (error) {
|
|
1980
|
+
wasm.tn_physics_free(record, RECORD_BYTES);
|
|
1981
|
+
throw fail(error, "physics options contain invalid gravity");
|
|
1982
|
+
}
|
|
1983
|
+
let simulation = wasm.tn_physics_create(record) >>> 0;
|
|
1984
|
+
if (simulation === 0) {
|
|
1985
|
+
wasm.tn_physics_free(record, RECORD_BYTES);
|
|
1986
|
+
throw new Error("Rapier simulation creation failed");
|
|
1987
|
+
}
|
|
1988
|
+
let nextId = 0;
|
|
1989
|
+
let nextJointId = 0;
|
|
1990
|
+
let nextVehicleId = 0;
|
|
1991
|
+
const first = { bytes: 0, ptr: 0 };
|
|
1992
|
+
const second = { bytes: 0, ptr: 0 };
|
|
1993
|
+
const ensure = (scratch, bytes) => {
|
|
1994
|
+
if (scratch.bytes >= bytes) return scratch.ptr;
|
|
1995
|
+
const size = Math.max(64, bytes, scratch.bytes * 2);
|
|
1996
|
+
if (scratch.bytes > 0) wasm.tn_physics_free(scratch.ptr, scratch.bytes);
|
|
1997
|
+
scratch.ptr = 0;
|
|
1998
|
+
scratch.bytes = 0;
|
|
1999
|
+
scratch.ptr = alloc(size);
|
|
2000
|
+
scratch.bytes = size;
|
|
2001
|
+
return scratch.ptr;
|
|
2002
|
+
};
|
|
2003
|
+
const live = () => {
|
|
2004
|
+
if (simulation === 0) throw new Error("physics simulation is disposed");
|
|
2005
|
+
};
|
|
2006
|
+
const copyIn = (scratch, values) => {
|
|
2007
|
+
const pointer = ensure(scratch, Math.max(4, values.byteLength));
|
|
2008
|
+
views();
|
|
2009
|
+
(values instanceof Float32Array ? f32 : u32).set(values, pointer >>> 2);
|
|
2010
|
+
return pointer;
|
|
2011
|
+
};
|
|
2012
|
+
const bulkRead = (name, output, float, width, method, label) => {
|
|
2013
|
+
live();
|
|
2014
|
+
if (!(float ? output instanceof Float32Array : output instanceof Uint32Array))
|
|
2015
|
+
throw new Error(`${method} requires a ${float ? "Float32Array" : "Uint32Array"}`);
|
|
2016
|
+
const target = output;
|
|
2017
|
+
const pointer = ensure(first, Math.max(4, target.byteLength));
|
|
2018
|
+
const count = wasm[name](simulation, pointer, target.length);
|
|
2019
|
+
if (count < 0) throw new Error(`${label} buffer is too small`);
|
|
2020
|
+
views();
|
|
2021
|
+
const start = pointer >>> 2;
|
|
2022
|
+
target.set((float ? f32 : u32).subarray(start, start + count * width));
|
|
2023
|
+
return count;
|
|
2024
|
+
};
|
|
2025
|
+
const queryHits = (pointer, count) => {
|
|
2026
|
+
views();
|
|
2027
|
+
const hits = [];
|
|
2028
|
+
for (let index = 0; index < count; index += 1) {
|
|
2029
|
+
const h = (pointer >>> 2) + index * (QUERY_HIT_BYTES / 4);
|
|
2030
|
+
hits.push({
|
|
2031
|
+
bodyId: u32[h],
|
|
2032
|
+
position: { x: f32[h + 1], y: f32[h + 2], z: f32[h + 3] }
|
|
2033
|
+
});
|
|
2034
|
+
}
|
|
2035
|
+
return hits;
|
|
2036
|
+
};
|
|
2037
|
+
const actuate = (name, args, message) => {
|
|
2038
|
+
live();
|
|
2039
|
+
try {
|
|
2040
|
+
const id = bodyId(args[0]);
|
|
2041
|
+
const vector = fields(args[1]);
|
|
2042
|
+
const status = wasm[name](
|
|
2043
|
+
simulation,
|
|
2044
|
+
id,
|
|
2045
|
+
finite(vector, "x"),
|
|
2046
|
+
finite(vector, "y"),
|
|
2047
|
+
finite(vector, "z")
|
|
2048
|
+
);
|
|
2049
|
+
if (status !== ACTUATION_OK) throw actuationError(status);
|
|
2050
|
+
} catch (error) {
|
|
2051
|
+
throw fail(error, message);
|
|
2052
|
+
}
|
|
2053
|
+
};
|
|
2054
|
+
const host = {
|
|
2055
|
+
createBody(options2) {
|
|
2056
|
+
live();
|
|
2057
|
+
if (options2 === void 0 || nextId > MAX_EXACT_FLOAT_ID)
|
|
2058
|
+
throw new Error("createBody requires options and an available id");
|
|
2059
|
+
let added;
|
|
2060
|
+
try {
|
|
2061
|
+
views();
|
|
2062
|
+
const value = fields(options2);
|
|
2063
|
+
const r2 = record >>> 2;
|
|
2064
|
+
const bodyType = ["dynamic", "fixed", "kinematic", "character"].indexOf(
|
|
2065
|
+
value.type
|
|
2066
|
+
);
|
|
2067
|
+
const shape = fields(value.shape);
|
|
2068
|
+
const shapeType = ["box", "sphere", "capsule", "trimesh", "heightfield"].indexOf(
|
|
2069
|
+
shape.kind
|
|
2070
|
+
);
|
|
2071
|
+
if (typeof value.type !== "string" || bodyType < 0) throw INVALID;
|
|
2072
|
+
if (typeof shape.kind !== "string" || shapeType < 0) throw INVALID;
|
|
2073
|
+
u32[r2] = nextId;
|
|
2074
|
+
u32[r2 + 1] = bodyType;
|
|
2075
|
+
u32[r2 + 2] = shapeType;
|
|
2076
|
+
writeVector(value, "position", r2 + 3);
|
|
2077
|
+
writeQuaternion(value, "rotation", r2 + 6);
|
|
2078
|
+
f32[r2 + 10] = finite(shape, "x");
|
|
2079
|
+
f32[r2 + 11] = optionalFinite(shape, "y", 0);
|
|
2080
|
+
f32[r2 + 12] = optionalFinite(shape, "z", 0);
|
|
2081
|
+
f32[r2 + 13] = optionalFinite(value, "mass", 0);
|
|
2082
|
+
u32[r2 + 14] = group(value, "collisionLayer", 1);
|
|
2083
|
+
u32[r2 + 15] = group(value, "collisionMask", 65535);
|
|
2084
|
+
u8[record + 64] = flag(value, "sensor", false) ? 1 : 0;
|
|
2085
|
+
u8[record + 65] = flag(value, "continuousCollision", true) ? 1 : 0;
|
|
2086
|
+
if (shapeType === 3) {
|
|
2087
|
+
const { vertices, indices } = shape;
|
|
2088
|
+
if (!(vertices instanceof Float32Array) || !(indices instanceof Uint32Array))
|
|
2089
|
+
throw INVALID;
|
|
2090
|
+
const vertexPointer = copyIn(first, vertices);
|
|
2091
|
+
const indexPointer = copyIn(second, indices);
|
|
2092
|
+
added = wasm.tn_physics_add_trimesh_body(
|
|
2093
|
+
simulation,
|
|
2094
|
+
record,
|
|
2095
|
+
vertexPointer,
|
|
2096
|
+
vertices.length,
|
|
2097
|
+
indexPointer,
|
|
2098
|
+
indices.length
|
|
2099
|
+
);
|
|
2100
|
+
} else if (shapeType === 4) {
|
|
2101
|
+
const heights = shape.heights;
|
|
2102
|
+
if (!(heights instanceof Float32Array)) throw INVALID;
|
|
2103
|
+
const rows = gridCount(shape, "rows");
|
|
2104
|
+
const columns = gridCount(shape, "columns");
|
|
2105
|
+
const scale = fields(shape.scale);
|
|
2106
|
+
const sx = finite(scale, "x");
|
|
2107
|
+
const sy = finite(scale, "y");
|
|
2108
|
+
const sz = finite(scale, "z");
|
|
2109
|
+
added = wasm.tn_physics_add_heightfield_body(
|
|
2110
|
+
simulation,
|
|
2111
|
+
record,
|
|
2112
|
+
copyIn(first, heights),
|
|
2113
|
+
heights.length,
|
|
2114
|
+
rows,
|
|
2115
|
+
columns,
|
|
2116
|
+
sx,
|
|
2117
|
+
sy,
|
|
2118
|
+
sz
|
|
2119
|
+
);
|
|
2120
|
+
} else added = wasm.tn_physics_add_body(simulation, record);
|
|
2121
|
+
} catch (error) {
|
|
2122
|
+
throw fail(error, "physics body options are invalid");
|
|
2123
|
+
}
|
|
2124
|
+
if (added === 0) throw new Error("physics body options are invalid");
|
|
2125
|
+
const id = nextId;
|
|
2126
|
+
nextId += 1;
|
|
2127
|
+
return id;
|
|
2128
|
+
},
|
|
2129
|
+
createJoint(options2) {
|
|
2130
|
+
live();
|
|
2131
|
+
if (options2 === void 0 || nextJointId > MAX_EXACT_FLOAT_ID)
|
|
2132
|
+
throw new Error("createJoint requires options and an available id");
|
|
2133
|
+
try {
|
|
2134
|
+
views();
|
|
2135
|
+
const value = fields(options2);
|
|
2136
|
+
const r2 = record >>> 2;
|
|
2137
|
+
u8.fill(0, record, record + 96);
|
|
2138
|
+
const jointType = ["pin", "hinge", "fixed"].indexOf(value.type);
|
|
2139
|
+
if (typeof value.type !== "string" || jointType < 0) throw INVALID;
|
|
2140
|
+
u32[r2] = nextJointId;
|
|
2141
|
+
u32[r2 + 1] = jointType;
|
|
2142
|
+
u32[r2 + 2] = bodyId(value.bodyA);
|
|
2143
|
+
u32[r2 + 3] = bodyId(value.bodyB);
|
|
2144
|
+
writeVector(value, "anchorA", r2 + 4);
|
|
2145
|
+
writeVector(value, "anchorB", r2 + 7);
|
|
2146
|
+
f32[r2 + 10] = 1;
|
|
2147
|
+
if (value.axis !== void 0) writeVector(value, "axis", r2 + 10);
|
|
2148
|
+
if (value.limit !== void 0) {
|
|
2149
|
+
if (jointType !== 1) throw INVALID;
|
|
2150
|
+
const limit = fields(value.limit);
|
|
2151
|
+
f32[r2 + 14] = finite(limit, "lower");
|
|
2152
|
+
f32[r2 + 15] = finite(limit, "upper");
|
|
2153
|
+
u8[record + 52] = 1;
|
|
2154
|
+
}
|
|
2155
|
+
if (jointType !== 2 && (value.frameA !== void 0 || value.frameB !== void 0))
|
|
2156
|
+
throw INVALID;
|
|
2157
|
+
f32[r2 + 19] = 1;
|
|
2158
|
+
f32[r2 + 23] = 1;
|
|
2159
|
+
if (value.frameA !== void 0) writeQuaternion(value, "frameA", r2 + 16);
|
|
2160
|
+
if (value.frameB !== void 0) writeQuaternion(value, "frameB", r2 + 20);
|
|
2161
|
+
} catch (error) {
|
|
2162
|
+
throw fail(error, "physics joint options are invalid");
|
|
2163
|
+
}
|
|
2164
|
+
const result = wasm.tn_physics_create_joint(simulation, record);
|
|
2165
|
+
if (result < 0) throw new Error("physics joint options were rejected");
|
|
2166
|
+
nextJointId += 1;
|
|
2167
|
+
return result;
|
|
2168
|
+
},
|
|
2169
|
+
removeBody(id) {
|
|
2170
|
+
live();
|
|
2171
|
+
if (typeof id !== "number") throw new Error("removeBody requires a numeric id");
|
|
2172
|
+
try {
|
|
2173
|
+
wasm.tn_physics_remove_body(simulation, bodyId(id));
|
|
2174
|
+
} catch (error) {
|
|
2175
|
+
throw fail(error, "removeBody received an invalid id");
|
|
2176
|
+
}
|
|
2177
|
+
},
|
|
2178
|
+
removeJoint(id) {
|
|
2179
|
+
live();
|
|
2180
|
+
try {
|
|
2181
|
+
wasm.tn_physics_remove_joint(simulation, bodyId(id));
|
|
2182
|
+
} catch (error) {
|
|
2183
|
+
throw fail(error, "removeJoint received an invalid id");
|
|
2184
|
+
}
|
|
2185
|
+
},
|
|
2186
|
+
configureCharacter(id, options2) {
|
|
2187
|
+
live();
|
|
2188
|
+
if (typeof id !== "number" || options2 === void 0)
|
|
2189
|
+
throw new Error("configureCharacter requires an id and options");
|
|
2190
|
+
try {
|
|
2191
|
+
bodyId(id);
|
|
2192
|
+
} catch (error) {
|
|
2193
|
+
throw fail(error, "configureCharacter received an invalid id");
|
|
2194
|
+
}
|
|
2195
|
+
let configured;
|
|
2196
|
+
try {
|
|
2197
|
+
views();
|
|
2198
|
+
const value = fields(options2);
|
|
2199
|
+
const r2 = record >>> 2;
|
|
2200
|
+
u8.fill(0, record, record + 40);
|
|
2201
|
+
u32[r2] = id;
|
|
2202
|
+
f32[r2 + 1] = finite(value, "offset");
|
|
2203
|
+
f32[r2 + 2] = finite(value, "maxSlopeClimbAngle");
|
|
2204
|
+
u32[r2 + 8] = group(value, "oneWayLayers", 0);
|
|
2205
|
+
if (value.autostep !== void 0) {
|
|
2206
|
+
const autostep = fields(value.autostep);
|
|
2207
|
+
f32[r2 + 4] = finite(autostep, "maxHeight");
|
|
2208
|
+
f32[r2 + 5] = finite(autostep, "minWidth");
|
|
2209
|
+
if (typeof autostep.includeDynamicBodies !== "boolean") throw INVALID;
|
|
2210
|
+
u8[record + 12] = 1;
|
|
2211
|
+
u8[record + 24] = autostep.includeDynamicBodies ? 1 : 0;
|
|
2212
|
+
}
|
|
2213
|
+
const snap = value.snapToGround;
|
|
2214
|
+
if (snap !== void 0) {
|
|
2215
|
+
if (typeof snap !== "number" || !Number.isFinite(snap)) throw INVALID;
|
|
2216
|
+
u8[record + 25] = 1;
|
|
2217
|
+
f32[r2 + 7] = snap;
|
|
2218
|
+
}
|
|
2219
|
+
u8[record + 36] = flag(value, "pushesDynamicBodies", false) ? 1 : 0;
|
|
2220
|
+
configured = wasm.tn_physics_configure_character(simulation, record);
|
|
2221
|
+
} catch (error) {
|
|
2222
|
+
throw fail(error, "character controller options are invalid");
|
|
2223
|
+
}
|
|
2224
|
+
if (configured === 0) throw new Error("character controller options are invalid");
|
|
2225
|
+
},
|
|
2226
|
+
createVehicle(options2) {
|
|
2227
|
+
live();
|
|
2228
|
+
if (options2 === void 0 || nextVehicleId > MAX_EXACT_FLOAT_ID)
|
|
2229
|
+
throw new Error("createVehicle requires options and an available id");
|
|
2230
|
+
let id;
|
|
2231
|
+
try {
|
|
2232
|
+
const value = fields(options2);
|
|
2233
|
+
const chassis = bodyId(value.bodyId);
|
|
2234
|
+
const axle = fields(value.axle);
|
|
2235
|
+
const ax = finite(axle, "x");
|
|
2236
|
+
const ay = finite(axle, "y");
|
|
2237
|
+
const az = finite(axle, "z");
|
|
2238
|
+
const axis = value.forwardAxis;
|
|
2239
|
+
if (axis !== 0 && axis !== 2) throw INVALID;
|
|
2240
|
+
const wheels = value.wheels;
|
|
2241
|
+
if (!(wheels instanceof Float32Array) || wheels.length < WHEEL_WIDTH) throw INVALID;
|
|
2242
|
+
const wheelPointer = copyIn(first, wheels);
|
|
2243
|
+
const r2 = record >>> 2;
|
|
2244
|
+
u32[r2] = nextVehicleId;
|
|
2245
|
+
u32[r2 + 1] = chassis;
|
|
2246
|
+
u32[r2 + 2] = axis;
|
|
2247
|
+
f32[r2 + 3] = ax;
|
|
2248
|
+
f32[r2 + 4] = ay;
|
|
2249
|
+
f32[r2 + 5] = az;
|
|
2250
|
+
u32[r2 + 6] = Math.floor(wheels.length / WHEEL_WIDTH);
|
|
2251
|
+
id = wasm.tn_physics_create_vehicle(simulation, record, wheelPointer);
|
|
2252
|
+
} catch (error) {
|
|
2253
|
+
throw fail(error, "vehicle options are invalid");
|
|
2254
|
+
}
|
|
2255
|
+
if (id < 0)
|
|
2256
|
+
throw new Error(
|
|
2257
|
+
"TN_VEHICLE_INVALID: the chassis is missing, is not dynamic, or the wheels are malformed."
|
|
2258
|
+
);
|
|
2259
|
+
nextVehicleId += 1;
|
|
2260
|
+
return id;
|
|
2261
|
+
},
|
|
2262
|
+
setVehicleInput(id, input) {
|
|
2263
|
+
live();
|
|
2264
|
+
let applied;
|
|
2265
|
+
try {
|
|
2266
|
+
const value = fields(input);
|
|
2267
|
+
applied = wasm.tn_physics_set_vehicle_input(
|
|
2268
|
+
simulation,
|
|
2269
|
+
bodyId(id),
|
|
2270
|
+
finite(value, "engineForce"),
|
|
2271
|
+
finite(value, "brake"),
|
|
2272
|
+
finite(value, "steering")
|
|
2273
|
+
);
|
|
2274
|
+
} catch (error) {
|
|
2275
|
+
throw fail(
|
|
2276
|
+
error,
|
|
2277
|
+
"TN_PHYSICS_NON_FINITE: vehicle input needs a finite engineForce, brake and steering."
|
|
2278
|
+
);
|
|
2279
|
+
}
|
|
2280
|
+
if (applied === 0) throw new Error("TN_VEHICLE_UNKNOWN: the vehicle left the backend.");
|
|
2281
|
+
},
|
|
2282
|
+
setVehicleWheelFriction(id, wheel, frictionSlip) {
|
|
2283
|
+
live();
|
|
2284
|
+
try {
|
|
2285
|
+
bodyId(id);
|
|
2286
|
+
} catch (error) {
|
|
2287
|
+
throw fail(error, "setVehicleWheelFriction requires an id, a wheel and a friction slip");
|
|
2288
|
+
}
|
|
2289
|
+
if (typeof wheel !== "number" || typeof frictionSlip !== "number")
|
|
2290
|
+
throw new Error("setVehicleWheelFriction requires an id, a wheel and a friction slip");
|
|
2291
|
+
if (!Number.isInteger(wheel) || wheel < 0 || wheel > 4294967295)
|
|
2292
|
+
throw new Error("TN_VEHICLE_INVALID: the wheel index is not a whole number.");
|
|
2293
|
+
if (wasm.tn_physics_set_vehicle_wheel_friction(simulation, id, wheel, frictionSlip) === 0)
|
|
2294
|
+
throw new Error(
|
|
2295
|
+
"TN_VEHICLE_UNKNOWN: the vehicle or wheel left the backend, or the friction slip is not finite and non-negative."
|
|
2296
|
+
);
|
|
2297
|
+
},
|
|
2298
|
+
readVehicleState(id, output) {
|
|
2299
|
+
live();
|
|
2300
|
+
try {
|
|
2301
|
+
bodyId(id);
|
|
2302
|
+
} catch (error) {
|
|
2303
|
+
throw fail(error, "readVehicleState requires an id and a Float32Array");
|
|
2304
|
+
}
|
|
2305
|
+
if (!(output instanceof Float32Array))
|
|
2306
|
+
throw new Error("readVehicleState requires an id and a Float32Array");
|
|
2307
|
+
const pointer = ensure(first, Math.max(4, output.byteLength));
|
|
2308
|
+
const count = wasm.tn_physics_read_vehicle_state(simulation, id, pointer, output.length);
|
|
2309
|
+
if (count < 0)
|
|
2310
|
+
throw new Error(
|
|
2311
|
+
"TN_VEHICLE_UNKNOWN: the vehicle left the backend or the buffer is too small."
|
|
2312
|
+
);
|
|
2313
|
+
views();
|
|
2314
|
+
output.set(f32.subarray(pointer >>> 2, (pointer >>> 2) + count));
|
|
2315
|
+
return count;
|
|
2316
|
+
},
|
|
2317
|
+
resetVehicle(id, position, yaw) {
|
|
2318
|
+
live();
|
|
2319
|
+
let reset;
|
|
2320
|
+
try {
|
|
2321
|
+
const value = fields(position);
|
|
2322
|
+
if (typeof yaw !== "number" || !(yaw >= -F32_MAX && yaw <= F32_MAX)) throw INVALID;
|
|
2323
|
+
reset = wasm.tn_physics_reset_vehicle(
|
|
2324
|
+
simulation,
|
|
2325
|
+
bodyId(id),
|
|
2326
|
+
finite(value, "x"),
|
|
2327
|
+
finite(value, "y"),
|
|
2328
|
+
finite(value, "z"),
|
|
2329
|
+
yaw
|
|
2330
|
+
);
|
|
2331
|
+
} catch (error) {
|
|
2332
|
+
throw fail(error, "TN_PHYSICS_NON_FINITE: a respawn needs a finite position and yaw.");
|
|
2333
|
+
}
|
|
2334
|
+
if (reset === 0) throw new Error("TN_VEHICLE_UNKNOWN: the vehicle left the backend.");
|
|
2335
|
+
},
|
|
2336
|
+
setBodyTransform(id, position) {
|
|
2337
|
+
live();
|
|
2338
|
+
if (typeof id !== "number" || !Number.isFinite(id) || position === void 0)
|
|
2339
|
+
throw new Error("setBodyTransform requires an id and position");
|
|
2340
|
+
if (id < 0 || id > MAX_EXACT_FLOAT_ID || !Number.isInteger(id))
|
|
2341
|
+
throw new Error("setBodyTransform received an invalid id");
|
|
2342
|
+
let moved;
|
|
2343
|
+
try {
|
|
2344
|
+
const value = fields(position);
|
|
2345
|
+
moved = wasm.tn_physics_set_body_transform(
|
|
2346
|
+
simulation,
|
|
2347
|
+
id,
|
|
2348
|
+
finite(value, "x"),
|
|
2349
|
+
finite(value, "y"),
|
|
2350
|
+
finite(value, "z")
|
|
2351
|
+
);
|
|
2352
|
+
} catch (error) {
|
|
2353
|
+
throw fail(error, "setBodyTransform received an invalid position");
|
|
2354
|
+
}
|
|
2355
|
+
if (moved === 0) throw new Error("setBodyTransform received an unknown id");
|
|
2356
|
+
},
|
|
2357
|
+
applyBodyImpulse(id, impulse) {
|
|
2358
|
+
actuate(
|
|
2359
|
+
"tn_physics_apply_body_impulse",
|
|
2360
|
+
[id, impulse],
|
|
2361
|
+
"TN_PHYSICS_NON_FINITE: impulse must be a finite { x, y, z }."
|
|
2362
|
+
);
|
|
2363
|
+
},
|
|
2364
|
+
applyBodyForce(id, force) {
|
|
2365
|
+
actuate(
|
|
2366
|
+
"tn_physics_apply_body_force",
|
|
2367
|
+
[id, force],
|
|
2368
|
+
"TN_PHYSICS_NON_FINITE: force must be a finite { x, y, z }."
|
|
2369
|
+
);
|
|
2370
|
+
},
|
|
2371
|
+
setBodyLinearVelocity(id, velocity) {
|
|
2372
|
+
actuate(
|
|
2373
|
+
"tn_physics_set_body_linear_velocity",
|
|
2374
|
+
[id, velocity],
|
|
2375
|
+
"TN_PHYSICS_NON_FINITE: velocity must be a finite { x, y, z }."
|
|
2376
|
+
);
|
|
2377
|
+
},
|
|
2378
|
+
applyBodyForceAtPoint(id, force, point) {
|
|
2379
|
+
live();
|
|
2380
|
+
try {
|
|
2381
|
+
const f = fields(force);
|
|
2382
|
+
const p = fields(point);
|
|
2383
|
+
const status = wasm.tn_physics_apply_body_force_at_point(
|
|
2384
|
+
simulation,
|
|
2385
|
+
bodyId(id),
|
|
2386
|
+
finite(f, "x"),
|
|
2387
|
+
finite(f, "y"),
|
|
2388
|
+
finite(f, "z"),
|
|
2389
|
+
finite(p, "x"),
|
|
2390
|
+
finite(p, "y"),
|
|
2391
|
+
finite(p, "z")
|
|
2392
|
+
);
|
|
2393
|
+
if (status !== ACTUATION_OK) throw actuationError(status);
|
|
2394
|
+
} catch (error) {
|
|
2395
|
+
throw fail(
|
|
2396
|
+
error,
|
|
2397
|
+
"TN_PHYSICS_NON_FINITE: force and point must be finite { x, y, z } values."
|
|
2398
|
+
);
|
|
2399
|
+
}
|
|
2400
|
+
},
|
|
2401
|
+
readBodyLinearVelocity(id) {
|
|
2402
|
+
live();
|
|
2403
|
+
try {
|
|
2404
|
+
bodyId(id);
|
|
2405
|
+
} catch (error) {
|
|
2406
|
+
throw fail(error, "readBodyLinearVelocity received an invalid id");
|
|
2407
|
+
}
|
|
2408
|
+
const status = wasm.tn_physics_read_body_linear_velocity(simulation, id, record);
|
|
2409
|
+
if (status !== ACTUATION_OK) throw actuationError(status);
|
|
2410
|
+
views();
|
|
2411
|
+
const r2 = record >>> 2;
|
|
2412
|
+
return { x: f32[r2], y: f32[r2 + 1], z: f32[r2 + 2] };
|
|
2413
|
+
},
|
|
2414
|
+
step(deltaTime, inputSnapshot) {
|
|
2415
|
+
live();
|
|
2416
|
+
if (typeof deltaTime !== "number") throw new Error("step requires a numeric deltaTime");
|
|
2417
|
+
let input = 0;
|
|
2418
|
+
let count = 0;
|
|
2419
|
+
if (inputSnapshot !== void 0) {
|
|
2420
|
+
if (typeof inputSnapshot !== "object" || inputSnapshot === null)
|
|
2421
|
+
throw new Error("physics input snapshot must be an object");
|
|
2422
|
+
const { kinematicCount, kinematicTransforms } = inputSnapshot;
|
|
2423
|
+
if (typeof kinematicCount !== "number" || !(kinematicTransforms instanceof Float32Array))
|
|
2424
|
+
throw new Error("physics input snapshot is malformed");
|
|
2425
|
+
if (!Number.isInteger(kinematicCount) || kinematicCount < 0 || kinematicCount > Math.floor(kinematicTransforms.length / 8))
|
|
2426
|
+
throw new Error("physics input snapshot has an invalid count");
|
|
2427
|
+
count = kinematicCount;
|
|
2428
|
+
if (count > 0) input = copyIn(first, kinematicTransforms.subarray(0, count * 8));
|
|
2429
|
+
}
|
|
2430
|
+
if (!Number.isFinite(deltaTime) || deltaTime <= 0 || wasm.tn_physics_step(simulation, deltaTime, input, count) === 0)
|
|
2431
|
+
throw new Error("physics step rejected its deltaTime or input");
|
|
2432
|
+
},
|
|
2433
|
+
readVisibleTransforms(output) {
|
|
2434
|
+
return bulkRead(
|
|
2435
|
+
"tn_physics_read_visible_transforms",
|
|
2436
|
+
output,
|
|
2437
|
+
true,
|
|
2438
|
+
8,
|
|
2439
|
+
"readVisibleTransforms",
|
|
2440
|
+
"transform"
|
|
2441
|
+
);
|
|
2442
|
+
},
|
|
2443
|
+
readCharacterStates(output) {
|
|
2444
|
+
return bulkRead(
|
|
2445
|
+
"tn_physics_read_character_states",
|
|
2446
|
+
output,
|
|
2447
|
+
true,
|
|
2448
|
+
6,
|
|
2449
|
+
"readCharacterStates",
|
|
2450
|
+
"character state"
|
|
2451
|
+
);
|
|
2452
|
+
},
|
|
2453
|
+
readBodySleepStates(output) {
|
|
2454
|
+
return bulkRead(
|
|
2455
|
+
"tn_physics_read_body_sleep_states",
|
|
2456
|
+
output,
|
|
2457
|
+
true,
|
|
2458
|
+
2,
|
|
2459
|
+
"readBodySleepStates",
|
|
2460
|
+
"sleep state"
|
|
2461
|
+
);
|
|
2462
|
+
},
|
|
2463
|
+
readAreaIntersections(output) {
|
|
2464
|
+
return bulkRead(
|
|
2465
|
+
"tn_physics_read_area_intersections",
|
|
2466
|
+
output,
|
|
2467
|
+
false,
|
|
2468
|
+
2,
|
|
2469
|
+
"readAreaIntersections",
|
|
2470
|
+
"area intersection"
|
|
2471
|
+
);
|
|
2472
|
+
},
|
|
2473
|
+
drainCollisionEvents(output) {
|
|
2474
|
+
return bulkRead(
|
|
2475
|
+
"tn_physics_drain_collision_events",
|
|
2476
|
+
output,
|
|
2477
|
+
false,
|
|
2478
|
+
4,
|
|
2479
|
+
"drainCollisionEvents",
|
|
2480
|
+
"collision event"
|
|
2481
|
+
);
|
|
2482
|
+
},
|
|
2483
|
+
readContacts(target, candidates, output) {
|
|
2484
|
+
live();
|
|
2485
|
+
try {
|
|
2486
|
+
bodyId(target);
|
|
2487
|
+
if (!(candidates instanceof Uint32Array) || !(output instanceof Float32Array))
|
|
2488
|
+
throw INVALID;
|
|
2489
|
+
} catch (error) {
|
|
2490
|
+
throw fail(error, "readContacts requires a body id, a Uint32Array and a Float32Array");
|
|
2491
|
+
}
|
|
2492
|
+
const candidatePointer = copyIn(second, candidates);
|
|
2493
|
+
const outputPointer = ensure(first, Math.max(4, output.byteLength));
|
|
2494
|
+
const count = wasm.tn_physics_read_contacts(
|
|
2495
|
+
simulation,
|
|
2496
|
+
target,
|
|
2497
|
+
candidatePointer,
|
|
2498
|
+
candidates.length,
|
|
2499
|
+
outputPointer,
|
|
2500
|
+
output.length
|
|
2501
|
+
);
|
|
2502
|
+
if (count < 0) throw new Error("readContacts target is not a live body");
|
|
2503
|
+
views();
|
|
2504
|
+
const written = Math.min(count, Math.floor(output.length / 8)) * 8;
|
|
2505
|
+
output.set(f32.subarray(outputPointer >>> 2, (outputPointer >>> 2) + written));
|
|
2506
|
+
return count;
|
|
2507
|
+
},
|
|
2508
|
+
setColliderShape(id, shape) {
|
|
2509
|
+
live();
|
|
2510
|
+
try {
|
|
2511
|
+
bodyId(id);
|
|
2512
|
+
fields(shape);
|
|
2513
|
+
} catch (error) {
|
|
2514
|
+
throw fail(error, "setColliderShape requires a body id and a shape");
|
|
2515
|
+
}
|
|
2516
|
+
if (typeof shape.kind !== "string")
|
|
2517
|
+
throw new Error("setColliderShape requires a shape kind");
|
|
2518
|
+
if (shape.kind !== "heightfield")
|
|
2519
|
+
throw new Error("setColliderShape supports heightfield shapes");
|
|
2520
|
+
try {
|
|
2521
|
+
const value = fields(shape);
|
|
2522
|
+
const heights = value.heights;
|
|
2523
|
+
if (!(heights instanceof Float32Array)) throw INVALID;
|
|
2524
|
+
const rows = gridCount(value, "rows");
|
|
2525
|
+
const columns = gridCount(value, "columns");
|
|
2526
|
+
const scale = fields(value.scale);
|
|
2527
|
+
const sx = finite(scale, "x");
|
|
2528
|
+
const sy = finite(scale, "y");
|
|
2529
|
+
const sz = finite(scale, "z");
|
|
2530
|
+
const pointer = copyIn(first, heights);
|
|
2531
|
+
if (wasm.tn_physics_set_heightfield_shape(
|
|
2532
|
+
simulation,
|
|
2533
|
+
id,
|
|
2534
|
+
pointer,
|
|
2535
|
+
heights.length,
|
|
2536
|
+
rows,
|
|
2537
|
+
columns,
|
|
2538
|
+
sx,
|
|
2539
|
+
sy,
|
|
2540
|
+
sz
|
|
2541
|
+
) === 0)
|
|
2542
|
+
throw INVALID;
|
|
2543
|
+
} catch (error) {
|
|
2544
|
+
throw fail(error, "setColliderShape shape was rejected");
|
|
2545
|
+
}
|
|
2546
|
+
},
|
|
2547
|
+
// The hot query: one export call, no object, records written in place.
|
|
2548
|
+
intersectRay(query, output) {
|
|
2549
|
+
live();
|
|
2550
|
+
if (output === void 0) throw new Error("intersectRay requires query options and output");
|
|
2551
|
+
if (!(output instanceof Float32Array))
|
|
2552
|
+
throw new Error("intersectRay requires a Float32Array output");
|
|
2553
|
+
if (output.length < 8) throw new Error("intersectRay output buffer is too small");
|
|
2554
|
+
const r2 = record >>> 2;
|
|
2555
|
+
try {
|
|
2556
|
+
views();
|
|
2557
|
+
const value = fields(query);
|
|
2558
|
+
writeVector(value, "from", r2);
|
|
2559
|
+
writeVector(value, "to", r2 + 3);
|
|
2560
|
+
u32[r2 + 6] = group(value, "collisionMask", 65535);
|
|
2561
|
+
u32[r2 + 7] = flag(value, "hitFromInside", false) ? 1 : 0;
|
|
2562
|
+
if (f32[r2] === f32[r2 + 3] && f32[r2 + 1] === f32[r2 + 4] && f32[r2 + 2] === f32[r2 + 5])
|
|
2563
|
+
throw INVALID;
|
|
2564
|
+
} catch (error) {
|
|
2565
|
+
throw fail(error, "intersectRay query options are invalid");
|
|
2566
|
+
}
|
|
2567
|
+
const status = wasm.tn_physics_intersect_ray(simulation, record, record + RAY_HIT);
|
|
2568
|
+
if (status < 0)
|
|
2569
|
+
throw new Error("intersectRay query arithmetic is invalid or unrepresentable");
|
|
2570
|
+
if (status === 0) return 0;
|
|
2571
|
+
if (status !== 1) throw new Error("intersectRay returned an invalid status");
|
|
2572
|
+
views();
|
|
2573
|
+
const h = r2 + RAY_HIT / 4;
|
|
2574
|
+
output[0] = u32[h];
|
|
2575
|
+
for (let index = 1; index < 8; index += 1) output[index] = f32[h + index];
|
|
2576
|
+
return 1;
|
|
2577
|
+
},
|
|
2578
|
+
intersectShape(query) {
|
|
2579
|
+
live();
|
|
2580
|
+
if (query === void 0) throw new Error("intersectShape requires query options");
|
|
2581
|
+
let pointer;
|
|
2582
|
+
let limit;
|
|
2583
|
+
try {
|
|
2584
|
+
const value = fields(query);
|
|
2585
|
+
const shape = fields(value.shape);
|
|
2586
|
+
const shapeType = ["box", "sphere", "capsule"].indexOf(shape.kind);
|
|
2587
|
+
if (typeof shape.kind !== "string" || shapeType < 0) throw INVALID;
|
|
2588
|
+
limit = maxResults(value);
|
|
2589
|
+
pointer = ensure(first, limit * QUERY_HIT_BYTES);
|
|
2590
|
+
views();
|
|
2591
|
+
const r2 = record >>> 2;
|
|
2592
|
+
u32[r2] = shapeType;
|
|
2593
|
+
f32[r2 + 1] = finite(shape, "x");
|
|
2594
|
+
f32[r2 + 2] = finite(shape, "y");
|
|
2595
|
+
f32[r2 + 3] = finite(shape, "z");
|
|
2596
|
+
writeVector(value, "position", r2 + 4);
|
|
2597
|
+
writeQuaternion(value, "rotation", r2 + 7);
|
|
2598
|
+
u32[r2 + 11] = group(value, "collisionMask", 65535);
|
|
2599
|
+
u32[r2 + 12] = limit;
|
|
2600
|
+
} catch (error) {
|
|
2601
|
+
throw fail(error, "intersectShape query options are invalid");
|
|
2602
|
+
}
|
|
2603
|
+
const count = wasm.tn_physics_intersect_shape(simulation, record, pointer, limit);
|
|
2604
|
+
if (count < 0) throw new Error("intersectShape query was rejected");
|
|
2605
|
+
return queryHits(pointer, count);
|
|
2606
|
+
},
|
|
2607
|
+
intersectPoint(query) {
|
|
2608
|
+
live();
|
|
2609
|
+
if (query === void 0) throw new Error("intersectPoint requires query options");
|
|
2610
|
+
let count;
|
|
2611
|
+
let pointer;
|
|
2612
|
+
try {
|
|
2613
|
+
const value = fields(query);
|
|
2614
|
+
const position = fields(value.position);
|
|
2615
|
+
const x = finite(position, "x");
|
|
2616
|
+
const y = finite(position, "y");
|
|
2617
|
+
const z = finite(position, "z");
|
|
2618
|
+
const mask = group(value, "collisionMask", 65535);
|
|
2619
|
+
const limit = maxResults(value);
|
|
2620
|
+
pointer = ensure(first, limit * QUERY_HIT_BYTES);
|
|
2621
|
+
count = wasm.tn_physics_intersect_point(simulation, x, y, z, mask, limit, pointer, limit);
|
|
2622
|
+
} catch (error) {
|
|
2623
|
+
throw fail(error, "intersectPoint query options are invalid");
|
|
2624
|
+
}
|
|
2625
|
+
if (count < 0) throw new Error("intersectPoint query was rejected");
|
|
2626
|
+
return queryHits(pointer, count);
|
|
2627
|
+
},
|
|
2628
|
+
dispose() {
|
|
2629
|
+
if (simulation === 0) return;
|
|
2630
|
+
wasm.tn_physics_destroy(simulation);
|
|
2631
|
+
simulation = 0;
|
|
2632
|
+
for (const scratch of [first, second]) {
|
|
2633
|
+
if (scratch.bytes > 0) wasm.tn_physics_free(scratch.ptr, scratch.bytes);
|
|
2634
|
+
scratch.bytes = 0;
|
|
2635
|
+
scratch.ptr = 0;
|
|
2636
|
+
}
|
|
2637
|
+
wasm.tn_physics_free(record, RECORD_BYTES);
|
|
2638
|
+
}
|
|
2639
|
+
};
|
|
2640
|
+
return host;
|
|
2641
|
+
};
|
|
2642
|
+
return { createSimulation, version };
|
|
2643
|
+
}
|
|
2644
|
+
|
|
1175
2645
|
// src/web.ts
|
|
1176
|
-
var
|
|
1177
|
-
var
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
2646
|
+
var ownedEngine = globalThis.__tnWebThreads !== void 0;
|
|
2647
|
+
var crateHost;
|
|
2648
|
+
var crateReady = ownedEngine ? (async () => {
|
|
2649
|
+
const response = await fetch(
|
|
2650
|
+
new URL("../wasm/threenative_native_physics.wasm", import.meta.url)
|
|
2651
|
+
);
|
|
2652
|
+
if (!response.ok)
|
|
2653
|
+
throw new Error(`TN_PHYSICS_WASM_LOAD: physics module returned ${response.status}`);
|
|
2654
|
+
const { instance } = await WebAssembly.instantiate(await response.arrayBuffer(), {});
|
|
2655
|
+
crateHost = createWasmPhysicsHost(instance.exports);
|
|
2656
|
+
})() : void 0;
|
|
2657
|
+
crateReady?.catch(() => void 0);
|
|
2658
|
+
if (crateReady !== void 0) {
|
|
2659
|
+
installPhysicsSimulationBackend(
|
|
2660
|
+
createNativePhysicsBackend(
|
|
2661
|
+
() => {
|
|
2662
|
+
if (crateHost === void 0)
|
|
2663
|
+
throw new Error("Physics used before the physics backend initialized.");
|
|
2664
|
+
return crateHost;
|
|
2665
|
+
},
|
|
2666
|
+
() => crateReady
|
|
2667
|
+
)
|
|
2668
|
+
);
|
|
2669
|
+
} else {
|
|
2670
|
+
installCompatBackend();
|
|
2671
|
+
}
|
|
1188
2672
|
var RAPIER_INIT_WARNING = "using deprecated parameters for the initialization function; pass a single object instead";
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
const
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
2673
|
+
function installCompatBackend() {
|
|
2674
|
+
let loaded;
|
|
2675
|
+
const rapierModule = import('@dimforge/rapier3d-compat').then((module) => {
|
|
2676
|
+
loaded = module;
|
|
2677
|
+
return module;
|
|
2678
|
+
});
|
|
2679
|
+
rapierModule.catch(() => void 0);
|
|
2680
|
+
const rapierReady = () => {
|
|
2681
|
+
if (loaded === void 0)
|
|
2682
|
+
throw new Error("Rapier used before the physics backend initialized.");
|
|
2683
|
+
return loaded;
|
|
1195
2684
|
};
|
|
1196
|
-
|
|
1197
|
-
|
|
2685
|
+
let initialized;
|
|
2686
|
+
const simulations = /* @__PURE__ */ new WeakMap();
|
|
2687
|
+
async function initializeRapier() {
|
|
2688
|
+
const rapier2 = await rapierModule;
|
|
2689
|
+
const previousWarn = console.warn;
|
|
2690
|
+
console.warn = (...args) => {
|
|
2691
|
+
if (args[0] === RAPIER_INIT_WARNING) return;
|
|
2692
|
+
previousWarn(...args);
|
|
2693
|
+
};
|
|
2694
|
+
try {
|
|
2695
|
+
await rapier2.init().finally(() => {
|
|
2696
|
+
console.warn = previousWarn;
|
|
2697
|
+
});
|
|
2698
|
+
} catch (error) {
|
|
1198
2699
|
console.warn = previousWarn;
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
console.warn = previousWarn;
|
|
1202
|
-
throw error;
|
|
2700
|
+
throw error;
|
|
2701
|
+
}
|
|
1203
2702
|
}
|
|
1204
|
-
|
|
1205
|
-
installPhysicsSimulationBackend({
|
|
1206
|
-
initialize: () => {
|
|
1207
|
-
initialized ??= initializeRapier();
|
|
1208
|
-
return initialized;
|
|
1209
|
-
},
|
|
1210
|
-
createSimulation: (options = {}) => {
|
|
2703
|
+
const wrap = (world) => {
|
|
1211
2704
|
const rapier2 = rapierReady();
|
|
1212
|
-
const world = new rapier2.World(options.gravity ?? { x: 0, y: -9.81, z: 0 });
|
|
1213
2705
|
const simulation = createWebPhysicsSimulation({
|
|
1214
2706
|
eventQueue: new rapier2.EventQueue(true),
|
|
1215
2707
|
rapier: rapier2,
|
|
@@ -1218,24 +2710,21 @@ installPhysicsSimulationBackend({
|
|
|
1218
2710
|
});
|
|
1219
2711
|
simulations.set(world, simulation);
|
|
1220
2712
|
return simulation;
|
|
1221
|
-
}
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
|
|
1236
|
-
},
|
|
1237
|
-
createShape: (shape) => createWebPhysicsShape(rapierReady(), shape)
|
|
1238
|
-
});
|
|
2713
|
+
};
|
|
2714
|
+
installPhysicsSimulationBackend({
|
|
2715
|
+
initialize: () => {
|
|
2716
|
+
initialized ??= initializeRapier();
|
|
2717
|
+
return initialized;
|
|
2718
|
+
},
|
|
2719
|
+
createSimulation: (options = {}) => wrap(new (rapierReady()).World(options.gravity ?? { x: 0, y: -9.81, z: 0 })),
|
|
2720
|
+
simulationForWorld: (world) => {
|
|
2721
|
+
if (typeof world !== "object" || world === null)
|
|
2722
|
+
throw new Error("Physics nodes expected a Rapier World.");
|
|
2723
|
+
return simulations.get(world) ?? wrap(world);
|
|
2724
|
+
},
|
|
2725
|
+
createShape: (shape) => createWebPhysicsShape(rapierReady(), shape)
|
|
2726
|
+
});
|
|
2727
|
+
}
|
|
1239
2728
|
|
|
1240
2729
|
// src/transformRecord.ts
|
|
1241
2730
|
function bulkTransformValue(values, index) {
|
|
@@ -1975,18 +3464,18 @@ var CollisionShape3D = class _CollisionShape3D {
|
|
|
1975
3464
|
return this;
|
|
1976
3465
|
}
|
|
1977
3466
|
};
|
|
1978
|
-
function
|
|
3467
|
+
function finite2(name, value) {
|
|
1979
3468
|
if (typeof value !== "number" || !Number.isFinite(value))
|
|
1980
3469
|
throw new Error(`Buoyancy3D.${name} must be finite.`);
|
|
1981
3470
|
return value;
|
|
1982
3471
|
}
|
|
1983
3472
|
function nonNegative(name, value) {
|
|
1984
|
-
|
|
3473
|
+
finite2(name, value);
|
|
1985
3474
|
if (value < 0) throw new Error(`Buoyancy3D.${name} must be non-negative.`);
|
|
1986
3475
|
return value;
|
|
1987
3476
|
}
|
|
1988
3477
|
function positive(name, value) {
|
|
1989
|
-
|
|
3478
|
+
finite2(name, value);
|
|
1990
3479
|
if (value <= 0) throw new Error(`Buoyancy3D.${name} must be positive.`);
|
|
1991
3480
|
return value;
|
|
1992
3481
|
}
|
|
@@ -2004,9 +3493,9 @@ function pointVector(value) {
|
|
|
2004
3493
|
z = value[2];
|
|
2005
3494
|
}
|
|
2006
3495
|
return new Vector3(
|
|
2007
|
-
|
|
2008
|
-
|
|
2009
|
-
|
|
3496
|
+
finite2("hullPoints.position.x", x),
|
|
3497
|
+
finite2("hullPoints.position.y", y),
|
|
3498
|
+
finite2("hullPoints.position.z", z)
|
|
2010
3499
|
);
|
|
2011
3500
|
}
|
|
2012
3501
|
function shapeVolume(body) {
|
|
@@ -2131,7 +3620,7 @@ var Buoyancy3D = class {
|
|
|
2131
3620
|
point.worldPosition.z,
|
|
2132
3621
|
this.#elapsed
|
|
2133
3622
|
);
|
|
2134
|
-
const height =
|
|
3623
|
+
const height = finite2("surface.height", result.height);
|
|
2135
3624
|
const fraction = clamp01(0.5 + (height - point.worldPosition.y) / point.spacing);
|
|
2136
3625
|
point.submergedFraction = fraction;
|
|
2137
3626
|
volume += point.volume;
|
|
@@ -2145,7 +3634,7 @@ var Buoyancy3D = class {
|
|
|
2145
3634
|
var ZERO_ANCHOR = { x: 0, y: 0, z: 0 };
|
|
2146
3635
|
var X_AXIS = { x: 1, y: 0, z: 0 };
|
|
2147
3636
|
var IDENTITY_FRAME = { x: 0, y: 0, z: 0, w: 1 };
|
|
2148
|
-
function
|
|
3637
|
+
function bodyId2(value, label) {
|
|
2149
3638
|
const candidate = typeof value === "object" && value !== null && "body" in value ? value.body : value;
|
|
2150
3639
|
if (typeof candidate !== "object" || candidate === null || !("id" in candidate) || typeof candidate.id !== "number")
|
|
2151
3640
|
throw new Error(`TN_PHYSICS_INVALID_BODY: ${label} must expose a body handle.`);
|
|
@@ -2164,8 +3653,8 @@ var Joint3D = class _Joint3D {
|
|
|
2164
3653
|
const jointOptions = {
|
|
2165
3654
|
anchorA: options.anchorA ?? ZERO_ANCHOR,
|
|
2166
3655
|
anchorB: options.anchorB ?? ZERO_ANCHOR,
|
|
2167
|
-
bodyA:
|
|
2168
|
-
bodyB:
|
|
3656
|
+
bodyA: bodyId2(options.bodyA, "bodyA"),
|
|
3657
|
+
bodyB: bodyId2(options.bodyB, "bodyB"),
|
|
2169
3658
|
...extra,
|
|
2170
3659
|
type
|
|
2171
3660
|
};
|
|
@@ -3222,7 +4711,7 @@ function growEvents(buffer, bodyCount) {
|
|
|
3222
4711
|
while (length < required) length *= 2;
|
|
3223
4712
|
return length === buffer.length ? buffer : new Uint32Array(length);
|
|
3224
4713
|
}
|
|
3225
|
-
function
|
|
4714
|
+
function isSmallBufferError2(error) {
|
|
3226
4715
|
return error instanceof Error && /buffer is too small/i.test(error.message);
|
|
3227
4716
|
}
|
|
3228
4717
|
function visibleId(buffer, offset) {
|
|
@@ -3281,8 +4770,8 @@ function rapier(options = {}) {
|
|
|
3281
4770
|
addBuoyancy: (buoyancy) => buoyancies.add(buoyancy),
|
|
3282
4771
|
eventQueue: physicsHandle(selected.rawEventQueue),
|
|
3283
4772
|
kinematicMotion: (colliderHandle) => kinematicMotions.get(colliderHandle),
|
|
3284
|
-
groundHolds: (
|
|
3285
|
-
const ground = bodiesById.get(
|
|
4773
|
+
groundHolds: (bodyId3) => {
|
|
4774
|
+
const ground = bodiesById.get(bodyId3);
|
|
3286
4775
|
return ground === void 0 || ground instanceof RigidBody3D && ground.type !== "dynamic";
|
|
3287
4776
|
},
|
|
3288
4777
|
numBodies: () => bodies.size,
|
|
@@ -3372,7 +4861,7 @@ function rapier(options = {}) {
|
|
|
3372
4861
|
visibleCount = simulation.readVisibleTransforms(visible);
|
|
3373
4862
|
break;
|
|
3374
4863
|
} catch (error) {
|
|
3375
|
-
if (!
|
|
4864
|
+
if (!isSmallBufferError2(error) || visibleResizeAttempts >= PHYSICS_BUFFER_RESIZE_ATTEMPTS)
|
|
3376
4865
|
throw error;
|
|
3377
4866
|
visibleResizeAttempts += 1;
|
|
3378
4867
|
visible = growFloat(visible, Math.ceil(visible.length / PHYSICS_TRANSFORM_STRIDE) * 2);
|
|
@@ -3399,7 +4888,7 @@ function rapier(options = {}) {
|
|
|
3399
4888
|
eventCount = simulation.drainCollisionEvents(events);
|
|
3400
4889
|
break;
|
|
3401
4890
|
} catch (error) {
|
|
3402
|
-
if (!
|
|
4891
|
+
if (!isSmallBufferError2(error) || events.length >= maximumEventValues) throw error;
|
|
3403
4892
|
events = new Uint32Array(Math.min(maximumEventValues, events.length * 2));
|
|
3404
4893
|
}
|
|
3405
4894
|
}
|
|
@@ -3429,9 +4918,9 @@ function rapier(options = {}) {
|
|
|
3429
4918
|
throw new Error("Physics area membership buffers lost a registered area.");
|
|
3430
4919
|
const current = buffers.current;
|
|
3431
4920
|
current.clear();
|
|
3432
|
-
for (const
|
|
3433
|
-
const body = bodiesById.get(
|
|
3434
|
-
if (body !== void 0) current.set(
|
|
4921
|
+
for (const bodyId3 of simulation.areaIntersections(area.body.id)) {
|
|
4922
|
+
const body = bodiesById.get(bodyId3);
|
|
4923
|
+
if (body !== void 0) current.set(bodyId3, body);
|
|
3435
4924
|
}
|
|
3436
4925
|
area.reconcileIntersections(current);
|
|
3437
4926
|
buffers.current = buffers.previous;
|
|
@@ -3575,9 +5064,9 @@ function rapier(options = {}) {
|
|
|
3575
5064
|
}
|
|
3576
5065
|
};
|
|
3577
5066
|
}
|
|
3578
|
-
function removeContactsFor(
|
|
5067
|
+
function removeContactsFor(bodyId3) {
|
|
3579
5068
|
for (const [key, pair] of activeContacts) {
|
|
3580
|
-
if (pair.includes(
|
|
5069
|
+
if (pair.includes(bodyId3)) activeContacts.delete(key);
|
|
3581
5070
|
}
|
|
3582
5071
|
}
|
|
3583
5072
|
}
|