@woosh/meep-engine 2.67.0 → 2.69.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.
Files changed (87) hide show
  1. package/build/bundle-worker-terrain.js +1 -1
  2. package/build/meep.cjs +698 -1270
  3. package/build/meep.min.js +1 -1
  4. package/build/meep.module.js +698 -1270
  5. package/package.json +1 -1
  6. package/src/core/bvh2/binary/2/BinaryUint32BVH.js +55 -28
  7. package/src/core/bvh2/binary/2/BinaryUint32BVH.spec.js +3 -3
  8. package/src/core/bvh2/binary/2/bvh32_query_user_data_overlaps_clipping_volume.js +94 -0
  9. package/src/core/bvh2/binary/2/bvh32_query_user_data_ray.js +17 -18
  10. package/src/core/bvh2/bvh3/query/BVHQueryIntersectsFrustum.js +4 -2
  11. package/src/core/geom/3d/aabb/AABB3.js +14 -14
  12. package/src/core/geom/3d/aabb/aabb3_array_intersects_clipping_volume_array.js +30 -0
  13. package/src/core/geom/3d/aabb/aabb3_intersects_clipping_volume_array.js +51 -0
  14. package/src/core/geom/3d/frustum/{read_frustum_planes_to_array.js → read_three_planes_to_array.js} +5 -3
  15. package/src/core/geom/3d/triangle/triangle_intersects_clipping_volume.js +51 -0
  16. package/src/engine/ecs/terrain/ecs/TerrainSystem.js +2 -2
  17. package/src/engine/ecs/terrain/ecs/makeTerrainWorkerProxy.js +1 -1
  18. package/src/engine/ecs/terrain/tiles/TerrainTile.js +9 -46
  19. package/src/engine/graphics/ecs/mesh/Mesh.d.ts +0 -1
  20. package/src/engine/graphics/ecs/mesh-v2/ShadedGeometry.d.ts +0 -5
  21. package/src/engine/graphics/ecs/mesh-v2/ShadedGeometry.js +0 -1
  22. package/src/engine/graphics/geometry/buffered/query/GeometrySpatialQueryAccelerator.d.ts +2 -2
  23. package/src/engine/graphics/geometry/buffered/query/GeometrySpatialQueryAccelerator.js +79 -36
  24. package/src/engine/graphics/geometry/buffered/query/bvh32_geometry_overlap_clipping_volume.js +88 -0
  25. package/src/engine/graphics/geometry/buffered/query/bvh32_geometry_raycast.js +108 -0
  26. package/src/engine/graphics/geometry/bvh/buffered/bvh32_from_indexed_geometry.js +4 -30
  27. package/src/engine/graphics/geometry/bvh/buffered/bvh32_from_unindexed_geometry.js +30 -0
  28. package/src/engine/graphics/geometry/bvh/buffered/bvh32_set_leaf_from_triangle.js +41 -0
  29. package/src/engine/graphics/material/optimization/prototypeMaterialOptimizer.js +16 -19
  30. package/src/engine/graphics/render/forward_plus/LightManager.js +2 -2
  31. package/src/engine/graphics/render/forward_plus/prototype/prototypeLightManager.js +46 -47
  32. package/src/engine/graphics/render/forward_plus/query/query_bvh_frustum_from_texture.js +2 -2
  33. package/src/engine/graphics/render/view/CameraView.js +8 -8
  34. package/src/core/bvh2/BVHTasks.js +0 -65
  35. package/src/core/bvh2/BinaryNode.d.ts +0 -13
  36. package/src/core/bvh2/BinaryNode.js +0 -1188
  37. package/src/core/bvh2/BinaryNode.spec.js +0 -309
  38. package/src/core/bvh2/LeafNode.d.ts +0 -7
  39. package/src/core/bvh2/LeafNode.js +0 -147
  40. package/src/core/bvh2/Node.d.ts +0 -9
  41. package/src/core/bvh2/Node.js +0 -196
  42. package/src/core/bvh2/NodeValidator.js +0 -197
  43. package/src/core/bvh2/StacklessTraverser.js +0 -154
  44. package/src/core/bvh2/StacklessTraverser.spec.js +0 -109
  45. package/src/core/bvh2/binary/BinaryBVH.js +0 -281
  46. package/src/core/bvh2/binary/IndexedBinaryBVH.js +0 -407
  47. package/src/core/bvh2/binary/IndexedBinaryBVH.spec.js +0 -27
  48. package/src/core/bvh2/binary/IndexedBinaryBVHVisitor.js +0 -11
  49. package/src/core/bvh2/binary/NodeType.js +0 -8
  50. package/src/core/bvh2/binary/RayLeafIntersectionVisitor.js +0 -59
  51. package/src/core/bvh2/serialization/deserializeBinaryNode.js +0 -40
  52. package/src/core/bvh2/serialization/deserializeBinaryNodeFromBinaryBuffer.js +0 -90
  53. package/src/core/bvh2/serialization/serializeBinaryNode.js +0 -31
  54. package/src/core/bvh2/serialization/serializeBinaryNodeToBinaryBuffer.js +0 -86
  55. package/src/core/bvh2/transform/BottomUpOptimizingRebuilder.js +0 -144
  56. package/src/core/bvh2/transform/RotationOptimizer.js +0 -123
  57. package/src/core/bvh2/transform/RotationOptimizer.spec.js +0 -303
  58. package/src/core/bvh2/transform/tryRotateSingleNode.js +0 -260
  59. package/src/core/bvh2/traversal/BVHVisitor.js +0 -30
  60. package/src/core/bvh2/traversal/RaycastBVHVisitor.js +0 -66
  61. package/src/core/bvh2/traversal/ThreeClippingPlaneComputingBVHVisitor.js +0 -384
  62. package/src/core/bvh2/traversal/ThreeFrustumsIntersectionBVHVisitor.js +0 -52
  63. package/src/core/bvh2/traversal/bvh_traverse_pre_order_using_stack.js +0 -43
  64. package/src/core/bvh2/traversal/queryBinaryNode_ClippingPlanes.d.ts +0 -5
  65. package/src/core/bvh2/traversal/queryBinaryNode_ClippingPlanes.js +0 -66
  66. package/src/core/bvh2/traversal/queryBinaryNode_CollectData.js +0 -49
  67. package/src/core/bvh2/traversal/queryBinaryNode_CollectLeaves.js +0 -51
  68. package/src/core/bvh2/traversal/queryBinaryNode_FrustumIntersections.js +0 -77
  69. package/src/core/bvh2/traversal/queryBinaryNode_SphereIntersections.js +0 -63
  70. package/src/core/bvh2/traversal/traverseBinaryNodeUsingVisitor.js +0 -50
  71. package/src/core/bvh2/traversal/traverseBinaryNodeUsingVisitor_DepthFirst_PreOrder.js +0 -34
  72. package/src/core/bvh2/util/find_least_common_ancestor.js +0 -34
  73. package/src/core/bvh2/visual/convert_bvh_to_dot_format_string.js +0 -50
  74. package/src/core/geom/2d/bvh/BinaryNode2.js +0 -152
  75. package/src/core/geom/2d/bvh/LeafNode2.js +0 -11
  76. package/src/core/geom/2d/bvh/Node2.js +0 -51
  77. package/src/core/geom/3d/aabb/aabb3_array_intersects_frustum_array.js +0 -20
  78. package/src/core/geom/3d/aabb/aabb3_intersects_frustum_array.js +0 -35
  79. package/src/engine/ecs/terrain/tiles/FirstRayIntersectionTerrainBVHVisitor.js +0 -74
  80. package/src/engine/graphics/geometry/buffered/query/ClippingPlaneContainmentComputingVisitor.js +0 -195
  81. package/src/engine/graphics/geometry/buffered/query/GeometryVisitor.js +0 -87
  82. package/src/engine/graphics/geometry/buffered/query/RaycastNearestHitComputingVisitor.js +0 -206
  83. package/src/engine/graphics/geometry/bvh/BVHFromGeometry.js +0 -72
  84. package/src/engine/graphics/geometry/bvh/buffered/BVHGeometryRaycaster.js +0 -240
  85. package/src/engine/graphics/geometry/bvh/buffered/BinaryBVHFromBufferGeometry.js +0 -123
  86. package/src/engine/graphics/geometry/bvh/buffered/IndexedTraingleBoundsComputer.js +0 -43
  87. package/src/engine/graphics/render/forward_plus/query/query_bvh_frustum_from_objects.js +0 -133
