@pixi/3d
    Preparing search index...

    Class Vector3

    A point or direction in 3D, such as a position, a velocity or an offset

    Math methods change the vector they're called on and return it, so calls chain. Pass an out vector as the last argument to write the result there instead and leave this one as it is. In code that runs every frame, create out vectors once and reuse them: a new Vector3() per frame is garbage the collector has to clean up.

    Where a method takes a point, any PointData3D works, such as a node's position or a plain { x, y, z } object.

    const toPlayer = new Vector3();

    app.ticker.add((ticker) => {
    player.position.subtract(enemy.position, toPlayer).normalize();
    enemy.position.addScaledVector(toPlayer, enemy.speed * (ticker.deltaMS / 1000));
    });

    Implements

    Index
    • Creates a new Vector3

      Parameters

      • x: number = 0

        The x component

      • y: number = 0

        The y component

      • z: number = 0

        The z component

      Returns Vector3

    x: number = 0

    The x component

    const groundSpeed = Math.hypot(velocity.x, velocity.z);
    
    y: number = 0

    The y component

    velocity.y -= gravity * dt;
    
    z: number = 0

    The z component

    velocity.z = runSpeed;
    
    • get shared(): Vector3

      A static Vector3 reset to (0, 0, 0) on every access, for a result used straight away

      Important

      This vector is shared and temporary. Do not store references to it.

      Returns Vector3

      const inRange = enemy.getGlobalPosition(Vector3.shared).distanceTo(player.position) < attackRange;
      
    • Adds v to this vector

      Parameters

      • v: PointData3D

        The point to add

      • out: PointLike3D = ...

        The target to write the result into, defaults to this vector

      Returns this

      The point the result was written into

      waypoint.add({ x: 0, y: 2, z: 0 });
      barrelBase.add(muzzleOffset, muzzlePosition);
    • Adds v * s to this vector, such as a velocity times the frame time

      Parameters

      • v: PointData3D

        The point to scale and add

      • s: number

        The scalar to multiply v by

      • out: PointLike3D = ...

        The target to write the result into, defaults to this vector

      Returns this

      The point the result was written into

      velocity.addScaledVector(gravity, dt);
      rayOrigin.addScaledVector(rayDirection, hitDistance, hitPoint);
    • Computes the angle between this vector and v

      Returns π/2 when either vector has zero length.

      Parameters

      Returns number

      The angle in radians, from 0 to π

      const canSeePlayer = forward.angleTo(toPlayer) < visionHalfAngle;
      
    • Transforms this point by m, translation included, without the divide Vector3#applyMatrix4 does

      Cheaper, and correct for any transform that doesn't project, such as a node's world transform.

      Parameters

      • m: Matrix4

        The matrix to apply

      • out: PointLike3D = ...

        The target to write the result into, defaults to this vector

      Returns this

      The point the result was written into

      muzzleOffset.applyAffineMatrix4(gun.worldTransform, muzzleWorldPosition);
      
    • Multiplies this vector by the 3x3 matrix m, such as a normal matrix

      Parameters

      • m: Matrix3

        The matrix to apply

      • out: PointLike3D = ...

        The target to write the result into, defaults to this vector

      Returns this

      The point the result was written into

      const normalMatrix = new Matrix3().getNormalMatrix(mesh.worldTransform);

      worldNormal.copyFrom(localNormal).applyMatrix3(normalMatrix).normalize();
    • Transforms this point by m, translation included, then divides by the resulting w

      The divide is what a projection matrix needs: with a camera's viewProjection, x and y come out from -1 to 1 across the visible area, and z from -1 at the near plane to 1 at the far plane. A point behind the camera can land inside that x and y range too, so check z as well. Use Vector3#applyAffineMatrix4 for transforms that don't project, and Vector3#transformDirection for directions.

      Parameters

      • m: Matrix4

        The matrix to apply

      • out: PointLike3D = ...

        The target to write the result into, defaults to this vector

      Returns this

      The point the result was written into

      point.applyMatrix4(camera.viewProjection, ndc);
      const onScreen = Math.abs(ndc.x) <= 1 && Math.abs(ndc.y) <= 1 && Math.abs(ndc.z) <= 1;
    • Rotates this vector by the unit Quaternion q

      Parameters

      • q: Quaternion

        The unit quaternion to rotate by

      • out: PointLike3D = ...

        The target to write the result into, defaults to this vector

      Returns this

      The point the result was written into

      forward.set(0, 0, 1).applyQuaternion(ship.quaternion);
      
    • Clamps each component of this vector between the matching components of min and max

      Assumes min <= max componentwise.

      Parameters

      • min: PointData3D

        The lower bound per component

      • max: PointData3D

        The upper bound per component

      • out: PointLike3D = ...

        The target to write the result into, defaults to this vector

      Returns this

      The point the result was written into

      cameraTarget.clamp(levelMin, levelMax);
      
    • Creates a copy of this vector

      Returns Vector3

      const startVelocity = velocity.clone();
      
    • Copies x, y, z from the given point into this vector

      Parameters

      Returns this

      respawnPoint.copyFrom(player.position);
      
    • Copies this vector's x, y, z into the given point

      Type Parameters

      Parameters

      • p: T

        The point to copy into

      Returns T

      The given point with values updated

      respawnPoint.copyTo(player.position);
      
    • Computes the cross product this × v, a vector at right angles to both

      Parameters

      • v: PointData3D

        The other vector

      • out: PointLike3D = ...

        The target to write the result into, defaults to this vector

      Returns this

      The point the result was written into

      edgeA.cross(edgeB, faceNormal).normalize();
      
    • Computes the distance from this point to v

      Parameters

      Returns number

      const reached = waypoint.distanceTo(player.position) < 0.5;
      
    • Computes the squared distance from this point to v

      Cheaper than Vector3#distanceTo and enough to compare distances.

      Parameters

      Returns number

      if (bulletPosition.distanceToSquared(target.position) < hitRadius * hitRadius) hit(target);
      
    • Divides each component of this vector by the matching component of v

      Parameters

      • v: PointData3D

        The point to divide by

      • out: PointLike3D = ...

        The target to write the result into, defaults to this vector

      Returns this

      The point the result was written into

      worldPosition.divide(cellSize, cell);
      const column = Math.floor(cell.x);
    • Divides each component of this vector by the scalar s

      Parameters

      • s: number

        The scalar to divide by

      • out: PointLike3D = ...

        The target to write the result into, defaults to this vector

      Returns this

      The point the result was written into

      const center = new Vector3();

      for (const point of points) center.add(point);
      center.divideScalar(points.length);
    • Computes the dot product of this vector and v

      For unit vectors it's the cosine of the angle between them: 1 when they point the same way, 0 at right angles and -1 when opposite.

      Parameters

      Returns number

      const targetIsAhead = forward.dot(toTarget) > 0;
      
    • Checks whether x, y and z all match p exactly, with no tolerance

      Parameters

      Returns boolean

      if (!aimPoint.equals(lastAimPoint)) recomputeAim();
      
    • Reads x, y, z from array starting at offset

      Parameters

      • array: Float32Array<ArrayBufferLike> | number[]

        The array to read from

      • offset: number = 0

        The index of the first component

      Returns this

      vertex.fromArray(positions, vertexIndex * 3);
      
    • Computes the length of this vector

      Returns number

      const speed = velocity.magnitude();
      
    • Computes the squared length of this vector

      Cheaper than Vector3#magnitude, since it skips the square root, and enough to compare lengths.

      Returns number

      if (velocity.magnitudeSquared() < 0.0001) velocity.set(0);
      
    • Sets each component to the larger of this vector's and v's

      Parameters

      • v: PointData3D

        The point to compare with

      • out: PointLike3D = ...

        The target to write the result into, defaults to this vector

      Returns this

      The point the result was written into

      const boundsMax = new Vector3(-Infinity, -Infinity, -Infinity);

      for (const point of points) boundsMax.max(point);
    • Sets each component to the smaller of this vector's and v's

      Parameters

      • v: PointData3D

        The point to compare with

      • out: PointLike3D = ...

        The target to write the result into, defaults to this vector

      Returns this

      The point the result was written into

      const boundsMin = new Vector3(Infinity, Infinity, Infinity);

      for (const point of points) boundsMin.min(point);
    • Multiplies each component of this vector by the matching component of v

      Parameters

      • v: PointData3D

        The point to multiply by

      • out: PointLike3D = ...

        The target to write the result into, defaults to this vector

      Returns this

      The point the result was written into

      velocity.multiply({ x: 1, y: 0, z: 1 }, groundVelocity);
      
    • Multiplies each component of this vector by the scalar s

      Parameters

      • s: number

        The scalar to multiply by

      • out: PointLike3D = ...

        The target to write the result into, defaults to this vector

      Returns this

      The point the result was written into

      velocity.multiplyScalar(friction);
      direction.multiplyScalar(speed * dt, step);
    • Negates each component of this vector, reversing its direction

      Parameters

      • out: PointLike3D = ...

        The target to write the result into, defaults to this vector

      Returns this

      The point the result was written into

      toThreat.negate(fleeDirection);
      
    • Scales this vector to unit length, keeping its direction

      A zero vector stays zero.

      Parameters

      • out: PointLike3D = ...

        The target to write the result into, defaults to this vector

      Returns this

      The point the result was written into

      input.normalize();
      toTarget.normalize(aimDirection);
    • Sets the x, y and z components

      Parameters

      • x: number = 0

        The x component

      • y: number = x

        The y component, defaults to x

      • z: number = x

        The z component, defaults to x

      Returns this

      velocity.set(0, jumpSpeed, 0);
      size.set(1); // (1, 1, 1)
    • Sets this point to the position the Cylindrical coordinate c describes

      theta is the angle around Y, starting at +Z and turning toward +X, and y is the height above the XZ plane.

      Parameters

      Returns this

      const slot = new Cylindrical(towerRadius, 0, 2);

      for (let i = 0; i < torchCount; i++) {
      slot.theta = (i / torchCount) * Math.PI * 2;
      torchPositions[i].setFromCylindrical(slot);
      }
    • Reads the first three values of column index of m into this vector

      In a transform, column 3 is the translation and columns 0 to 2 are the X, Y and Z axes, scaled.

      Parameters

      • m: Matrix4

        The matrix to read from

      • index: number

        The column index, 0 to 3

      Returns this

      const worldPosition = new Vector3().setFromMatrixColumn(node.worldTransform, 3);
      
    • Sets this point to the position the Spherical coordinate s describes

      phi is the angle down from the +Y axis and theta the angle around Y, starting at +Z and turning toward +X.

      Parameters

      • s: Spherical

        The spherical coordinate to convert

      Returns this

      orbit.theta += turnSpeed * dt;
      cameraOffset.setFromSpherical(orbit);
    • Scales this vector so its length equals length, keeping its direction

      A zero vector stays zero.

      Parameters

      • length: number

        The target length

      • out: PointLike3D = ...

        The target to write the result into, defaults to this vector

      Returns this

      The point the result was written into

      if (velocity.magnitude() > maxSpeed) velocity.setLength(maxSpeed);
      
    • Subtracts v from this vector

      Parameters

      • v: PointData3D

        The point to subtract

      • out: PointLike3D = ...

        The target to write the result into, defaults to this vector

      Returns this

      The point the result was written into

      const toTarget = new Vector3();

      targetPosition.subtract(turretPosition, toTarget);
    • Writes this vector's values into array starting at offset

      Allocates a new array when one is not supplied.

      Type Parameters

      • T extends Float32Array<ArrayBufferLike> | number[] = number[]

      Parameters

      • array: T = ...

        The array to write into

      • offset: number = 0

        The index to write the first component at

      Returns T

      The array the values were written into

      velocity.toArray(velocities, particleIndex * 3);
      
    • Returns a readable string representation of this vector

      Returns string

      console.log(velocity.toString());
      
    • Turns this direction by the rotation and scale in m, ignoring its translation, then normalizes it

      Parameters

      • m: Matrix4

        The matrix whose rotation to apply

      • out: PointLike3D = ...

        The target to write the result into, defaults to this vector

      Returns this

      The point the result was written into

      const cameraForward = new Vector3(0, 0, -1).transformDirection(camera.worldTransform);