File size: 7,353 Bytes
b797337
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/** @import { Raycaster, Intersection } from 'three' */
/** @import { GeometryBVH } from '../core/GeometryBVH.js' */
import { Mesh, Points, Line, LineLoop, LineSegments, Sphere, BatchedMesh, REVISION } from 'three';
import { MeshBVH } from '../core/MeshBVH.js';

const IS_REVISION_166 = parseInt( REVISION ) >= 166;

// TODO: how can we expand these raycast functions?
const _raycastFunctions = {
	'Mesh': Mesh.prototype.raycast,
	'Line': Line.prototype.raycast,
	'LineSegments': LineSegments.prototype.raycast,
	'LineLoop': LineLoop.prototype.raycast,
	'Points': Points.prototype.raycast,
	'BatchedMesh': BatchedMesh.prototype.raycast,
};

const _mesh = /* @__PURE__ */ new Mesh();
const _batchIntersects = [];

/**
 * An accelerated raycast function with the same signature as `THREE.Mesh.raycast`. Uses the BVH
 * for raycasting if it's available otherwise it falls back to the built-in approach. The results
 * of the function are designed to be identical to the results of the conventional
 * `THREE.Mesh.raycast` results.
 *
 * If the raycaster object being used has a property `firstHitOnly` set to `true`, then the
 * raycasting will terminate as soon as it finds the closest intersection to the ray's origin and
 * return only that intersection. This is typically several times faster than searching for all
 * intersections.
 *
 * @section Extension Utilities
 * @param {Raycaster} raycaster
 * @param {Array<Intersection>} intersects
 * @returns {void}
 */
export function acceleratedRaycast( raycaster, intersects ) {

	if ( this.isBatchedMesh ) {

		acceleratedBatchedMeshRaycast.call( this, raycaster, intersects );

	} else {

		const { geometry } = this;
		if ( geometry.boundsTree ) {

			geometry.boundsTree.raycastObject3D( this, raycaster, intersects );

		} else {

			let raycastFunction;
			if ( this instanceof Mesh ) {

				raycastFunction = _raycastFunctions.Mesh;

			} else if ( this instanceof LineSegments ) {

				raycastFunction = _raycastFunctions.LineSegments;

			} else if ( this instanceof LineLoop ) {

				raycastFunction = _raycastFunctions.LineLoop;

			} else if ( this instanceof Line ) {

				raycastFunction = _raycastFunctions.Line;

			} else if ( this instanceof Points ) {

				raycastFunction = _raycastFunctions.Points;

			} else {

				throw new Error( 'BVH: Fallback raycast function not found.' );

			}

			raycastFunction.call( this, raycaster, intersects );

		}

	}

}

function acceleratedBatchedMeshRaycast( raycaster, intersects ) {

	if ( this.boundsTrees ) {

		// TODO: remove use of geometry info, instance info when r170 is minimum version
		const boundsTrees = this.boundsTrees;
		const drawInfo = this._drawInfo || this._instanceInfo;
		const drawRanges = this._drawRanges || this._geometryInfo;
		const matrixWorld = this.matrixWorld;

		_mesh.material = this.material;
		_mesh.geometry = this.geometry;

		const oldBoundsTree = _mesh.geometry.boundsTree;
		const oldDrawRange = _mesh.geometry.drawRange;

		if ( _mesh.geometry.boundingSphere === null ) {

			_mesh.geometry.boundingSphere = new Sphere();

		}

		// TODO: provide new method to get instances count instead of 'drawInfo.length'
		for ( let i = 0, l = drawInfo.length; i < l; i ++ ) {

			if ( ! this.getVisibleAt( i ) ) {

				continue;

			}

			// TODO: use getGeometryIndex
			const geometryId = drawInfo[ i ].geometryIndex;

			_mesh.geometry.boundsTree = boundsTrees[ geometryId ];

			this.getMatrixAt( i, _mesh.matrixWorld ).premultiply( matrixWorld );

			if ( ! _mesh.geometry.boundsTree ) {

				this.getBoundingBoxAt( geometryId, _mesh.geometry.boundingBox );
				this.getBoundingSphereAt( geometryId, _mesh.geometry.boundingSphere );

				const drawRange = drawRanges[ geometryId ];
				_mesh.geometry.setDrawRange( drawRange.start, drawRange.count );

			}

			_mesh.raycast( raycaster, _batchIntersects );

			for ( let j = 0, l = _batchIntersects.length; j < l; j ++ ) {

				const intersect = _batchIntersects[ j ];
				intersect.object = this;
				intersect.batchId = i;
				intersects.push( intersect );

			}

			_batchIntersects.length = 0;

		}

		_mesh.geometry.boundsTree = oldBoundsTree;
		_mesh.geometry.drawRange = oldDrawRange;
		_mesh.material = null;
		_mesh.geometry = null;

	} else {

		_raycastFunctions.BatchedMesh.call( this, raycaster, intersects );

	}

}

/**
 * A pre-made BufferGeometry extension function that builds a new BVH, assigns it to `boundsTree`
 * for BufferGeometry, and applies the new index buffer to the geometry. Comparable to
 * `computeBoundingBox` and `computeBoundingSphere`.
 *
 * ```js
 * THREE.BufferGeometry.prototype.computeBoundsTree = computeBoundsTree;
 * ```
 *
 * @section Extension Utilities
 * @param {Object} [options]
 * @returns {GeometryBVH}
 */
export function computeBoundsTree( options = {} ) {

	const { type = MeshBVH } = options;
	this.boundsTree = new type( this, options );
	return this.boundsTree;

}

/**
 * A BufferGeometry extension function that disposes of the BVH.
 *
 * ```js
 * THREE.BufferGeometry.prototype.disposeBoundsTree = disposeBoundsTree;
 * ```
 *
 * @section Extension Utilities
 * @returns {void}
 */
export function disposeBoundsTree() {

	this.boundsTree = null;

}

/**
 * Equivalent of `computeBoundsTree` for `BatchedMesh`. Creates the
 * `BatchedMesh.boundsTrees` array if it does not exist. If `index` is `-1`
 * BVHs for all available geometries are generated and the full array is
 * returned; otherwise only the BVH at that geometry index is generated and
 * returned.
 *
 * ```js
 * THREE.BatchedMesh.prototype.computeBoundsTree = computeBatchedBoundsTree;
 * ```
 *
 * @section Extension Utilities
 * @param {number} [index=-1]
 * @param {Object} [options]
 * @returns {GeometryBVH | Array<GeometryBVH> | null}
 */
export function computeBatchedBoundsTree( index = - 1, options = {} ) {

	if ( ! IS_REVISION_166 ) {

		throw new Error( 'BatchedMesh: Three r166+ is required to compute bounds trees.' );

	}

	options = {
		...options,
		range: null
	};

	const drawRanges = this._drawRanges || this._geometryInfo;
	const geometryCount = this._geometryCount;
	if ( ! this.boundsTrees ) {

		this.boundsTrees = new Array( geometryCount ).fill( null );

	}

	const boundsTrees = this.boundsTrees;
	while ( boundsTrees.length < geometryCount ) {

		boundsTrees.push( null );

	}

	if ( index < 0 ) {

		for ( let i = 0; i < geometryCount; i ++ ) {

			options.range = drawRanges[ i ];
			boundsTrees[ i ] = new MeshBVH( this.geometry, options );

		}

		return boundsTrees;

	} else {

		if ( index < drawRanges.length ) {

			options.range = drawRanges[ index ];
			boundsTrees[ index ] = new MeshBVH( this.geometry, options );

		}

		return boundsTrees[ index ] || null;

	}

}

/**
 * Equivalent of `disposeBoundsTree` for `BatchedMesh`. Sets entries in
 * `BatchedMesh.boundsTrees` to `null`. If `index` is `-1` all BVHs are
 * disposed; otherwise only the BVH at that geometry index is disposed.
 *
 * ```js
 * THREE.BatchedMesh.prototype.disposeBoundsTree = disposeBatchedBoundsTree;
 * ```
 *
 * @section Extension Utilities
 * @param {number} [index=-1]
 * @returns {void}
 */
export function disposeBatchedBoundsTree( index = - 1 ) {

	if ( index < 0 ) {

		this.boundsTrees.fill( null );

	} else {

		if ( index < this.boundsTrees.length ) {

			this.boundsTrees[ index ] = null;

		}

	}

}