getRayFromCoordinate

getRayFromCoordinate

Converts normalized screen coordinates to a 3D ray from the camera.

Signature

getRayFromCoordinate(x: number, y: number): Promise<Ray>

Parameters

Parameter
Type
Required
Description

x

number

Yes

Normalized X coordinate (0 = left, 1 = right)

y

number

Yes

Normalized Y coordinate (0 = top, 1 = bottom)

Returns

Promise<Ray> — Ray from camera through the screen point.

type Ray = {
  start: Vector3;    // Ray origin (camera position)
  direction: Vector3; // Normalized direction vector
};

Usage

// Get ray from center of canvas
const ray = await api.getRayFromCoordinate(0.5, 0.5);
console.log("Ray start:", ray.start);
console.log("Ray direction:", ray.direction);

// Get ray from click position
iframe.addEventListener("click", async (e) => {
  const rect = iframe.getBoundingClientRect();
  const x = (e.clientX - rect.left) / rect.width;
  const y = (e.clientY - rect.top) / rect.height;
  const ray = await api.getRayFromCoordinate(x, y);
  
  // Use with raycastObject
  const hit = await api.raycastObject("MyObject", ray);
});

Notes

  • Coordinates are normalized 0-1, not pixels

  • (0, 0) = top-left corner, (1, 1) = bottom-right corner

  • (0.5, 0.5) = center of canvas

  • Useful for custom click handling with raycastObject()

Last updated