@yali-engine/physics 0.0.0-stage → 0.4.0
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/character.d.ts +129 -0
- package/dist/character.js +96 -0
- package/dist/character.js.map +1 -0
- package/dist/cloth-asset.d.ts +296 -0
- package/dist/cloth-asset.js +706 -0
- package/dist/cloth-asset.js.map +1 -0
- package/dist/cloth.d.ts +261 -0
- package/dist/cloth.js +1578 -0
- package/dist/cloth.js.map +1 -0
- package/dist/collision-asset.d.ts +51 -0
- package/dist/collision-asset.js +157 -0
- package/dist/collision-asset.js.map +1 -0
- package/dist/components.d.ts +152 -0
- package/dist/components.js +193 -0
- package/dist/components.js.map +1 -0
- package/dist/gpu-shaders.gen.d.ts +5 -0
- package/dist/gpu-shaders.gen.js +8 -0
- package/dist/gpu-shaders.gen.js.map +1 -0
- package/dist/gpu.d.ts +2 -0
- package/dist/gpu.js +5 -0
- package/dist/gpu.js.map +1 -0
- package/dist/index.d.ts +19 -0
- package/dist/index.js +20 -0
- package/dist/index.js.map +1 -0
- package/dist/physics.d.ts +94 -0
- package/dist/physics.js +132 -0
- package/dist/physics.js.map +1 -0
- package/dist/plugin.d.ts +22 -0
- package/dist/plugin.js +102 -0
- package/dist/plugin.js.map +1 -0
- package/package.json +29 -3
- package/README.md +0 -3
package/dist/physics.js
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
import { requireFeature, YaliError } from '@yali-engine/core';
|
|
2
|
+
const RAY_IN = 8;
|
|
3
|
+
const RAY_OUT = 5;
|
|
4
|
+
/**
|
|
5
|
+
* The physics world of an app (the `physics` resource): ray casts, collision assets, settings
|
|
6
|
+
* and stats. Bodies and colliders are entities (`physics.RigidBody`, `physics.Collider`); this is
|
|
7
|
+
* everything else.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* const physics = app.resource<Physics>('physics');
|
|
11
|
+
* const hit = physics.raycast([0, 10, 0], [0, -1, 0], 100);
|
|
12
|
+
* if (hit) console.log(hit.entity, hit.distance);
|
|
13
|
+
*/
|
|
14
|
+
export class Physics {
|
|
15
|
+
world;
|
|
16
|
+
#x;
|
|
17
|
+
#rays = new Float64Array(0);
|
|
18
|
+
constructor(world, fixedDelta, settings = {}) {
|
|
19
|
+
requireFeature(world.core, 'physics');
|
|
20
|
+
this.world = world;
|
|
21
|
+
this.#x = world.core.exports;
|
|
22
|
+
this.#fixedDelta = fixedDelta;
|
|
23
|
+
this.configure(settings);
|
|
24
|
+
}
|
|
25
|
+
#fixedDelta;
|
|
26
|
+
/** What debug draw shows; both off by default. The plugin hands the lines to `render.debug`. */
|
|
27
|
+
debug = { colliders: false, contacts: false, maxSegments: 200_000 };
|
|
28
|
+
#origin = [0, 0, 0];
|
|
29
|
+
#lines = { f32: new Float32Array(0), u32: new Uint32Array(0), first: 0, count: 0, origin: this.#origin };
|
|
30
|
+
/**
|
|
31
|
+
* This step's debug segments (what `debug` asks for), or null when it asks for nothing. The
|
|
32
|
+
* views point into the core's memory: read them before the next step. Allocation-free.
|
|
33
|
+
*/
|
|
34
|
+
debugLines() {
|
|
35
|
+
const flags = (this.debug.colliders ? 1 : 0) | (this.debug.contacts ? 2 : 0);
|
|
36
|
+
if (flags === 0)
|
|
37
|
+
return null;
|
|
38
|
+
const ptr = this.world.scratch(40);
|
|
39
|
+
this.world.check(this.#x.yali_physics_debug(this.world.handle, flags, this.debug.maxSegments, ptr));
|
|
40
|
+
const heap = this.world.core.heap;
|
|
41
|
+
const f = heap.f64;
|
|
42
|
+
const at = ptr >>> 3;
|
|
43
|
+
const l = this.#lines;
|
|
44
|
+
l.f32 = heap.f32;
|
|
45
|
+
l.u32 = heap.u32;
|
|
46
|
+
l.first = f[at] / 16;
|
|
47
|
+
l.count = f[at + 1];
|
|
48
|
+
this.#origin[0] = f[at + 2];
|
|
49
|
+
this.#origin[1] = f[at + 3];
|
|
50
|
+
this.#origin[2] = f[at + 4];
|
|
51
|
+
return l;
|
|
52
|
+
}
|
|
53
|
+
/** Gravity and substeps (the fixed step is the app's). */
|
|
54
|
+
configure(s) {
|
|
55
|
+
const g = s.gravity ?? [0, -9.81, 0];
|
|
56
|
+
this.world.check(this.#x.yali_physics_settings(this.world.handle, this.#fixedDelta, g[0], g[1], g[2], s.substeps ?? 4));
|
|
57
|
+
}
|
|
58
|
+
/** The first collider the ray hits within `maxDistance` (layers in `mask`), or null. */
|
|
59
|
+
raycast(origin, dir, maxDistance = 1e6, mask = 0xffffffff) {
|
|
60
|
+
const out = this.raycasts([[origin, dir, maxDistance, mask]]);
|
|
61
|
+
return out[0];
|
|
62
|
+
}
|
|
63
|
+
/** Many rays in one call into the core (each `[origin, dir, maxDistance, mask]`). */
|
|
64
|
+
raycasts(rays) {
|
|
65
|
+
const n = rays.length;
|
|
66
|
+
const bytes = n * (RAY_IN + RAY_OUT) * 8;
|
|
67
|
+
const ptr = this.world.scratch(bytes);
|
|
68
|
+
const heap = this.world.core.heap;
|
|
69
|
+
const input = ptr >>> 3;
|
|
70
|
+
const output = input + n * RAY_IN;
|
|
71
|
+
for (let i = 0; i < n; i++) {
|
|
72
|
+
const [o, d, max, mask] = rays[i];
|
|
73
|
+
heap.f64.set([o[0], o[1], o[2], d[0], d[1], d[2], max, mask >>> 0], input + i * RAY_IN);
|
|
74
|
+
}
|
|
75
|
+
this.world.check(this.#x.yali_physics_ray_casts(this.world.handle, ptr, n, ptr + n * RAY_IN * 8));
|
|
76
|
+
const f = this.world.core.heap.f64;
|
|
77
|
+
const hits = [];
|
|
78
|
+
for (let i = 0; i < n; i++) {
|
|
79
|
+
const at = output + i * RAY_OUT;
|
|
80
|
+
hits.push(f[at] < 0 ? null : { entity: f[at], distance: f[at + 1], normal: [f[at + 2], f[at + 3], f[at + 4]] });
|
|
81
|
+
}
|
|
82
|
+
return hits;
|
|
83
|
+
}
|
|
84
|
+
/** Adds a convex hull of `points` (x, y, z triples, local frame); returns its asset index. */
|
|
85
|
+
addHull(points) {
|
|
86
|
+
if (points.length < 12 || points.length % 3 !== 0) {
|
|
87
|
+
throw new YaliError('PHYSICS_INVALID', `A hull needs at least 4 points as x, y, z triples; got ${points.length} numbers.`, 'Pass a Float32Array of 3n numbers, n ≥ 4.');
|
|
88
|
+
}
|
|
89
|
+
const ptr = this.world.scratch(points.byteLength);
|
|
90
|
+
this.world.core.heap.f32.set(points, ptr >>> 2);
|
|
91
|
+
return this.world.check(this.#x.yali_physics_add_hull(this.world.handle, ptr, points.length / 3));
|
|
92
|
+
}
|
|
93
|
+
/** Adds a static triangle mesh; returns its asset index. */
|
|
94
|
+
addMesh(vertices, indices, doubleSided = false) {
|
|
95
|
+
const ptr = this.world.scratch(vertices.byteLength + indices.byteLength);
|
|
96
|
+
const heap = this.world.core.heap;
|
|
97
|
+
heap.f32.set(vertices, ptr >>> 2);
|
|
98
|
+
heap.u32.set(indices, (ptr + vertices.byteLength) >>> 2);
|
|
99
|
+
return this.world.check(this.#x.yali_physics_add_mesh(this.world.handle, ptr, vertices.length / 3, ptr + vertices.byteLength, indices.length, doubleSided ? 1 : 0));
|
|
100
|
+
}
|
|
101
|
+
/** Adds a heightfield (`rows × cols` heights, row-major); returns its asset index. */
|
|
102
|
+
addHeightfield(rows, cols, heights, cell, heightScale = 1) {
|
|
103
|
+
const ptr = this.world.scratch(heights.byteLength);
|
|
104
|
+
this.world.core.heap.f32.set(heights, ptr >>> 2);
|
|
105
|
+
return this.world.check(this.#x.yali_physics_add_heightfield(this.world.handle, rows, cols, ptr, cell[0], heightScale, cell[1]));
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Saves the physics state (ADR-0091: bodies, contacts with their warm-start impulses, joints,
|
|
109
|
+
* sleep); returns its id. Restoring it continues bit for bit. The ECS world's own state is the
|
|
110
|
+
* caller's to save (rollback, replay, undo).
|
|
111
|
+
*/
|
|
112
|
+
snapshot() {
|
|
113
|
+
return this.world.check(this.#x.yali_physics_snapshot(this.world.handle));
|
|
114
|
+
}
|
|
115
|
+
/** Back to snapshot `id`, between frames; every body's Transform and velocity is written back. */
|
|
116
|
+
restore(id) {
|
|
117
|
+
this.world.check(this.#x.yali_physics_restore(this.world.handle, id));
|
|
118
|
+
}
|
|
119
|
+
/** Frees snapshot `id`. */
|
|
120
|
+
dropSnapshot(id) {
|
|
121
|
+
this.world.check(this.#x.yali_physics_snapshot_free(this.world.handle, id));
|
|
122
|
+
}
|
|
123
|
+
/** The last step's counts. */
|
|
124
|
+
stats() {
|
|
125
|
+
const ptr = this.world.scratch(24);
|
|
126
|
+
this.world.check(this.#x.yali_physics_stats(this.world.handle, ptr));
|
|
127
|
+
const u = this.world.core.heap.u32;
|
|
128
|
+
const i = ptr >>> 2;
|
|
129
|
+
return { bodies: u[i], awake: u[i + 1], pairs: u[i + 2], contacts: u[i + 3], colors: u[i + 4], threads: u[i + 5] };
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
//# sourceMappingURL=physics.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"physics.js","sourceRoot":"","sources":["../src/physics.ts"],"names":[],"mappings":"AAAA,OAAO,EAAe,cAAc,EAAc,SAAS,EAAE,MAAM,mBAAmB,CAAC;AAmEvF,MAAM,MAAM,GAAG,CAAC,CAAC;AACjB,MAAM,OAAO,GAAG,CAAC,CAAC;AAElB;;;;;;;;;GASG;AACH,MAAM,OAAO,OAAO;IACT,KAAK,CAAQ;IACb,EAAE,CAAiB;IAC5B,KAAK,GAAG,IAAI,YAAY,CAAC,CAAC,CAAC,CAAC;IAE5B,YAAY,KAAY,EAAE,UAAkB,EAAE,WAA4B,EAAE;QAC1E,cAAc,CAAC,KAAK,CAAC,IAAI,EAAE,SAAS,CAAC,CAAC;QACtC,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;QACnB,IAAI,CAAC,EAAE,GAAG,KAAK,CAAC,IAAI,CAAC,OAAoC,CAAC;QAC1D,IAAI,CAAC,WAAW,GAAG,UAAU,CAAC;QAC9B,IAAI,CAAC,S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EAAE,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,EAAE,QAAQ,EAAE,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,EAAE,OAAO,EAAE,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,EAAE,CAAC;IACrH,CAAC;CACF"}
|
package/dist/plugin.d.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { YaliPlugin } from '@yali-engine/core';
|
|
2
|
+
import { type PhysicsDebug, type PhysicsSettings } from './physics.ts';
|
|
3
|
+
/** `physicsPlugin`'s options: the physics settings, and debug draw to start with. */
|
|
4
|
+
export interface PhysicsPluginOptions extends PhysicsSettings {
|
|
5
|
+
/** Debug draw from the start (`true`: colliders); change it any time on `Physics.debug`. */
|
|
6
|
+
readonly debug?: boolean | Partial<PhysicsDebug>;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* `@yali-engine/physics` as a plugin (ADR-0082): Kanduka, compiled into the core module's `physics`
|
|
10
|
+
* variant, steps every fixed step after the transform snapshot. Provides the `physics` resource
|
|
11
|
+
* (`Physics`: ray casts, collision assets, stats) and the `physicsComponents` resource
|
|
12
|
+
* (`RigidBody`, `Collider`). Fails at startup, with the fix, when the loaded core has no physics.
|
|
13
|
+
* Registers the tools `physics.stats`, `physics.raycast` and `physics.debug`, and four counters.
|
|
14
|
+
* With debug draw on (`debug`, or `Physics.debug` at run time) and `@yali-engine/render` present, every
|
|
15
|
+
* collider's wireframe (and, if asked, each contact) is drawn over the scene (ADR-0087).
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* const app = App.create({ core, plugins: [transformsPlugin(), physicsPlugin({ gravity: [0, -9.81, 0] }), game] });
|
|
19
|
+
* // in game's build:
|
|
20
|
+
* const { RigidBody, Collider } = b.app.resource<PhysicsComponents>('physicsComponents');
|
|
21
|
+
*/
|
|
22
|
+
export declare function physicsPlugin(settings?: PhysicsPluginOptions): YaliPlugin;
|
package/dist/plugin.js
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { registerPhysics } from "./components.js";
|
|
2
|
+
import { Physics } from "./physics.js";
|
|
3
|
+
/**
|
|
4
|
+
* `@yali-engine/physics` as a plugin (ADR-0082): Kanduka, compiled into the core module's `physics`
|
|
5
|
+
* variant, steps every fixed step after the transform snapshot. Provides the `physics` resource
|
|
6
|
+
* (`Physics`: ray casts, collision assets, stats) and the `physicsComponents` resource
|
|
7
|
+
* (`RigidBody`, `Collider`). Fails at startup, with the fix, when the loaded core has no physics.
|
|
8
|
+
* Registers the tools `physics.stats`, `physics.raycast` and `physics.debug`, and four counters.
|
|
9
|
+
* With debug draw on (`debug`, or `Physics.debug` at run time) and `@yali-engine/render` present, every
|
|
10
|
+
* collider's wireframe (and, if asked, each contact) is drawn over the scene (ADR-0087).
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* const app = App.create({ core, plugins: [transformsPlugin(), physicsPlugin({ gravity: [0, -9.81, 0] }), game] });
|
|
14
|
+
* // in game's build:
|
|
15
|
+
* const { RigidBody, Collider } = b.app.resource<PhysicsComponents>('physicsComponents');
|
|
16
|
+
*/
|
|
17
|
+
export function physicsPlugin(settings = {}) {
|
|
18
|
+
return {
|
|
19
|
+
name: '@yali-engine/physics',
|
|
20
|
+
version: '0.4.0',
|
|
21
|
+
dependencies: [{ name: '@yali-engine/transforms', range: '^0.4.0', optional: true }],
|
|
22
|
+
build(b) {
|
|
23
|
+
const world = b.app.world;
|
|
24
|
+
const physics = new Physics(world, b.app.time.fixedDelta, settings);
|
|
25
|
+
const components = registerPhysics(world);
|
|
26
|
+
b.provide('physics', physics);
|
|
27
|
+
b.provide('physicsComponents', components);
|
|
28
|
+
if (settings.debug)
|
|
29
|
+
Object.assign(physics.debug, settings.debug === true ? { colliders: true } : settings.debug);
|
|
30
|
+
// Extract: after the camera moved (PostUpdate), so `render.debug` places lines for this frame.
|
|
31
|
+
let sink;
|
|
32
|
+
b.system({
|
|
33
|
+
name: 'physics.debugDraw',
|
|
34
|
+
stage: 'Extract',
|
|
35
|
+
run: () => {
|
|
36
|
+
if (!physics.debug.colliders && !physics.debug.contacts)
|
|
37
|
+
return;
|
|
38
|
+
if (sink === undefined) {
|
|
39
|
+
try {
|
|
40
|
+
sink = b.app.resource('render').debug ?? null;
|
|
41
|
+
}
|
|
42
|
+
catch {
|
|
43
|
+
sink = null; // no renderer (headless): nothing to draw into
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
const lines = sink && physics.debugLines();
|
|
47
|
+
if (lines)
|
|
48
|
+
sink.add(lines.f32, lines.u32, lines.first, lines.count, lines.origin);
|
|
49
|
+
},
|
|
50
|
+
});
|
|
51
|
+
let last = physics.stats();
|
|
52
|
+
b.system({ name: 'physics.stats', stage: 'EndFrame', run: () => (last = physics.stats()) });
|
|
53
|
+
b.counter('physics.bodies', () => last.bodies);
|
|
54
|
+
b.counter('physics.awake', () => last.awake);
|
|
55
|
+
b.counter('physics.pairs', () => last.pairs);
|
|
56
|
+
b.counter('physics.contacts', () => last.contacts);
|
|
57
|
+
b.tool({
|
|
58
|
+
name: 'physics.stats',
|
|
59
|
+
description: 'Physics counts from the last step: bodies, awake bodies, pairs, contact constraints.',
|
|
60
|
+
input: { type: 'object', properties: {} },
|
|
61
|
+
run: () => physics.stats(),
|
|
62
|
+
});
|
|
63
|
+
b.tool({
|
|
64
|
+
name: 'physics.debug',
|
|
65
|
+
description: 'Turns physics debug draw on or off (collider wireframes, contact points); returns what is drawn.',
|
|
66
|
+
input: {
|
|
67
|
+
type: 'object',
|
|
68
|
+
properties: {
|
|
69
|
+
colliders: { type: 'boolean', description: 'Collider wireframes.' },
|
|
70
|
+
contacts: { type: 'boolean', description: 'Contact points and normals.' },
|
|
71
|
+
},
|
|
72
|
+
},
|
|
73
|
+
run: (args) => {
|
|
74
|
+
if (typeof args.colliders === 'boolean')
|
|
75
|
+
physics.debug.colliders = args.colliders;
|
|
76
|
+
if (typeof args.contacts === 'boolean')
|
|
77
|
+
physics.debug.contacts = args.contacts;
|
|
78
|
+
return { ...physics.debug };
|
|
79
|
+
},
|
|
80
|
+
});
|
|
81
|
+
b.tool({
|
|
82
|
+
name: 'physics.raycast',
|
|
83
|
+
description: 'Casts a ray into the physics world; returns the entity hit, the distance and the normal, or null.',
|
|
84
|
+
input: {
|
|
85
|
+
type: 'object',
|
|
86
|
+
properties: {
|
|
87
|
+
origin: { type: 'array', description: 'World position [x, y, z].' },
|
|
88
|
+
direction: { type: 'array', description: 'Direction [x, y, z] (normalized for you).' },
|
|
89
|
+
maxDistance: { type: 'number', description: 'Default 1e6.' },
|
|
90
|
+
},
|
|
91
|
+
required: ['origin', 'direction'],
|
|
92
|
+
},
|
|
93
|
+
run: (args) => {
|
|
94
|
+
const o = args.origin;
|
|
95
|
+
const d = args.direction;
|
|
96
|
+
return physics.raycast([o[0], o[1], o[2]], [d[0], d[1], d[2]], args.maxDistance ?? 1e6);
|
|
97
|
+
},
|
|
98
|
+
});
|
|
99
|
+
},
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
//# sourceMappingURL=plugin.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"plugin.js","sourceRoot":"","sources":["../src/plugin.ts"],"names":[],"mappings":"AAEA,OAAO,EAA0B,eAAe,EAAE,MAAM,iBAAiB,CAAC;AAC1E,OAAO,EAAE,OAAO,EAA2C,MAAM,cAAc,CAAC;AAahF;;;;;;;;;;;;;GAaG;AACH,MAAM,UAAU,aAAa,CAAC,WAAiC,EAAE;IAC/D,OAAO;QACL,IAAI,EAAE,sBAAsB;QAC5B,OAAO,EAAE,OAAO;QAChB,YAAY,EAAE,CAAC,EAAE,IAAI,EAAE,yBAAyB,EAAE,KAAK,EAAE,QAAQ,EAAE,QAAQ,EAAE,IAAI,EAAE,CAAC;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,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,EAAG,IAAI,CAAC,WAAkC,IAAI,GAAG,CAAC,CAAC;gBAClH,CAAC;aACF,CAAC,CAAC;QACL,CAAC;KACF,CAAC;AACJ,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,32 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@yali-engine/physics",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.4.0",
|
|
4
|
+
"description": "Yali physics: Kanduka (Yali's own Soft Step solver) as ECS components, ray casts and collision assets. Needs the core's physics variant (ADR-0082, ADR-0084).",
|
|
5
|
+
"license": "MIT OR Apache-2.0",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"sideEffects": false,
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"default": "./dist/index.js"
|
|
12
|
+
},
|
|
13
|
+
"./gpu": {
|
|
14
|
+
"types": "./dist/gpu.d.ts",
|
|
15
|
+
"default": "./dist/gpu.js"
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
"publishConfig": {
|
|
19
|
+
"access": "public"
|
|
20
|
+
},
|
|
21
|
+
"files": [
|
|
22
|
+
"dist"
|
|
23
|
+
],
|
|
24
|
+
"dependencies": {
|
|
25
|
+
"@webgpu/types": "^0.1.60",
|
|
26
|
+
"@yali-engine/core": "0.4.0"
|
|
27
|
+
},
|
|
28
|
+
"scripts": {
|
|
29
|
+
"typecheck": "tsc -p tsconfig.json",
|
|
30
|
+
"test": "node --experimental-strip-types --no-warnings --test \"test/*.test.ts\""
|
|
31
|
+
}
|
|
6
32
|
}
|
package/README.md
DELETED