Raycasting is a fundamental technique in 3D graphics used to determine if a ray (an imaginary line extending infinitely in one direction) intersects with any objects in a 3D scene. In Three.js, this is commonly used for enabling user interaction, such as detecting mouse clicks or hovers on 3D objects.
The process generally involves:
THREE.Raycaster: The object that performs the actual raycasting calculations.THREE.Vector2: Used to store the normalized device coordinates (NDC) of your mouse position. NDC range from -1 to +1 across the viewport.const raycaster = new THREE.Raycaster();
const mouse = new THREE.Vector2();
Attach an event listener (e.g., mousemove or click) to your renderer's DOM element to update the mouse vector whenever the mouse moves or is clicked. You need to convert the screen coordinates to NDC.
function onMouseMove(event) {
// Calculate mouse position in normalized device coordinates (-1 to +1)
mouse.x = (event.clientX / window.innerWidth) * 2 - 1;
mouse.y = -(event.clientY / window.innerHeight) * 2 + 1;
}
window.addEventListener('mousemove', onMouseMove, false);
In your animation loop (or within your event handler), you'll update the raycaster and check for intersections.
// Update the picking ray with the camera and mouse position
raycaster.setFromCamera(mouse, camera);
// Calculate objects intersecting the picking ray
const intersects = raycaster.intersectObjects(scene.children, true); // 'true' for recursive check
if (intersects.length > 0) {
const intersectedObject = intersects[0].object;
console.log('Intersected object:', intersectedObject.name || intersectedObject.uuid);
// Perform actions, e.g., change color, display info, select object
} else {
// No intersection
}
This example demonstrates how to change the color of a cube when you hover over it and log a message when you click it.