@@ -612,8 +612,8 @@ export class MeshLayer extends PerspectiveViewRenderLayer<ThreeDimensionalRender
612
612
rank ,
613
613
) ;
614
614
const { fragmentIds } = chunk ;
615
- const closestVertex = new Float32Array ( rank ) ;
616
- let closestDistanceSq = Number . POSITIVE_INFINITY ;
615
+ let vertexCount = 0 ;
616
+ const fragmentChunks : FragmentChunk [ ] = [ ] ;
617
617
for ( const fragmentId of fragmentIds ) {
618
618
const { key : fragmentKey } = this . source . getFragmentKey ( key , fragmentId ) ;
619
619
const fragmentChunk = this . source . fragmentSource . chunks . get ( fragmentKey ) ;
@@ -625,9 +625,21 @@ export class MeshLayer extends PerspectiveViewRenderLayer<ThreeDimensionalRender
625
625
) {
626
626
continue ;
627
627
}
628
+ vertexCount += meshData . vertexPositions . length / rank ;
629
+ fragmentChunks . push ( fragmentChunk ) ;
630
+ }
631
+ // Only check up to ~100,000 vertices.
632
+ // Not exact because it restarts the interval for each fragment chunk.
633
+ const TARGET_VERTEX_COUNT = 100 * 1000 ;
634
+ const sampleRate = TARGET_VERTEX_COUNT / vertexCount ;
635
+ const sampleInterval = Math . max ( 1 , Math . floor ( 1 / sampleRate ) ) ;
636
+ const closestVertex = new Float32Array ( rank ) ;
637
+ let closestDistanceSq = Number . POSITIVE_INFINITY ;
638
+ for ( const fragmentChunk of fragmentChunks ) {
639
+ const { meshData } = fragmentChunk ;
628
640
const { vertexPositions } = meshData ;
629
641
if ( vertexPositions . length < rank ) continue ;
630
- for ( let i = 0 ; i < vertexPositions . length ; i += rank ) {
642
+ for ( let i = 0 ; i < vertexPositions . length ; i += rank * sampleInterval ) {
631
643
let distanceSq = 0 ;
632
644
for ( let j = 0 ; j < rank ; j ++ ) {
633
645
distanceSq +=
0 commit comments