Raycast Picking

Three.js lessons
/

Raycast Picking

Intermediate

Animation & interaction

Lesson 10 of 15

Turn a pointer position into a camera ray and identify the nearest mesh.

Hover over three shapes to highlight the object intersected by a Raycaster.

How this scene works

Pointer coordinates begin in browser pixels. Subtract the canvas bounding rectangle, divide by its dimensions, then map X and Y into normalized device coordinates from −1 to 1, flipping Y. setFromCamera builds the ray; intersectObjects returns hits nearest-first. Only the authored pickable objects are tested, not helpers or lights. The listener is removed with onCleanup so rerunning the code does not accumulate event handlers.

Raycast Picking

Ready to run

Helper snippet · Three.js 0.183.1 · WebGL

Loading draft

1 files · local only

Ctrl/Cmd+Enter: run · Ctrl/Cmd+F: find · Tab: leave editor
Run your code to start the scene.
Drag to orbit, scroll to zoom, right-drag to pan.
Drag: orbit · Scroll: zoom · Right-drag: pan

Make it your own

Replace pointermove with pointerdown so tapping or clicking selects a shape. Give each shape a distinct highlight color and log the selected object's name.

Change one thing, run the scene, and compare. Pause animation to inspect the result; reset the example to return to its original code and files.

FAQ