class Vector3 extends MathArray extends Array
import {Vector3} from '@math.gl/core';
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
Vector3
extends Vector
extends MathArray
extends Array
Gets or sets element 0, 1 or 2 respectively
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(array = this)
Vector3.check(array = this)
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
.
// MODIFIERS
Scale component wise with a scalar or another Vector3
.
scale
(Number|Vector3) - scale component wise with a scalar or another Vector3
.negate()
inverse()
normalize()
cross(vector)
lerp(vector, coeff)
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)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)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)Transforms the vector by the provided 4x4 matrix.
Note: Scales the resulting vector to ensure that w
, if non-zero, is set to 1
.
Transforms the vector by the provided 3x3 matrix.
Transform the vector's x
and y
values by the provided 2x2 matrix.
Transform the vector by the provided quaternion
.