🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEthreejs

threejs Documentation

📖 INDEX

# scene
# object3d
# clock
# raycaster
# layers
# eventdispatcher
# bufferattribute
# buffergeometry
# perspectivecamera
# orthographiccamera
# cubecamera
# arraycamera
# stereocamera
# boxgeometry
# spheregeometry
# planegeometry
# cylindergeometry
# conegeometry
# torusgeometry
# torusknotgeometry
# circlegeometry
# ringgeometry
# shapegeometry
# extrudegeometry
# lathegeometry
# tubegeometry
# polyhedrongeometry
# icosahedrongeometry
# octahedrongeometry
# tetrahedrongeometry
# edgesgeometry
# wireframegeometry
# meshbasicmaterial
# meshlambertmaterial
# meshphongmaterial
# meshstandardmaterial
# meshphysicalmaterial
# meshtoonmaterial
# meshnormalmaterial
# meshmatcapmaterial
# meshdepthmaterial
# linebasicmaterial
# linedashedmaterial
# pointsmaterial
# spritematerial
# shadermaterial
# rawshadermaterial
# shadowmaterial
# ambientlight
# directionallight
# pointlight
# spotlight
# hemispherelight
# rectarealight
# textureloader
# gltfloader
# objloader
# fbxloader
# cubetextureloader
# audioloader
# fontloader
# fileloader
# imageloader
# objectloader
# webglrenderer
# webglrendertarget
# webglcuberendertarget
# css2drenderer
# css3drenderer
# svgrenderer
# vector2
# vector3
# vector4
# matrix3
# matrix4
# quaternion
# euler
# color
# mathutils
# box2
# box3
# sphere
# ray
# plane
# line3
# frustum
# axeshelper
# gridhelper
# boxhelper
# camerahelper
# directionallighthelper
# pointlighthelper
# spotlighthelper
# hemispherelighthelper
# skeletonhelper
# polargridhelper
# arrowhelper
# planehelper
# orbitcontrols
# flycontrols
# firstpersoncontrols
# pointerlockcontrols
# transformcontrols
# trackballcontrols
# dragcontrols
# deviceorientationcontrols
# mapcontrols
# texture
# datatexture
# canvastexture
# videotexture
# cubetexture
# datatexture3d
# compressedtexture
# depthtexture
# animationmixer
# animationclip
# keyframetrack
# animationaction
# propertybinding
# animationobjectgroup
# mesh
# points
# line
# lineloop
# linesegments
# group
# sprite
# bone
# skeleton
# skinnedmesh
# lod
# instancedmesh
LOADING ENGINE...

Raycaster

THREE.JS REFERENCE // raycaster

Used for mouse picking (working out what objects in the 3d space the mouse is over).

Syntax

const raycaster = new THREE.Raycaster();

Visual Explanation

THREE.JS RENDERING PIPELINE [Scene Graph] | +-- Mesh (Geometry + Material) | | | +-- Raycaster() | [WebGL Renderer] | +-- Vertex Shader -> Fragment Shader -> Screen

Performance

GPU / CPU Overhead

In 3D graphics, instancing and managing GPU memory is critical. Mismanaging Raycaster can lead to an excess of draw calls or memory leaks, severely dropping the frames per second (FPS).

Examples

Example 01Basic Usage
const raycaster = new THREE.Raycaster();

Real-world Use Cases

Interactive 3D Environments
import * as THREE from 'three';

const scene = new THREE.Scene();
// Using Raycaster in a scene setup
const instance = new Raycaster();
scene.add(instance);

Common Mistakes

X Re-instantiating Geometries, Materials or Raycaster inside the render loop.

Always instantiate objects outside the render loop and reuse them or update their properties dynamically.

// Bad
function animate() {
    const item = new Raycaster(); // Memory leak!
    requestAnimationFrame(animate);
}

// Good
const item = new Raycaster();
function animate() {
    item.rotation.y += 0.01;
    requestAnimationFrame(animate);
}

When NOT to use it

Scenario

When standard 2D Canvas or simple DOM elements suffice.

Alternative

Three.js and WebGL bring significant overhead. Don't use them for basic UI elements or static 2D images.

Differences

FunctionDifference
Raw WebGLWriting raw WebGL requires hundreds of lines of boilerplate just to draw a triangle. Raycaster abstracts the underlying matrix math and buffer handling into a friendly API.

Best Practices

  • Consult the official Three.js documentation for deeper understanding.
  • Be mindful of performance when creating many objects or geometries.

Interview Question

How do you optimize a scene that heavily relies on Raycaster?

Hint: Think about InstancedMesh, merged geometries, and reducing draw calls.

You should aim to minimize draw calls by merging geometries if they share the same material, using InstancedMesh for identical objects, and properly calling .dispose() on materials/geometries when they are no longer needed to free GPU memory.

Exercises

HardCreate a scene utilizing Raycaster while keeping the FPS above 60.
View Solution
const instance = new Raycaster();
// Proper disposal logic
window.addEventListener('unload', () => {
    instance.dispose();
});

Related Functions

THREE.SceneTHREE.WebGLRendererTHREE.PerspectiveCameraTHREE.Mesh