Loading skill
Install any skill in seconds. Free to start, no credit card required.
Get Started Free →Covers Three.js user interaction: raycasting for object picking, camera controls, and mouse and touch input handling.
Use this skill ↓100% 95%−5 ptsof test cases passed
Passed 21 of 22 test cases with the skill, and 22 of 22 without it.
3 of 3 safety checks passed
Not used yet
import * as THREE from "three";
import { OrbitControls } from "three/addons/controls/OrbitControls.js";
// Camera controls
const controls = new OrbitControls(camera, renderer.domElement);
controls.enableDamping = true;
// Raycasting for click detection
const raycaster = new THREE.Raycaster();
const mouse = new THREE.Vector2();
function onClick(event) {
mouse.x = (event.clientX / window.innerWidth) * 2 - 1;
mouse.y = -(event.clientY / window.innerHeight) * 2 + 1;
raycaster.setFromCamera(mouse, camera);
const intersects = raycaster.intersectObjects(scene.children);
if (intersects.length > 0) {
console.log("Clicked:", intersects[0].object);
}
}
window.addEventListener("click", onClick);
const raycaster = new THREE.Raycaster();
// From camera (mouse picking)
raycaster.setFromCamera(mousePosition, camera);
Skills that do similar work, with their benchmark result.
Next to this one in the catalogue, plus more from the same author.