Utility to help with raycasting (determining what objects in the 3d space the mouse is over).

// set our main GPUCurtains instance
const gpuCurtains = new GPUCurtains({
container: '#canvas' // selector of our WebGPU canvas container
})

// set the GPU device
// note this is asynchronous
await gpuCurtains.setDevice()

// create a mesh with a box geometry
// will use the normals colors as default shading
const mesh = new Mesh(gpuCurtains, {
label: 'My mesh',
geometry: new BoxGeometry(),
})

const raycaster = new Raycaster(gpuCurtains)

window.addEventListener('mousemove', (e) => {
raycaster.setFromMouse(e)

const intersections = raycaster.intersectObject(mesh)

if(intersections.length) {
// the mouse is hovering the mesh
mesh.scale.set(1.25)
} else {
// the mouse is not hovering the mesh
mesh.scale.set(1)
}
})

Constructors

Properties

type: string

The type of the Raycaster.

renderer: CameraRenderer

The CameraRenderer used.

camera: Camera

The Camera used.

pointer: Vec2

Pointer position in normalized device coordinates (in the [-1, 1] range).

ray: { origin: Vec3; direction: Vec3 }

Ray used to test for intersections.

Type declaration

  • origin: Vec3

    Origin of the ray in world space (Camera position).

  • direction: Vec3

    Normalized direction of the ray in world space.

Methods

  • Set the pointer normalized device coordinates (in the [-1, 1] range).

    Parameters

    • x: number = 0

      input position along the X axis in the [-1, 1] range where -1 represents the left edge and 1 the right edge.

    • y: number = 0

      input position along the Y axis in the [-1, 1] range where -1 represents the bottom edge and 1 the top edge.

    Returns void

  • Ray-Triangle Intersection with Möller–Trumbore Algorithm.

    Parameters

    • intersectionPoint: Vec3

      Vec3 to store the intersection point if any.

    Returns boolean

    • Whether an intersection point has been found or not.