@woosh/meep-engine 2.131.34 → 2.131.35

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/package.json CHANGED
@@ -5,7 +5,7 @@
5
5
  "description": "Pure JavaScript game engine. Fully featured and production ready.",
6
6
  "type": "module",
7
7
  "author": "Alexander Goldring",
8
- "version": "2.131.34",
8
+ "version": "2.131.35",
9
9
  "main": "build/meep.module.js",
10
10
  "module": "build/meep.module.js",
11
11
  "exports": {
@@ -1 +1 @@
1
- {"version":3,"file":"ebvh_optimize_treelet.d.ts","sourceRoot":"","sources":["../../../../../src/core/bvh2/bvh3/ebvh_optimize_treelet.js"],"names":[],"mappings":"AAuNA;;;;;;;GAOG;AACH,uDAHW,MAAM,iBACN,MAAM,QA+IhB"}
1
+ {"version":3,"file":"ebvh_optimize_treelet.d.ts","sourceRoot":"","sources":["../../../../../src/core/bvh2/bvh3/ebvh_optimize_treelet.js"],"names":[],"mappings":"AAgRA;;;;;;;GAOG;AACH,uDAHW,MAAM,iBACN,MAAM,QA+IhB"}
@@ -1,15 +1,16 @@
1
1
  import { assert } from "../../assert.js";
2
2
  import { lsb_32 } from "../../binary/lsb_32.js";
3
3
  import { bitCount } from "../../binary/operations/bitCount.js";
4
- import { NULL_NODE } from "./BVH.js";
4
+ import { ELEMENT_WORD_COUNT, NULL_NODE } from "./BVH.js";
5
5
 
6
6
  // Common continuous memory region for better cache performance
7
- const scratch_memory = new ArrayBuffer((256 * 3 + 16) * 4);
7
+ const scratch_memory = new ArrayBuffer((256 * 3 + 16) * 4 + 256 * 6 * 4);
8
8
 
9
- const scratch_areas = new Float32Array(scratch_memory, 0, 256);
10
- const scratch_cost = new Float32Array(scratch_memory, 1024, 256);
11
- const scratch_partitions = new Uint32Array(scratch_memory, 2048, 256);
12
- const scratch_treelet = new Uint32Array(scratch_memory, 3072, 16);
9
+ const scratch_bounds = new Float32Array(scratch_memory, 0, 256 * 6);
10
+ const scratch_areas = new Float32Array(scratch_memory, 6144, 256);
11
+ const scratch_cost = new Float32Array(scratch_memory, 7168, 256);
12
+ const scratch_partitions = new Uint32Array(scratch_memory, 8192, 256);
13
+ const scratch_treelet = new Uint32Array(scratch_memory, 9216, 16);
13
14
 
14
15
  /**
15
16
  *
@@ -74,28 +75,84 @@ function optimize_treelet(bvh, root, leaves, leaf_count) {
74
75
 
75
76
  // see "Fast Parallel Construction of High-Quality Bounding Volume Hierarchies", Algorithm 2
76
77
 
78
+ const bvh_float32 = bvh.data_float32;
79
+
77
80
  // Calculate surface area for each subset
78
- for (let i = 0; i < leaf_count; i++) {
79
- const leaf_0 = leaves[i];
80
- for (let j = i + 1; j < leaf_count; j++) {
81
- const leaf_1 = leaves[j];
81
+ const mask_limit = (1 << leaf_count) - 1;
82
+
83
+ for (let s = 1; s <= mask_limit; s++) {
84
+ let min_x, min_y, min_z, max_x, max_y, max_z;
85
+
86
+ // Check if 's' is a power of 2 (i.e., a single leaf)
87
+ // (s & (s-1)) === 0 is the standard trick for this
88
+ const is_a_leaf = (s & (s - 1)) === 0;
89
+
90
+ if (is_a_leaf) {
91
+ // --- CASE A: LEAF ---
92
+ // Map the mask bit back to a linear index (0..7)
93
+ const leaf_index = lsb_32(s);
94
+
95
+ // Read from your compact linear array
96
+ const node = leaves[leaf_index];
97
+
98
+ const source_offset = node * ELEMENT_WORD_COUNT;
99
+
100
+ min_x = bvh_float32[source_offset];
101
+ min_y = bvh_float32[source_offset + 1];
102
+ min_z = bvh_float32[source_offset + 2];
103
+ max_x = bvh_float32[source_offset + 3];
104
+ max_y = bvh_float32[source_offset + 4];
105
+ max_z = bvh_float32[source_offset + 5];
106
+
107
+ // Initialize base cost for leaf (SAH_COST_TRIANGLE * Area)
108
+ // (Calculated below)
109
+ } else {
110
+ // --- CASE B: SUBSET ---
111
+ // Split s into: LSB (Leaf) + Rest (Subset)
112
+ // Both of these are < s, so they are guaranteed to be initialized.
113
+ const mask_lsb = s & -s; // Extract lowest set bit
114
+ const mask_rest = s ^ mask_lsb; // The rest of the bits
115
+
116
+ const i_lsb = mask_lsb * 6;
117
+ const i_rest = mask_rest * 6;
118
+
119
+ // Merge bounds
120
+ min_x = Math.min(scratch_bounds[i_lsb], scratch_bounds[i_rest]);
121
+ min_y = Math.min(scratch_bounds[i_lsb + 1], scratch_bounds[i_rest + 1]);
122
+ min_z = Math.min(scratch_bounds[i_lsb + 2], scratch_bounds[i_rest + 2]);
123
+ max_x = Math.max(scratch_bounds[i_lsb + 3], scratch_bounds[i_rest + 3]);
124
+ max_y = Math.max(scratch_bounds[i_lsb + 4], scratch_bounds[i_rest + 4]);
125
+ max_z = Math.max(scratch_bounds[i_lsb + 5], scratch_bounds[i_rest + 5]);
126
+ }
82
127
 
83
- const s = (1 << i) | (1 << j);
128
+ // --- STORE ---
129
+ const dest_offset = s * 6;
84
130
 
85
- scratch_areas[s] = bvh.node_get_combined_surface_area(leaf_0, leaf_1)
86
- }
87
- }
131
+ scratch_bounds[dest_offset] = min_x;
132
+ scratch_bounds[dest_offset + 1] = min_y;
133
+ scratch_bounds[dest_offset + 2] = min_z;
134
+ scratch_bounds[dest_offset + 3] = max_x;
135
+ scratch_bounds[dest_offset + 4] = max_y;
136
+ scratch_bounds[dest_offset + 5] = max_z;
88
137
 
89
- // Initialize costs of individual leaves
90
- for (let i = 0; i < leaf_count; i++) {
91
- const leaf = leaves[i];
138
+ // Calculate Surface Area
139
+ const dx = max_x - min_x;
140
+ const dy = max_y - min_y;
141
+ const dz = max_z - min_z;
92
142
 
93
- scratch_cost[1 << i] = SAH_COST_TRIANGLE * bvh.node_get_surface_area(leaf);
143
+ const area = 2.0 * (dx * dy + dy * dz + dz * dx);
144
+
145
+ scratch_areas[s] = area;
146
+
147
+ if(is_a_leaf){
148
+ // Initialize costs of individual leaves
149
+ scratch_cost[s] = SAH_COST_TRIANGLE * area;
150
+ }
94
151
  }
95
152
 
96
153
  // Optimize every subset of leaves
97
154
  for (let k = 2; k <= leaf_count; k++) {
98
- for (let s = 1; s <= (1 << leaf_count) - 1; s++) {
155
+ for (let s = 1; s <= mask_limit; s++) {
99
156
  if (bitCount(s) !== k) {
100
157
  continue;
101
158
  }
@@ -322,7 +379,7 @@ export function ebvh_optimize_treelet(
322
379
 
323
380
  } else if (came_from_type === CAME_FROM_TYPE_PARENT) {
324
381
  const child1 = bvh.node_get_child1(node);
325
- const child2 = bvh.node_get_child1(node);
382
+ const child2 = bvh.node_get_child2(node);
326
383
 
327
384
  if (child1 !== NULL_NODE) {
328
385
  came_from_type = CAME_FROM_TYPE_PARENT;