Skip to main content
Version: 0.10.0

UeRaycaster

A raycaster utility for performing ray-object intersection tests in 3D space.
Supports raycasting against objects and their children, with parameters customizable per object type.
Works with both perspective and orthographic cameras.

Constructor​

UeRaycaster(origin = vec3_create(), direction = vec3_create(0, 0, -1), near = 0, far = infinity)

Properties​

PropertyTypeDescription
rayUeRayRay used for intersection testing
nearnumberNear clipping distance for intersections
farnumberFar clipping distance for intersections
cameraObjectCamera reference used for coordinate conversion
layersObjectObjects must share at least one layer with the raycaster (layer 0 is enabled by default)

Methods​

MethodReturnsDescription
set(origin, direction)selfSets the ray origin and direction
setFromCamera(camera)selfSets the ray based on mouse coordinates and camera (supports both perspective and orthographic)
intersectObject(object, recursive = true, sort = true, hits = [])ArrayIntersects ray with an object and optionally its descendants recursively, returning sorted hits by distance
intersectObjects(objects, recursive = true, sort = true, hits = [])ArrayIntersects ray with multiple objects, optionally recursive, returning sorted hits by distance

Camera Support​

The raycaster works differently depending on the camera type:

Perspective Camera​

  • Ray origin is at the camera position
  • Ray direction is calculated from camera position to the mouse point in world space
  • Rays diverge from the camera (like a cone)

Orthographic Camera​

  • Ray origin is calculated from mouse position on the viewing plane
  • Ray direction is the camera's forward vector
  • All rays are parallel (characteristic of orthographic projection)

Usage example​

With Perspective Camera​

const camera = new UePerspectiveCamera();
const raycaster = new UeRaycaster();
raycaster.setFromCamera(camera);
const hits = raycaster.intersectObjects(scene.children);
if (array_length(hits) > 0) {
show_debug_message("Hit object:", hits[0].object);
}

With Orthographic Camera​

const camera = new UeOrthographicCamera();
const raycaster = new UeRaycaster();
raycaster.setFromCamera(camera);
const hits = raycaster.intersectObjects(scene.children);
// Raycasting works the same way, but uses parallel rays
if (array_length(hits) > 0) {
show_debug_message("Hit object:", hits[0].object);
}