@pixi/3d
    Preparing search index...

    Class Ray

    A half-line that starts at origin and runs along direction, for picking and line-of-sight tests

    View3D#screenToRay builds one through a pointer position, in world space. Test a ray against shapes in its own space; Ray#applyMatrix4 moves it into a mesh's local space. Keep direction unit length, and call direction.normalize() after changing it by hand; the distance, closest-point and sphere methods give wrong results otherwise.

    const hit = new Vector3();

    view3d.on('pointertap', (e) => {
    const ray = view3d.screenToRay(e.global);

    if (ray.intersectPlane(ground, hit)) unit.moveTo(hit);
    });
    Index
    • Creates a new Ray

      Left out, the ray starts at the origin and points down -Z. Vectors passed in become the ray's own; they are not copied.

      Parameters

      • Optionalorigin: Vector3

        The origin of the ray

      • Optionaldirection: Vector3

        The unit-length direction of the ray

      Returns Ray

    direction: Vector3

    The unit-length direction the ray runs in

    sightLine.direction.copyFrom(target.position).subtract(sightLine.origin).normalize();
    
    origin: Vector3

    The point the ray starts from

    muzzle.getGlobalPosition(shotRay.origin);
    
    type: SHAPE_PRIMITIVE_3D = 'ray'

    Names the shape without an instanceof check, like the type on pixi's 2D shapes

    'ray'
    
    • Transforms this ray by an affine matrix

      The direction comes out unit length, so a t measured along the new ray is in the new space's units: under scale, compare hits as points, not as t values.

      matrix must be affine (bottom row [0, 0, 0, 1]), as a Container3D#worldTransform always is and a projection matrix isn't.

      Parameters

      • matrix: Matrix4

        The affine matrix to transform by

      • out: Ray = ...

        Target to write the result into instead of mutating this ray

      Returns this

      The result ray for chaining

      const worldToLocal = new Matrix4().getInverse(mesh.worldTransform);
      const localRay = pointerRay.applyMatrix4(worldToLocal, new Ray());
      const overMesh = localRay.intersectsBox(mesh.geometry.bounds3D);
    • Writes the point origin + direction * t into out

      With a unit-length direction, t is the distance from origin.

      Parameters

      • t: number

        The distance along the ray

      • Optionalout: Vector3

        Optional vector to write into; a new Vector3 when omitted

      Returns Vector3

      The point along the ray

      const reticle = new Vector3();

      aimRay.at(20, reticle);
    • Creates a new Ray with the same origin and direction

      Returns Ray

      The new ray

      const reflected = incoming.clone();
      
    • Writes the closest point on this ray to point into out

      Points behind origin give origin. Requires a unit-length direction.

      Parameters

      • point: PointData3D

        The point to measure from

      • Optionalout: Vector3

        Optional vector to write into; a new Vector3 when omitted

      Returns Vector3

      The closest point on this ray

      const nearest = new Vector3();

      aimRay.closestPointToPoint(enemy.position, nearest);
    • Copies ray into this ray

      Parameters

      • ray: Ray

        The ray to copy from

      Returns this

      This ray for chaining

      lastPointerRay.copyFrom(pointerRay);
      
    • Copies this ray into target and returns it

      Type Parameters

      Parameters

      • target: T

        The ray to copy into

      Returns T

      The given target

      pointerRay.copyTo(lastPointerRay);
      
    • Returns the squared Euclidean distance from point to the closest point on this ray

      Cheaper than Ray#distanceToPoint; compare it against a squared radius. Requires a unit-length direction.

      Parameters

      Returns number

      The squared distance to the closest point on this ray

      const locked = enemies.find((enemy: Container3D) => aimRay.distanceSqToPoint(enemy.position) < 0.25);
      
    • Returns the Euclidean distance from point to the closest point on this ray

      Requires a unit-length direction.

      Parameters

      Returns number

      The distance to the closest point on this ray

      const nearMiss = aimRay.distanceToPoint(target.position) < 0.5;
      
    • Returns true when origin and direction match ray exactly (no epsilon tolerance)

      Parameters

      • ray: Ray

        The ray to compare with

      Returns boolean

      Whether the origin and direction match

      if (!pointerRay.equals(lastPointerRay)) updateHover(pointerRay);
      
    • Intersects this ray with box

      Works with a direction of any length.

      Parameters

      • box: Box

        The box to intersect

      • Optionalout: Vector3

        Optional vector to write into; a new Vector3 when omitted

      Returns Vector3 | null

      The entry point (or the exit point from inside), or null when the ray misses

      const hit = new Vector3();

      if (localRay.intersectBox(mesh.geometry.bounds3D, hit)) decal.position.copyFrom(hit);

      Ray#intersectBoxT For the distance to the same hit, without the point.

    • Intersects this ray with box and returns the distance to the hit rather than the point

      Same hit as Ray#intersectBox; Ray#at turns t into the point. With a unit-length direction, t is the distance from origin. A ray that runs along one of the box's faces still gets the right entry or exit distance.

      Parameters

      • box: Box

        The box to intersect

      Returns number

      The distance t to the entry point, or to the exit point when the origin is inside the box, or -1 when the ray misses

      const t = sightLine.intersectBoxT(wallBounds);
      const blocked = t !== -1 && t < distanceToTarget;
    • Intersects this ray with plane

      A ray that lies in the plane hits at its origin. Works with a direction of any length.

      Parameters

      • plane: Plane

        The plane to intersect

      • Optionalout: Vector3

        Optional vector to write into; a new Vector3 when omitted

      Returns Vector3 | null

      The hit point, or null when the ray points away from the plane or runs parallel to it

      const ground = new Plane(new Vector3(0, 1, 0), 0);
      const target = new Vector3();

      if (pointerRay.intersectPlane(ground, target)) cursor.position.copyFrom(target);
    • Returns true when this ray hits box

      Parameters

      • box: Box

        The box to test

      Returns boolean

      Whether the ray hits the box

      const hovered = pointerRay.intersectsBox(buttonBounds);
      
    • Intersects this ray with sphere

      An empty sphere is never hit. Requires a unit-length direction.

      Parameters

      • sphere: Sphere

        The sphere to intersect

      • Optionalout: Vector3

        Optional vector to write into; a new Vector3 when omitted

      Returns Vector3 | null

      The entry point (or the exit point from inside), or null when the ray misses

      const hit = new Vector3();

      if (ray.intersectSphere(planet.boundingSphere, hit)) flag.position.copyFrom(hit);

      Ray#intersectSphereT For the distance to the same hit, without the point.

    • Intersects this ray with sphere and returns the distance to the hit rather than the point

      Same hit as Ray#intersectSphere; Ray#at turns the distance into the point. Compare distances to find the nearest of several hits without making a point for each. Requires a unit-length direction.

      Parameters

      • sphere: Sphere

        The sphere to intersect

      Returns number

      The distance t to the hit (>= 0), or -1 when the ray misses

      let nearest: Mesh3D | null = null;
      let nearestT = Infinity;

      for (const target of targets) {
      const t = ray.intersectSphereT(target.boundingSphere);

      if (t !== -1 && t < nearestT) {
      nearestT = t;
      nearest = target;
      }
      }
    • Returns true when this ray hits plane, including when its origin lies on the plane

      Parameters

      • plane: Plane

        The plane to test

      Returns boolean

      Whether the ray hits the plane

      const canLand = dropRay.intersectsPlane(ground);
      
    • Returns true when this ray hits sphere

      Requires a unit-length direction. An empty sphere is never hit.

      Parameters

      • sphere: Sphere

        The sphere to test

      Returns boolean

      Whether the ray hits the sphere

      const hovered = enemies.filter((enemy: Mesh3D) => pointerRay.intersectsSphere(enemy.boundingSphere));
      
    • Sets the origin and direction of this ray, copying their values

      Parameters

      Returns this

      This ray for chaining

      camera.worldTransform.extractForward(forward);
      aimRay.set(camera.getGlobalPosition(eye), forward);
    • Advanced

      Returns a string representation of this ray

      Returns string

      A string in the form [@pixi/3d/math:Ray origin=(0, 0, 0) direction=(0, 0, -1)]