Skip to main content

vec3

Class

3-component floating-point vector used for 3D positions, directions, and RGB color values.

Index

Example

const a = new vec3(1, 2, 3);
const b = new vec3(4, 5, 6);

const sum = a.add(b);
console.log('a + b:', sum.x, sum.y, sum.z);

const cross = a.cross(b);
console.log('a x b:', cross.x, cross.y, cross.z);

const scaled = a.uniformScale(3);
console.log('a * 3:', scaled.x, scaled.y, scaled.z);

console.log('length of a:', a.length);
console.log('dot(a, b):', a.dot(b));

const norm = a.normalize();
console.log('normalized a:', norm.x, norm.y, norm.z);

Constructors

Constructor

new vec3(x, y, z): vec3

Constructs a vec3 with the given x, y, z components.

Parameters

ParameterType
xnumber
ynumber
znumber

Returns

vec3

Properties

b

b: number

Blue channel component, equivalent to z.


g

g: number

Green channel component, equivalent to y.


length

length: number

The Euclidean length of this vector.


lengthSquared

lengthSquared: number

The squared Euclidean length of this vector.


r

r: number

Red channel component, equivalent to x.


x

x: number

X component of this vector.


y

y: number

Y component of this vector.


z

z: number

Z component of this vector.

Methods

add()

add(vec): vec3

Returns a new vec3 that is the sum of this vector and the given vector.

Parameters

ParameterType
vecvec3

Returns

vec3


addInPlace()

addInPlace(vec): void

Adds the given vector to this vector in place.

Parameters

ParameterType
vecvec3

Returns

void


angleTo()

angleTo(vec): number

Returns the angle in radians between this vector and the given vector.

Parameters

ParameterType
vecvec3

Returns

number


back()

static back(): vec3

Static vec3 pointing in the negative Z direction (0, 0, -1).

Returns

vec3


clampLength()

clampLength(length): vec3

Returns a new vec3 with its length clamped to the given maximum length.

Parameters

ParameterType
lengthnumber

Returns

vec3


clampLengthInPlace()

clampLengthInPlace(length): void

Clamps the length of this vector to the given maximum length in place.

Parameters

ParameterType
lengthnumber

Returns

void


clone()

clone(): vec3

Returns a new vec3 with the same component values as this vector.

Returns

vec3


copyFrom()

copyFrom(source): void

Copies the component values from the given vector into this vector.

Parameters

ParameterType
sourcevec2 | vec3 | vec4

Returns

void


cross()

cross(vec): vec3

Returns the cross product of this vector and the given vector.

Parameters

ParameterType
vecvec3

Returns

vec3


crossInPlace()

crossInPlace(vec): void

Computes the cross product of this vector and the given vector, storing the result in this vector.

Parameters

ParameterType
vecvec3

Returns

void


distance()

distance(vec): number

Returns the Euclidean distance between this vector and the given vector.

Parameters

ParameterType
vecvec3

Returns

number


distanceSquared()

distanceSquared(vec): number

Returns the squared Euclidean distance between this vector and the given vector.

Parameters

ParameterType
vecvec3

Returns

number


div()

div(vec): vec3

Returns a new vec3 that is this vector divided component-wise by the given vector.

Parameters

ParameterType
vecvec3

Returns

vec3


divInPlace()

divInPlace(vec): void

Divides this vector component-wise by the given vector in place.

Parameters

ParameterType
vecvec3

Returns

void


dot()

dot(vec): number

Returns the dot product of this vector and the given vector.

Parameters

ParameterType
vecvec3

Returns

number


down()

static down(): vec3

Static vec3 pointing in the negative Y direction (0, -1, 0).

Returns

vec3


equal()

equal(vec): boolean

Returns true if this vector's components are equal to the given vector's components.

Parameters

ParameterType
vecvec3

Returns

boolean


fill()

fill(scalar): void

Sets all components of this vector to the given value.

Parameters

ParameterType
scalarnumber

Returns

void


forward()

static forward(): vec3

Static vec3 pointing in the positive Z direction (0, 0, 1).

Returns

vec3


left()

static left(): vec3

Static vec3 pointing in the negative X direction (-1, 0, 0).

Returns

vec3


lerp()

static lerp(from, to, t): vec3

Returns a new vec3 linearly interpolated between two vectors by factor t.

Parameters

ParameterType
fromvec3
tovec3
tnumber

Returns

vec3


lerpInPlace()

lerpInPlace(target, t): void

Linearly interpolates this vector toward the given target vector by factor t, in place.

Parameters

ParameterType
targetvec3
tnumber

Returns

void


max()

static max(a, b): vec3

Returns a new vec3 with the component-wise maximum of two vectors.

Parameters

ParameterType
avec3
bvec3

Returns

vec3


min()

static min(a, b): vec3

Returns a new vec3 with the component-wise minimum of two vectors.

Parameters

ParameterType
avec3
bvec3

Returns

vec3


moveTowards()

moveTowards(target, step): vec3

