@threenative/physics 0.3.4 → 0.3.5
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-DI_kCf7I.d.ts → Area3D-vbmjpxsN.d.ts} +34 -1
- package/dist/index.d.ts +129 -3
- package/dist/index.js +514 -8
- package/dist/native/index.d.ts +3 -2
- package/dist/native/index.js +489 -4
- package/dist/navigation/index.d.ts +1 -1
- package/package.json +5 -4
package/dist/index.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import * as rapier from '@dimforge/rapier3d-compat';
|
|
2
2
|
import { Matrix4, Vector3, Quaternion, Box3, Object3D, Mesh, InstancedMesh } from 'three';
|
|
3
|
+
import { snowDiscFootprint } from '@threenative/core/world';
|
|
3
4
|
|
|
4
5
|
// src/web.ts
|
|
5
6
|
|
|
@@ -30,6 +31,7 @@ function physicsColliderHandle(id, raw) {
|
|
|
30
31
|
var PHYSICS_TRANSFORM_STRIDE = 8;
|
|
31
32
|
var PHYSICS_COLLISION_EVENT_STRIDE = 4;
|
|
32
33
|
var PHYSICS_SLEEP_STATE_STRIDE = 2;
|
|
34
|
+
var PHYSICS_CONTACT_STRIDE = 8;
|
|
33
35
|
var MAX_PHYSICS_QUERY_RESULTS = 1024;
|
|
34
36
|
var F32_EPSILON = Math.fround(2 ** -23);
|
|
35
37
|
var F32_EPSILON_SQUARED = Math.fround(F32_EPSILON * F32_EPSILON);
|
|
@@ -84,10 +86,10 @@ function requirePhysicsJointCreateOptions(options) {
|
|
|
84
86
|
if (options.frameB !== void 0) requireFiniteRotation(options.frameB, "joint frameB");
|
|
85
87
|
return options;
|
|
86
88
|
}
|
|
87
|
-
function requireVehicleNumber(value, label,
|
|
88
|
-
if (typeof value !== "number" || !Number.isFinite(value) || (
|
|
89
|
+
function requireVehicleNumber(value, label, positive3) {
|
|
90
|
+
if (typeof value !== "number" || !Number.isFinite(value) || (positive3 ? value <= 0 : value < 0))
|
|
89
91
|
throw new Error(
|
|
90
|
-
`TN_VEHICLE_INVALID: ${label} must be a finite ${
|
|
92
|
+
`TN_VEHICLE_INVALID: ${label} must be a finite ${positive3 ? "positive" : "non-negative"} number.`
|
|
91
93
|
);
|
|
92
94
|
}
|
|
93
95
|
function requirePhysicsVehicleCreateOptions(options) {
|
|
@@ -275,7 +277,11 @@ function createWebPhysicsShape(rapier3, shape, sensor = shape.sensor) {
|
|
|
275
277
|
else if (shape.kind === "trimesh") {
|
|
276
278
|
if (shape.vertices === void 0 || shape.indices === void 0)
|
|
277
279
|
throw new Error("CollisionShape3D.trimesh is missing mesh data.");
|
|
278
|
-
descriptor = rapier3.ColliderDesc.trimesh(
|
|
280
|
+
descriptor = rapier3.ColliderDesc.trimesh(
|
|
281
|
+
shape.vertices,
|
|
282
|
+
shape.indices,
|
|
283
|
+
rapier3.TriMeshFlags.FIX_INTERNAL_EDGES
|
|
284
|
+
);
|
|
279
285
|
} else if (shape.kind === "convexHull") {
|
|
280
286
|
if (shape.vertices === void 0)
|
|
281
287
|
throw new Error("CollisionShape3D.convexHull is missing vertices.");
|
|
@@ -288,7 +294,8 @@ function createWebPhysicsShape(rapier3, shape, sensor = shape.sensor) {
|
|
|
288
294
|
shape.rows - 1,
|
|
289
295
|
shape.columns - 1,
|
|
290
296
|
shape.heights,
|
|
291
|
-
shape.scale
|
|
297
|
+
shape.scale,
|
|
298
|
+
rapier3.HeightFieldFlags.FIX_INTERNAL_EDGES
|
|
292
299
|
);
|
|
293
300
|
}
|
|
294
301
|
descriptor.setCollisionGroups(interactionGroups(shape.collisionLayer, shape.collisionMask));
|
|
@@ -327,10 +334,14 @@ function characterState(simulationBody, byCollider) {
|
|
|
327
334
|
let groundCollider;
|
|
328
335
|
let groundBody;
|
|
329
336
|
let groundNormal;
|
|
337
|
+
let walkable = false;
|
|
338
|
+
let contacts = 0;
|
|
330
339
|
for (let index = 0; index < controller.numComputedCollisions(); index += 1) {
|
|
331
340
|
const collision = controller.computedCollision(index);
|
|
332
341
|
if (collision?.collider === null || collision?.collider === void 0) continue;
|
|
342
|
+
contacts += 1;
|
|
333
343
|
if ((collision.normal1.y ?? Number.NEGATIVE_INFINITY) >= 0.5) {
|
|
344
|
+
walkable = true;
|
|
334
345
|
const contacted = byCollider.get(collision.collider.handle);
|
|
335
346
|
if (contacted !== void 0) {
|
|
336
347
|
groundCollider = contacted.id;
|
|
@@ -340,7 +351,7 @@ function characterState(simulationBody, byCollider) {
|
|
|
340
351
|
}
|
|
341
352
|
}
|
|
342
353
|
}
|
|
343
|
-
const grounded = controller.computedGrounded();
|
|
354
|
+
const grounded = controller.computedGrounded() && (walkable || contacts === 0);
|
|
344
355
|
if (groundCollider !== void 0) simulationBody.groundCollider = groundCollider;
|
|
345
356
|
else if (!grounded) simulationBody.groundCollider = void 0;
|
|
346
357
|
state.grounded = grounded;
|
|
@@ -384,6 +395,32 @@ function writeRapierTransformRecord(entry, renderBuffer, offset) {
|
|
|
384
395
|
renderBuffer[offset + 6] = rotation.z;
|
|
385
396
|
renderBuffer[offset + 7] = rotation.w;
|
|
386
397
|
}
|
|
398
|
+
function writeContactRecord(buffer, record, colliderId, manifold, flipped) {
|
|
399
|
+
const solved = manifold.numSolverContacts();
|
|
400
|
+
let impulse = 0;
|
|
401
|
+
for (let point = 0; point < manifold.numContacts(); point += 1)
|
|
402
|
+
impulse += manifold.contactImpulse(point);
|
|
403
|
+
let x = 0;
|
|
404
|
+
let y = 0;
|
|
405
|
+
let z = 0;
|
|
406
|
+
for (let point = 0; point < solved; point += 1) {
|
|
407
|
+
const world = manifold.solverContactPoint(point);
|
|
408
|
+
x += world.x;
|
|
409
|
+
y += world.y;
|
|
410
|
+
z += world.z;
|
|
411
|
+
}
|
|
412
|
+
const normal = manifold.normal();
|
|
413
|
+
const sign = flipped ? -1 : 1;
|
|
414
|
+
const offset = record * PHYSICS_CONTACT_STRIDE;
|
|
415
|
+
buffer[offset] = colliderId;
|
|
416
|
+
buffer[offset + 1] = x / solved;
|
|
417
|
+
buffer[offset + 2] = y / solved;
|
|
418
|
+
buffer[offset + 3] = z / solved;
|
|
419
|
+
buffer[offset + 4] = normal.x * sign;
|
|
420
|
+
buffer[offset + 5] = normal.y * sign;
|
|
421
|
+
buffer[offset + 6] = normal.z * sign;
|
|
422
|
+
buffer[offset + 7] = impulse;
|
|
423
|
+
}
|
|
387
424
|
function createWebPhysicsSimulation(options) {
|
|
388
425
|
const bodies = /* @__PURE__ */ new Map();
|
|
389
426
|
const byCollider = /* @__PURE__ */ new Map();
|
|
@@ -697,7 +734,10 @@ function createWebPhysicsSimulation(options) {
|
|
|
697
734
|
}
|
|
698
735
|
}
|
|
699
736
|
options.world.step(options.eventQueue);
|
|
700
|
-
for (const entry of bodies.values())
|
|
737
|
+
for (const entry of bodies.values()) {
|
|
738
|
+
entry.body.resetForces(true);
|
|
739
|
+
entry.body.resetTorques(true);
|
|
740
|
+
}
|
|
701
741
|
dirtyBodies.clear();
|
|
702
742
|
},
|
|
703
743
|
readVisibleTransforms: (renderBuffer) => {
|
|
@@ -862,6 +902,41 @@ function createWebPhysicsSimulation(options) {
|
|
|
862
902
|
}
|
|
863
903
|
return current;
|
|
864
904
|
},
|
|
905
|
+
readContacts: (target, colliders, buffer) => {
|
|
906
|
+
requireLive();
|
|
907
|
+
if (!(buffer instanceof Float32Array))
|
|
908
|
+
throw new Error("IPhysicsSimulation.readContacts requires a Float32Array buffer.");
|
|
909
|
+
if (!(colliders instanceof Uint32Array))
|
|
910
|
+
throw new Error("IPhysicsSimulation.readContacts requires a Uint32Array of collider ids.");
|
|
911
|
+
const targetCollider = bodies.get(target.id)?.collider;
|
|
912
|
+
if (targetCollider === void 0)
|
|
913
|
+
throw new Error(
|
|
914
|
+
`IPhysicsSimulation contact target ${String(target.id)} is not a live collider.`
|
|
915
|
+
);
|
|
916
|
+
const capacity = Math.floor(buffer.length / PHYSICS_CONTACT_STRIDE);
|
|
917
|
+
let count = 0;
|
|
918
|
+
for (let index = 0; index < colliders.length; index += 1) {
|
|
919
|
+
const otherId = colliders[index];
|
|
920
|
+
if (otherId === target.id) continue;
|
|
921
|
+
const entry = bodies.get(otherId);
|
|
922
|
+
if (entry === void 0 || entry.body.isSleeping()) continue;
|
|
923
|
+
options.world.contactPair(targetCollider, entry.collider, (manifold, flipped) => {
|
|
924
|
+
if (manifold.numSolverContacts() === 0) return;
|
|
925
|
+
if (count < capacity) writeContactRecord(buffer, count, otherId, manifold, flipped);
|
|
926
|
+
count += 1;
|
|
927
|
+
});
|
|
928
|
+
}
|
|
929
|
+
return count;
|
|
930
|
+
},
|
|
931
|
+
setColliderShape: (collider, shape) => {
|
|
932
|
+
requireLive();
|
|
933
|
+
const entry = bodies.get(collider.id);
|
|
934
|
+
if (entry === void 0)
|
|
935
|
+
throw new Error(
|
|
936
|
+
`IPhysicsSimulation shape target ${String(collider.id)} is not a live collider.`
|
|
937
|
+
);
|
|
938
|
+
entry.collider.setShape(createWebPhysicsShape(options.rapier, shape).shape);
|
|
939
|
+
},
|
|
865
940
|
createVehicle: (vehicleOptions) => {
|
|
866
941
|
requireLive();
|
|
867
942
|
const requested = requirePhysicsVehicleCreateOptions(vehicleOptions);
|
|
@@ -1231,6 +1306,19 @@ var CharacterBody3D = class {
|
|
|
1231
1306
|
#beforeY = 0;
|
|
1232
1307
|
#desiredY = 0;
|
|
1233
1308
|
#disposed = false;
|
|
1309
|
+
#interpolate = true;
|
|
1310
|
+
/** Draw between solved ticks; false presents this body at its true tick pose. */
|
|
1311
|
+
get interpolate() {
|
|
1312
|
+
return this.#interpolate;
|
|
1313
|
+
}
|
|
1314
|
+
set interpolate(enabled) {
|
|
1315
|
+
if (this.#disposed)
|
|
1316
|
+
throw new Error("CharacterBody3D.interpolate cannot be used after dispose.");
|
|
1317
|
+
if (typeof enabled !== "boolean") throw new Error("interpolate must be a boolean.");
|
|
1318
|
+
if (enabled === this.#interpolate) return;
|
|
1319
|
+
this.#interpolate = enabled;
|
|
1320
|
+
this.#physics?.updateInterpolation?.(this);
|
|
1321
|
+
}
|
|
1234
1322
|
constructor(options) {
|
|
1235
1323
|
this.#simulation = requirePhysicsSimulation(options.physics, options.world);
|
|
1236
1324
|
this.#physics = options.physics;
|
|
@@ -1334,6 +1422,7 @@ var CharacterBody3D = class {
|
|
|
1334
1422
|
if (this.#disposed) throw new Error("CharacterBody3D.teleport cannot be used after dispose.");
|
|
1335
1423
|
this.#simulation.setBodyTransform(this.body.id, position);
|
|
1336
1424
|
this.object.position.set(position.x, position.y, position.z);
|
|
1425
|
+
this.#physics?.resetInterpolation?.(this.object);
|
|
1337
1426
|
this.velocity.set(0, 0, 0);
|
|
1338
1427
|
this.#desired.x = 0;
|
|
1339
1428
|
this.#desired.y = 0;
|
|
@@ -1887,6 +1976,18 @@ var RigidBody3D = class {
|
|
|
1887
1976
|
#motion = { x: 0, y: 0, z: 0 };
|
|
1888
1977
|
#zeroMotion = { x: 0, y: 0, z: 0 };
|
|
1889
1978
|
#disposed = false;
|
|
1979
|
+
#interpolate = true;
|
|
1980
|
+
/** Draw between solved ticks; false presents this body at its true tick pose. */
|
|
1981
|
+
get interpolate() {
|
|
1982
|
+
return this.#interpolate;
|
|
1983
|
+
}
|
|
1984
|
+
set interpolate(enabled) {
|
|
1985
|
+
this.#requireLive("interpolate");
|
|
1986
|
+
if (typeof enabled !== "boolean") throw new Error("interpolate must be a boolean.");
|
|
1987
|
+
if (enabled === this.#interpolate) return;
|
|
1988
|
+
this.#interpolate = enabled;
|
|
1989
|
+
this.#physics?.updateInterpolation?.(this);
|
|
1990
|
+
}
|
|
1890
1991
|
constructor(options) {
|
|
1891
1992
|
const initial = resolveInitialTransform(options);
|
|
1892
1993
|
const type = options.type ?? "dynamic";
|
|
@@ -1954,6 +2055,23 @@ var RigidBody3D = class {
|
|
|
1954
2055
|
this.#motion.z = object.position.z - this.#lastPosition.z;
|
|
1955
2056
|
return this.#motion;
|
|
1956
2057
|
}
|
|
2058
|
+
/** Assign a world position to teleport; mutate object.position for ordinary kinematic motion. */
|
|
2059
|
+
get position() {
|
|
2060
|
+
const position = this.#object?.position ?? this.#simulation.readBodyTransform?.(this.body.id)?.position;
|
|
2061
|
+
if (position === void 0) throw new Error("RigidBody3D position is unavailable.");
|
|
2062
|
+
return position;
|
|
2063
|
+
}
|
|
2064
|
+
set position(position) {
|
|
2065
|
+
this.#requireLive("position");
|
|
2066
|
+
this.#simulation.setBodyTransform(this.body.id, position);
|
|
2067
|
+
this.#lastPosition.x = position.x;
|
|
2068
|
+
this.#lastPosition.y = position.y;
|
|
2069
|
+
this.#lastPosition.z = position.z;
|
|
2070
|
+
if (this.#object !== void 0) {
|
|
2071
|
+
this.#object.position.set(position.x, position.y, position.z);
|
|
2072
|
+
this.#physics?.resetInterpolation?.(this.#object);
|
|
2073
|
+
}
|
|
2074
|
+
}
|
|
1957
2075
|
syncToPhysics() {
|
|
1958
2076
|
}
|
|
1959
2077
|
/**
|
|
@@ -2198,6 +2316,378 @@ var PhysicsSoftBodyCollision = class {
|
|
|
2198
2316
|
function softBodyCollision(...bodies) {
|
|
2199
2317
|
return new PhysicsSoftBodyCollision(bodies);
|
|
2200
2318
|
}
|
|
2319
|
+
function unionBounds(current, next) {
|
|
2320
|
+
if (current === void 0) return next;
|
|
2321
|
+
const column = Math.min(current.column, next.column);
|
|
2322
|
+
const row = Math.min(current.row, next.row);
|
|
2323
|
+
return {
|
|
2324
|
+
column,
|
|
2325
|
+
columns: Math.max(current.column + current.columns, next.column + next.columns) - column,
|
|
2326
|
+
row,
|
|
2327
|
+
rows: Math.max(current.row + current.rows, next.row + next.rows) - row
|
|
2328
|
+
};
|
|
2329
|
+
}
|
|
2330
|
+
var ZERO_SAMPLE = {
|
|
2331
|
+
bank: 0,
|
|
2332
|
+
coverage: 0,
|
|
2333
|
+
disturbance: 0,
|
|
2334
|
+
relief: 0,
|
|
2335
|
+
shape: 0
|
|
2336
|
+
};
|
|
2337
|
+
function clamp(value, minimum, maximum) {
|
|
2338
|
+
return Math.max(minimum, Math.min(maximum, value));
|
|
2339
|
+
}
|
|
2340
|
+
function smoothstep(edge0, edge1, value) {
|
|
2341
|
+
const t = clamp((value - edge0) / (edge1 - edge0), 0, 1);
|
|
2342
|
+
return t * t * (3 - 2 * t);
|
|
2343
|
+
}
|
|
2344
|
+
function positive2(value, name) {
|
|
2345
|
+
if (!Number.isFinite(value) || value <= 0)
|
|
2346
|
+
throw new Error(`attachSnowPhysics ${name} must be a positive finite number.`);
|
|
2347
|
+
return value;
|
|
2348
|
+
}
|
|
2349
|
+
function capsuleFootprint(halfHeight, radius) {
|
|
2350
|
+
positive2(radius, "capsule radius");
|
|
2351
|
+
if (!Number.isFinite(halfHeight) || halfHeight < 0)
|
|
2352
|
+
throw new Error("attachSnowPhysics capsule halfHeight must be a non-negative finite number.");
|
|
2353
|
+
const softness = 0.2 * radius;
|
|
2354
|
+
return {
|
|
2355
|
+
extent: halfHeight + radius + 2 * softness,
|
|
2356
|
+
sample: (x, z) => {
|
|
2357
|
+
const along = clamp(z, -halfHeight, halfHeight);
|
|
2358
|
+
return edgeSample(Math.hypot(x, z - along) - radius, softness);
|
|
2359
|
+
}
|
|
2360
|
+
};
|
|
2361
|
+
}
|
|
2362
|
+
function boxFootprint(halfWidth, halfDepth) {
|
|
2363
|
+
positive2(halfWidth, "box halfWidth");
|
|
2364
|
+
positive2(halfDepth, "box halfDepth");
|
|
2365
|
+
const edge = 0.15 * Math.min(halfWidth, halfDepth);
|
|
2366
|
+
return {
|
|
2367
|
+
extent: Math.hypot(halfWidth + 2 * edge, halfDepth + 2 * edge),
|
|
2368
|
+
sample: (x, z) => edgeSample(Math.max(Math.abs(x) - halfWidth, Math.abs(z) - halfDepth), edge)
|
|
2369
|
+
};
|
|
2370
|
+
}
|
|
2371
|
+
function edgeSample(outside, edge) {
|
|
2372
|
+
if (outside >= 2 * edge) return ZERO_SAMPLE;
|
|
2373
|
+
if (outside >= edge) {
|
|
2374
|
+
const bank = 1 - (outside - edge) / edge;
|
|
2375
|
+
return { bank, coverage: 0, disturbance: bank * 0.5, relief: 0, shape: 1 };
|
|
2376
|
+
}
|
|
2377
|
+
const coverage = outside <= 0 ? 1 : 1 - smoothstep(0, edge, outside);
|
|
2378
|
+
return { bank: 0, coverage, disturbance: coverage, relief: 0, shape: 1 };
|
|
2379
|
+
}
|
|
2380
|
+
var SUPPORTED_SHAPES = ["sphere", "box", "capsule"];
|
|
2381
|
+
function rotateAxis(q, axis) {
|
|
2382
|
+
const { w, x, y, z } = q;
|
|
2383
|
+
if (axis === 0) return [1 - 2 * (y * y + z * z), 2 * (x * y + w * z), 2 * (x * z - w * y)];
|
|
2384
|
+
if (axis === 1) return [2 * (x * y - w * z), 1 - 2 * (x * x + z * z), 2 * (y * z + w * x)];
|
|
2385
|
+
return [2 * (x * z + w * y), 2 * (y * z - w * x), 1 - 2 * (x * x + y * y)];
|
|
2386
|
+
}
|
|
2387
|
+
function orientedFootprint(watched, transform, cellSize) {
|
|
2388
|
+
const { position, rotation } = transform;
|
|
2389
|
+
if (watched.custom !== void 0) {
|
|
2390
|
+
const forward = rotateAxis(rotation, 2);
|
|
2391
|
+
return {
|
|
2392
|
+
footprint: watched.custom,
|
|
2393
|
+
rotation: Math.atan2(forward[0], forward[2]),
|
|
2394
|
+
x: position.x,
|
|
2395
|
+
z: position.z
|
|
2396
|
+
};
|
|
2397
|
+
}
|
|
2398
|
+
const shape = watched.shape;
|
|
2399
|
+
if (shape === void 0) throw new Error("attachSnowPhysics watched body lost its shape.");
|
|
2400
|
+
if (shape.kind === "box") return boxPrint(watched.cache, shape, transform);
|
|
2401
|
+
if (shape.kind === "capsule") return capsulePrint(watched.cache, shape, transform, cellSize);
|
|
2402
|
+
return {
|
|
2403
|
+
footprint: cached(watched.cache, 0, () => snowDiscFootprint(shape.x)),
|
|
2404
|
+
rotation: 0,
|
|
2405
|
+
x: position.x,
|
|
2406
|
+
z: position.z
|
|
2407
|
+
};
|
|
2408
|
+
}
|
|
2409
|
+
function cached(cache, key, create) {
|
|
2410
|
+
let footprint = cache.get(key);
|
|
2411
|
+
if (footprint === void 0) {
|
|
2412
|
+
footprint = create();
|
|
2413
|
+
cache.set(key, footprint);
|
|
2414
|
+
}
|
|
2415
|
+
return footprint;
|
|
2416
|
+
}
|
|
2417
|
+
function boxPrint(cache, shape, { position, rotation }) {
|
|
2418
|
+
const axes = [rotateAxis(rotation, 0), rotateAxis(rotation, 1), rotateAxis(rotation, 2)];
|
|
2419
|
+
const halves = [shape.x, shape.y, shape.z];
|
|
2420
|
+
let down = 0;
|
|
2421
|
+
for (let axis2 = 1; axis2 < 3; axis2 += 1)
|
|
2422
|
+
if (Math.abs(axes[axis2]?.[1] ?? 0) > Math.abs(axes[down]?.[1] ?? 0)) down = axis2;
|
|
2423
|
+
const across = down === 0 ? 1 : 0;
|
|
2424
|
+
const along = down === 2 ? 1 : 2;
|
|
2425
|
+
const normal = axes[down];
|
|
2426
|
+
const toFace = halves[down] * (normal[1] > 0 ? -1 : 1);
|
|
2427
|
+
const axis = axes[across];
|
|
2428
|
+
return {
|
|
2429
|
+
footprint: cached(
|
|
2430
|
+
cache,
|
|
2431
|
+
down,
|
|
2432
|
+
() => boxFootprint(halves[across], halves[along])
|
|
2433
|
+
),
|
|
2434
|
+
rotation: Math.atan2(-axis[2], axis[0]),
|
|
2435
|
+
x: position.x + normal[0] * toFace,
|
|
2436
|
+
z: position.z + normal[2] * toFace
|
|
2437
|
+
};
|
|
2438
|
+
}
|
|
2439
|
+
function capsulePrint(cache, shape, { position, rotation }, cellSize) {
|
|
2440
|
+
const spine = rotateAxis(rotation, 1);
|
|
2441
|
+
const reach = shape.x * Math.hypot(spine[0], spine[2]);
|
|
2442
|
+
const key = Math.round(reach / (cellSize / 2));
|
|
2443
|
+
return {
|
|
2444
|
+
footprint: cached(cache, key, () => capsuleFootprint(key * cellSize / 2, shape.y)),
|
|
2445
|
+
rotation: Math.atan2(spine[0], spine[2]),
|
|
2446
|
+
x: position.x,
|
|
2447
|
+
z: position.z
|
|
2448
|
+
};
|
|
2449
|
+
}
|
|
2450
|
+
function attachSnowPhysics(options) {
|
|
2451
|
+
const simulation = options.physics.simulation;
|
|
2452
|
+
if (typeof simulation.readContacts !== "function" || typeof simulation.setColliderShape !== "function" || typeof simulation.readBodyTransform !== "function")
|
|
2453
|
+
throw new Error(
|
|
2454
|
+
"attachSnowPhysics requires a physics backend with persistent solved contacts, in-place shape refresh and body transform reads."
|
|
2455
|
+
);
|
|
2456
|
+
const readContacts = simulation.readContacts.bind(simulation);
|
|
2457
|
+
const setColliderShape = simulation.setColliderShape.bind(simulation);
|
|
2458
|
+
const readBodyTransform = simulation.readBodyTransform.bind(simulation);
|
|
2459
|
+
const snow = options.snow;
|
|
2460
|
+
const field = snow.field;
|
|
2461
|
+
const loadScale = finiteOr(options.loadScale, 1, "loadScale");
|
|
2462
|
+
const supportNormal = clamp(finiteOr(options.supportNormal, 0.35, "supportNormal"), 0, 1);
|
|
2463
|
+
const cellSize = Math.min(snow.cellWidth, snow.cellDepth);
|
|
2464
|
+
const colliderTolerance = finiteOr(options.colliderTolerance, 5e-4, "colliderTolerance");
|
|
2465
|
+
if (colliderTolerance < 0)
|
|
2466
|
+
throw new Error("attachSnowPhysics colliderTolerance must be non-negative.");
|
|
2467
|
+
const watched = /* @__PURE__ */ new Map();
|
|
2468
|
+
let colliderHeights = field.toColliderHeights();
|
|
2469
|
+
const changes = field.trackChanges();
|
|
2470
|
+
let unsynced;
|
|
2471
|
+
let surfaceVersion = snow.version;
|
|
2472
|
+
let contacts = 0;
|
|
2473
|
+
let supported = 0;
|
|
2474
|
+
let load = 0;
|
|
2475
|
+
let version2 = snow.version;
|
|
2476
|
+
let disposed = false;
|
|
2477
|
+
function surfaceDescriptor() {
|
|
2478
|
+
return {
|
|
2479
|
+
collisionLayer: options.collisionLayer ?? 1,
|
|
2480
|
+
collisionMask: options.collisionMask ?? 65535,
|
|
2481
|
+
columns: field.columns,
|
|
2482
|
+
heights: colliderHeights,
|
|
2483
|
+
kind: "heightfield",
|
|
2484
|
+
rows: field.rows,
|
|
2485
|
+
// Rapier's heightfield scale is the full extent, and its own axis order is the transposed
|
|
2486
|
+
// buffer `toColliderHeights` already produced.
|
|
2487
|
+
scale: { x: field.width, y: 1, z: field.depth },
|
|
2488
|
+
sensor: false,
|
|
2489
|
+
x: 0,
|
|
2490
|
+
y: 0,
|
|
2491
|
+
z: 0
|
|
2492
|
+
};
|
|
2493
|
+
}
|
|
2494
|
+
const registration = simulation.createBody({
|
|
2495
|
+
entity: "threenative.snow.surface",
|
|
2496
|
+
mass: 0,
|
|
2497
|
+
position: { x: field.origin.x, y: 0, z: field.origin.z },
|
|
2498
|
+
rotation: { w: 1, x: 0, y: 0, z: 0 },
|
|
2499
|
+
sensor: false,
|
|
2500
|
+
shape: surfaceDescriptor(),
|
|
2501
|
+
type: "fixed"
|
|
2502
|
+
});
|
|
2503
|
+
const surface = registration.collider;
|
|
2504
|
+
const surfaceBody = registration.body;
|
|
2505
|
+
let contactBuffer = new Float32Array(PHYSICS_CONTACT_STRIDE * 64);
|
|
2506
|
+
let colliderScratch = new Uint32Array(8);
|
|
2507
|
+
function colliderGap(canonical) {
|
|
2508
|
+
let gap = 0;
|
|
2509
|
+
for (let index = 0; index < canonical.length; index += 1)
|
|
2510
|
+
gap = Math.max(
|
|
2511
|
+
gap,
|
|
2512
|
+
Math.abs(canonical[index] - colliderHeights[index])
|
|
2513
|
+
);
|
|
2514
|
+
return gap;
|
|
2515
|
+
}
|
|
2516
|
+
function windowGap(window) {
|
|
2517
|
+
let gap = 0;
|
|
2518
|
+
const rows = field.rows;
|
|
2519
|
+
for (let column = window.column; column < window.column + window.columns; column += 1) {
|
|
2520
|
+
for (let row = window.row; row < window.row + window.rows; row += 1) {
|
|
2521
|
+
const installed = colliderHeights[column * rows + row];
|
|
2522
|
+
gap = Math.max(gap, Math.abs(field.colliderHeight(row, column) - installed));
|
|
2523
|
+
}
|
|
2524
|
+
}
|
|
2525
|
+
return gap;
|
|
2526
|
+
}
|
|
2527
|
+
function refreshSurface() {
|
|
2528
|
+
const changed = changes.take();
|
|
2529
|
+
if (changed !== void 0) unsynced = unionBounds(unsynced, changed);
|
|
2530
|
+
if (unsynced !== void 0 && windowGap(unsynced) > colliderTolerance) {
|
|
2531
|
+
colliderHeights = field.toColliderHeights();
|
|
2532
|
+
setColliderShape(surface, surfaceDescriptor());
|
|
2533
|
+
unsynced = void 0;
|
|
2534
|
+
}
|
|
2535
|
+
surfaceVersion = snow.version;
|
|
2536
|
+
}
|
|
2537
|
+
function colliderIds() {
|
|
2538
|
+
if (colliderScratch.length < watched.size) colliderScratch = new Uint32Array(watched.size * 2);
|
|
2539
|
+
let index = 0;
|
|
2540
|
+
for (const id of watched.keys()) {
|
|
2541
|
+
colliderScratch[index] = id;
|
|
2542
|
+
index += 1;
|
|
2543
|
+
}
|
|
2544
|
+
return colliderScratch.subarray(0, index);
|
|
2545
|
+
}
|
|
2546
|
+
function readAll(ids) {
|
|
2547
|
+
let found = readContacts(surface, ids, contactBuffer);
|
|
2548
|
+
if (found * PHYSICS_CONTACT_STRIDE > contactBuffer.length) {
|
|
2549
|
+
contactBuffer = new Float32Array(found * 2 * PHYSICS_CONTACT_STRIDE);
|
|
2550
|
+
found = readContacts(surface, ids, contactBuffer);
|
|
2551
|
+
if (found * PHYSICS_CONTACT_STRIDE > contactBuffer.length)
|
|
2552
|
+
throw new Error("attachSnowPhysics contact count changed between two reads of one step.");
|
|
2553
|
+
}
|
|
2554
|
+
return found;
|
|
2555
|
+
}
|
|
2556
|
+
function add(body, footprint) {
|
|
2557
|
+
if (disposed) throw new Error("attachSnowPhysics binding is disposed.");
|
|
2558
|
+
const shape = "shape" in body ? body.shape.descriptor : void 0;
|
|
2559
|
+
if (footprint === void 0 && !SUPPORTED_SHAPES.includes(shape?.kind))
|
|
2560
|
+
throw new Error(
|
|
2561
|
+
`attachSnowPhysics cannot derive a contact profile for a '${shape?.kind ?? "unknown"}' shape; supported shapes are ${SUPPORTED_SHAPES.join(", ")}. Pass an explicit footprint instead.`
|
|
2562
|
+
);
|
|
2563
|
+
watched.set(body.collider.id, {
|
|
2564
|
+
body,
|
|
2565
|
+
cache: /* @__PURE__ */ new Map(),
|
|
2566
|
+
custom: footprint,
|
|
2567
|
+
impulse: 0,
|
|
2568
|
+
load: 0,
|
|
2569
|
+
shape
|
|
2570
|
+
});
|
|
2571
|
+
}
|
|
2572
|
+
for (const body of options.bodies ?? []) add(body);
|
|
2573
|
+
function gather() {
|
|
2574
|
+
const found = readAll(colliderIds());
|
|
2575
|
+
contacts = found;
|
|
2576
|
+
for (let index = 0; index < found; index += 1) {
|
|
2577
|
+
const offset = index * PHYSICS_CONTACT_STRIDE;
|
|
2578
|
+
const entry = watched.get(contactBuffer[offset]);
|
|
2579
|
+
if (entry === void 0 || contactBuffer[offset + 5] < supportNormal) continue;
|
|
2580
|
+
entry.impulse += Math.max(0, contactBuffer[offset + 7]);
|
|
2581
|
+
}
|
|
2582
|
+
}
|
|
2583
|
+
function press(deltaTime) {
|
|
2584
|
+
gather();
|
|
2585
|
+
for (const entry of watched.values()) {
|
|
2586
|
+
entry.load = 0;
|
|
2587
|
+
if (entry.impulse <= 0) continue;
|
|
2588
|
+
const transform = readBodyTransform(entry.body.body.id);
|
|
2589
|
+
if (transform === void 0) throw new Error("attachSnowPhysics lost a watched body.");
|
|
2590
|
+
const oriented = orientedFootprint(entry, transform, cellSize);
|
|
2591
|
+
const bodyLoad = entry.impulse / deltaTime * loadScale;
|
|
2592
|
+
snow.stamp({
|
|
2593
|
+
area: footprintArea(oriented.footprint),
|
|
2594
|
+
duration: deltaTime,
|
|
2595
|
+
footprint: oriented.footprint,
|
|
2596
|
+
load: bodyLoad,
|
|
2597
|
+
rotation: oriented.rotation,
|
|
2598
|
+
x: oriented.x,
|
|
2599
|
+
z: oriented.z
|
|
2600
|
+
});
|
|
2601
|
+
supported += 1;
|
|
2602
|
+
load += bodyLoad;
|
|
2603
|
+
entry.load = bodyLoad;
|
|
2604
|
+
entry.impulse = 0;
|
|
2605
|
+
}
|
|
2606
|
+
}
|
|
2607
|
+
const binding = {
|
|
2608
|
+
add,
|
|
2609
|
+
deposition: finiteOr(options.deposition, 0, "deposition"),
|
|
2610
|
+
wind: finiteOr(options.wind, 0, "wind"),
|
|
2611
|
+
get contacts() {
|
|
2612
|
+
return contacts;
|
|
2613
|
+
},
|
|
2614
|
+
get supported() {
|
|
2615
|
+
return supported;
|
|
2616
|
+
},
|
|
2617
|
+
get surface() {
|
|
2618
|
+
return surface;
|
|
2619
|
+
},
|
|
2620
|
+
get surfaceVersion() {
|
|
2621
|
+
return surfaceVersion;
|
|
2622
|
+
},
|
|
2623
|
+
get version() {
|
|
2624
|
+
return version2;
|
|
2625
|
+
},
|
|
2626
|
+
observe() {
|
|
2627
|
+
return {
|
|
2628
|
+
colliderError: colliderGap(field.toColliderHeights()),
|
|
2629
|
+
colliderVersion: surfaceVersion,
|
|
2630
|
+
contacts,
|
|
2631
|
+
load,
|
|
2632
|
+
loadProvenance: "solver-impulse-per-step",
|
|
2633
|
+
supported,
|
|
2634
|
+
version: snow.version
|
|
2635
|
+
};
|
|
2636
|
+
},
|
|
2637
|
+
remove(body) {
|
|
2638
|
+
watched.delete(body.collider.id);
|
|
2639
|
+
},
|
|
2640
|
+
loadOf(body) {
|
|
2641
|
+
return watched.get(body.collider.id)?.load ?? 0;
|
|
2642
|
+
},
|
|
2643
|
+
step(deltaTime) {
|
|
2644
|
+
if (disposed) return;
|
|
2645
|
+
if (!Number.isFinite(deltaTime) || deltaTime <= 0)
|
|
2646
|
+
throw new Error("attachSnowPhysics step requires a positive finite delta time.");
|
|
2647
|
+
contacts = 0;
|
|
2648
|
+
supported = 0;
|
|
2649
|
+
load = 0;
|
|
2650
|
+
if (watched.size > 0) press(deltaTime);
|
|
2651
|
+
const deposition = Math.max(0, finiteOr(binding.deposition, 0, "deposition"));
|
|
2652
|
+
const wind = Math.max(0, finiteOr(binding.wind, 0, "wind"));
|
|
2653
|
+
if (deposition > 0 || wind > 0) snow.recover(deltaTime, deposition, wind);
|
|
2654
|
+
refreshSurface();
|
|
2655
|
+
version2 = snow.version;
|
|
2656
|
+
},
|
|
2657
|
+
dispose() {
|
|
2658
|
+
if (disposed) return;
|
|
2659
|
+
disposed = true;
|
|
2660
|
+
changes.dispose();
|
|
2661
|
+
watched.clear();
|
|
2662
|
+
simulation.removeBody(surfaceBody.id);
|
|
2663
|
+
}
|
|
2664
|
+
};
|
|
2665
|
+
return binding;
|
|
2666
|
+
}
|
|
2667
|
+
function finiteOr(value, fallback, name) {
|
|
2668
|
+
if (value === void 0) return fallback;
|
|
2669
|
+
if (!Number.isFinite(value)) throw new Error(`attachSnowPhysics ${name} must be finite.`);
|
|
2670
|
+
return value;
|
|
2671
|
+
}
|
|
2672
|
+
var MIN_CONTACT_AREA = 1e-4;
|
|
2673
|
+
var footprintAreas = /* @__PURE__ */ new WeakMap();
|
|
2674
|
+
function footprintArea(footprint) {
|
|
2675
|
+
const cached2 = footprintAreas.get(footprint);
|
|
2676
|
+
if (cached2 !== void 0) return cached2;
|
|
2677
|
+
const steps = 24;
|
|
2678
|
+
const step = footprint.extent * 2 / steps;
|
|
2679
|
+
let area = 0;
|
|
2680
|
+
for (let row = 0; row < steps; row += 1) {
|
|
2681
|
+
const z = -footprint.extent + (row + 0.5) * step;
|
|
2682
|
+
for (let column = 0; column < steps; column += 1) {
|
|
2683
|
+
const x = -footprint.extent + (column + 0.5) * step;
|
|
2684
|
+
area += footprint.sample(x, z).coverage * step * step;
|
|
2685
|
+
}
|
|
2686
|
+
}
|
|
2687
|
+
const measured = Math.max(area, MIN_CONTACT_AREA);
|
|
2688
|
+
footprintAreas.set(footprint, measured);
|
|
2689
|
+
return measured;
|
|
2690
|
+
}
|
|
2201
2691
|
|
|
2202
2692
|
// src/plugin.ts
|
|
2203
2693
|
var PHYSICS_DEBUG_BODY_LIMIT = 100;
|
|
@@ -2244,10 +2734,21 @@ function rapier2(options = {}) {
|
|
|
2244
2734
|
const activeContacts = /* @__PURE__ */ new Map();
|
|
2245
2735
|
let debugSeries = [];
|
|
2246
2736
|
let completedSteps = 0;
|
|
2737
|
+
let gameContext;
|
|
2738
|
+
const interpolationRemovals = /* @__PURE__ */ new Map();
|
|
2739
|
+
function updateInterpolation(body) {
|
|
2740
|
+
interpolationRemovals.get(body)?.();
|
|
2741
|
+
interpolationRemovals.delete(body);
|
|
2742
|
+
if (body.interpolate && body.object !== void 0 && gameContext?.interpolate !== void 0)
|
|
2743
|
+
interpolationRemovals.set(body, gameContext.interpolate(body.object));
|
|
2744
|
+
}
|
|
2247
2745
|
let unregisterObservations;
|
|
2248
2746
|
function buildContext(selected) {
|
|
2249
2747
|
return {
|
|
2748
|
+
resetInterpolation: (object) => gameContext?.resetInterpolation?.(object),
|
|
2749
|
+
updateInterpolation,
|
|
2250
2750
|
add: (body) => {
|
|
2751
|
+
updateInterpolation(body);
|
|
2251
2752
|
bodies.add(body);
|
|
2252
2753
|
bodiesById.set(body.body.id, body);
|
|
2253
2754
|
},
|
|
@@ -2263,6 +2764,8 @@ function rapier2(options = {}) {
|
|
|
2263
2764
|
kinematicMotion: (colliderHandle) => kinematicMotions.get(colliderHandle),
|
|
2264
2765
|
numBodies: () => bodies.size,
|
|
2265
2766
|
remove: (body) => {
|
|
2767
|
+
interpolationRemovals.get(body)?.();
|
|
2768
|
+
interpolationRemovals.delete(body);
|
|
2266
2769
|
bodies.delete(body);
|
|
2267
2770
|
bodiesById.delete(body.body.id);
|
|
2268
2771
|
removeContactsFor(body.body.id);
|
|
@@ -2280,6 +2783,7 @@ function rapier2(options = {}) {
|
|
|
2280
2783
|
}
|
|
2281
2784
|
return {
|
|
2282
2785
|
setup: async (ctx, runtime) => {
|
|
2786
|
+
gameContext = ctx;
|
|
2283
2787
|
await backend.initialize();
|
|
2284
2788
|
const selected = backend.createSimulation(options);
|
|
2285
2789
|
simulation = selected;
|
|
@@ -2688,6 +3192,8 @@ var VehicleBody3D = class extends RigidBody3D {
|
|
|
2688
3192
|
requireFinite(yaw, "yaw");
|
|
2689
3193
|
this.#simulation.resetVehicle(this.#vehicle, position, yaw);
|
|
2690
3194
|
this.#object.position.set(position.x, position.y, position.z);
|
|
3195
|
+
this.#object.rotation.set(0, yaw, 0);
|
|
3196
|
+
this.physics?.resetInterpolation?.(this.#object);
|
|
2691
3197
|
}
|
|
2692
3198
|
dispose() {
|
|
2693
3199
|
super.dispose();
|
|
@@ -2719,4 +3225,4 @@ var VehicleBody3D = class extends RigidBody3D {
|
|
|
2719
3225
|
}
|
|
2720
3226
|
};
|
|
2721
3227
|
|
|
2722
|
-
export { Area3D, Buoyancy3D, CharacterBody3D, CollisionShape3D, Joint3D, MAX_PHYSICS_QUERY_RESULTS, PHYSICS_COLLISION_EVENT_STRIDE, PHYSICS_TRANSFORM_STRIDE, PHYSICS_VEHICLE_WHEEL_STRIDE, PhysicsDirectSpaceState3D, RigidBody3D, VehicleBody3D, buildStaticColliders, interactionGroups, rapier2 as rapier, softBodyCollision };
|
|
3228
|
+
export { Area3D, Buoyancy3D, CharacterBody3D, CollisionShape3D, Joint3D, MAX_PHYSICS_QUERY_RESULTS, PHYSICS_COLLISION_EVENT_STRIDE, PHYSICS_TRANSFORM_STRIDE, PHYSICS_VEHICLE_WHEEL_STRIDE, PhysicsDirectSpaceState3D, RigidBody3D, VehicleBody3D, attachSnowPhysics, boxFootprint, buildStaticColliders, capsuleFootprint, interactionGroups, rapier2 as rapier, softBodyCollision };
|
package/dist/native/index.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
export { A as Area3D, $ as AreaEvent, a0 as AreaHandler, B as Buoyancy3D,
|
|
2
|
-
export { IBuildStaticCollidersOptions, IFixedJoint3DOptions, IHingeJoint3DOptions, IJoint3DOptions, IPinJoint3DOptions, IStaticColliderContext, IVehicleBody3DOptions, IVehicleWheel3D, Joint3D, PhysicsJointBody, StaticColliderPredicate, VehicleBody3D, VehicleForwardAxis, buildStaticColliders, interactionGroups, softBodyCollision } from '../index.js';
|
|
1
|
+
export { A as Area3D, $ as AreaEvent, a0 as AreaHandler, B as Buoyancy3D, h as BuoyancyPointPosition, i as CharacterBody3D, C as CollisionShape3D, a1 as CollisionShapeKind, a2 as IArea3DOptions, a3 as IAreaContact, j as IBuoyancy3DOptions, k as IBuoyancyHullPoint, l as IBuoyancySurface, m as IBuoyancySurfaceSample, a4 as ICharacterBody3DOptions, o as IIntersectPointOptions, p as IIntersectRayOptions, q as IIntersectShapeOptions, r as IPhysicsBodyCreateOptions, b as IPhysicsBodyHandle, s as IPhysicsCharacterOptions, f as IPhysicsColliderHandle, I as IPhysicsContext, t as IPhysicsHandle, u as IPhysicsInputSnapshot, v as IPhysicsJointCreateOptions, e as IPhysicsJointLimit, a5 as IPhysicsOptions, w as IPhysicsPointQuery, x as IPhysicsQueryHit, y as IPhysicsRayHit, z as IPhysicsRayQuery, d as IPhysicsRotation, D as IPhysicsRuntimeSimulation, E as IPhysicsShapeDescriptor, F as IPhysicsShapeQuery, G as IPhysicsSimulation, c as IPhysicsVector3, H as IPhysicsVehicleCreateOptions, J as IPhysicsVehicleInput, K as IPhysicsVehicleState, a as IPhysicsWorldHandle, L as IPointHit, M as IRayHit, N as IRigidBody3DOptions, O as IShapeHit, Q as MAX_PHYSICS_QUERY_RESULTS, S as PHYSICS_COLLISION_EVENT_STRIDE, T as PHYSICS_TRANSFORM_STRIDE, U as PHYSICS_VEHICLE_WHEEL_STRIDE, P as PhysicsBody3D, V as PhysicsDirectSpaceState3D, W as PhysicsJointKind, a6 as PhysicsPlugin, X as PhysicsQueryVector3, Y as PhysicsShapeKind, R as RigidBody3D, Z as RigidBodyType, _ as rapier } from '../Area3D-vbmjpxsN.js';
|
|
2
|
+
export { IBuildStaticCollidersOptions, IFixedJoint3DOptions, IHingeJoint3DOptions, IJoint3DOptions, IPinJoint3DOptions, ISnowPhysicsBinding, ISnowPhysicsObservation, ISnowPhysicsOptions, IStaticColliderContext, IVehicleBody3DOptions, IVehicleWheel3D, Joint3D, PhysicsJointBody, StaticColliderPredicate, VehicleBody3D, VehicleForwardAxis, attachSnowPhysics, boxFootprint, buildStaticColliders, capsuleFootprint, interactionGroups, softBodyCollision } from '../index.js';
|
|
3
3
|
import 'three';
|
|
4
4
|
import '@threenative/core';
|
|
5
5
|
import 'recast-navigation';
|
|
6
|
+
import '@threenative/core/world';
|