Skip to main content

Vector3

From v1.0

Vector3 extends JavaScript Array through its core base classes.

Usageโ€‹

import {Vector3} from '@math.gl/core/matrix';
const vector = new Vector3(1, 1, 1);

Accessors

v.x = 2;
assert(v[0] === v.x);

Simple rotations

const v = new Vector3([1, 0, 0]);
v.rotateX({radians: Math.PI / 4}); // Rotate around the origin
v.rotateX({radians: Math.PI / 4, origin: [1, 1, 0]}); // Rotate around the specified point

Scaling with constants

const u = v.scale(-1); // Reverse direction vector

Scaling with vectors is very flexible, you can e.g. set a component to zero, or flip a component's sign.

const u = v.scale([1, 1, 0]); // Set z component to zero
const w = v.scale([1, -1, 1]); // Flip y component

Inheritanceโ€‹

Vector3 extends Vector extends MathArray extends Array

Membersโ€‹

x, y, zโ€‹

Gets or sets element 0, 1 or 2 respectively

Methodsโ€‹

Many of the most commonly used Vector3 methods are inherited from MathArray:

  • Vector3.clone()
  • Vector3.copy(array)
  • Vector3.set(...args)
  • Vector3.fromArray(array, offset = 0)
  • Vector3.toString()
  • Vector3.toArray(array = [], offset = 0)
  • Vector3.equals(array)
  • Vector3.exactEquals(array)
  • Vector3.validate()
  • Vector3.check()
  • Vector3.normalize()

Note that Vector3 is a subclass of the built in JavaScript Array and can thus e.g. be supplied as a parameter to any function expecting an Array.

constructor(x = 0, y = 0, z = 0)โ€‹

set(x, y, z)โ€‹

len()โ€‹

Returns the magnitude.

length is the array component count.

distance(vector)โ€‹

angle(vector)โ€‹

dot(vector)โ€‹

// MODIFIERS

add(...vectors)โ€‹

subtract(...vectors)โ€‹

multiply(...vectors)โ€‹

divide(...vectors)โ€‹

scale(scale)โ€‹

Scale component wise with a scalar or another Vector3.

  • scale (Number|Vector3) - scale component wise with a scalar or another Vector3.

negateโ€‹

negate()

inverseโ€‹

inverse()

normalizeโ€‹

normalize()

crossโ€‹

cross(vector)

lerpโ€‹

lerp(vector, coeff)

rotateXโ€‹

Rotate a 3D vector around the x-axis

rotateX({radians, origin})

  • radians (Number) - angle to rotate.
  • origin=[0, 0, 0] (Vector3) - the origin of the rotation (optional)

rotateYโ€‹

Rotate a 3D vector around the y-axis

rotateY({radians, origin})

  • radians (Number) - angle to rotate.
  • origin=[0, 0, 0] (Vector3) - the origin of the rotation (optional)

rotateZ(radians)โ€‹

Rotate a 3D vector around the z-axis

rotateZ({radians, origin})

  • radians (Number) - angle to rotate.
  • origin=[0, 0, 0] (Vector3) - the origin of the rotation (optional)

transform(matrix4 : Number[16]) : thisโ€‹

Transforms the vector by the provided 4x4 matrix.

Note: Scales the resulting vector to ensure that w, if non-zero, is set to 1.

transformByMatrix3(matrix3 : Number[9]) : thisโ€‹

Transforms the vector by the provided 3x3 matrix.

transformByMatrix2(matrix2 : Number[4]) : thisโ€‹

Transform the vector's x and y values by the provided 2x2 matrix.

transformByQuaternion(quaternion : Number[4]) : thisโ€‹

Transform the vector by the provided quaternion.

The functional API also provides vec3Slerp, vec3Bezier, and vec3Hermite. See functional math for imports, output ownership, and limitations.