@woosh/meep-engine 2.148.0 → 2.149.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.
@@ -1,201 +1,201 @@
1
- import {
2
- COLUMN_CHILD_1,
3
- COLUMN_CHILD_2,
4
- COLUMN_USER_DATA,
5
- ELEMENT_WORD_COUNT,
6
- NULL_NODE,
7
- } from "../../../core/bvh2/bvh3/BVH.js";
8
- import { SCRATCH_UINT32_TRAVERSAL_STACK } from "../../../core/collection/SCRATCH_UINT32_TRAVERSAL_STACK.js";
9
- import { returnTrue } from "../../../core/function/returnTrue.js";
10
- import {
11
- aabb3_near_distance_to_intersection_ray_segment
12
- } from "../../../core/geom/3d/aabb/aabb3_near_distance_to_intersection_ray_segment.js";
13
- import { body_id_index } from "../body/BodyStorage.js";
14
- import { RAY_REFINE_UNSUPPORTED, refine_ray_hit } from "../narrowphase/refine_ray_hit.js";
15
-
16
- const stack = SCRATCH_UINT32_TRAVERSAL_STACK;
17
-
18
- /**
19
- * Reusable nearest-hit accumulator + scratch. Module-scoped so {@link raycast}
20
- * doesn't allocate per call; the physics step is single-threaded so contention
21
- * isn't a concern.
22
- * @type {{best_t:number, best_body:number}}
23
- */
24
- const acc = { best_t: Infinity, best_body: 0 };
25
- const best_normal = new Float64Array(3); // winning hit's world normal
26
- const cand_normal = new Float64Array(3); // per-leaf candidate normal
27
-
28
- /**
29
- * Outward AABB-face normal at a hit point — the fallback normal for a leaf
30
- * whose shape has no exact ray test (composite convex). The hit point is
31
- * projected into the AABB's normalised local space (centre = 0, faces = ±1);
32
- * the dominant component's axis is the entry face and its sign the outward
33
- * normal.
34
- */
35
- function aabb_face_normal(out, min_x, min_y, min_z, max_x, max_y, max_z, hx, hy, hz) {
36
- const cx = (min_x + max_x) * 0.5;
37
- const cy = (min_y + max_y) * 0.5;
38
- const cz = (min_z + max_z) * 0.5;
39
- const px = (hx - cx) * 2 / (max_x - min_x);
40
- const py = (hy - cy) * 2 / (max_y - min_y);
41
- const pz = (hz - cz) * 2 / (max_z - min_z);
42
- const apx = px < 0 ? -px : px;
43
- const apy = py < 0 ? -py : py;
44
- const apz = pz < 0 ? -pz : pz;
45
- if (apx >= apy && apx >= apz) { out[0] = px >= 0 ? 1 : -1; out[1] = 0; out[2] = 0; }
46
- else if (apy >= apz) { out[0] = 0; out[1] = py >= 0 ? 1 : -1; out[2] = 0; }
47
- else { out[0] = 0; out[1] = 0; out[2] = pz >= 0 ? 1 : -1; }
48
- }
49
-
50
- /**
51
- * Walk a single BVH along a ray, refining each crossing leaf against its true
52
- * shape geometry and keeping the nearest confirmed hit.
53
- *
54
- * Pruning stays on the *inflated leaf AABB* entry distance `t_near`: a shape
55
- * hit is always at or beyond its tight AABB entry, which is at or beyond the
56
- * inflated-AABB entry, so a subtree whose AABB entry is past the current best
57
- * cannot contain a closer hit. A leaf whose ray crosses the fat AABB but misses
58
- * the true shape contributes nothing — the correctness gain over broadphase.
59
- *
60
- * @param {BVH} bvh @param {number} root
61
- * @param {PhysicsSystem} system
62
- * @param {number} ox @param {number} oy @param {number} oz ray origin
63
- * @param {number} dx @param {number} dy @param {number} dz ray dir (unit)
64
- * @param {number} inv_dx @param {number} inv_dy @param {number} inv_dz
65
- * @param {number} max_distance
66
- * @param {(entity:number, collider:Collider)=>boolean} filter
67
- */
68
- function bvh_raycast_nearest(
69
- bvh, root, system,
70
- ox, oy, oz, dx, dy, dz,
71
- inv_dx, inv_dy, inv_dz,
72
- max_distance, filter
73
- ) {
74
- if (root === NULL_NODE) return;
75
-
76
- const float32 = bvh.__data_float32;
77
- const uint32 = bvh.__data_uint32;
78
-
79
- let pointer = stack.pointer;
80
- const stack_top = pointer;
81
- stack[pointer++] = root;
82
-
83
- while (pointer > stack_top) {
84
- pointer--;
85
- const node = stack[pointer];
86
- const address = node * ELEMENT_WORD_COUNT;
87
-
88
- const t_near = aabb3_near_distance_to_intersection_ray_segment(
89
- float32[address], float32[address + 1], float32[address + 2],
90
- float32[address + 3], float32[address + 4], float32[address + 5],
91
- ox, oy, oz,
92
- inv_dx, inv_dy, inv_dz,
93
- 0, max_distance
94
- );
95
-
96
- // No intersection or this subtree can't beat the best known hit — prune.
97
- if (t_near >= acc.best_t) continue;
98
-
99
- const child_1 = uint32[address + COLUMN_CHILD_1];
100
- if (child_1 !== NULL_NODE) {
101
- stack[pointer++] = uint32[address + COLUMN_CHILD_2];
102
- stack[pointer++] = child_1;
103
- continue;
104
- }
105
-
106
- // Leaf — refine against the true shape.
107
- const body_id = uint32[address + COLUMN_USER_DATA];
108
- const entity = system.entityOf(body_id);
109
- if (entity < 0) continue; // unlinked concurrently
110
- const idx = body_id_index(body_id);
111
- const collider = system.__primary_collider(idx);
112
- if (collider === null) continue;
113
- if (!filter(entity, collider)) continue;
114
-
115
- const tr = system.__transforms[idx];
116
- const refined = refine_ray_hit(
117
- collider.shape, tr.position, tr.rotation,
118
- ox, oy, oz, dx, dy, dz, acc.best_t, cand_normal
119
- );
120
-
121
- if (refined === RAY_REFINE_UNSUPPORTED) {
122
- // No exact ray test for this shape — keep the broadphase AABB hit.
123
- if (t_near < acc.best_t) {
124
- acc.best_t = t_near;
125
- acc.best_body = body_id;
126
- aabb_face_normal(best_normal,
127
- float32[address], float32[address + 1], float32[address + 2],
128
- float32[address + 3], float32[address + 4], float32[address + 5],
129
- ox + dx * t_near, oy + dy * t_near, oz + dz * t_near);
130
- }
131
- } else if (refined < acc.best_t) { // a refined miss is Infinity → never wins
132
- acc.best_t = refined;
133
- acc.best_body = body_id;
134
- best_normal[0] = cand_normal[0];
135
- best_normal[1] = cand_normal[1];
136
- best_normal[2] = cand_normal[2];
137
- }
138
- }
139
-
140
- stack.pointer = stack_top;
141
- }
142
-
143
- /**
144
- * Raycast against both broadphase trees (static + dynamic) of a
145
- * {@link PhysicsSystem}, refined against each candidate's true shape geometry.
146
- * Fills `result` with the nearest hit and returns `true` on hit, `false` on
147
- * miss. `result.t` is the exact surface distance and `result.normal` the true
148
- * surface normal for sphere / box / capsule / mesh / heightmap colliders;
149
- * composite convex shapes (no exact ray test yet) fall back to the broadphase
150
- * AABB hit + AABB-face normal.
151
- *
152
- * Multi-collider bodies resolve their primary (first-attached) collider — the
153
- * BVH leaf encodes only `body_id`; per-collider rays need the leaf user-data to
154
- * carry the collider index (future work).
155
- *
156
- * @param {PhysicsSystem} system
157
- * @param {Ray3} ray origin + unit direction + `tMax`
158
- * @param {PhysicsSurfacePoint} result populated on hit; untouched on miss
159
- * @param {(entity:number, collider:Collider)=>boolean} [filter] called once per
160
- * crossing leaf; defaults to {@link returnTrue}.
161
- * @returns {boolean} true on hit, false on miss
162
- */
163
- export function raycast(system, ray, result, filter = returnTrue) {
164
- const ox = ray.origin_x, oy = ray.origin_y, oz = ray.origin_z;
165
- const dx = ray.direction_x, dy = ray.direction_y, dz = ray.direction_z;
166
- const max_distance = ray.tMax;
167
-
168
- acc.best_t = max_distance;
169
- acc.best_body = 0;
170
- best_normal[0] = 0; best_normal[1] = 0; best_normal[2] = 0;
171
-
172
- const inv_dx = 1 / dx, inv_dy = 1 / dy, inv_dz = 1 / dz;
173
-
174
- bvh_raycast_nearest(
175
- system.staticBvh, system.staticBvh.root, system,
176
- ox, oy, oz, dx, dy, dz, inv_dx, inv_dy, inv_dz, max_distance, filter
177
- );
178
- bvh_raycast_nearest(
179
- system.dynamicBvh, system.dynamicBvh.root, system,
180
- ox, oy, oz, dx, dy, dz, inv_dx, inv_dy, inv_dz, max_distance, filter
181
- );
182
-
183
- // Any hit updates best_t to strictly below max_distance (both the refined
184
- // and AABB-fallback paths require t < current best); a miss leaves it at
185
- // max_distance. This is body_id-agnostic — the first body packs to id 0.
186
- if (acc.best_t >= max_distance) return false;
187
-
188
- const entity = system.entityOf(acc.best_body);
189
- if (entity < 0) return false; // body unlinked concurrently → treat as miss
190
-
191
- const t = acc.best_t;
192
- const rp = result.position;
193
- const rn = result.normal;
194
- rp[0] = ox + dx * t; rp[1] = oy + dy * t; rp[2] = oz + dz * t;
195
- rn[0] = best_normal[0]; rn[1] = best_normal[1]; rn[2] = best_normal[2];
196
- result.t = t;
197
- result.entity = entity;
198
- result.body_id = acc.best_body;
199
-
200
- return true;
201
- }
1
+ import {
2
+ COLUMN_CHILD_1,
3
+ COLUMN_CHILD_2,
4
+ COLUMN_USER_DATA,
5
+ ELEMENT_WORD_COUNT,
6
+ NULL_NODE,
7
+ } from "../../../core/bvh2/bvh3/BVH.js";
8
+ import { SCRATCH_UINT32_TRAVERSAL_STACK } from "../../../core/collection/SCRATCH_UINT32_TRAVERSAL_STACK.js";
9
+ import { returnTrue } from "../../../core/function/returnTrue.js";
10
+ import {
11
+ aabb3_near_distance_to_intersection_ray_segment
12
+ } from "../../../core/geom/3d/aabb/aabb3_near_distance_to_intersection_ray_segment.js";
13
+ import { body_id_index } from "../body/BodyStorage.js";
14
+ import { RAY_REFINE_UNSUPPORTED, refine_ray_hit } from "../narrowphase/refine_ray_hit.js";
15
+
16
+ const stack = SCRATCH_UINT32_TRAVERSAL_STACK;
17
+
18
+ /**
19
+ * Reusable nearest-hit accumulator + scratch. Module-scoped so {@link raycast}
20
+ * doesn't allocate per call; the physics step is single-threaded so contention
21
+ * isn't a concern.
22
+ * @type {{best_t:number, best_body:number}}
23
+ */
24
+ const acc = { best_t: Infinity, best_body: 0 };
25
+ const best_normal = new Float64Array(3); // winning hit's world normal
26
+ const cand_normal = new Float64Array(3); // per-leaf candidate normal
27
+
28
+ /**
29
+ * Outward AABB-face normal at a hit point — the fallback normal for a leaf
30
+ * whose shape has no exact ray test (composite convex). The hit point is
31
+ * projected into the AABB's normalised local space (centre = 0, faces = ±1);
32
+ * the dominant component's axis is the entry face and its sign the outward
33
+ * normal.
34
+ */
35
+ function aabb_face_normal(out, min_x, min_y, min_z, max_x, max_y, max_z, hx, hy, hz) {
36
+ const cx = (min_x + max_x) * 0.5;
37
+ const cy = (min_y + max_y) * 0.5;
38
+ const cz = (min_z + max_z) * 0.5;
39
+ const px = (hx - cx) * 2 / (max_x - min_x);
40
+ const py = (hy - cy) * 2 / (max_y - min_y);
41
+ const pz = (hz - cz) * 2 / (max_z - min_z);
42
+ const apx = px < 0 ? -px : px;
43
+ const apy = py < 0 ? -py : py;
44
+ const apz = pz < 0 ? -pz : pz;
45
+ if (apx >= apy && apx >= apz) { out[0] = px >= 0 ? 1 : -1; out[1] = 0; out[2] = 0; }
46
+ else if (apy >= apz) { out[0] = 0; out[1] = py >= 0 ? 1 : -1; out[2] = 0; }
47
+ else { out[0] = 0; out[1] = 0; out[2] = pz >= 0 ? 1 : -1; }
48
+ }
49
+
50
+ /**
51
+ * Walk a single BVH along a ray, refining each crossing leaf against its true
52
+ * shape geometry and keeping the nearest confirmed hit.
53
+ *
54
+ * Pruning stays on the *inflated leaf AABB* entry distance `t_near`: a shape
55
+ * hit is always at or beyond its tight AABB entry, which is at or beyond the
56
+ * inflated-AABB entry, so a subtree whose AABB entry is past the current best
57
+ * cannot contain a closer hit. A leaf whose ray crosses the fat AABB but misses
58
+ * the true shape contributes nothing — the correctness gain over broadphase.
59
+ *
60
+ * @param {BVH} bvh @param {number} root
61
+ * @param {PhysicsSystem} system
62
+ * @param {number} ox @param {number} oy @param {number} oz ray origin
63
+ * @param {number} dx @param {number} dy @param {number} dz ray dir (unit)
64
+ * @param {number} inv_dx @param {number} inv_dy @param {number} inv_dz
65
+ * @param {number} max_distance
66
+ * @param {(entity:number, collider:Collider)=>boolean} filter
67
+ */
68
+ function bvh_raycast_nearest(
69
+ bvh, root, system,
70
+ ox, oy, oz, dx, dy, dz,
71
+ inv_dx, inv_dy, inv_dz,
72
+ max_distance, filter
73
+ ) {
74
+ if (root === NULL_NODE) return;
75
+
76
+ const float32 = bvh.__data_float32;
77
+ const uint32 = bvh.__data_uint32;
78
+
79
+ let pointer = stack.pointer;
80
+ const stack_top = pointer;
81
+ stack[pointer++] = root;
82
+
83
+ while (pointer > stack_top) {
84
+ pointer--;
85
+ const node = stack[pointer];
86
+ const address = node * ELEMENT_WORD_COUNT;
87
+
88
+ const t_near = aabb3_near_distance_to_intersection_ray_segment(
89
+ float32[address], float32[address + 1], float32[address + 2],
90
+ float32[address + 3], float32[address + 4], float32[address + 5],
91
+ ox, oy, oz,
92
+ inv_dx, inv_dy, inv_dz,
93
+ 0, max_distance
94
+ );
95
+
96
+ // No intersection or this subtree can't beat the best known hit — prune.
97
+ if (t_near >= acc.best_t) continue;
98
+
99
+ const child_1 = uint32[address + COLUMN_CHILD_1];
100
+ if (child_1 !== NULL_NODE) {
101
+ stack[pointer++] = uint32[address + COLUMN_CHILD_2];
102
+ stack[pointer++] = child_1;
103
+ continue;
104
+ }
105
+
106
+ // Leaf — refine against the true shape.
107
+ const body_id = uint32[address + COLUMN_USER_DATA];
108
+ const entity = system.entityOf(body_id);
109
+ if (entity < 0) continue; // unlinked concurrently
110
+ const idx = body_id_index(body_id);
111
+ const collider = system.__primary_collider(idx);
112
+ if (collider === null) continue;
113
+ if (!filter(entity, collider)) continue;
114
+
115
+ const tr = system.__transforms[idx];
116
+ const refined = refine_ray_hit(
117
+ collider.shape, tr.position, tr.rotation,
118
+ ox, oy, oz, dx, dy, dz, acc.best_t, cand_normal
119
+ );
120
+
121
+ if (refined === RAY_REFINE_UNSUPPORTED) {
122
+ // No exact ray test for this shape — keep the broadphase AABB hit.
123
+ if (t_near < acc.best_t) {
124
+ acc.best_t = t_near;
125
+ acc.best_body = body_id;
126
+ aabb_face_normal(best_normal,
127
+ float32[address], float32[address + 1], float32[address + 2],
128
+ float32[address + 3], float32[address + 4], float32[address + 5],
129
+ ox + dx * t_near, oy + dy * t_near, oz + dz * t_near);
130
+ }
131
+ } else if (refined < acc.best_t) { // a refined miss is Infinity → never wins
132
+ acc.best_t = refined;
133
+ acc.best_body = body_id;
134
+ best_normal[0] = cand_normal[0];
135
+ best_normal[1] = cand_normal[1];
136
+ best_normal[2] = cand_normal[2];
137
+ }
138
+ }
139
+
140
+ stack.pointer = stack_top;
141
+ }
142
+
143
+ /**
144
+ * Raycast against both broadphase trees (static + dynamic) of a
145
+ * {@link PhysicsSystem}, refined against each candidate's true shape geometry.
146
+ * Fills `result` with the nearest hit and returns `true` on hit, `false` on
147
+ * miss. `result.t` is the exact surface distance and `result.normal` the true
148
+ * surface normal for sphere / box / capsule / mesh / heightmap colliders;
149
+ * composite convex shapes (no exact ray test yet) fall back to the broadphase
150
+ * AABB hit + AABB-face normal.
151
+ *
152
+ * Multi-collider bodies resolve their primary (first-attached) collider — the
153
+ * BVH leaf encodes only `body_id`; per-collider rays need the leaf user-data to
154
+ * carry the collider index (future work).
155
+ *
156
+ * @param {PhysicsSystem} system
157
+ * @param {Ray3} ray origin + unit direction + `tMax`
158
+ * @param {PhysicsSurfacePoint} result populated on hit; untouched on miss
159
+ * @param {(entity:number, collider:Collider)=>boolean} [filter] called once per
160
+ * crossing leaf; defaults to {@link returnTrue}.
161
+ * @returns {boolean} true on hit, false on miss
162
+ */
163
+ export function raycast(system, ray, result, filter = returnTrue) {
164
+ const ox = ray.origin_x, oy = ray.origin_y, oz = ray.origin_z;
165
+ const dx = ray.direction_x, dy = ray.direction_y, dz = ray.direction_z;
166
+ const max_distance = ray.tMax;
167
+
168
+ acc.best_t = max_distance;
169
+ acc.best_body = 0;
170
+ best_normal[0] = 0; best_normal[1] = 0; best_normal[2] = 0;
171
+
172
+ const inv_dx = 1 / dx, inv_dy = 1 / dy, inv_dz = 1 / dz;
173
+
174
+ bvh_raycast_nearest(
175
+ system.staticBvh, system.staticBvh.root, system,
176
+ ox, oy, oz, dx, dy, dz, inv_dx, inv_dy, inv_dz, max_distance, filter
177
+ );
178
+ bvh_raycast_nearest(
179
+ system.dynamicBvh, system.dynamicBvh.root, system,
180
+ ox, oy, oz, dx, dy, dz, inv_dx, inv_dy, inv_dz, max_distance, filter
181
+ );
182
+
183
+ // Any hit updates best_t to strictly below max_distance (both the refined
184
+ // and AABB-fallback paths require t < current best); a miss leaves it at
185
+ // max_distance. This is body_id-agnostic — the first body packs to id 0.
186
+ if (acc.best_t >= max_distance) return false;
187
+
188
+ const entity = system.entityOf(acc.best_body);
189
+ if (entity < 0) return false; // body unlinked concurrently → treat as miss
190
+
191
+ const t = acc.best_t;
192
+ const rp = result.position;
193
+ const rn = result.normal;
194
+ rp[0] = ox + dx * t; rp[1] = oy + dy * t; rp[2] = oz + dz * t;
195
+ rn[0] = best_normal[0]; rn[1] = best_normal[1]; rn[2] = best_normal[2];
196
+ result.t = t;
197
+ result.entity = entity;
198
+ result.body_id = acc.best_body;
199
+
200
+ return true;
201
+ }