Skip to content

Commit 743862c

Browse files
committed
MeshLayer.getObjectPosition now samples up to ~100,000 vertices
1 parent 06ed3e5 commit 743862c

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

src/mesh/frontend.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -612,8 +612,8 @@ export class MeshLayer extends PerspectiveViewRenderLayer<ThreeDimensionalRender
612612
rank,
613613
);
614614
const { fragmentIds } = chunk;
615-
const closestVertex = new Float32Array(rank);
616-
let closestDistanceSq = Number.POSITIVE_INFINITY;
615+
let vertexCount = 0;
616+
const fragmentChunks: FragmentChunk[] = [];
617617
for (const fragmentId of fragmentIds) {
618618
const { key: fragmentKey } = this.source.getFragmentKey(key, fragmentId);
619619
const fragmentChunk = this.source.fragmentSource.chunks.get(fragmentKey);
@@ -625,9 +625,21 @@ export class MeshLayer extends PerspectiveViewRenderLayer<ThreeDimensionalRender
625625
) {
626626
continue;
627627
}
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;
628640
const { vertexPositions } = meshData;
629641
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) {
631643
let distanceSq = 0;
632644
for (let j = 0; j < rank; j++) {
633645
distanceSq +=

0 commit comments

Comments
 (0)