@threenative/physics 0.3.0 → 0.3.2
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-CnqoAfEr.d.ts → Area3D-BQDiAwQb.d.ts} +7 -1
- package/dist/index.d.ts +31 -4
- package/dist/index.js +182 -74
- package/dist/native/index.d.ts +2 -2
- package/dist/native/index.js +183 -73
- package/dist/navigation/index.d.ts +1 -1
- package/package.json +21 -3
|
@@ -71,6 +71,8 @@ interface IPhysicsBodyCreateOptions {
|
|
|
71
71
|
readonly mass: number;
|
|
72
72
|
/** Must match `shape.sensor`; conflicting values are rejected during body creation. */
|
|
73
73
|
readonly sensor: boolean;
|
|
74
|
+
/** Enable continuous collision for fast-moving dynamic bodies. Defaults true for dynamic bodies and is always false for non-dynamic bodies. */
|
|
75
|
+
readonly continuousCollision?: boolean;
|
|
74
76
|
}
|
|
75
77
|
interface IPhysicsVector3 {
|
|
76
78
|
readonly x: number;
|
|
@@ -280,6 +282,8 @@ interface IRigidBody3DOptions {
|
|
|
280
282
|
readonly collisionLayer?: number;
|
|
281
283
|
/** Godot's collision_mask — which layers this body scans. Default 0xffff. */
|
|
282
284
|
readonly collisionMask?: number;
|
|
285
|
+
/** Enable continuous collision for fast-moving dynamic bodies. Dynamic defaults true; fixed and kinematic bodies are always disabled. */
|
|
286
|
+
readonly continuousCollision?: boolean;
|
|
283
287
|
}
|
|
284
288
|
declare class RigidBody3D {
|
|
285
289
|
#private;
|
|
@@ -290,6 +294,8 @@ declare class RigidBody3D {
|
|
|
290
294
|
readonly shape: CollisionShape3D;
|
|
291
295
|
readonly type: RigidBodyType;
|
|
292
296
|
readonly mass: number;
|
|
297
|
+
/** The effective continuous-collision setting; dynamic defaults true, fixed and kinematic are always false. */
|
|
298
|
+
readonly continuousCollision: boolean;
|
|
293
299
|
buoyancy: Buoyancy3D | undefined;
|
|
294
300
|
constructor(options: IRigidBody3DOptions);
|
|
295
301
|
get physics(): IPhysicsContext | undefined;
|
|
@@ -710,4 +716,4 @@ declare class Area3D {
|
|
|
710
716
|
dispose(): void;
|
|
711
717
|
}
|
|
712
718
|
|
|
713
|
-
export { type
|
|
719
|
+
export { type ICharacterBody3DOptions as $, Area3D as A, Buoyancy3D as B, CharacterBody3D as C, type IPhysicsShapeDescriptor as D, type IPhysicsShapeQuery as E, type IPhysicsSimulation as F, type IPointHit as G, type IRayHit as H, type IPhysicsContext as I, type IRigidBody3DOptions as J, type IShapeHit as K, PHYSICS_TRANSFORM_STRIDE as L, MAX_PHYSICS_QUERY_RESULTS as M, type PhysicsBody3D as N, PhysicsDirectSpaceState3D as O, PHYSICS_COLLISION_EVENT_STRIDE as P, type PhysicsJointKind as Q, RigidBody3D as R, type PhysicsQueryVector3 as S, type PhysicsShapeKind as T, type RigidBodyType as U, rapier as V, type AreaEvent as W, type AreaHandler as X, type CollisionShapeKind as Y, type IArea3DOptions as Z, type IAreaContact as _, type IPhysicsWorldHandle as a, type IPhysicsOptions as a0, type PhysicsPlugin as a1, type INavigationAgent3DOptions as a2, type INavigationContext as a3, type INavigationObstacle3DOptions as a4, type INavigationRegion3DOptions as a5, NavigationAgent3D as a6, type NavigationAgentEvent as a7, type NavigationAgentHandler as a8, NavigationObstacle3D as a9, type NavigationPlugin as aa, NavigationRegion3D as ab, recast as ac, type IPhysicsBodyHandle as b, type IPhysicsVector3 as c, type IPhysicsRotation as d, type IPhysicsJointLimit as e, type BuoyancyPointPosition as f, CollisionShape3D as g, type IBuoyancy3DOptions as h, type IBuoyancyHullPoint as i, type IBuoyancySurface as j, type IBuoyancySurfaceSample as k, type ICollisionShapeHandle as l, type IIntersectPointOptions as m, type IIntersectRayOptions as n, type IIntersectShapeOptions as o, type IPhysicsBodyCreateOptions as p, type IPhysicsCharacterOptions as q, type IPhysicsColliderHandle as r, type IPhysicsHandle as s, type IPhysicsInputSnapshot as t, type IPhysicsJointCreateOptions as u, type IPhysicsPointQuery as v, type IPhysicsQueryHit as w, type IPhysicsRayHit as x, type IPhysicsRayQuery as y, type IPhysicsRuntimeSimulation as z };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { I as IPhysicsContext, a as IPhysicsWorldHandle, b as IPhysicsBodyHandle, c as IPhysicsVector3, d as IPhysicsRotation, e as IPhysicsJointLimit, R as RigidBody3D } from './Area3D-
|
|
2
|
-
export { A as Area3D, B as Buoyancy3D, f as BuoyancyPointPosition, C as CharacterBody3D, g as CollisionShape3D, h as IBuoyancy3DOptions, i as IBuoyancyHullPoint, j as IBuoyancySurface, k as IBuoyancySurfaceSample, l as ICollisionShapeHandle, m as IIntersectPointOptions, n as IIntersectRayOptions, o as IIntersectShapeOptions, p as IPhysicsBodyCreateOptions, q as IPhysicsCharacterOptions, r as IPhysicsColliderHandle, s as IPhysicsHandle, t as IPhysicsInputSnapshot, u as IPhysicsJointCreateOptions, v as IPhysicsPointQuery, w as IPhysicsQueryHit, x as IPhysicsRayHit, y as IPhysicsRayQuery, z as IPhysicsRuntimeSimulation, D as IPhysicsShapeDescriptor, E as IPhysicsShapeQuery, F as IPhysicsSimulation, G as IPointHit, H as IRayHit, J as IShapeHit, M as MAX_PHYSICS_QUERY_RESULTS, P as PHYSICS_COLLISION_EVENT_STRIDE,
|
|
1
|
+
import { I as IPhysicsContext, a as IPhysicsWorldHandle, b as IPhysicsBodyHandle, c as IPhysicsVector3, d as IPhysicsRotation, e as IPhysicsJointLimit, R as RigidBody3D } from './Area3D-BQDiAwQb.js';
|
|
2
|
+
export { A as Area3D, B as Buoyancy3D, f as BuoyancyPointPosition, C as CharacterBody3D, g as CollisionShape3D, h as IBuoyancy3DOptions, i as IBuoyancyHullPoint, j as IBuoyancySurface, k as IBuoyancySurfaceSample, l as ICollisionShapeHandle, m as IIntersectPointOptions, n as IIntersectRayOptions, o as IIntersectShapeOptions, p as IPhysicsBodyCreateOptions, q as IPhysicsCharacterOptions, r as IPhysicsColliderHandle, s as IPhysicsHandle, t as IPhysicsInputSnapshot, u as IPhysicsJointCreateOptions, v as IPhysicsPointQuery, w as IPhysicsQueryHit, x as IPhysicsRayHit, y as IPhysicsRayQuery, z as IPhysicsRuntimeSimulation, D as IPhysicsShapeDescriptor, E as IPhysicsShapeQuery, F as IPhysicsSimulation, G as IPointHit, H as IRayHit, J as IRigidBody3DOptions, K as IShapeHit, M as MAX_PHYSICS_QUERY_RESULTS, P as PHYSICS_COLLISION_EVENT_STRIDE, L as PHYSICS_TRANSFORM_STRIDE, N as PhysicsBody3D, O as PhysicsDirectSpaceState3D, Q as PhysicsJointKind, S as PhysicsQueryVector3, T as PhysicsShapeKind, U as RigidBodyType, V as rapier } from './Area3D-BQDiAwQb.js';
|
|
3
|
+
import { Object3D } from 'three';
|
|
3
4
|
import { ISoftBodyCollision } from '@threenative/core';
|
|
4
|
-
import 'three';
|
|
5
5
|
import 'recast-navigation';
|
|
6
6
|
|
|
7
7
|
/** A body handle or a shared physics node exposing one. */
|
|
@@ -42,10 +42,37 @@ declare class Joint3D {
|
|
|
42
42
|
dispose(): void;
|
|
43
43
|
}
|
|
44
44
|
|
|
45
|
+
type StaticColliderPredicate = (object: Object3D) => boolean;
|
|
46
|
+
interface IStaticColliderContext {
|
|
47
|
+
readonly physics: IPhysicsContext;
|
|
48
|
+
}
|
|
49
|
+
interface IBuildStaticCollidersOptions {
|
|
50
|
+
/** Game-owned filter for decorative or otherwise non-colliding meshes. */
|
|
51
|
+
readonly predicate?: StaticColliderPredicate;
|
|
52
|
+
/** Alias for predicate when the call site wants the intent to read explicitly. */
|
|
53
|
+
readonly shouldCollide?: StaticColliderPredicate;
|
|
54
|
+
/** Meshes whose lowest point is at or above this height are outside the reachable world. */
|
|
55
|
+
readonly reachableCeiling?: number;
|
|
56
|
+
/** Collision layer occupied by the generated fixed bodies. */
|
|
57
|
+
readonly collisionLayer?: number;
|
|
58
|
+
/** Collision mask scanned by the generated fixed bodies. */
|
|
59
|
+
readonly collisionMask?: number;
|
|
60
|
+
}
|
|
61
|
+
type StaticColliderFilter = StaticColliderPredicate | IBuildStaticCollidersOptions;
|
|
62
|
+
/**
|
|
63
|
+
* Turn selected meshes under an authored scene root into fixed trimesh bodies.
|
|
64
|
+
*
|
|
65
|
+
* The root and predicate belong to the game; this helper owns the reusable walk, reachability
|
|
66
|
+
* cull, exact pierced-mesh shape and world-space instance carrier. It returns every generated
|
|
67
|
+
* body so the game can dispose or inspect the cold-path registrations, and throws when the filter
|
|
68
|
+
* produces no colliders rather than letting a level silently become non-solid.
|
|
69
|
+
*/
|
|
70
|
+
declare function buildStaticColliders(context: IStaticColliderContext, root: Object3D, filter?: StaticColliderFilter): readonly RigidBody3D[];
|
|
71
|
+
|
|
45
72
|
/** Adapt existing physics bodies into `SoftBody3D` collision input without a second shape API. */
|
|
46
73
|
declare function softBodyCollision(...bodies: readonly RigidBody3D[]): ISoftBodyCollision;
|
|
47
74
|
|
|
48
75
|
/** Rapier packs membership in the high 16 bits and the filter in the low 16. */
|
|
49
76
|
declare function interactionGroups(layer: number, mask: number): number;
|
|
50
77
|
|
|
51
|
-
export { type IFixedJoint3DOptions, type IHingeJoint3DOptions, type IJoint3DOptions, IPhysicsBodyHandle, IPhysicsContext, IPhysicsJointLimit, IPhysicsRotation, IPhysicsVector3, IPhysicsWorldHandle, type IPinJoint3DOptions, Joint3D, type PhysicsJointBody, RigidBody3D, interactionGroups, softBodyCollision };
|
|
78
|
+
export { type IBuildStaticCollidersOptions, type IFixedJoint3DOptions, type IHingeJoint3DOptions, type IJoint3DOptions, IPhysicsBodyHandle, IPhysicsContext, IPhysicsJointLimit, IPhysicsRotation, IPhysicsVector3, IPhysicsWorldHandle, type IPinJoint3DOptions, type IStaticColliderContext, Joint3D, type PhysicsJointBody, RigidBody3D, type StaticColliderPredicate, buildStaticColliders, interactionGroups, softBodyCollision };
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import * as rapier from '@dimforge/rapier3d-compat';
|
|
2
|
-
import { Vector3, Quaternion,
|
|
2
|
+
import { Matrix4, Vector3, Quaternion, Box3, Object3D, Mesh, InstancedMesh } from 'three';
|
|
3
3
|
|
|
4
4
|
// src/web.ts
|
|
5
5
|
|
|
@@ -41,6 +41,9 @@ function f32LengthSquared(values) {
|
|
|
41
41
|
}
|
|
42
42
|
return lengthSquared;
|
|
43
43
|
}
|
|
44
|
+
function effectiveContinuousCollision(type, requested) {
|
|
45
|
+
return type === "dynamic" && (requested ?? true);
|
|
46
|
+
}
|
|
44
47
|
function requireFiniteVector(vector, label) {
|
|
45
48
|
if (typeof vector?.x !== "number" || typeof vector.y !== "number" || typeof vector.z !== "number" || !Number.isFinite(vector.x) || !Number.isFinite(vector.y) || !Number.isFinite(vector.z))
|
|
46
49
|
throw new Error(`TN_PHYSICS_NON_FINITE: ${label} must be a finite { x, y, z }.`);
|
|
@@ -265,9 +268,10 @@ function createWebPhysicsShape(rapier3, shape, sensor = shape.sensor) {
|
|
|
265
268
|
descriptor.setActiveEvents(rapier3.ActiveEvents.COLLISION_EVENTS);
|
|
266
269
|
return descriptor;
|
|
267
270
|
}
|
|
268
|
-
function bodyDescription(rapier3, type, position, rotation, mass) {
|
|
271
|
+
function bodyDescription(rapier3, type, position, rotation, mass, continuousCollision) {
|
|
269
272
|
const description = type === "fixed" ? rapier3.RigidBodyDesc.fixed() : type === "kinematic" || type === "character" ? rapier3.RigidBodyDesc.kinematicPositionBased() : rapier3.RigidBodyDesc.dynamic();
|
|
270
273
|
description.setTranslation(position.x, position.y, position.z).setRotation({ x: rotation.x, y: rotation.y, z: rotation.z, w: rotation.w });
|
|
274
|
+
if (type === "dynamic") description.setCcdEnabled(continuousCollision);
|
|
271
275
|
if (mass !== 0) description.setAdditionalMass(mass);
|
|
272
276
|
return description;
|
|
273
277
|
}
|
|
@@ -404,6 +408,8 @@ function createWebPhysicsSimulation(options) {
|
|
|
404
408
|
const sensor = requirePhysicsBodySensor(bodyOptions);
|
|
405
409
|
if (!Number.isFinite(bodyOptions.mass) || bodyOptions.mass < 0)
|
|
406
410
|
throw new Error("Physics body mass must be a finite non-negative number.");
|
|
411
|
+
if (bodyOptions.continuousCollision !== void 0 && typeof bodyOptions.continuousCollision !== "boolean")
|
|
412
|
+
throw new Error("Physics body continuousCollision must be a boolean.");
|
|
407
413
|
requireFiniteVector(bodyOptions.position, "body position");
|
|
408
414
|
requireFiniteRotation(bodyOptions.rotation, "body rotation");
|
|
409
415
|
const id = nextId;
|
|
@@ -415,7 +421,8 @@ function createWebPhysicsSimulation(options) {
|
|
|
415
421
|
bodyOptions.type,
|
|
416
422
|
bodyOptions.position,
|
|
417
423
|
bodyOptions.rotation,
|
|
418
|
-
bodyOptions.mass
|
|
424
|
+
bodyOptions.mass,
|
|
425
|
+
effectiveContinuousCollision(bodyOptions.type, bodyOptions.continuousCollision)
|
|
419
426
|
)
|
|
420
427
|
);
|
|
421
428
|
const rawCollider = options.world.createCollider(rawShape, rawBody);
|
|
@@ -1681,75 +1688,6 @@ var PhysicsDirectSpaceState3D = class {
|
|
|
1681
1688
|
return this.#simulation.intersectPoint(pointQuery(options));
|
|
1682
1689
|
}
|
|
1683
1690
|
};
|
|
1684
|
-
var ONE = new Vector3(1, 1, 1);
|
|
1685
|
-
var PhysicsSoftBodyCollision = class {
|
|
1686
|
-
capacity;
|
|
1687
|
-
#bodies;
|
|
1688
|
-
#bodyMatrix = new Matrix4();
|
|
1689
|
-
#localMatrix = new Matrix4();
|
|
1690
|
-
#corner = new Vector3();
|
|
1691
|
-
#minimum = new Vector3();
|
|
1692
|
-
#maximum = new Vector3();
|
|
1693
|
-
constructor(bodies) {
|
|
1694
|
-
if (bodies.length === 0)
|
|
1695
|
-
throw new Error("softBodyCollision requires at least one RigidBody3D.");
|
|
1696
|
-
for (const body of bodies) {
|
|
1697
|
-
if (body.shape.descriptor.kind !== "box")
|
|
1698
|
-
throw new Error("softBodyCollision currently supports box shapes only.");
|
|
1699
|
-
if (body.object === void 0)
|
|
1700
|
-
throw new Error("softBodyCollision requires an object transform for every body.");
|
|
1701
|
-
}
|
|
1702
|
-
this.#bodies = bodies;
|
|
1703
|
-
this.capacity = bodies.length;
|
|
1704
|
-
}
|
|
1705
|
-
writeBoxes(target, worldToLocal) {
|
|
1706
|
-
if (target.length !== this.capacity * 8)
|
|
1707
|
-
throw new Error(
|
|
1708
|
-
`softBodyCollision expected ${this.capacity * 8} packed values, received ${target.length}.`
|
|
1709
|
-
);
|
|
1710
|
-
for (let index = 0; index < this.#bodies.length; index += 1) {
|
|
1711
|
-
const body = this.#bodies[index];
|
|
1712
|
-
const object = body.object;
|
|
1713
|
-
if (object === void 0)
|
|
1714
|
-
throw new Error("softBodyCollision body lost its object transform.");
|
|
1715
|
-
this.#bodyMatrix.compose(object.position, object.quaternion, ONE);
|
|
1716
|
-
this.#localMatrix.multiplyMatrices(worldToLocal, this.#bodyMatrix);
|
|
1717
|
-
this.#minimum.set(
|
|
1718
|
-
Number.POSITIVE_INFINITY,
|
|
1719
|
-
Number.POSITIVE_INFINITY,
|
|
1720
|
-
Number.POSITIVE_INFINITY
|
|
1721
|
-
);
|
|
1722
|
-
this.#maximum.set(
|
|
1723
|
-
Number.NEGATIVE_INFINITY,
|
|
1724
|
-
Number.NEGATIVE_INFINITY,
|
|
1725
|
-
Number.NEGATIVE_INFINITY
|
|
1726
|
-
);
|
|
1727
|
-
const shape = body.shape.descriptor;
|
|
1728
|
-
for (let corner = 0; corner < 8; corner += 1) {
|
|
1729
|
-
this.#corner.set(
|
|
1730
|
-
(corner & 1) === 0 ? -shape.x : shape.x,
|
|
1731
|
-
(corner & 2) === 0 ? -shape.y : shape.y,
|
|
1732
|
-
(corner & 4) === 0 ? -shape.z : shape.z
|
|
1733
|
-
).applyMatrix4(this.#localMatrix);
|
|
1734
|
-
this.#minimum.min(this.#corner);
|
|
1735
|
-
this.#maximum.max(this.#corner);
|
|
1736
|
-
}
|
|
1737
|
-
const offset = index * 8;
|
|
1738
|
-
target[offset] = (this.#minimum.x + this.#maximum.x) / 2;
|
|
1739
|
-
target[offset + 1] = (this.#minimum.y + this.#maximum.y) / 2;
|
|
1740
|
-
target[offset + 2] = (this.#minimum.z + this.#maximum.z) / 2;
|
|
1741
|
-
target[offset + 3] = 0;
|
|
1742
|
-
target[offset + 4] = (this.#maximum.x - this.#minimum.x) / 2;
|
|
1743
|
-
target[offset + 5] = (this.#maximum.y - this.#minimum.y) / 2;
|
|
1744
|
-
target[offset + 6] = (this.#maximum.z - this.#minimum.z) / 2;
|
|
1745
|
-
target[offset + 7] = 0;
|
|
1746
|
-
}
|
|
1747
|
-
return this.#bodies.length;
|
|
1748
|
-
}
|
|
1749
|
-
};
|
|
1750
|
-
function softBodyCollision(...bodies) {
|
|
1751
|
-
return new PhysicsSoftBodyCollision(bodies);
|
|
1752
|
-
}
|
|
1753
1691
|
|
|
1754
1692
|
// src/RigidBody3D.ts
|
|
1755
1693
|
var RigidBody3D = class {
|
|
@@ -1760,6 +1698,8 @@ var RigidBody3D = class {
|
|
|
1760
1698
|
shape;
|
|
1761
1699
|
type;
|
|
1762
1700
|
mass;
|
|
1701
|
+
/** The effective continuous-collision setting; dynamic defaults true, fixed and kinematic are always false. */
|
|
1702
|
+
continuousCollision;
|
|
1763
1703
|
buoyancy;
|
|
1764
1704
|
#simulation;
|
|
1765
1705
|
#physics;
|
|
@@ -1781,6 +1721,9 @@ var RigidBody3D = class {
|
|
|
1781
1721
|
this.#object = options.object;
|
|
1782
1722
|
this.type = type;
|
|
1783
1723
|
this.mass = options.mass ?? 0;
|
|
1724
|
+
if (options.continuousCollision !== void 0 && typeof options.continuousCollision !== "boolean")
|
|
1725
|
+
throw new Error("RigidBody3D continuousCollision must be a boolean.");
|
|
1726
|
+
this.continuousCollision = effectiveContinuousCollision(type, options.continuousCollision);
|
|
1784
1727
|
this.buoyancy = void 0;
|
|
1785
1728
|
const shape = options.shape.descriptor;
|
|
1786
1729
|
if (options.collisionLayer !== void 0 || options.collisionMask !== void 0) {
|
|
@@ -1795,7 +1738,8 @@ var RigidBody3D = class {
|
|
|
1795
1738
|
rotation: initial.rotation,
|
|
1796
1739
|
sensor: shape.sensor,
|
|
1797
1740
|
shape,
|
|
1798
|
-
type: this.type
|
|
1741
|
+
type: this.type,
|
|
1742
|
+
continuousCollision: this.continuousCollision
|
|
1799
1743
|
});
|
|
1800
1744
|
options.shape.bindRaw(registration.rawShape);
|
|
1801
1745
|
this.body = registration.body;
|
|
@@ -1913,6 +1857,170 @@ var RigidBody3D = class {
|
|
|
1913
1857
|
}
|
|
1914
1858
|
};
|
|
1915
1859
|
|
|
1860
|
+
// src/static-colliders.ts
|
|
1861
|
+
var DEFAULT_REACHABLE_CEILING = 4.5;
|
|
1862
|
+
var scratchInstance = new Matrix4();
|
|
1863
|
+
var scratchWorld = new Matrix4();
|
|
1864
|
+
var scratchPosition = new Vector3();
|
|
1865
|
+
var scratchScale = new Vector3();
|
|
1866
|
+
var scratchRotation = new Quaternion();
|
|
1867
|
+
var scratchBounds = new Box3();
|
|
1868
|
+
function optionsFor(filter) {
|
|
1869
|
+
return typeof filter === "function" ? { predicate: filter } : filter ?? {};
|
|
1870
|
+
}
|
|
1871
|
+
function validateCeiling(value) {
|
|
1872
|
+
if (!Number.isFinite(value))
|
|
1873
|
+
throw new Error(
|
|
1874
|
+
`buildStaticColliders reachableCeiling must be finite, received ${String(value)}.`
|
|
1875
|
+
);
|
|
1876
|
+
return value;
|
|
1877
|
+
}
|
|
1878
|
+
function selectPredicate(options) {
|
|
1879
|
+
const predicate = options.predicate ?? options.shouldCollide ?? (() => true);
|
|
1880
|
+
return (object) => {
|
|
1881
|
+
const selected = predicate(object);
|
|
1882
|
+
if (typeof selected !== "boolean")
|
|
1883
|
+
throw new Error(`buildStaticColliders predicate must return a boolean for '${object.name}'.`);
|
|
1884
|
+
return selected;
|
|
1885
|
+
};
|
|
1886
|
+
}
|
|
1887
|
+
function configureCarrier(carrier, transform, scale) {
|
|
1888
|
+
transform.decompose(scratchPosition, scratchRotation, scale);
|
|
1889
|
+
carrier.position.copy(scratchPosition);
|
|
1890
|
+
carrier.quaternion.copy(scratchRotation);
|
|
1891
|
+
carrier.scale.copy(scale);
|
|
1892
|
+
carrier.updateMatrixWorld(true);
|
|
1893
|
+
}
|
|
1894
|
+
function isReachable(carrier, ceiling) {
|
|
1895
|
+
scratchBounds.setFromObject(carrier, true);
|
|
1896
|
+
return !scratchBounds.isEmpty() && scratchBounds.min.y < ceiling;
|
|
1897
|
+
}
|
|
1898
|
+
function addCollider(context, source, transform, options) {
|
|
1899
|
+
const carrier = new Mesh(source.geometry);
|
|
1900
|
+
const shapeSource = new Mesh(source.geometry);
|
|
1901
|
+
configureCarrier(carrier, transform, scratchScale);
|
|
1902
|
+
shapeSource.scale.copy(scratchScale);
|
|
1903
|
+
if (!isReachable(carrier, options.reachableCeiling ?? DEFAULT_REACHABLE_CEILING))
|
|
1904
|
+
return void 0;
|
|
1905
|
+
return new RigidBody3D({
|
|
1906
|
+
collisionLayer: options.collisionLayer,
|
|
1907
|
+
collisionMask: options.collisionMask,
|
|
1908
|
+
object: carrier,
|
|
1909
|
+
physics: context.physics,
|
|
1910
|
+
shape: CollisionShape3D.fromMesh(shapeSource, "trimesh"),
|
|
1911
|
+
type: "fixed"
|
|
1912
|
+
});
|
|
1913
|
+
}
|
|
1914
|
+
function buildStaticColliders(context, root, filter) {
|
|
1915
|
+
if (context?.physics === void 0)
|
|
1916
|
+
throw new Error("buildStaticColliders requires a physics context.");
|
|
1917
|
+
if (!(root instanceof Object3D))
|
|
1918
|
+
throw new Error("buildStaticColliders requires an Object3D root.");
|
|
1919
|
+
const options = optionsFor(filter);
|
|
1920
|
+
const ceiling = validateCeiling(options.reachableCeiling ?? DEFAULT_REACHABLE_CEILING);
|
|
1921
|
+
const predicate = selectPredicate(options);
|
|
1922
|
+
const bodies = [];
|
|
1923
|
+
root.updateMatrixWorld(true);
|
|
1924
|
+
try {
|
|
1925
|
+
root.traverse((object) => {
|
|
1926
|
+
if (!predicate(object) || !(object instanceof Mesh)) return;
|
|
1927
|
+
if (object instanceof InstancedMesh) {
|
|
1928
|
+
for (let index = 0; index < object.count; index += 1) {
|
|
1929
|
+
object.getMatrixAt(index, scratchInstance);
|
|
1930
|
+
scratchWorld.multiplyMatrices(object.matrixWorld, scratchInstance);
|
|
1931
|
+
const body2 = addCollider(context, object, scratchWorld, {
|
|
1932
|
+
...options,
|
|
1933
|
+
reachableCeiling: ceiling
|
|
1934
|
+
});
|
|
1935
|
+
if (body2 !== void 0) bodies.push(body2);
|
|
1936
|
+
}
|
|
1937
|
+
return;
|
|
1938
|
+
}
|
|
1939
|
+
const body = addCollider(context, object, object.matrixWorld, {
|
|
1940
|
+
...options,
|
|
1941
|
+
reachableCeiling: ceiling
|
|
1942
|
+
});
|
|
1943
|
+
if (body !== void 0) bodies.push(body);
|
|
1944
|
+
});
|
|
1945
|
+
} catch (error) {
|
|
1946
|
+
for (const body of bodies) body.dispose();
|
|
1947
|
+
throw error;
|
|
1948
|
+
}
|
|
1949
|
+
console.info(`TN_STATIC_COLLIDERS:${JSON.stringify({ count: bodies.length })}`);
|
|
1950
|
+
if (bodies.length === 0)
|
|
1951
|
+
throw new Error("buildStaticColliders produced zero colliders; check the scene and predicate.");
|
|
1952
|
+
return bodies;
|
|
1953
|
+
}
|
|
1954
|
+
var ONE = new Vector3(1, 1, 1);
|
|
1955
|
+
var PhysicsSoftBodyCollision = class {
|
|
1956
|
+
capacity;
|
|
1957
|
+
#bodies;
|
|
1958
|
+
#bodyMatrix = new Matrix4();
|
|
1959
|
+
#localMatrix = new Matrix4();
|
|
1960
|
+
#corner = new Vector3();
|
|
1961
|
+
#minimum = new Vector3();
|
|
1962
|
+
#maximum = new Vector3();
|
|
1963
|
+
constructor(bodies) {
|
|
1964
|
+
if (bodies.length === 0)
|
|
1965
|
+
throw new Error("softBodyCollision requires at least one RigidBody3D.");
|
|
1966
|
+
for (const body of bodies) {
|
|
1967
|
+
if (body.shape.descriptor.kind !== "box")
|
|
1968
|
+
throw new Error("softBodyCollision currently supports box shapes only.");
|
|
1969
|
+
if (body.object === void 0)
|
|
1970
|
+
throw new Error("softBodyCollision requires an object transform for every body.");
|
|
1971
|
+
}
|
|
1972
|
+
this.#bodies = bodies;
|
|
1973
|
+
this.capacity = bodies.length;
|
|
1974
|
+
}
|
|
1975
|
+
writeBoxes(target, worldToLocal) {
|
|
1976
|
+
if (target.length !== this.capacity * 8)
|
|
1977
|
+
throw new Error(
|
|
1978
|
+
`softBodyCollision expected ${this.capacity * 8} packed values, received ${target.length}.`
|
|
1979
|
+
);
|
|
1980
|
+
for (let index = 0; index < this.#bodies.length; index += 1) {
|
|
1981
|
+
const body = this.#bodies[index];
|
|
1982
|
+
const object = body.object;
|
|
1983
|
+
if (object === void 0)
|
|
1984
|
+
throw new Error("softBodyCollision body lost its object transform.");
|
|
1985
|
+
this.#bodyMatrix.compose(object.position, object.quaternion, ONE);
|
|
1986
|
+
this.#localMatrix.multiplyMatrices(worldToLocal, this.#bodyMatrix);
|
|
1987
|
+
this.#minimum.set(
|
|
1988
|
+
Number.POSITIVE_INFINITY,
|
|
1989
|
+
Number.POSITIVE_INFINITY,
|
|
1990
|
+
Number.POSITIVE_INFINITY
|
|
1991
|
+
);
|
|
1992
|
+
this.#maximum.set(
|
|
1993
|
+
Number.NEGATIVE_INFINITY,
|
|
1994
|
+
Number.NEGATIVE_INFINITY,
|
|
1995
|
+
Number.NEGATIVE_INFINITY
|
|
1996
|
+
);
|
|
1997
|
+
const shape = body.shape.descriptor;
|
|
1998
|
+
for (let corner = 0; corner < 8; corner += 1) {
|
|
1999
|
+
this.#corner.set(
|
|
2000
|
+
(corner & 1) === 0 ? -shape.x : shape.x,
|
|
2001
|
+
(corner & 2) === 0 ? -shape.y : shape.y,
|
|
2002
|
+
(corner & 4) === 0 ? -shape.z : shape.z
|
|
2003
|
+
).applyMatrix4(this.#localMatrix);
|
|
2004
|
+
this.#minimum.min(this.#corner);
|
|
2005
|
+
this.#maximum.max(this.#corner);
|
|
2006
|
+
}
|
|
2007
|
+
const offset = index * 8;
|
|
2008
|
+
target[offset] = (this.#minimum.x + this.#maximum.x) / 2;
|
|
2009
|
+
target[offset + 1] = (this.#minimum.y + this.#maximum.y) / 2;
|
|
2010
|
+
target[offset + 2] = (this.#minimum.z + this.#maximum.z) / 2;
|
|
2011
|
+
target[offset + 3] = 0;
|
|
2012
|
+
target[offset + 4] = (this.#maximum.x - this.#minimum.x) / 2;
|
|
2013
|
+
target[offset + 5] = (this.#maximum.y - this.#minimum.y) / 2;
|
|
2014
|
+
target[offset + 6] = (this.#maximum.z - this.#minimum.z) / 2;
|
|
2015
|
+
target[offset + 7] = 0;
|
|
2016
|
+
}
|
|
2017
|
+
return this.#bodies.length;
|
|
2018
|
+
}
|
|
2019
|
+
};
|
|
2020
|
+
function softBodyCollision(...bodies) {
|
|
2021
|
+
return new PhysicsSoftBodyCollision(bodies);
|
|
2022
|
+
}
|
|
2023
|
+
|
|
1916
2024
|
// src/plugin.ts
|
|
1917
2025
|
var PHYSICS_DEBUG_BODY_LIMIT = 100;
|
|
1918
2026
|
var PHYSICS_DEBUG_SAMPLE_LIMIT = 100;
|
|
@@ -2264,4 +2372,4 @@ function physicsEntityId(ctx, body) {
|
|
|
2264
2372
|
return stableId;
|
|
2265
2373
|
}
|
|
2266
2374
|
|
|
2267
|
-
export { Area3D, Buoyancy3D, CharacterBody3D, CollisionShape3D, Joint3D, MAX_PHYSICS_QUERY_RESULTS, PHYSICS_COLLISION_EVENT_STRIDE, PHYSICS_TRANSFORM_STRIDE, PhysicsDirectSpaceState3D, RigidBody3D, interactionGroups, rapier2 as rapier, softBodyCollision };
|
|
2375
|
+
export { Area3D, Buoyancy3D, CharacterBody3D, CollisionShape3D, Joint3D, MAX_PHYSICS_QUERY_RESULTS, PHYSICS_COLLISION_EVENT_STRIDE, PHYSICS_TRANSFORM_STRIDE, PhysicsDirectSpaceState3D, RigidBody3D, buildStaticColliders, interactionGroups, rapier2 as rapier, softBodyCollision };
|
package/dist/native/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
export { A as Area3D,
|
|
2
|
-
export { IFixedJoint3DOptions, IHingeJoint3DOptions, IJoint3DOptions, IPinJoint3DOptions, Joint3D, PhysicsJointBody, interactionGroups, softBodyCollision } from '../index.js';
|
|
1
|
+
export { A as Area3D, W as AreaEvent, X as AreaHandler, B as Buoyancy3D, f as BuoyancyPointPosition, C as CharacterBody3D, g as CollisionShape3D, Y as CollisionShapeKind, Z as IArea3DOptions, _ as IAreaContact, h as IBuoyancy3DOptions, i as IBuoyancyHullPoint, j as IBuoyancySurface, k as IBuoyancySurfaceSample, $ as ICharacterBody3DOptions, m as IIntersectPointOptions, n as IIntersectRayOptions, o as IIntersectShapeOptions, p as IPhysicsBodyCreateOptions, b as IPhysicsBodyHandle, q as IPhysicsCharacterOptions, r as IPhysicsColliderHandle, I as IPhysicsContext, s as IPhysicsHandle, t as IPhysicsInputSnapshot, u as IPhysicsJointCreateOptions, e as IPhysicsJointLimit, a0 as IPhysicsOptions, v as IPhysicsPointQuery, w as IPhysicsQueryHit, x as IPhysicsRayHit, y as IPhysicsRayQuery, d as IPhysicsRotation, z as IPhysicsRuntimeSimulation, D as IPhysicsShapeDescriptor, E as IPhysicsShapeQuery, F as IPhysicsSimulation, c as IPhysicsVector3, a as IPhysicsWorldHandle, G as IPointHit, H as IRayHit, J as IRigidBody3DOptions, K as IShapeHit, M as MAX_PHYSICS_QUERY_RESULTS, P as PHYSICS_COLLISION_EVENT_STRIDE, L as PHYSICS_TRANSFORM_STRIDE, N as PhysicsBody3D, O as PhysicsDirectSpaceState3D, Q as PhysicsJointKind, a1 as PhysicsPlugin, S as PhysicsQueryVector3, T as PhysicsShapeKind, R as RigidBody3D, U as RigidBodyType, V as rapier } from '../Area3D-BQDiAwQb.js';
|
|
2
|
+
export { IBuildStaticCollidersOptions, IFixedJoint3DOptions, IHingeJoint3DOptions, IJoint3DOptions, IPinJoint3DOptions, IStaticColliderContext, Joint3D, PhysicsJointBody, StaticColliderPredicate, buildStaticColliders, interactionGroups, softBodyCollision } from '../index.js';
|
|
3
3
|
import 'three';
|
|
4
4
|
import '@threenative/core';
|
|
5
5
|
import 'recast-navigation';
|
package/dist/native/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Vector3, Quaternion,
|
|
1
|
+
import { Matrix4, Vector3, Quaternion, Box3, Object3D, Mesh, InstancedMesh } from 'three';
|
|
2
2
|
|
|
3
3
|
// src/handles.ts
|
|
4
4
|
function physicsHandle(raw) {
|
|
@@ -38,6 +38,9 @@ function f32LengthSquared(values) {
|
|
|
38
38
|
}
|
|
39
39
|
return lengthSquared;
|
|
40
40
|
}
|
|
41
|
+
function effectiveContinuousCollision(type, requested) {
|
|
42
|
+
return type === "dynamic" && (requested ?? true);
|
|
43
|
+
}
|
|
41
44
|
function requireFiniteVector(vector, label) {
|
|
42
45
|
if (typeof vector?.x !== "number" || typeof vector.y !== "number" || typeof vector.z !== "number" || !Number.isFinite(vector.x) || !Number.isFinite(vector.y) || !Number.isFinite(vector.z))
|
|
43
46
|
throw new Error(`TN_PHYSICS_NON_FINITE: ${label} must be a finite { x, y, z }.`);
|
|
@@ -402,6 +405,8 @@ function createNativePhysicsSimulation(raw, version) {
|
|
|
402
405
|
requireLive();
|
|
403
406
|
if (!Number.isFinite(options.mass) || options.mass < 0)
|
|
404
407
|
throw new Error("Physics body mass must be a finite non-negative number.");
|
|
408
|
+
if (options.continuousCollision !== void 0 && typeof options.continuousCollision !== "boolean")
|
|
409
|
+
throw new Error("Physics body continuousCollision must be a boolean.");
|
|
405
410
|
requireFiniteVector(options.position, "body position");
|
|
406
411
|
requireFiniteRotation(options.rotation, "body rotation");
|
|
407
412
|
const shape = primitiveShape(options.shape);
|
|
@@ -414,7 +419,11 @@ function createNativePhysicsSimulation(raw, version) {
|
|
|
414
419
|
rotation: options.rotation,
|
|
415
420
|
sensor,
|
|
416
421
|
shape,
|
|
417
|
-
type: options.type
|
|
422
|
+
type: options.type,
|
|
423
|
+
continuousCollision: effectiveContinuousCollision(
|
|
424
|
+
options.type,
|
|
425
|
+
options.continuousCollision
|
|
426
|
+
)
|
|
418
427
|
});
|
|
419
428
|
if (!Number.isInteger(id) || id < 0)
|
|
420
429
|
throw new Error("TN_NATIVE_PHYSICS_INVALID: runtime returned an invalid body id");
|
|
@@ -1470,75 +1479,6 @@ var PhysicsDirectSpaceState3D = class {
|
|
|
1470
1479
|
return this.#simulation.intersectPoint(pointQuery(options));
|
|
1471
1480
|
}
|
|
1472
1481
|
};
|
|
1473
|
-
var ONE = new Vector3(1, 1, 1);
|
|
1474
|
-
var PhysicsSoftBodyCollision = class {
|
|
1475
|
-
capacity;
|
|
1476
|
-
#bodies;
|
|
1477
|
-
#bodyMatrix = new Matrix4();
|
|
1478
|
-
#localMatrix = new Matrix4();
|
|
1479
|
-
#corner = new Vector3();
|
|
1480
|
-
#minimum = new Vector3();
|
|
1481
|
-
#maximum = new Vector3();
|
|
1482
|
-
constructor(bodies) {
|
|
1483
|
-
if (bodies.length === 0)
|
|
1484
|
-
throw new Error("softBodyCollision requires at least one RigidBody3D.");
|
|
1485
|
-
for (const body of bodies) {
|
|
1486
|
-
if (body.shape.descriptor.kind !== "box")
|
|
1487
|
-
throw new Error("softBodyCollision currently supports box shapes only.");
|
|
1488
|
-
if (body.object === void 0)
|
|
1489
|
-
throw new Error("softBodyCollision requires an object transform for every body.");
|
|
1490
|
-
}
|
|
1491
|
-
this.#bodies = bodies;
|
|
1492
|
-
this.capacity = bodies.length;
|
|
1493
|
-
}
|
|
1494
|
-
writeBoxes(target, worldToLocal) {
|
|
1495
|
-
if (target.length !== this.capacity * 8)
|
|
1496
|
-
throw new Error(
|
|
1497
|
-
`softBodyCollision expected ${this.capacity * 8} packed values, received ${target.length}.`
|
|
1498
|
-
);
|
|
1499
|
-
for (let index = 0; index < this.#bodies.length; index += 1) {
|
|
1500
|
-
const body = this.#bodies[index];
|
|
1501
|
-
const object = body.object;
|
|
1502
|
-
if (object === void 0)
|
|
1503
|
-
throw new Error("softBodyCollision body lost its object transform.");
|
|
1504
|
-
this.#bodyMatrix.compose(object.position, object.quaternion, ONE);
|
|
1505
|
-
this.#localMatrix.multiplyMatrices(worldToLocal, this.#bodyMatrix);
|
|
1506
|
-
this.#minimum.set(
|
|
1507
|
-
Number.POSITIVE_INFINITY,
|
|
1508
|
-
Number.POSITIVE_INFINITY,
|
|
1509
|
-
Number.POSITIVE_INFINITY
|
|
1510
|
-
);
|
|
1511
|
-
this.#maximum.set(
|
|
1512
|
-
Number.NEGATIVE_INFINITY,
|
|
1513
|
-
Number.NEGATIVE_INFINITY,
|
|
1514
|
-
Number.NEGATIVE_INFINITY
|
|
1515
|
-
);
|
|
1516
|
-
const shape = body.shape.descriptor;
|
|
1517
|
-
for (let corner = 0; corner < 8; corner += 1) {
|
|
1518
|
-
this.#corner.set(
|
|
1519
|
-
(corner & 1) === 0 ? -shape.x : shape.x,
|
|
1520
|
-
(corner & 2) === 0 ? -shape.y : shape.y,
|
|
1521
|
-
(corner & 4) === 0 ? -shape.z : shape.z
|
|
1522
|
-
).applyMatrix4(this.#localMatrix);
|
|
1523
|
-
this.#minimum.min(this.#corner);
|
|
1524
|
-
this.#maximum.max(this.#corner);
|
|
1525
|
-
}
|
|
1526
|
-
const offset = index * 8;
|
|
1527
|
-
target[offset] = (this.#minimum.x + this.#maximum.x) / 2;
|
|
1528
|
-
target[offset + 1] = (this.#minimum.y + this.#maximum.y) / 2;
|
|
1529
|
-
target[offset + 2] = (this.#minimum.z + this.#maximum.z) / 2;
|
|
1530
|
-
target[offset + 3] = 0;
|
|
1531
|
-
target[offset + 4] = (this.#maximum.x - this.#minimum.x) / 2;
|
|
1532
|
-
target[offset + 5] = (this.#maximum.y - this.#minimum.y) / 2;
|
|
1533
|
-
target[offset + 6] = (this.#maximum.z - this.#minimum.z) / 2;
|
|
1534
|
-
target[offset + 7] = 0;
|
|
1535
|
-
}
|
|
1536
|
-
return this.#bodies.length;
|
|
1537
|
-
}
|
|
1538
|
-
};
|
|
1539
|
-
function softBodyCollision(...bodies) {
|
|
1540
|
-
return new PhysicsSoftBodyCollision(bodies);
|
|
1541
|
-
}
|
|
1542
1482
|
|
|
1543
1483
|
// src/RigidBody3D.ts
|
|
1544
1484
|
var RigidBody3D = class {
|
|
@@ -1549,6 +1489,8 @@ var RigidBody3D = class {
|
|
|
1549
1489
|
shape;
|
|
1550
1490
|
type;
|
|
1551
1491
|
mass;
|
|
1492
|
+
/** The effective continuous-collision setting; dynamic defaults true, fixed and kinematic are always false. */
|
|
1493
|
+
continuousCollision;
|
|
1552
1494
|
buoyancy;
|
|
1553
1495
|
#simulation;
|
|
1554
1496
|
#physics;
|
|
@@ -1570,6 +1512,9 @@ var RigidBody3D = class {
|
|
|
1570
1512
|
this.#object = options.object;
|
|
1571
1513
|
this.type = type;
|
|
1572
1514
|
this.mass = options.mass ?? 0;
|
|
1515
|
+
if (options.continuousCollision !== void 0 && typeof options.continuousCollision !== "boolean")
|
|
1516
|
+
throw new Error("RigidBody3D continuousCollision must be a boolean.");
|
|
1517
|
+
this.continuousCollision = effectiveContinuousCollision(type, options.continuousCollision);
|
|
1573
1518
|
this.buoyancy = void 0;
|
|
1574
1519
|
const shape = options.shape.descriptor;
|
|
1575
1520
|
if (options.collisionLayer !== void 0 || options.collisionMask !== void 0) {
|
|
@@ -1584,7 +1529,8 @@ var RigidBody3D = class {
|
|
|
1584
1529
|
rotation: initial.rotation,
|
|
1585
1530
|
sensor: shape.sensor,
|
|
1586
1531
|
shape,
|
|
1587
|
-
type: this.type
|
|
1532
|
+
type: this.type,
|
|
1533
|
+
continuousCollision: this.continuousCollision
|
|
1588
1534
|
});
|
|
1589
1535
|
options.shape.bindRaw(registration.rawShape);
|
|
1590
1536
|
this.body = registration.body;
|
|
@@ -1702,6 +1648,170 @@ var RigidBody3D = class {
|
|
|
1702
1648
|
}
|
|
1703
1649
|
};
|
|
1704
1650
|
|
|
1651
|
+
// src/static-colliders.ts
|
|
1652
|
+
var DEFAULT_REACHABLE_CEILING = 4.5;
|
|
1653
|
+
var scratchInstance = new Matrix4();
|
|
1654
|
+
var scratchWorld = new Matrix4();
|
|
1655
|
+
var scratchPosition = new Vector3();
|
|
1656
|
+
var scratchScale = new Vector3();
|
|
1657
|
+
var scratchRotation = new Quaternion();
|
|
1658
|
+
var scratchBounds = new Box3();
|
|
1659
|
+
function optionsFor(filter) {
|
|
1660
|
+
return typeof filter === "function" ? { predicate: filter } : filter ?? {};
|
|
1661
|
+
}
|
|
1662
|
+
function validateCeiling(value) {
|
|
1663
|
+
if (!Number.isFinite(value))
|
|
1664
|
+
throw new Error(
|
|
1665
|
+
`buildStaticColliders reachableCeiling must be finite, received ${String(value)}.`
|
|
1666
|
+
);
|
|
1667
|
+
return value;
|
|
1668
|
+
}
|
|
1669
|
+
function selectPredicate(options) {
|
|
1670
|
+
const predicate = options.predicate ?? options.shouldCollide ?? (() => true);
|
|
1671
|
+
return (object) => {
|
|
1672
|
+
const selected = predicate(object);
|
|
1673
|
+
if (typeof selected !== "boolean")
|
|
1674
|
+
throw new Error(`buildStaticColliders predicate must return a boolean for '${object.name}'.`);
|
|
1675
|
+
return selected;
|
|
1676
|
+
};
|
|
1677
|
+
}
|
|
1678
|
+
function configureCarrier(carrier, transform, scale) {
|
|
1679
|
+
transform.decompose(scratchPosition, scratchRotation, scale);
|
|
1680
|
+
carrier.position.copy(scratchPosition);
|
|
1681
|
+
carrier.quaternion.copy(scratchRotation);
|
|
1682
|
+
carrier.scale.copy(scale);
|
|
1683
|
+
carrier.updateMatrixWorld(true);
|
|
1684
|
+
}
|
|
1685
|
+
function isReachable(carrier, ceiling) {
|
|
1686
|
+
scratchBounds.setFromObject(carrier, true);
|
|
1687
|
+
return !scratchBounds.isEmpty() && scratchBounds.min.y < ceiling;
|
|
1688
|
+
}
|
|
1689
|
+
function addCollider(context, source, transform, options) {
|
|
1690
|
+
const carrier = new Mesh(source.geometry);
|
|
1691
|
+
const shapeSource = new Mesh(source.geometry);
|
|
1692
|
+
configureCarrier(carrier, transform, scratchScale);
|
|
1693
|
+
shapeSource.scale.copy(scratchScale);
|
|
1694
|
+
if (!isReachable(carrier, options.reachableCeiling ?? DEFAULT_REACHABLE_CEILING))
|
|
1695
|
+
return void 0;
|
|
1696
|
+
return new RigidBody3D({
|
|
1697
|
+
collisionLayer: options.collisionLayer,
|
|
1698
|
+
collisionMask: options.collisionMask,
|
|
1699
|
+
object: carrier,
|
|
1700
|
+
physics: context.physics,
|
|
1701
|
+
shape: CollisionShape3D.fromMesh(shapeSource, "trimesh"),
|
|
1702
|
+
type: "fixed"
|
|
1703
|
+
});
|
|
1704
|
+
}
|
|
1705
|
+
function buildStaticColliders(context, root, filter) {
|
|
1706
|
+
if (context?.physics === void 0)
|
|
1707
|
+
throw new Error("buildStaticColliders requires a physics context.");
|
|
1708
|
+
if (!(root instanceof Object3D))
|
|
1709
|
+
throw new Error("buildStaticColliders requires an Object3D root.");
|
|
1710
|
+
const options = optionsFor(filter);
|
|
1711
|
+
const ceiling = validateCeiling(options.reachableCeiling ?? DEFAULT_REACHABLE_CEILING);
|
|
1712
|
+
const predicate = selectPredicate(options);
|
|
1713
|
+
const bodies = [];
|
|
1714
|
+
root.updateMatrixWorld(true);
|
|
1715
|
+
try {
|
|
1716
|
+
root.traverse((object) => {
|
|
1717
|
+
if (!predicate(object) || !(object instanceof Mesh)) return;
|
|
1718
|
+
if (object instanceof InstancedMesh) {
|
|
1719
|
+
for (let index = 0; index < object.count; index += 1) {
|
|
1720
|
+
object.getMatrixAt(index, scratchInstance);
|
|
1721
|
+
scratchWorld.multiplyMatrices(object.matrixWorld, scratchInstance);
|
|
1722
|
+
const body2 = addCollider(context, object, scratchWorld, {
|
|
1723
|
+
...options,
|
|
1724
|
+
reachableCeiling: ceiling
|
|
1725
|
+
});
|
|
1726
|
+
if (body2 !== void 0) bodies.push(body2);
|
|
1727
|
+
}
|
|
1728
|
+
return;
|
|
1729
|
+
}
|
|
1730
|
+
const body = addCollider(context, object, object.matrixWorld, {
|
|
1731
|
+
...options,
|
|
1732
|
+
reachableCeiling: ceiling
|
|
1733
|
+
});
|
|
1734
|
+
if (body !== void 0) bodies.push(body);
|
|
1735
|
+
});
|
|
1736
|
+
} catch (error) {
|
|
1737
|
+
for (const body of bodies) body.dispose();
|
|
1738
|
+
throw error;
|
|
1739
|
+
}
|
|
1740
|
+
console.info(`TN_STATIC_COLLIDERS:${JSON.stringify({ count: bodies.length })}`);
|
|
1741
|
+
if (bodies.length === 0)
|
|
1742
|
+
throw new Error("buildStaticColliders produced zero colliders; check the scene and predicate.");
|
|
1743
|
+
return bodies;
|
|
1744
|
+
}
|
|
1745
|
+
var ONE = new Vector3(1, 1, 1);
|
|
1746
|
+
var PhysicsSoftBodyCollision = class {
|
|
1747
|
+
capacity;
|
|
1748
|
+
#bodies;
|
|
1749
|
+
#bodyMatrix = new Matrix4();
|
|
1750
|
+
#localMatrix = new Matrix4();
|
|
1751
|
+
#corner = new Vector3();
|
|
1752
|
+
#minimum = new Vector3();
|
|
1753
|
+
#maximum = new Vector3();
|
|
1754
|
+
constructor(bodies) {
|
|
1755
|
+
if (bodies.length === 0)
|
|
1756
|
+
throw new Error("softBodyCollision requires at least one RigidBody3D.");
|
|
1757
|
+
for (const body of bodies) {
|
|
1758
|
+
if (body.shape.descriptor.kind !== "box")
|
|
1759
|
+
throw new Error("softBodyCollision currently supports box shapes only.");
|
|
1760
|
+
if (body.object === void 0)
|
|
1761
|
+
throw new Error("softBodyCollision requires an object transform for every body.");
|
|
1762
|
+
}
|
|
1763
|
+
this.#bodies = bodies;
|
|
1764
|
+
this.capacity = bodies.length;
|
|
1765
|
+
}
|
|
1766
|
+
writeBoxes(target, worldToLocal) {
|
|
1767
|
+
if (target.length !== this.capacity * 8)
|
|
1768
|
+
throw new Error(
|
|
1769
|
+
`softBodyCollision expected ${this.capacity * 8} packed values, received ${target.length}.`
|
|
1770
|
+
);
|
|
1771
|
+
for (let index = 0; index < this.#bodies.length; index += 1) {
|
|
1772
|
+
const body = this.#bodies[index];
|
|
1773
|
+
const object = body.object;
|
|
1774
|
+
if (object === void 0)
|
|
1775
|
+
throw new Error("softBodyCollision body lost its object transform.");
|
|
1776
|
+
this.#bodyMatrix.compose(object.position, object.quaternion, ONE);
|
|
1777
|
+
this.#localMatrix.multiplyMatrices(worldToLocal, this.#bodyMatrix);
|
|
1778
|
+
this.#minimum.set(
|
|
1779
|
+
Number.POSITIVE_INFINITY,
|
|
1780
|
+
Number.POSITIVE_INFINITY,
|
|
1781
|
+
Number.POSITIVE_INFINITY
|
|
1782
|
+
);
|
|
1783
|
+
this.#maximum.set(
|
|
1784
|
+
Number.NEGATIVE_INFINITY,
|
|
1785
|
+
Number.NEGATIVE_INFINITY,
|
|
1786
|
+
Number.NEGATIVE_INFINITY
|
|
1787
|
+
);
|
|
1788
|
+
const shape = body.shape.descriptor;
|
|
1789
|
+
for (let corner = 0; corner < 8; corner += 1) {
|
|
1790
|
+
this.#corner.set(
|
|
1791
|
+
(corner & 1) === 0 ? -shape.x : shape.x,
|
|
1792
|
+
(corner & 2) === 0 ? -shape.y : shape.y,
|
|
1793
|
+
(corner & 4) === 0 ? -shape.z : shape.z
|
|
1794
|
+
).applyMatrix4(this.#localMatrix);
|
|
1795
|
+
this.#minimum.min(this.#corner);
|
|
1796
|
+
this.#maximum.max(this.#corner);
|
|
1797
|
+
}
|
|
1798
|
+
const offset = index * 8;
|
|
1799
|
+
target[offset] = (this.#minimum.x + this.#maximum.x) / 2;
|
|
1800
|
+
target[offset + 1] = (this.#minimum.y + this.#maximum.y) / 2;
|
|
1801
|
+
target[offset + 2] = (this.#minimum.z + this.#maximum.z) / 2;
|
|
1802
|
+
target[offset + 3] = 0;
|
|
1803
|
+
target[offset + 4] = (this.#maximum.x - this.#minimum.x) / 2;
|
|
1804
|
+
target[offset + 5] = (this.#maximum.y - this.#minimum.y) / 2;
|
|
1805
|
+
target[offset + 6] = (this.#maximum.z - this.#minimum.z) / 2;
|
|
1806
|
+
target[offset + 7] = 0;
|
|
1807
|
+
}
|
|
1808
|
+
return this.#bodies.length;
|
|
1809
|
+
}
|
|
1810
|
+
};
|
|
1811
|
+
function softBodyCollision(...bodies) {
|
|
1812
|
+
return new PhysicsSoftBodyCollision(bodies);
|
|
1813
|
+
}
|
|
1814
|
+
|
|
1705
1815
|
// src/plugin.ts
|
|
1706
1816
|
var PHYSICS_DEBUG_BODY_LIMIT = 100;
|
|
1707
1817
|
var PHYSICS_DEBUG_SAMPLE_LIMIT = 100;
|
|
@@ -2053,4 +2163,4 @@ function physicsEntityId(ctx, body) {
|
|
|
2053
2163
|
return stableId;
|
|
2054
2164
|
}
|
|
2055
2165
|
|
|
2056
|
-
export { Area3D, Buoyancy3D, CharacterBody3D, CollisionShape3D, Joint3D, MAX_PHYSICS_QUERY_RESULTS, PHYSICS_COLLISION_EVENT_STRIDE, PHYSICS_TRANSFORM_STRIDE, PhysicsDirectSpaceState3D, RigidBody3D, interactionGroups, rapier, softBodyCollision };
|
|
2166
|
+
export { Area3D, Buoyancy3D, CharacterBody3D, CollisionShape3D, Joint3D, MAX_PHYSICS_QUERY_RESULTS, PHYSICS_COLLISION_EVENT_STRIDE, PHYSICS_TRANSFORM_STRIDE, PhysicsDirectSpaceState3D, RigidBody3D, buildStaticColliders, interactionGroups, rapier, softBodyCollision };
|
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import '@threenative/core';
|
|
2
2
|
import 'recast-navigation';
|
|
3
|
-
export { a2 as INavigationAgent3DOptions, a3 as INavigationContext, a4 as INavigationObstacle3DOptions, a5 as INavigationRegion3DOptions, a6 as NavigationAgent3D, a7 as NavigationAgentEvent, a8 as NavigationAgentHandler, a9 as NavigationObstacle3D, aa as NavigationPlugin, ab as NavigationRegion3D, ac as recast } from '../Area3D-
|
|
3
|
+
export { a2 as INavigationAgent3DOptions, a3 as INavigationContext, a4 as INavigationObstacle3DOptions, a5 as INavigationRegion3DOptions, a6 as NavigationAgent3D, a7 as NavigationAgentEvent, a8 as NavigationAgentHandler, a9 as NavigationObstacle3D, aa as NavigationPlugin, ab as NavigationRegion3D, ac as recast } from '../Area3D-BQDiAwQb.js';
|
|
4
4
|
import 'three';
|
package/package.json
CHANGED
|
@@ -1,8 +1,26 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@threenative/physics",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.2",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"description": "Thin Rapier physics bindings for ThreeNative games.",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"threenative",
|
|
8
|
+
"three",
|
|
9
|
+
"threejs",
|
|
10
|
+
"physics",
|
|
11
|
+
"rapier",
|
|
12
|
+
"navigation",
|
|
13
|
+
"gamedev"
|
|
14
|
+
],
|
|
15
|
+
"homepage": "https://github.com/ThreeNativeHQ/threenative#readme",
|
|
16
|
+
"bugs": {
|
|
17
|
+
"url": "https://github.com/ThreeNativeHQ/threenative/issues"
|
|
18
|
+
},
|
|
19
|
+
"repository": {
|
|
20
|
+
"type": "git",
|
|
21
|
+
"url": "git+https://github.com/ThreeNativeHQ/threenative.git",
|
|
22
|
+
"directory": "packages/physics"
|
|
23
|
+
},
|
|
6
24
|
"type": "module",
|
|
7
25
|
"files": [
|
|
8
26
|
"dist",
|
|
@@ -37,7 +55,7 @@
|
|
|
37
55
|
"recast-navigation": "0.43.1"
|
|
38
56
|
},
|
|
39
57
|
"peerDependencies": {
|
|
40
|
-
"@threenative/core": ">=0.3.
|
|
58
|
+
"@threenative/core": ">=0.3.2 <0.4.0",
|
|
41
59
|
"three": ">=0.185.0 <0.186.0"
|
|
42
60
|
},
|
|
43
61
|
"devDependencies": {
|
|
@@ -46,7 +64,7 @@
|
|
|
46
64
|
"three": "0.185.1",
|
|
47
65
|
"tsup": "8.5.1",
|
|
48
66
|
"typescript": "5.9.3",
|
|
49
|
-
"@threenative/core": "0.3.
|
|
67
|
+
"@threenative/core": "0.3.2"
|
|
50
68
|
},
|
|
51
69
|
"scripts": {
|
|
52
70
|
"build": "tsup --config tsup.config.ts && publint",
|