-
Notifications
You must be signed in to change notification settings - Fork 386
Expand file tree
/
Copy pathraycastTraverse.js
More file actions
180 lines (112 loc) · 3.87 KB
/
raycastTraverse.js
File metadata and controls
180 lines (112 loc) · 3.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import { Ray, Vector3 } from 'three';
// In three.js r165 and higher raycast traversal can be ended early
const _localRay = /* @__PURE__ */ new Ray();
const _vec = /* @__PURE__ */ new Vector3();
const _hitArray = [];
function distanceSort( a, b ) {
return a.distance - b.distance;
}
function intersectTileScene( tile, raycaster, renderer, intersects ) {
const { scene } = tile.engineData;
if ( ! scene ) {
return;
}
const didRaycast = renderer.invokeOnePlugin( plugin => plugin.raycastTile && plugin.raycastTile( tile, scene, raycaster, intersects ) );
if ( ! didRaycast ) {
raycaster.intersectObject( scene, true, intersects );
}
}
function intersectTileSceneFirstHist( tile, raycaster, renderer ) {
intersectTileScene( tile, raycaster, renderer, _hitArray );
_hitArray.sort( distanceSort );
const hit = _hitArray[ 0 ] || null;
_hitArray.length = 0;
return hit;
}
function isTileInitialized( tile ) {
return 'traversal' in tile;
}
// Returns the closest hit when traversing the tree
export function raycastTraverseFirstHit( renderer, tile, raycaster, localRay = null ) {
const { group, activeTiles } = renderer;
// get the ray in the local group frame
if ( localRay === null ) {
localRay = _localRay;
localRay.copy( raycaster.ray ).applyMatrix4( group.matrixWorldInverse );
}
// get a set of intersections so we intersect the nearest one first
const array = [];
const children = tile.children;
for ( let i = 0, l = children.length; i < l; i ++ ) {
const child = children[ i ];
if ( ! isTileInitialized( child ) || ! child.traversal.used ) {
continue;
}
// track the tile and hit distance for sorting
const boundingVolume = child.engineData.boundingVolume;
if ( boundingVolume.intersectRay( localRay, _vec ) !== null ) {
_vec.applyMatrix4( group.matrixWorld );
array.push( {
distance: _vec.distanceToSquared( raycaster.ray.origin ),
tile: child,
} );
}
}
// sort them by ascending distance
array.sort( distanceSort );
// If the root is active make sure we've checked it
let bestHit = null;
let bestHitDistSq = Infinity;
if ( activeTiles.has( tile ) ) {
const hit = intersectTileSceneFirstHist( tile, raycaster, renderer );
if ( hit ) {
bestHit = hit;
bestHitDistSq = hit.distance * hit.distance;
}
}
// traverse until we find the best hit and early out if a tile bounds
// couldn't possible include a best hit
for ( let i = 0, l = array.length; i < l; i ++ ) {
const data = array[ i ];
const boundingVolumeDistSq = data.distance;
const tile = data.tile;
if ( boundingVolumeDistSq > bestHitDistSq ) {
break;
}
const hit = raycastTraverseFirstHit( renderer, tile, raycaster, localRay );
if ( hit ) {
const hitDistSq = hit.distance * hit.distance;
if ( hitDistSq < bestHitDistSq ) {
bestHit = hit;
bestHitDistSq = hitDistSq;
}
}
}
return bestHit;
}
export function raycastTraverse( renderer, tile, raycaster, intersects, localRay = null ) {
// if the tile has not been asynchronously initialized then there's no point in
// traversing the tiles to check intersections.
if ( ! isTileInitialized( tile ) ) {
return;
}
const { group, activeTiles } = renderer;
const { boundingVolume } = tile.engineData;
// get the ray in the local group frame
if ( localRay === null ) {
localRay = _localRay;
localRay.copy( raycaster.ray ).applyMatrix4( group.matrixWorldInverse );
}
// exit early if the tile isn't used or the bounding volume is not intersected
if ( ! tile.traversal.used || ! boundingVolume.intersectsRay( localRay ) ) {
return;
}
// only intersect the tile geometry if it's active
if ( activeTiles.has( tile ) ) {
intersectTileScene( tile, raycaster, renderer, intersects );
}
const children = tile.children;
for ( let i = 0, l = children.length; i < l; i ++ ) {
raycastTraverse( renderer, children[ i ], raycaster, intersects, localRay );
}
}