@woosh/meep-engine 2.39.27 → 2.39.28

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.
@@ -14,6 +14,7 @@ import { aabb3_intersects_ray } from "./aabb3_intersects_ray.js";
14
14
  import { aabb3_compute_surface_area } from "./aabb3_compute_surface_area.js";
15
15
  import { aabb3_intersects_frustum_array } from "./aabb3_intersects_frustum_array.js";
16
16
  import { aabb3_matrix4_project } from "../../geom/3d/aabb/aabb3_matrix4_project.js";
17
+ import { aabb3_signed_distance_sqr_to_point } from "./aabb3_signed_distance_sqr_to_point.js";
17
18
 
18
19
 
19
20
  export class AABB3 {
@@ -209,35 +210,15 @@ export class AABB3 {
209
210
  * @returns {number} Squared distance to point, value is negative if the point is inside the box
210
211
  */
211
212
  distanceToPoint2(x, y, z) {
212
- const _x0 = this.x0;
213
- const _y0 = this.y0;
214
- const _z0 = this.z0;
215
- const _x1 = this.x1;
216
- const _y1 = this.y1;
217
- const _z1 = this.z1;
218
-
219
- //do projection
220
- const xp0 = _x0 - x;
221
- const xp1 = x - _x1;
222
- const yp0 = _y0 - y;
223
- const yp1 = y - _y1;
224
- const zp0 = _z0 - z;
225
- const zp1 = z - _z1;
226
-
227
- //calculate separation in each axis
228
- const dx = Math.max(xp0, xp1);
229
- const dy = Math.max(yp0, yp1);
230
- const dz = Math.max(zp0, zp1);
231
-
232
- //straight-line distance
233
- const distance2 = dx * dx + dy * dy + dz * dz;
234
-
235
- if (dx < 0 && dy < 0 && dz < 0) {
236
- //penetration
237
- return -distance2;
238
- } else {
239
- return distance2;
240
- }
213
+ return aabb3_signed_distance_sqr_to_point(
214
+ this.x0,
215
+ this.y0,
216
+ this.z0,
217
+ this.x1,
218
+ this.y1,
219
+ this.z1,
220
+ x, y, z
221
+ );
241
222
  }
242
223
 
243
224
  /**
@@ -0,0 +1,39 @@
1
+ import { max2 } from "../../math/max2.js";
2
+
3
+ /**
4
+ * Compute squared distance to point, value is negative if the point is inside the box
5
+ * @param {number} x0
6
+ * @param {number} y0
7
+ * @param {number} z0
8
+ * @param {number} x1
9
+ * @param {number} y1
10
+ * @param {number} z1
11
+ * @param {number} point_x
12
+ * @param {number} point_y
13
+ * @param {number} point_z
14
+ * @returns {number}
15
+ */
16
+ export function aabb3_signed_distance_sqr_to_point(x0, y0, z0, x1, y1, z1, point_x, point_y, point_z) {
17
+ //do projection
18
+ const xp0 = x0 - point_x;
19
+ const xp1 = point_x - x1;
20
+ const yp0 = y0 - point_y;
21
+ const yp1 = point_y - y1;
22
+ const zp0 = z0 - point_z;
23
+ const zp1 = point_z - z1;
24
+
25
+ //calculate separation in each axis
26
+ const dx = max2(xp0, xp1);
27
+ const dy = max2(yp0, yp1);
28
+ const dz = max2(zp0, zp1);
29
+
30
+ //straight-line distance
31
+ const distance_sqr = dx * dx + dy * dy + dz * dz;
32
+
33
+ if (dx < 0 && dy < 0 && dz < 0) {
34
+ //penetration, negative distance
35
+ return -distance_sqr;
36
+ } else {
37
+ return distance_sqr;
38
+ }
39
+ }
@@ -49,6 +49,12 @@ const ELEMENT_WORD_COUNT = 10;
49
49
  */
50
50
  const INITIAL_CAPACITY = 128;
51
51
 
52
+ /**
53
+ *
54
+ * @type {number[]}
55
+ */
56
+ const scratch_stack = [];
57
+
52
58
  /**
53
59
  * Bounding Volume Hierarchy implementation. Stores unsigned integer values at leaves, these are typically IDs or Index values.
54
60
  * Highly optimized both in terms of memory usage and CPU. Most of the code inlined. No allocation are performed during usage (except for growing the tree capacity).
@@ -874,6 +880,40 @@ export class ExplicitBinaryBoundingVolumeHierarchy {
874
880
  return iA;
875
881
  }
876
882
 
883
+ /**
884
+ * Release all nodes, this essentially resets the tree to empty state
885
+ */
886
+ release_all() {
887
+ let cursor = 0;
888
+ const stack = scratch_stack;
889
+
890
+ const root = this.__root;
891
+
892
+ if (root !== NULL_NODE) {
893
+ stack[cursor++] = root;
894
+ }
895
+
896
+ const uint32 = this.__data_uint32;
897
+
898
+ while (cursor > 0) {
899
+ cursor--;
900
+
901
+ const node = stack[cursor];
902
+
903
+ const node_address = node * ELEMENT_WORD_COUNT;
904
+
905
+ const child1 = uint32[node_address + COLUMN_CHILD_1];
906
+ const child2 = uint32[node_address + COLUMN_CHILD_2];
907
+
908
+ if (child1 !== NULL_NODE) {
909
+ stack[cursor++] = child2;
910
+ stack[cursor++] = child1;
911
+ }
912
+
913
+ this.release_node(node);
914
+ }
915
+ }
916
+
877
917
  /**
878
918
  *
879
919
  * @param {(node:number, tree:ExplicitBinaryBoundingVolumeHierarchy)=>void} callback
@@ -46,7 +46,7 @@ export function bvh_query_user_data_generic(
46
46
  */
47
47
  const node = traversal_stack[traversal_cursor];
48
48
 
49
- // test node against the ray
49
+ // test node against the query
50
50
  const pass = query.evaluate(node, bvh);
51
51
 
52
52
  if (!pass) {
@@ -0,0 +1,96 @@
1
+ import { NULL_NODE } from "./ExplicitBinaryBoundingVolumeHierarchy.js";
2
+ import { aabb3_signed_distance_sqr_to_point } from "../aabb3/aabb3_signed_distance_sqr_to_point.js";
3
+ import { max2 } from "../../math/max2.js";
4
+
5
+ /**
6
+ *
7
+ * @type {number[]}
8
+ */
9
+ const traversal_stack = [];
10
+
11
+ /**
12
+ *
13
+ * @type {number[]}
14
+ */
15
+ const scratch_aabb = [];
16
+
17
+ /**
18
+ *
19
+ * @type {number}
20
+ */
21
+ let traversal_cursor = 0;
22
+
23
+ /**
24
+ *
25
+ * @param {ExplicitBinaryBoundingVolumeHierarchy} bvh
26
+ * @param {number} x
27
+ * @param {number} y
28
+ * @param {number} z
29
+ * @param {number} [max_distance=Infinity] how far to search
30
+ * @returns {number} user data of the leaf, or -1 if not found
31
+ */
32
+ export function bvh_query_user_data_nearest_to_point(bvh, x, y, z, max_distance = Infinity) {
33
+ const stack_frame_address = traversal_cursor;
34
+
35
+ const root = bvh.root;
36
+
37
+ if (root === NULL_NODE) {
38
+ return -1;
39
+ }
40
+
41
+ traversal_stack[traversal_cursor++] = root;
42
+
43
+ let best_leaf_data = -1;
44
+ let best_distance = max_distance;
45
+
46
+ while (traversal_cursor > stack_frame_address) {
47
+ traversal_cursor--;
48
+
49
+ /**
50
+ *
51
+ * @type {number}
52
+ */
53
+ const node = traversal_stack[traversal_cursor];
54
+
55
+ bvh.node_get_aabb(node, scratch_aabb);
56
+
57
+ // compute distance to box
58
+ const distance = max2(0, aabb3_signed_distance_sqr_to_point(
59
+ scratch_aabb[0], scratch_aabb[1], scratch_aabb[2],
60
+ scratch_aabb[3], scratch_aabb[4], scratch_aabb[5],
61
+ x, y, z
62
+ ));
63
+
64
+ if (distance > best_distance) {
65
+ // node is too far
66
+ continue;
67
+ }
68
+
69
+ const node_is_leaf = bvh.node_is_leaf(node);
70
+
71
+ if (!node_is_leaf) {
72
+
73
+ // read in-order
74
+ const child1 = bvh.node_get_child1(node);
75
+ const child2 = bvh.node_get_child2(node);
76
+
77
+ // write to stack in reverse order, so that fist child ends up being visited first
78
+ traversal_stack[traversal_cursor++] = child1;
79
+ traversal_stack[traversal_cursor++] = child2;
80
+
81
+ } else {
82
+
83
+ // leaf node
84
+ if (distance < best_distance) {
85
+ best_distance = distance;
86
+ best_leaf_data = bvh.node_get_user_data(node);
87
+ }
88
+
89
+ }
90
+ }
91
+
92
+ // drop stack frame
93
+ traversal_cursor = stack_frame_address;
94
+
95
+ return best_leaf_data;
96
+ }
@@ -13,7 +13,6 @@ import { TerrainLayer } from "../../../engine/ecs/terrain/ecs/layers/TerrainLaye
13
13
  import Terrain from "../../../engine/ecs/terrain/ecs/Terrain.js";
14
14
  import Vector3 from "../../../core/geom/Vector3.js";
15
15
  import { BlenderCameraOrientationGizmo } from "../v2/BlenderCameraOrientationGizmo.js";
16
- import Quaternion from "../../../core/geom/Quaternion.js";
17
16
  import { Transform } from "../../../engine/ecs/transform/Transform.js";
18
17
  import TopDownCameraController from "../../../engine/graphics/ecs/camera/topdown/TopDownCameraController.js";
19
18
 
package/package.json CHANGED
@@ -5,7 +5,7 @@
5
5
  "productName": "Meep",
6
6
  "description": "production-ready JavaScript game engine based on Entity Component System Architecture",
7
7
  "author": "Alexander Goldring",
8
- "version": "2.39.27",
8
+ "version": "2.39.28",
9
9
  "dependencies": {
10
10
  "gl-matrix": "3.4.3",
11
11
  "fast-levenshtein": "2.0.6",