SELECT MULTIPLE 3D OBJECTS
WITH A RAYCASTER
By Shane Brumback · · Updated · Free Three.js tutorial · Level 1
To select multiple 3D objects in Three.js, cast a ray from the mouse with a Raycaster, keep every hit mesh in a Set instead of replacing the last one, and project object positions to the screen to pick everything inside a dragged box. Selected shapes then lift up and glow.
A beginner walkthrough of Raycaster, shift-click multi-select, drag box selection and an eased glow highlight in Three.js. The scene behind this page is the live demo, and the full copy-paste source is below.
LIVE DEMO
The shapes lighting up behind this page are a live Three.js scene. Launch the full-screen demo to click a shape, shift-click to add more, or drag a box to grab a whole group. Right-drag orbits, scroll zooms, Ctrl+A selects all and Delete clears. Launch asks for a free Google sign-in first. Press Esc or Exit to return.
Free Google sign-in · Click / Shift-click / Drag to select · Right-drag to orbit · Esc to exit
READY FOR ENDLESS TERRAIN?
This Level 1 demo picks shapes on a flat table. The Level 3 premium Terrain LOD tutorial streams endless hills with sand, grass, rock and snow blended in an onBeforeCompile shader, with LOD skirts, worker chunks and grass that follows you. Members unlock the full code; sign in to reveal it.
How Raycaster Selection Works
A Raycaster shoots an invisible line from the camera through the point you clicked and reports every Mesh it passes through, nearest first. Selecting one object is just taking the first hit. Selecting many means remembering earlier picks in a Set, toggling with Shift, and adding a drag rectangle that checks where each object lands on the screen.
New to picking? Start with selecting a single 3D cube with a raycaster, then try raycasting against Minecraft models. The glossy look comes from the animating simple 3D objects tutorial.
How It Works
- Set up the scene, camera and renderer. We make a dark 3D world, a camera that looks down at a table of shapes, and a renderer with soft shadows.
- Free the left mouse button for selecting. We let the camera orbit with the right mouse button or two fingers, so a left click or one-finger drag is free for picking.
- Light the scene and add a reflective floor. We add reflections, a soft sky light and a shadow-casting key light, then a shiny floor with a cyan grid.
- Build a grid of pickable shapes with a halo. We lay out 24 glossy shapes in rows and give each one a hidden glowing shell that appears when it is selected.
- Track the selection in a Set. We keep a list of what is selected so adding, removing and clearing are each one line.
- Cast a ray from the mouse. We turn the click position into a laser beam from the camera and ask which shape it hits first.
- Select everything inside a drag box. We work out where each shape sits on the screen and keep the ones inside the rectangle you dragged.
- Handle click, shift-click and drag. A plain click picks one shape, shift-click adds or removes, and a drag draws a box; on phones taps always add.
- Animate the highlight. Selected shapes smoothly lift up, spin faster, glow cyan and show a pulsing outline.
The detailed code breakdown and the full copy-paste file are below. Every step is an exact excerpt of the full file, and the full file is the same code as the live demo.
Want selected objects to do more? Make them follow another object, or turn the shapes into buttons in the interactive 3D menu tutorial.
Frequently Asked Questions
How do you select multiple objects with a Three.js Raycaster?
Keep the selected meshes in a Set. On each click, cast a ray with raycaster.setFromCamera and intersectObjects, then add the hit mesh to the Set, or toggle it when Shift is held, instead of replacing the old selection.
How does box or drag selection work in Three.js?
Record where the drag starts and ends, then project each object position to the screen with Vector3.project(camera). Any object whose screen point falls inside the rectangle gets selected.
How do you highlight a selected 3D object?
Raise the emissive color of its material and show a slightly larger copy of the mesh with a BackSide basic material behind it as an outline. Easing a glow value each frame makes the highlight fade in smoothly.
Why does the raycaster need normalized device coordinates?
setFromCamera expects the pointer as x and y from -1 to 1 across the canvas, so you divide clientX by the width, times 2, minus 1, and flip the y axis.
Can I orbit the camera and still click to select?
Yes. Set OrbitControls mouseButtons LEFT to null and RIGHT to ROTATE so left click and drag are free for selecting, and use two fingers to orbit on touch screens.
Why do I need to sign in to launch the live demo?
The tutorial and its full code are free to read. The full-screen Live Demo asks for a free Google sign-in first, the same gate used across the site, so I know who is learning from these tutorials.
Play It and Keep Learning
Click-to-select and raycast picking like this power Super Soldier Battle, free in your browser. Browse every Three.js tutorial, read the articles, or search the site.
Need Three.js interaction or WebGL work? I take freelance and contract work: contact me.
Play Game