Returns a new vec3 moved from this vector toward the target by at most maxDelta distance.

Parameters

ParameterType
targetvec3
stepnumber

Returns

vec3


moveTowardsInPlace()

moveTowardsInPlace(point, magnitude): void

Moves this vector toward the target by at most maxDelta distance, in place.

Parameters

ParameterType
pointvec3
magnitudenumber

Returns

void


mult()

mult(vec): vec3

Returns a new vec3 that is this vector multiplied component-wise by the given vector.

Parameters

ParameterType
vecvec3

Returns

vec3


multInPlace()

multInPlace(vec): void

Multiplies this vector component-wise by the given vector in place.

Parameters

ParameterType
vecvec3

Returns

void


normalize()

normalize(): vec3

Returns a new vec3 with the same direction as this vector but with length 1.

Returns

vec3


normalizeInPlace()

normalizeInPlace(): void

Normalizes this vector to unit length in place.

Returns

void


one()

static one(): vec3

Static vec3 with all components set to 1.

Returns

vec3


orthonormalize()

static orthonormalize(a, b): void

Returns an orthonormalized version of the given vector relative to a reference vector.

Parameters

ParameterType
avec3
bvec3

Returns

void


project()

project(onto): vec3

Returns the projection of this vector onto the given vector.

Parameters

ParameterType
ontovec3

Returns

vec3


projectInPlace()

projectInPlace(onto): void

Projects this vector onto the given vector in place.

Parameters

ParameterType
ontovec3

Returns

void


projectOnPlane()

projectOnPlane(planeNormal): vec3

Returns the projection of this vector onto the plane defined by the given normal.

Parameters

ParameterType
planeNormalvec3

Returns

vec3


projectOnPlaneInPlace()

projectOnPlaneInPlace(planeNormal): void

Projects this vector onto the plane defined by the given normal, in place.

Parameters

ParameterType
planeNormalvec3

Returns

void


randomUnitVector()

static randomUnitVector(): vec3

Returns a new vec3 with a random unit direction.

Returns

vec3


reflect()

reflect(planeNormal): vec3

Returns the reflection of this vector off a surface with the given normal.

Parameters

ParameterType
planeNormalvec3

Returns

vec3


reflectInPlace()

reflectInPlace(planeNormal): void

Reflects this vector off a surface with the given normal, in place.

Parameters

ParameterType
planeNormalvec3

Returns

void


static right(): vec3

Static vec3 pointing in the positive X direction (1, 0, 0).

Returns

vec3


rotateTowards()

rotateTowards(target, step): vec3

Returns a new vec3 rotated from this vector toward the target, by at most maxAngle radians.

Parameters

ParameterType
targetvec3
stepnumber

Returns

vec3


rotateTowardsInPlace()

rotateTowardsInPlace(target, step): void

Rotates this vector toward the target by at most maxAngle radians, in place.

Parameters

ParameterType
targetvec3
stepnumber

Returns

void


scale()

scale(vec): vec3

Returns a new vec3 that is this vector multiplied by the given scalar.

Parameters

ParameterType
vecvec3

Returns

vec3


scaleInPlace()

scaleInPlace(vec): void

Multiplies this vector by the given scalar in place.

Parameters

ParameterType
vecvec3

Returns

void


setRandomUnitVector()

setRandomUnitVector(): void

Sets this vector to a random unit vector in place.

Returns

void


setRGB()

setRGB(r?, g?, b?): void

Sets the r, g, b components of this vector.

Parameters

ParameterType
r?number
g?number
b?number

Returns

void


setXYZ()

setXYZ(x?, y?, z?): void

Sets the x, y, z components of this vector.

Parameters

ParameterType
x?number
y?number
z?number

Returns

void


slerp()

static slerp(from, to, t): vec3

Returns a new vec3 spherically interpolated between two vectors by factor t.

Parameters

ParameterType
fromvec3
tovec3
tnumber

Returns

vec3


slerpInPlace()

slerpInPlace(target, t): void

Spherically interpolates this vector toward the given target vector by factor t, in place.

Parameters

ParameterType
targetvec3
tnumber

Returns

void


sub()

sub(vec): vec3

Returns a new vec3 that is this vector minus the given vector.

Parameters

ParameterType
vecvec3

Returns

vec3


subInPlace()

subInPlace(vec): void

Subtracts the given vector from this vector in place.

Parameters

ParameterType
vecvec3

Returns

void


toString()

toString(): string

Returns a string representation of this vector's components.

Returns

string


uniformScale()

uniformScale(scale): vec3

Returns a new vec3 with all components multiplied by the given scalar.

Parameters

ParameterType
scalenumber

Returns

vec3


uniformScaleInPlace()

uniformScaleInPlace(scale): void

Multiplies all components of this vector by the given scalar in place.

Parameters

ParameterType
scalenumber

Returns

void


up()

static up(): vec3

Static vec3 pointing in the positive Y direction (0, 1, 0).

Returns

vec3


zero()

static zero(): vec3

Static vec3 with all components set to 0.

Returns

vec3