Skip to content

Class: GPUPicker

Defined in: src/utils/GPUPicker.ts:214

GPU-based picking for fast, accurate vertex selection on large meshes.

Uses render-to-texture to encode vertex indices into colors, then reads the pixel under the mouse to determine the exact picked vertex.

Benefits over raycasting:

  • O(1) picking time regardless of mesh size
  • Exact vertex index (no face-to-vertex approximation needed)
  • Works correctly with complex shaders and deformations

Example

typescript
const picker = new GPUPicker(renderer);

// Register surfaces
picker.addSurface('brain', brainMesh);

// Pick at mouse position
const result = picker.pick(mouseX, mouseY, camera);
if (result.vertexIndex !== null) {
  console.log(`Picked vertex ${result.vertexIndex} on ${result.surfaceId}`);
}

Constructors

Constructor

ts
new GPUPicker(renderer): GPUPicker;

Defined in: src/utils/GPUPicker.ts:236

Parameters

renderer

WebGLRenderer

Returns

GPUPicker

Methods

addSurface()

ts
addSurface(id, mesh): void;

Defined in: src/utils/GPUPicker.ts:267

Add a surface mesh to the picker. Creates a pick mesh with vertex ID attributes.

Parameters

id

string

mesh

Mesh

Returns

void


removeSurface()

ts
removeSurface(id): boolean;

Defined in: src/utils/GPUPicker.ts:302

Remove a surface from the picker.

Parameters

id

string

Returns

boolean


syncTransforms()

ts
syncTransforms(): void;

Defined in: src/utils/GPUPicker.ts:318

Update pick meshes to match their original mesh transforms. Call this before picking if meshes have moved.

Returns

void


setEnabled()

ts
setEnabled(enabled): void;

Defined in: src/utils/GPUPicker.ts:330

Enable or disable GPU picking.

Parameters

enabled

boolean

Returns

void


isEnabled()

ts
isEnabled(): boolean;

Defined in: src/utils/GPUPicker.ts:337

Check if GPU picking is enabled.

Returns

boolean


setThrottleMs()

ts
setThrottleMs(ms): void;

Defined in: src/utils/GPUPicker.ts:344

Set the minimum time between pick operations (for throttling).

Parameters

ms

number

Returns

void


pick()

ts
pick(
   cssX, 
   cssY, 
   camera, 
   rect?): GPUPickResult;

Defined in: src/utils/GPUPicker.ts:357

Perform GPU picking at the given screen coordinates.

Parameters

cssX

number

X coordinate in CSS pixels (from event.clientX)

cssY

number

Y coordinate in CSS pixels (from event.clientY)

camera

Camera

The camera to pick from

rect?

DOMRect

Optional bounding rect of the canvas (auto-detected if not provided)

Returns

GPUPickResult

Pick result with surface ID, vertex index, and world position


pickNDC()

ts
pickNDC(
   ndcX, 
   ndcY, 
   camera): GPUPickResult;

Defined in: src/utils/GPUPicker.ts:475

Pick using normalized device coordinates (-1 to 1).

Parameters

ndcX

number

ndcY

number

camera

Camera

Returns

GPUPickResult


isSupported()

ts
static isSupported(renderer): boolean;

Defined in: src/utils/GPUPicker.ts:501

Check if the renderer supports GPU picking.

Parameters

renderer

WebGLRenderer

Returns

boolean


dispose()

ts
dispose(): void;

Defined in: src/utils/GPUPicker.ts:509

Dispose of all resources.

Returns

void

Released under the MIT License.