HTML MASTER CLASS /// LEARN TAGS /// BUILD STRUCTURE /// SEMANTIC WEB /// HTML MASTER CLASS /// LEARN TAGS ///
Total XP: 0|💻 threejs XP: 0

Raycasting & Interaction in Three.js 3D WebGL

Learn about Raycasting & Interaction in this comprehensive Three.js 3D WebGL tutorial. Explore the THREE.Raycaster class and how React Three Fiber simplifies 3D events.

LOADING ENGINE...

Skill Matrix

UNLOCK NODES BY LEARNING NEW TAGS.

System Hub

Core AI logic.

Quick Quiz //

What is the primary danger of ignoring this AI concept?


Listen up. If you're building modern applications, understanding Raycasting & Interaction in Three.js 3D WebGL is non-negotiable. This is where simple logic turns into intelligent behavior.

1Threejs raycaster Part 1

So far, our 3D worlds have been purely visual. But what if you want to click on a 3D object to select it or trigger an event?

Look, here's the reality in production: if you don't fully grasp this, you're going to introduce massive bottlenecks or incorrect predictions. I've seen junior devs deploy models that hallucinate wildly because they missed this exact nuance. It's all about understanding the data pipeline and model parameters.

Let's break down the code. Notice how we're structuring this logic. We aren't just hacking things together; we're designing for scale and accuracy. If you mess up the inference loop or create new tensors every frame here, the runtime won't optimize it, and you'll get massive memory leaks. Always follow ML engineering best practices.

+
// 🖱️ Adding interactivity
localhost:3000
Browser Preview
WebGL Output
3D Scene rendered. Objects: 4, Draw Calls: Optimized.

2Threejs raycaster Part 2

You can

Look, here's the reality in production: if you don't fully grasp this, you're going to introduce massive bottlenecks or incorrect predictions. I've seen junior devs deploy models that hallucinate wildly because they missed this exact nuance. It's all about understanding the data pipeline and model parameters.

Let's break down the code. Notice how we're structuring this logic. We aren't just hacking things together; we're designing for scale and accuracy. If you mess up the inference loop or create new tensors every frame here, the runtime won't optimize it, and you'll get massive memory leaks. Always follow ML engineering best practices.

+
const raycaster = new THREE.Raycaster();
const mouse = new THREE.Vector2();
localhost:3000
Browser Preview
WebGL Output
3D Scene rendered. Objects: 4, Draw Calls: Optimized.

3Threejs raycaster Part 3

Here is how it works: you convert the mouse

Look, here's the reality in production: if you don't fully grasp this, you're going to introduce massive bottlenecks or incorrect predictions. I've seen junior devs deploy models that hallucinate wildly because they missed this exact nuance. It's all about understanding the data pipeline and model parameters.

Let's break down the code. Notice how we're structuring this logic. We aren't just hacking things together; we're designing for scale and accuracy. If you mess up the inference loop or create new tensors every frame here, the runtime won't optimize it, and you'll get massive memory leaks. Always follow ML engineering best practices.

+
window.addEventListener('mousemove', (event) => {
  mouse.x = (event.clientX / window.innerWidth) * 2 - 1;
  mouse.y = -(event.clientY / window.innerHeight) * 2 + 1;
});
localhost:3000
Browser Preview
WebGL Output
3D Scene rendered. Objects: 4, Draw Calls: Optimized.

4Threejs raycaster Part 4

Then, you tell the Raycaster to shoot a ray from the camera through that mouse coordinate. It returns an array of all objects the ray hit!

Look, here's the reality in production: if you don't fully grasp this, you're going to introduce massive bottlenecks or incorrect predictions. I've seen junior devs deploy models that hallucinate wildly because they missed this exact nuance. It's all about understanding the data pipeline and model parameters.

Let's break down the code. Notice how we're structuring this logic. We aren't just hacking things together; we're designing for scale and accuracy. If you mess up the inference loop or create new tensors every frame here, the runtime won't optimize it, and you'll get massive memory leaks. Always follow ML engineering best practices.

+
raycaster.setFromCamera(mouse, camera);
const intersects = raycaster.intersectObjects(scene.children);

if (intersects.length > 0) {
  console.log('Hit:', intersects[0].object);
}
localhost:3000
Browser Preview
WebGL Output
3D Scene rendered. Objects: 4, Draw Calls: Optimized.

5Threejs raycaster Part 5

Doing this in vanilla Three.js requires a lot of boilerplate. React Three Fiber handles ALL of this for you automatically!

Look, here's the reality in production: if you don't fully grasp this, you're going to introduce massive bottlenecks or incorrect predictions. I've seen junior devs deploy models that hallucinate wildly because they missed this exact nuance. It's all about understanding the data pipeline and model parameters.

Let's break down the code. Notice how we're structuring this logic. We aren't just hacking things together; we're designing for scale and accuracy. If you mess up the inference loop or create new tensors every frame here, the runtime won't optimize it, and you'll get massive memory leaks. Always follow ML engineering best practices.

+
// In vanilla: 20 lines of code.
// In R3F: Just use standard React events!
localhost:3000
Browser Preview
WebGL Output
3D Scene rendered. Objects: 4, Draw Calls: Optimized.

6Threejs raycaster Part 6

In R3F, you can attach standard React event listeners like onClick, onPointerOver, and onPointerOut directly to your meshes!

Look, here's the reality in production: if you don't fully grasp this, you're going to introduce massive bottlenecks or incorrect predictions. I've seen junior devs deploy models that hallucinate wildly because they missed this exact nuance. It's all about understanding the data pipeline and model parameters.

Let's break down the code. Notice how we're structuring this logic. We aren't just hacking things together; we're designing for scale and accuracy. If you mess up the inference loop or create new tensors every frame here, the runtime won't optimize it, and you'll get massive memory leaks. Always follow ML engineering best practices.

+
<mesh 
  onClick={(e) => console.log('Clicked!')} 
  onPointerOver={(e) => setHovered(true)}
  onPointerOut={(e) => setHovered(false)}
>
  <boxGeometry />
</mesh>
localhost:3000
Browser Preview
WebGL Output
3D Scene rendered. Objects: 4, Draw Calls: Optimized.

7Threejs raycaster Part 7

In React Three Fiber, if you want an action to occur when the user clicks on a 3D sphere, which prop do you use on the <mesh> tag?

Look, here's the reality in production: if you don't fully grasp this, you're going to introduce massive bottlenecks or incorrect predictions. I've seen junior devs deploy models that hallucinate wildly because they missed this exact nuance. It's all about understanding the data pipeline and model parameters.

Let's break down the code. Notice how we're structuring this logic. We aren't just hacking things together; we're designing for scale and accuracy. If you mess up the inference loop or create new tensors every frame here, the runtime won't optimize it, and you'll get massive memory leaks. Always follow ML engineering best practices.

+
<mesh ???={() => console.log('Boom')} />
localhost:3000
Browser Preview
WebGL Output
3D Scene rendered. Objects: 4, Draw Calls: Optimized.

8Threejs raycaster Part 8

Let

Look, here's the reality in production: if you don't fully grasp this, you're going to introduce massive bottlenecks or incorrect predictions. I've seen junior devs deploy models that hallucinate wildly because they missed this exact nuance. It's all about understanding the data pipeline and model parameters.

Let's break down the code. Notice how we're structuring this logic. We aren't just hacking things together; we're designing for scale and accuracy. If you mess up the inference loop or create new tensors every frame here, the runtime won't optimize it, and you'll get massive memory leaks. Always follow ML engineering best practices.

+
const InteractiveBox = (props) => {
  const [hovered, setHover] = React.useState(false);
  const [active, setActive] = React.useState(false);
  
  return (
    <mesh
      {...props}
      scale={active ? 1.5 : 1}
      onClick={() => setActive(!active)}
      onPointerOver={() => setHover(true)}
      onPointerOut={() => setHover(false)}
    >
      <boxGeometry args={[1, 1, 1]} />
      <meshStandardMaterial color={hovered ? 'hotpink' : 'orange'} />
    </mesh>
  );
};

const App = () => {
  return (
    <Canvas camera={{ position: [0, 0, 5] }}>
      <ambientLight intensity={0.5} />
      <directionalLight position={[10, 10, 10]} intensity={1} />
      <InteractiveBox position={[-1.2, 0, 0]} />
      <InteractiveBox position={[1.2, 0, 0]} />
    </Canvas>
  );
};

render(<App />);
localhost:3000
Browser Preview
WebGL Output
3D Scene rendered. Objects: 4, Draw Calls: Optimized.

9Threejs raycaster Part 9

R3F passes an

Look, here's the reality in production: if you don't fully grasp this, you're going to introduce massive bottlenecks or incorrect predictions. I've seen junior devs deploy models that hallucinate wildly because they missed this exact nuance. It's all about understanding the data pipeline and model parameters.

Let's break down the code. Notice how we're structuring this logic. We aren't just hacking things together; we're designing for scale and accuracy. If you mess up the inference loop or create new tensors every frame here, the runtime won't optimize it, and you'll get massive memory leaks. Always follow ML engineering best practices.

+
<mesh onClick={(e) => {
  e.stopPropagation(); // Stop raycasting
  console.log('Hit front object only!');
}} />
localhost:3000
Browser Preview
WebGL Output
3D Scene rendered. Objects: 4, Draw Calls: Optimized.

10Threejs raycaster Part 10

Amazing! You can now build fully interactive 3D interfaces, games, and data visualizations. We are reaching the advanced stages!

Look, here's the reality in production: if you don't fully grasp this, you're going to introduce massive bottlenecks or incorrect predictions. I've seen junior devs deploy models that hallucinate wildly because they missed this exact nuance. It's all about understanding the data pipeline and model parameters.

Let's break down the code. Notice how we're structuring this logic. We aren't just hacking things together; we're designing for scale and accuracy. If you mess up the inference loop or create new tensors every frame here, the runtime won't optimize it, and you'll get massive memory leaks. Always follow ML engineering best practices.

+
// 🎯 Interactivity complete!
localhost:3000
Browser Preview
WebGL Output
3D Scene rendered. Objects: 4, Draw Calls: Optimized.

?Frequently Asked Questions

Pascual Vila

Pascual Vila

Frontend Instructor // Code Syllabus

Continue Learning