threejs-core-raycaster

Implement mouse picking, hover detection, and object selection with the Three.js Raycaster API.

1|Updated Aug 20, 2023
One-click install
npx skills add https://github.com/imanlangaran/mini-projects --skill threejs-core-raycaster-imanlangaran
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: threejs-core-raycaster
Source: https://github.com/imanlangaran/mini-projects/tree/main/react/three-gsap/.claude/skills/threejs-core-raycaster
Command: npx skills add https://github.com/imanlangaran/mini-projects --skill threejs-core-raycaster-imanlangaran

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill requires three, and includes references (resource) components.

What problem does it solve? Implementing mouse picking and object selection in Three.js often leads to performance problems and subtle bugs, such as raycasting on every mousemove event, traversing the entire scene graph, or miscomputing NDC coordinates on non-fullscreen canvases. This Skill provides correct patterns and API references for the Three.js Raycaster so these mistakes are avoided from the start. ## Core Features & Use Cases - Mouse Picking & Hover Detection: Correct NDC conversion, setFromCamera usage, and throttled raycasting inside the render loop. - InstancedMesh Picking: Retrieve instanceId from intersections and update per-instance colors with proper needsUpdate handling. - Layer-Based Filtering & Performance: Use raycaster.layers, flat selectable arrays, and reusable optionalTarget arrays to keep raycasting fast. - Use Case: You are building a 3D product configurator where users click meshes to select parts. This Skill guides you to maintain a flat array of selectable objects, convert mouse coordinates with getBoundingClientRect, and reuse the intersections array each frame. ## Quick Start Ask the AI to implement click-to-select object picking in your Three.js scene using the Raycaster with proper throttling and layer filtering.

Frequently Asked Questions about threejs-core-raycaster

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I implement mouse click picking in Three.js?

Convert mouse coordinates to normalized device coordinates using getBoundingClientRect, call raycaster.setFromCamera with the camera, then call intersectObjects on a flat array of selectable objects. Check that the returned intersections array is non-empty before accessing the first hit.

How to detect hover over 3D objects in Three.js?

Update mouse NDC coordinates in a pointermove listener and set a needsRaycast flag, then perform the raycast inside the requestAnimationFrame render loop. This throttles intersection tests to the display refresh rate instead of the higher mouse event rate.

How do I pick individual instances of an InstancedMesh?

Raycast against the InstancedMesh and read the instanceId property from the intersection result. Always verify instanceId is not undefined before using it, since regular Mesh intersections do not include this property.

Why is my Three.js raycast missing the object I click?

The most common cause is incorrect NDC conversion on a non-fullscreen canvas; use getBoundingClientRect instead of window.innerWidth and innerHeight. Also verify the object is on a layer enabled in raycaster.layers and that the direction vector is normalized when using raycaster.set.

Why does raycasting cause frame drops in my Three.js scene?

Raycasting on every mousemove event or against the entire scene with recursive true is expensive. Throttle raycasts to the render loop, maintain a flat array of selectable objects with recursive false, and reuse the intersections array via the optionalTarget parameter.

Does Three.js Raycaster work with Sprite objects?

Yes, but you must set raycaster.camera before intersecting Sprites. Sprites are camera-facing billboards, and the raycaster needs the camera reference to compute their screen-space bounding boxes correctly.