@@ -1,6 +1,5 @@
1
1
  import {Object3D} from "three";
2
2
  import {AABB3} from "../../../../core/geom/3d/aabb/AABB3";
3
- import {LeafNode} from "../../../../core/bvh2/LeafNode";
4
3
 
5
4
  export default class Mesh {
6
5
  url: string
@@ -7,11 +7,6 @@ export class ShadedGeometry {
7
7
  material: Material
8
8
  readonly mode: DrawMode
9
9
 
10
- /**
11
- * @deprecated
12
- */
13
- readonly bvh: Readonly<AABB3>
14
-
15
10
  getBoundingBox(destination: AABB3): void
16
11
 
17
12
  from(geometry: BufferGeometry, material: Material, draw_mode?: DrawMode): void
@@ -215,7 +215,6 @@ export class ShadedGeometry {
215
215
 
216
216
  /**
217
217
  * @deprecated
218
- * @returns {LeafNode<ShadedGeometry>}
219
218
  */
220
219
  get bvh() {
221
220
  throw new Error('Deprecated, use #getBoundingBox method instead');
@@ -1,11 +1,11 @@
1
1
  import {BufferGeometry, Plane} from "three";
2
- import Vector3 from "../../../../../core/geom/Vector3";
3
2
  import {SurfacePoint3} from "../../../../../core/geom/3d/SurfacePoint3";
3
+ import Vector3 from "../../../../../core/geom/Vector3";
4
4
 
5
5
  export class GeometrySpatialQueryAccelerator {
6
6
  constructor(cache_size?: number)
7
7
 
8
- queryContainmentViaClippingPlanes(geometry: BufferGeometry, planes: Plane[]): number
8
+ queryContainmentViaClippingPlanes(geometry: BufferGeometry, planes: Plane[]): boolean
9
9
 
10
10
  queryRaycastNearest(destination: SurfacePoint3, geometry: BufferGeometry, ray_origin: Vector3, ray_direction: Vector3): boolean
11
11
 
@@ -1,16 +1,21 @@
1
- import { Cache } from "../../../../../core/cache/Cache.js";
2
- import { buildUnsorted, buildUnsortedUnindexed } from "../../bvh/buffered/BinaryBVHFromBufferGeometry.js";
3
- import { ClippingPlaneContainmentComputingVisitor } from "./ClippingPlaneContainmentComputingVisitor.js";
4
- import { RaycastNearestHitComputingVisitor } from "./RaycastNearestHitComputingVisitor.js";
5
1
  import { assert } from "../../../../../core/assert.js";
2
+ import { BinaryUint32BVH } from "../../../../../core/bvh2/binary/2/BinaryUint32BVH.js";
3
+ import { Cache } from "../../../../../core/cache/Cache.js";
4
+ import { read_three_planes_to_array } from "../../../../../core/geom/3d/frustum/read_three_planes_to_array.js";
5
+ import { bvh32_from_indexed_geometry } from "../../bvh/buffered/bvh32_from_indexed_geometry.js";
6
+ import { bvh32_from_unindexed_geometry } from "../../bvh/buffered/bvh32_from_unindexed_geometry.js";
6
7
  import { deinterleaveBufferAttribute } from "../deinterleaveBufferAttribute.js";
8
+ import { bvh32_geometry_overlap_clipping_volume } from "./bvh32_geometry_overlap_clipping_volume.js";
9
+ import { bvh32_geometry_raycast } from "./bvh32_geometry_raycast.js";
10
+
11
+ const scratch_planes = []
7
12
 
8
13
  export class GeometrySpatialQueryAccelerator {
9
14
  constructor(cache_size = 100 * 1024 * 1024) {
10
15
 
11
16
  /**
12
17
  *
13
- * @type {Cache<THREE.BufferGeometry, IndexedBinaryBVH>}
18
+ * @type {Cache<THREE.BufferGeometry, BinaryUint32BVH>}
14
19
  */
15
20
  this.cache = new Cache({
16
21
  maxWeight: cache_size,
@@ -22,15 +27,13 @@ export class GeometrySpatialQueryAccelerator {
22
27
  },
23
28
  /**
24
29
  *
25
- * @param {IndexedBinaryBVH} tree
30
+ * @param {BinaryUint32BVH} tree
26
31
  */
27
32
  valueWeigher(tree) {
28
33
  return tree.estimateByteSize();
29
34
  }
30
35
  });
31
36
 
32
- this.__visitor_clip = new ClippingPlaneContainmentComputingVisitor();
33
- this.__visitor_raycast = new RaycastNearestHitComputingVisitor();
34
37
  }
35
38
 
36
39
  /**
@@ -53,23 +56,46 @@ export class GeometrySpatialQueryAccelerator {
53
56
  *
54
57
  * @param {THREE.BufferGeometry} geometry
55
58
  * @param {Plane[]} planes
56
- * @returns {number} -1,0,1. -1 means fully outside, 0 means partially inside, 1 means fully inside
59
+ * @returns {boolean} true means some overlap exists, false otherwise
57
60
  */
58
61
  queryContainmentViaClippingPlanes(geometry, planes) {
59
62
  assert.notNull(geometry, 'geometry');
60
63
  assert.defined(geometry, 'geometry');
61
64
 
65
+
62
66
  const bvh = this.__acquireBVH(geometry);
63
67
 
64
- this.__visitor_clip.reset();
68
+ read_three_planes_to_array(planes, scratch_planes);
69
+
70
+ const geometryIndices = geometry.getIndex()?.array;
71
+ const position_attribute = geometry.getAttribute('position');
72
+
73
+ let position_data;
74
+ let stride = 3;
75
+ let position_data_offset = 0;
76
+
77
+ if (position_attribute.isInterleavedBufferAttribute) {
65
78
 
66
- this.__visitor_clip.setGeometry(geometry);
67
- this.__visitor_clip.setPlanes(planes);
68
- this.__visitor_clip.setRoot(bvh);
79
+ position_data = position_attribute.data.array;
69
80
 
70
- bvh.traversePreOrderStack(this.__visitor_clip, 0);
81
+ stride = position_attribute.data.stride;
82
+ position_data_offset = position_attribute.offset;
83
+
84
+ } else {
71
85
 
72
- return this.__visitor_clip.getContainment();
86
+ position_data = position_attribute.array;
87
+
88
+ stride = 3;
89
+ position_data_offset = 0;
90
+
91
+ }
92
+
93
+ return bvh32_geometry_overlap_clipping_volume(
94
+ bvh,
95
+ position_data, position_data_offset, stride,
96
+ geometryIndices,
97
+ scratch_planes, 0, planes.length
98
+ );
73
99
  }
74
100
 
75
101
  /**
@@ -119,11 +145,33 @@ export class GeometrySpatialQueryAccelerator {
119
145
  return false;
120
146
  }
121
147
 
122
- this.__visitor_raycast.reset();
148
+ const geometryIndices = geometry.getIndex()?.array;
149
+ const position_attribute = geometry.getAttribute('position');
150
+
151
+ let position_data;
152
+ let stride = 3;
153
+ let position_data_offset = 0;
154
+
155
+ if (position_attribute.isInterleavedBufferAttribute) {
156
+
157
+ position_data = position_attribute.data.array;
123
158
 
124
- this.__visitor_raycast.setGeometry(geometry);
125
- this.__visitor_raycast.setRoot(bvh);
126
- this.__visitor_raycast.setRay(
159
+ stride = position_attribute.data.stride;
160
+ position_data_offset = position_attribute.offset;
161
+
162
+ } else {
163
+
164
+ position_data = position_attribute.array;
165
+
166
+ stride = 3;
167
+ position_data_offset = 0;
168
+
169
+ }
170
+
171
+ return bvh32_geometry_raycast(
172
+ destination, bvh,
173
+ position_data, position_data_offset, stride,
174
+ geometryIndices,
127
175
  ray_origin_x,
128
176
  ray_origin_y,
129
177
  ray_origin_z,
@@ -132,16 +180,6 @@ export class GeometrySpatialQueryAccelerator {
132
180
  ray_direction_z
133
181
  );
134
182
 
135
- bvh.traversePreOrderStack(this.__visitor_raycast, 0);
136
-
137
- if (this.__visitor_raycast.isHitFound()) {
138
-
139
- destination.copy(this.__visitor_raycast.getNearestHit());
140
-
141
- return true;
142
- } else {
143
- return false;
144
- }
145
183
  }
146
184
 
147
185
  /**
@@ -169,7 +207,7 @@ export class GeometrySpatialQueryAccelerator {
169
207
  /**
170
208
  * @private
171
209
  * @param {THREE.BufferGeometry} geometry
172
- * @returns {IndexedBinaryBVH}
210
+ * @returns {BinaryUint32BVH}
173
211
  */
174
212
  __acquireBVH(geometry) {
175
213
  assert.notNull(geometry, 'geometry');
@@ -191,7 +229,7 @@ export class GeometrySpatialQueryAccelerator {
191
229
  /**
192
230
  * @private
193
231
  * @param {THREE.BufferGeometry} geometry
194
- * @returns {IndexedBinaryBVH}
232
+ * @returns {BinaryUint32BVH}
195
233
  */
196
234
  __buildBVH(geometry) {
197
235
  const position_attribute = geometry.getAttribute('position');
@@ -200,14 +238,19 @@ export class GeometrySpatialQueryAccelerator {
200
238
  const de_interleaved_position_attribute = deinterleaveBufferAttribute(position_attribute);
201
239
  const vertices = de_interleaved_position_attribute.array;
202
240
 
241
+ const bvh = new BinaryUint32BVH();
242
+
203
243
  if (index_attribute === undefined || index_attribute === null) {
204
- return buildUnsortedUnindexed(vertices);
205
- }
244
+ bvh32_from_unindexed_geometry(bvh, vertices);
245
+ } else {
246
+
247
+ const de_interleaved_index_attribute = deinterleaveBufferAttribute(index_attribute);
248
+ const indices = de_interleaved_index_attribute.array;
206
249
 
207
- const de_interleaved_index_attribute = deinterleaveBufferAttribute(index_attribute);
208
- const indices = de_interleaved_index_attribute.array;
250
+ bvh32_from_indexed_geometry(bvh, vertices, indices);
251
+ }
209
252
 
210
- return buildUnsorted(vertices, indices);
253
+ return bvh;
211
254
  }
212
255
 
213
256
  }
@@ -0,0 +1,88 @@
1
+ import { assert } from "../../../../../core/assert.js";
2
+ import {
3
+ bvh32_query_user_data_overlaps_clipping_volume
4
+ } from "../../../../../core/bvh2/binary/2/bvh32_query_user_data_overlaps_clipping_volume.js";
5
+ import {
6
+ triangle_intersects_clipping_volume
7
+ } from "../../../../../core/geom/3d/triangle/triangle_intersects_clipping_volume.js";
8
+
9
+ const scratch_array = []
10
+
11
+ /**
12
+ *
13
+ * @param {BinaryUint32BVH} bvh
14
+ * @param {number[]|ArrayLike<number>} vertices
15
+ * @param {number} vertex_offset Unless you're using an interleaved buffer of some kind, this will be 0
16
+ * @param {number} vertex_stride Unless you're using an interleaved buffer, this should be 3
17
+ * @param {number[]|ArrayLike<number>|undefined} indices if this is set to undefined - implicit indexing will be used
18
+ * @param {number[]|Float32Array} planes
19
+ * @param {number} planes_offset
20
+ * @param {number} plane_count
21
+ * @returns {boolean}
22
+ */
23
+ export function bvh32_geometry_overlap_clipping_volume(
24
+ bvh,
25
+ vertices, vertex_offset, vertex_stride,
26
+ indices,
27
+ planes, planes_offset, plane_count
28
+ ) {
29
+
30
+ const hit_count = bvh32_query_user_data_overlaps_clipping_volume(
31
+ scratch_array, 0,
32
+ bvh,
33
+ planes, planes_offset, plane_count
34
+ );
35
+
36
+ let a, b, c;
37
+
38
+ for (let i = 0; i < hit_count; i++) {
39
+ const triangle_index = scratch_array[i];
40
+
41
+ const index3 = triangle_index * 3;
42
+
43
+ if (indices !== undefined) {
44
+ assert.lessThan(index3 + 2, indices.length, 'triangle index overflow, possibly geometry changed but tree was not rebuilt?');
45
+
46
+ a = indices[index3];
47
+ b = indices[index3 + 1];
48
+ c = indices[index3 + 2];
49
+ } else {
50
+ // implicit indices
51
+ a = index3;
52
+ b = index3 + 1;
53
+ c = index3 + 2;
54
+ }
55
+
56
+ const a_address = a * vertex_stride + vertex_offset;
57
+ const b_address = b * vertex_stride + vertex_offset;
58
+ const c_address = c * vertex_stride + vertex_offset;
59
+
60
+ assert.lessThan(a_address + 2, vertices.length, 'a-vertex overflow');
61
+ assert.lessThan(b_address + 2, vertices.length, 'b-vertex overflow');
62
+ assert.lessThan(c_address + 2, vertices.length, 'c-vertex overflow');
63
+
64
+ const ax = vertices[a_address];
65
+ const ay = vertices[a_address + 1];
66
+ const az = vertices[a_address + 2];
67
+
68
+ const bx = vertices[b_address];
69
+ const by = vertices[b_address + 1];
70
+ const bz = vertices[b_address + 2];
71
+
72
+ const cx = vertices[c_address];
73
+ const cy = vertices[c_address + 1];
74
+ const cz = vertices[c_address + 2];
75
+
76
+ if (triangle_intersects_clipping_volume(
77
+ planes, planes_offset, plane_count,
78
+ ax, ay, az,
79
+ bx, by, bz,
80
+ cx, cy, cz
81
+ )) {
82
+ return true;
83
+ }
84
+
85
+ }
86
+
87
+ return false;
88
+ }
@@ -0,0 +1,108 @@
1
+ import { assert } from "../../../../../core/assert.js";
2
+ import { bvh32_query_user_data_ray } from "../../../../../core/bvh2/binary/2/bvh32_query_user_data_ray.js";
3
+ import { SurfacePoint3 } from "../../../../../core/geom/3d/SurfacePoint3.js";
4
+ import { computeTriangleRayIntersection } from "../../../../../core/geom/3d/triangle/computeTriangleRayIntersection.js";
5
+
6
+ const scratch_array = []
7
+ const scratch_hit = new SurfacePoint3()
8
+
9
+ /**
10
+ *
11
+ * @param {SurfacePoint3} result
12
+ * @param {BinaryUint32BVH} bvh
13
+ * @param {number[]|ArrayLike<number>} vertices
14
+ * @param {number} vertex_offset Unless you're using an interleaved buffer of some kind, this will be 0
15
+ * @param {number} vertex_stride Unless you're using an interleaved buffer, this should be 3
16
+ * @param {number[]|ArrayLike<number>|undefined} indices if this is set to undefined - implicit indexing will be used
17
+ * @param {number} originX
18
+ * @param {number} originY
19
+ * @param {number} originZ
20
+ * @param {number} directionX
21
+ * @param {number} directionY
22
+ * @param {number} directionZ
23
+ * @returns {boolean}
24
+ */
25
+ export function bvh32_geometry_raycast(
26
+ result,
27
+ bvh,
28
+ vertices, vertex_offset, vertex_stride,
29
+ indices,
30
+ originX, originY, originZ,
31
+ directionX, directionY, directionZ
32
+ ) {
33
+ let hit_found = false;
34
+
35
+ const hit_count = bvh32_query_user_data_ray(
36
+ scratch_array, 0,
37
+ bvh,
38
+ originX, originY, originZ,
39
+ directionX, directionY, directionZ
40
+ );
41
+
42
+ let best_distance = Infinity;
43
+
44
+ let a, b, c;
45
+
46
+ for (let i = 0; i < hit_count; i++) {
47
+ const triangle_index = scratch_array[i];
48
+
49
+ const index3 = triangle_index * 3;
50
+
51
+ if (indices !== undefined) {
52
+ assert.lessThan(index3 + 2, indices.length, 'triangle index overflow, possibly geometry changed but tree was not rebuilt?');
53
+
54
+ a = indices[index3];
55
+ b = indices[index3 + 1];
56
+ c = indices[index3 + 2];
57
+ } else {
58
+ // implicit indices
59
+ a = index3;
60
+ b = index3 + 1;
61
+ c = index3 + 2;
62
+ }
63
+
64
+ const a_address = a * vertex_stride + vertex_offset;
65
+ const b_address = b * vertex_stride + vertex_offset;
66
+ const c_address = c * vertex_stride + vertex_offset;
67
+
68
+ assert.lessThan(a_address + 2, vertices.length, 'a-vertex overflow');
69
+ assert.lessThan(b_address + 2, vertices.length, 'b-vertex overflow');
70
+ assert.lessThan(c_address + 2, vertices.length, 'c-vertex overflow');
71
+
72
+ const ax = vertices[a_address];
73
+ const ay = vertices[a_address + 1];
74
+ const az = vertices[a_address + 2];
75
+
76
+ const bx = vertices[b_address];
77
+ const by = vertices[b_address + 1];
78
+ const bz = vertices[b_address + 2];
79
+
80
+ const cx = vertices[c_address];
81
+ const cy = vertices[c_address + 1];
82
+ const cz = vertices[c_address + 2];
83
+
84
+ const triangle_hit_found = computeTriangleRayIntersection(
85
+ scratch_hit,
86
+ originX, originY, originZ,
87
+ directionX, directionY, directionZ,
88
+ ax, ay, az,
89
+ bx, by, bz,
90
+ cx, cy, cz
91
+ )
92
+
93
+ if (!triangle_hit_found) {
94
+ continue;
95
+ }
96
+
97
+ hit_found = true;
98
+
99
+ const distance_sqr = scratch_hit.position._distanceSqrTo(originX, originY, originZ);
100
+
101
+ if (distance_sqr < best_distance) {
102
+ best_distance = distance_sqr;
103
+ result.copy(scratch_hit);
104
+ }
105
+ }
106
+
107
+ return hit_found;
108
+ }
@@ -1,5 +1,4 @@
1
- import { max3 } from "../../../../../core/math/max3.js";
2
- import { min3 } from "../../../../../core/math/min3.js";
1
+ import { bvh32_set_leaf_from_triangle } from "./bvh32_set_leaf_from_triangle.js";
3
2
 
4
3
  /**
5
4
  *
@@ -23,36 +22,11 @@ export function bvh32_from_indexed_geometry(bvh, vertices, indices) {
23
22
  const iB = indices[index3 + 1];
24
23
  const iC = indices[index3 + 2];
25
24
 
26
- const a = iA * 3;
27
- const b = iB * 3;
28
- const c = iC * 3;
29
-
30
- // read actual positions of each vertex
31
- const aX = vertices[a];
32
- const aY = vertices[a + 1];
33
- const aZ = vertices[a + 2];
34
-
35
- const bX = vertices[b];
36
- const bY = vertices[b + 1];
37
- const bZ = vertices[b + 2];
38
-
39
- const cX = vertices[c];
40
- const cY = vertices[c + 1];
41
- const cZ = vertices[c + 2];
42
-
43
- // compute bounds of the triangle
44
- const x0 = min3(aX, bX, cX);
45
- const y0 = min3(aY, bY, cY);
46
- const z0 = min3(aZ, bZ, cZ);
47
-
48
- const x1 = max3(aX, bX, cX);
49
- const y1 = max3(aY, bY, cY);
50
- const z1 = max3(aZ, bZ, cZ);
51
-
52
- bvh.setLeafData(i, i, x0, y0, z0, x1, y1, z1)
25
+ bvh32_set_leaf_from_triangle(bvh, i, vertices, iA, iB, iC);
53
26
  }
54
27
 
55
28
  // finalize build
56
29
  // bvh.sort_morton(MATRIX_4_IDENTITY);
57
30
  bvh.build();
58
- }
31
+ }
32
+
@@ -0,0 +1,30 @@
1
+ import { bvh32_set_leaf_from_triangle } from "./bvh32_set_leaf_from_triangle.js";
2
+
3
+ /**
4
+ *
5
+ * @param {BinaryUint32BVH} bvh
6
+ * @param {Float32Array} vertices
7
+ */
8
+ export function bvh32_from_unindexed_geometry(bvh, vertices) {
9
+
10
+ const triangle_count = indices.length / 3;
11
+
12
+ bvh.setLeafCount(triangle_count);
13
+ bvh.initialize_structure();
14
+
15
+ for (let i = 0; i < triangle_count; i++) {
16
+
17
+ const index3 = i * 3;
18
+
19
+ // generate triangle vertex indices
20
+ const iA = index3;
21
+ const iB = index3 + 1;
22
+ const iC = index3 + 2;
23
+
24
+ bvh32_set_leaf_from_triangle(bvh, i, vertices, iA, iB, iC);
25
+ }
26
+
27
+ // finalize build
28
+ // bvh.sort_morton(MATRIX_4_IDENTITY);
29
+ bvh.build();
30
+ }
@@ -0,0 +1,41 @@
1
+ import { max3 } from "../../../../../core/math/max3.js";
2
+ import { min3 } from "../../../../../core/math/min3.js";
3
+
4
+ /**
5
+ *
6
+ * @param {BinaryUint32BVH} bvh
7
+ * @param {number} triangle_index
8
+ * @param {number[]|Float32Array} vertices
9
+ * @param {number} a_index
10
+ * @param {number} b_index
11
+ * @param {number} c_index
12
+ */
13
+ export function bvh32_set_leaf_from_triangle(bvh, triangle_index, vertices, a_index, b_index, c_index) {
14
+ const a = a_index * 3;
15
+ const b = b_index * 3;
16
+ const c = c_index * 3;
17
+
18
+ // read actual positions of each vertex
19
+ const aX = vertices[a];
20
+ const aY = vertices[a + 1];
21
+ const aZ = vertices[a + 2];
22
+
23
+ const bX = vertices[b];
24
+ const bY = vertices[b + 1];
25
+ const bZ = vertices[b + 2];
26
+
27
+ const cX = vertices[c];
28
+ const cY = vertices[c + 1];
29
+ const cZ = vertices[c + 2];
30
+
31
+ // compute bounds of the triangle
32
+ const x0 = min3(aX, bX, cX);
33
+ const y0 = min3(aY, bY, cY);
34
+ const z0 = min3(aZ, bZ, cZ);
35
+
36
+ const x1 = max3(aX, bX, cX);
37
+ const y1 = max3(aY, bY, cY);
38
+ const z1 = max3(aZ, bZ, cZ);
39
+
40
+ bvh.setLeafData(triangle_index, triangle_index, x0, y0, z0, x1, y1, z1)
41
+ }
@@ -1,15 +1,12 @@
1
- import { EngineHarness } from "../../../EngineHarness.js";
2
- import { EngineConfiguration } from "../../../EngineConfiguration.js";
3
- import { ShadedGeometrySystem } from "../../ecs/mesh-v2/ShadedGeometrySystem.js";
4
- import { AttachmentSystem } from "../../../ecs/attachment/AttachmentSystem.js";
5
- import { TransformAttachmentSystem } from "../../../ecs/transform-attachment/TransformAttachmentSystem.js";
6
1
  import { GameAssetType } from "../../../asset/GameAssetType.js";
7
2
  import { GLTFAssetLoader } from "../../../asset/loaders/GLTFAssetLoader.js";
3
+ import { AttachmentSystem } from "../../../ecs/attachment/AttachmentSystem.js";
4
+ import { TransformAttachmentSystem } from "../../../ecs/transform-attachment/TransformAttachmentSystem.js";
5
+ import { EngineConfiguration } from "../../../EngineConfiguration.js";
6
+ import { EngineHarness } from "../../../EngineHarness.js";
7
+ import { ShadedGeometrySystem } from "../../ecs/mesh-v2/ShadedGeometrySystem.js";
8
8
  import { three_object_to_entity_composition } from "../../ecs/mesh-v2/three_object_to_entity_composition.js";
9
9
  import { MaterialOptimizationContext } from "./MaterialOptimizationContext.js";
10
- import {
11
- bvh_traverse_pre_order_using_stack
12
- } from "../../../../core/bvh2/traversal/bvh_traverse_pre_order_using_stack.js";
13
10
 
14
11
  const eh = new EngineHarness();
15
12
 
@@ -23,17 +20,17 @@ function optimize(engine) {
23
20
 
24
21
  const optimizer = new MaterialOptimizationContext();
25
22
 
26
- engine.graphics.layers.layers.forEach(l => {
27
- bvh_traverse_pre_order_using_stack(l.bvh, node => {
28
- if (node.isLeafNode) {
29
- const object = l.extractRenderable(node.object);
30
-
31
- if (object) {
32
- optimizer.addObject(object);
33
- }
34
- }
35
- });
36
- });
23
+ // engine.graphics.layers.layers.forEach(l => {
24
+ // bvh_traverse_pre_order_using_stack(l.bvh, node => {
25
+ // if (node.isLeafNode) {
26
+ // const object = l.extractRenderable(node.object);
27
+ //
28
+ // if (object) {
29
+ // optimizer.addObject(object);
30
+ // }
31
+ // }
32
+ // });
33
+ // });
37
34
 
38
35
  optimizer.update();
39
36
 
@@ -26,7 +26,7 @@ import { array_copy } from "../../../../core/collection/array/array_copy.js";
26
26
  import { arrayQuickSort } from "../../../../core/collection/array/arrayQuickSort.js";
27
27
  import { arraySwapElements } from "../../../../core/collection/array/arraySwapElements.js";
28
28
  import { read_cluster_frustum_corners } from "../../../../core/geom/3d/frustum/read_cluster_frustum_corners.js";
29
- import { read_frustum_planes_to_array } from "../../../../core/geom/3d/frustum/read_frustum_planes_to_array.js";
29
+ import { read_three_planes_to_array } from "../../../../core/geom/3d/frustum/read_three_planes_to_array.js";
30
30
  import { slice_frustum_linear_to_points } from "../../../../core/geom/3d/frustum/slice_frustum_linear_to_points.js";
31
31
  import { v3_morton_encode_transformed } from "../../../../core/geom/3d/morton/v3_morton_encode_transformed.js";
32
32
  import { v3_distance } from "../../../../core/geom/vec3/v3_distance.js";
@@ -727,7 +727,7 @@ export class LightManager {
727
727
 
728
728
  const nodes = [];
729
729
 
730
- read_frustum_planes_to_array(this.__view_frustum.planes, scratch_frustum_planes)
730
+ read_three_planes_to_array(this.__view_frustum.planes, scratch_frustum_planes)
731
731
 
732
732
  /*
733
733
  Search is done in 2 phases: