@@ -2,6 +2,7 @@ const localVector = new THREE.Vector3();
2
2
const localVector2 = new THREE . Vector3 ( ) ;
3
3
const localVector3 = new THREE . Vector3 ( ) ;
4
4
const localVector4 = new THREE . Vector3 ( ) ;
5
+ const localVector5 = new THREE . Vector3 ( ) ;
5
6
const localVector2D = new THREE . Vector2 ( ) ;
6
7
const localQuaternion = new THREE . Quaternion ( ) ;
7
8
const localRaycaster = new THREE . Raycaster ( ) ;
@@ -17,6 +18,7 @@ const _makePromise = () => {
17
18
p . reject = reject ;
18
19
return p ;
19
20
} ;
21
+ const _floorVector = v => v . set ( Math . floor ( v . x ) , Math . floor ( v . y ) , Math . floor ( v . z ) ) ;
20
22
21
23
export class XRRaycaster {
22
24
constructor ( { width = 512 , height = 512 , fov = 60 , aspect = 1 , depth = 3 , near = 0.1 , far = 300 , renderer = new THREE . WebGLRenderer ( ) , onRender = ( target , camera ) => { } } = { } ) {
@@ -224,6 +226,8 @@ export class XRChunker extends EventTarget {
224
226
} ) ;
225
227
return worker ;
226
228
} ) ( ) ;
229
+
230
+ this . _neededCoordsCache = [ ] ;
227
231
}
228
232
getChunkAt ( x , y , z ) {
229
233
for ( let i = 0 ; i < this . chunks . length ; i ++ ) {
@@ -242,14 +246,26 @@ export class XRChunker extends EventTarget {
242
246
const quaternion = localQuaternion . fromArray ( q ) ;
243
247
const scale = localVector2 . fromArray ( s ) ;
244
248
245
- const _floorVector = v => v . set ( Math . floor ( v . x ) , Math . floor ( v . y ) , Math . floor ( v . z ) ) ;
246
249
const cameraCenter = _floorVector ( localVector3 . copy ( position ) ) . add ( localVector4 . set ( 0.5 , 0.5 , 0.5 ) ) ;
247
250
248
- const neededCoords = [ ] ;
251
+ const neededCoords = this . _neededCoordsCache ;
252
+ let numNeededCoords = 0 ;
253
+ const _hasNeededCoord = coord => {
254
+ for ( let i = 0 ; i < numNeededCoords ; i ++ ) {
255
+ if ( neededCoords [ i ] . equals ( coord ) ) {
256
+ return true ;
257
+ }
258
+ }
259
+ return false ;
260
+ } ;
249
261
const _addNeededCoord = ( x , y , z ) => {
250
- const c = _floorVector ( cameraCenter . clone ( ) . add ( localVector4 . set ( x , y , z ) . applyQuaternion ( quaternion ) ) ) ;
251
- if ( ! neededCoords . some ( c2 => c2 . equals ( c ) ) ) {
252
- neededCoords . push ( c ) ;
262
+ const c = _floorVector ( localVector4 . copy ( cameraCenter ) . add ( localVector5 . set ( x , y , z ) . applyQuaternion ( quaternion ) ) ) ;
263
+ if ( ! _hasNeededCoord ( c ) ) {
264
+ const index = numNeededCoords ++ ;
265
+ if ( index >= neededCoords . length ) {
266
+ neededCoords . push ( new THREE . Vector3 ( ) ) ;
267
+ }
268
+ neededCoords [ index ] . copy ( c ) ;
253
269
}
254
270
}
255
271
for ( let z = 0 ; z >= - scale . z / 2 ; z -- ) {
@@ -270,18 +286,21 @@ export class XRChunker extends EventTarget {
270
286
}
271
287
}
272
288
273
- const missingChunkCoords = neededCoords . filter ( c => ! this . getChunkAt ( c . x , c . y , c . z ) ) ;
274
- const outrangedChunks = this . chunks . filter ( chunk => ! neededCoords . some ( coord => coord . equals ( chunk . object . position ) ) ) ;
275
- for ( let i = 0 ; i < outrangedChunks . length ; i ++ ) {
276
- const chunk = outrangedChunks [ i ] ;
277
- this . chunks . splice ( this . chunks . indexOf ( chunk ) , 1 ) ;
278
- this . dispatchEvent ( new MessageEvent ( 'removechunk' , { data : chunk } ) ) ;
279
- }
280
- for ( let i = 0 ; i < missingChunkCoords . length ; i ++ ) {
281
- const coord = missingChunkCoords [ i ] ;
282
- const chunk = new XRChunk ( coord . x , coord . y , coord . z ) ;
283
- this . chunks . push ( chunk ) ;
284
- this . dispatchEvent ( new MessageEvent ( 'addchunk' , { data : chunk } ) ) ;
289
+ this . chunks = this . chunks . filter ( chunk => {
290
+ if ( _hasNeededCoord ( chunk . object . position ) ) {
291
+ return true ;
292
+ } else {
293
+ this . dispatchEvent ( new MessageEvent ( 'removechunk' , { data : chunk } ) ) ;
294
+ return false ;
295
+ }
296
+ } ) ;
297
+ for ( let i = 0 ; i < numNeededCoords ; i ++ ) {
298
+ const coord = neededCoords [ i ] ;
299
+ if ( ! this . getChunkAt ( coord . x , coord . y , coord . z ) ) {
300
+ const chunk = new XRChunk ( coord . x , coord . y , coord . z ) ;
301
+ this . chunks . push ( chunk ) ;
302
+ this . dispatchEvent ( new MessageEvent ( 'addchunk' , { data : chunk } ) ) ;
303
+ }
285
304
}
286
305
}
287
306
async updateMesh ( getPointCloud ) {
0 commit comments