vec3
Class3-component floating-point vector used for 3D positions, directions, and RGB color values.
Index
Properties
Methods
- add
- addInPlace
- angleTo
- back
- clampLength
- clampLengthInPlace
- clone
- copyFrom
- cross
- crossInPlace
- distance
- distanceSquared
- div
- divInPlace
- dot
- down
- equal
- fill
- forward
- left
- lerp
- lerpInPlace
- max
- min
- moveTowards
- moveTowardsInPlace
- mult
- multInPlace
- normalize
- normalizeInPlace
- one
- orthonormalize
- project
- projectInPlace
- projectOnPlane
- projectOnPlaneInPlace
- randomUnitVector
- reflect
- reflectInPlace
- right
- rotateTowards
- rotateTowardsInPlace
- scale
- scaleInPlace
- setRandomUnitVector
- setRGB
- setXYZ
- slerp
- slerpInPlace
- sub
- subInPlace
- toString
- uniformScale
- uniformScaleInPlace
- up
- zero
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
| Parameter | Type |
|---|---|
x | number |
y | number |
z | number |
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
| Parameter | Type |
|---|---|
vec | vec3 |
Returns
vec3
addInPlace()
addInPlace(
vec):void
Adds the given vector to this vector in place.
Parameters
| Parameter | Type |
|---|---|
vec | vec3 |
Returns
void
angleTo()
angleTo(
vec):number
Returns the angle in radians between this vector and the given vector.
Parameters
| Parameter | Type |
|---|---|
vec | vec3 |
Returns
number
back()
staticback():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
| Parameter | Type |
|---|---|
length | number |
Returns
vec3
clampLengthInPlace()
clampLengthInPlace(
length):void
Clamps the length of this vector to the given maximum length in place.
Parameters
| Parameter | Type |
|---|---|
length | number |
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
Returns
void
cross()
cross(
vec):vec3
Returns the cross product of this vector and the given vector.
Parameters
| Parameter | Type |
|---|---|
vec | vec3 |
Returns
vec3
crossInPlace()
crossInPlace(
vec):void
Computes the cross product of this vector and the given vector, storing the result in this vector.
Parameters
| Parameter | Type |
|---|---|
vec | vec3 |
Returns
void
distance()
distance(
vec):number
Returns the Euclidean distance between this vector and the given vector.
Parameters
| Parameter | Type |
|---|---|
vec | vec3 |
Returns
number
distanceSquared()
distanceSquared(
vec):number
Returns the squared Euclidean distance between this vector and the given vector.
Parameters
| Parameter | Type |
|---|---|
vec | vec3 |
Returns
number
div()
div(
vec):vec3
Returns a new vec3 that is this vector divided component-wise by the given vector.
Parameters
| Parameter | Type |
|---|---|
vec | vec3 |
Returns
vec3
divInPlace()
divInPlace(
vec):void
Divides this vector component-wise by the given vector in place.
Parameters
| Parameter | Type |
|---|---|
vec | vec3 |
Returns
void
dot()
dot(
vec):number
Returns the dot product of this vector and the given vector.
Parameters
| Parameter | Type |
|---|---|
vec | vec3 |
Returns
number
down()
staticdown():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
| Parameter | Type |
|---|---|
vec | vec3 |
Returns
boolean
fill()
fill(
scalar):void
Sets all components of this vector to the given value.
Parameters
| Parameter | Type |
|---|---|
scalar | number |
Returns
void
forward()
staticforward():vec3
Static vec3 pointing in the positive Z direction (0, 0, 1).
Returns
vec3
left()
staticleft():vec3
Static vec3 pointing in the negative X direction (-1, 0, 0).
Returns
vec3
lerp()
staticlerp(from,to,t):vec3
Returns a new vec3 linearly interpolated between two vectors by factor t.
Parameters
| Parameter | Type |
|---|---|
from | vec3 |
to | vec3 |
t | number |
Returns
vec3
lerpInPlace()
lerpInPlace(
target,t):void
Linearly interpolates this vector toward the given target vector by factor t, in place.
Parameters
| Parameter | Type |
|---|---|
target | vec3 |
t | number |
Returns
void
max()
staticmax(a,b):vec3
Returns a new vec3 with the component-wise maximum of two vectors.
Parameters
| Parameter | Type |
|---|---|
a | vec3 |
b | vec3 |
Returns
vec3
min()
staticmin(a,b):vec3
Returns a new vec3 with the component-wise minimum of two vectors.
Parameters
| Parameter | Type |
|---|---|
a | vec3 |
b | vec3 |
Returns
vec3
moveTowards()
moveTowards(
target,step):vec3
Returns a new vec3 moved from this vector toward the target by at most maxDelta distance.
Parameters
| Parameter | Type |
|---|---|
target | vec3 |
step | number |
Returns
vec3
moveTowardsInPlace()
moveTowardsInPlace(
point,magnitude):void
Moves this vector toward the target by at most maxDelta distance, in place.
Parameters
| Parameter | Type |
|---|---|
point | vec3 |
magnitude | number |
Returns
void
mult()
mult(
vec):vec3
Returns a new vec3 that is this vector multiplied component-wise by the given vector.
Parameters
| Parameter | Type |
|---|---|
vec | vec3 |
Returns
vec3
multInPlace()
multInPlace(
vec):void
Multiplies this vector component-wise by the given vector in place.
Parameters
| Parameter | Type |
|---|---|
vec | vec3 |
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()
staticone():vec3
Static vec3 with all components set to 1.
Returns
vec3
orthonormalize()
staticorthonormalize(a,b):void
Returns an orthonormalized version of the given vector relative to a reference vector.
Parameters
| Parameter | Type |
|---|---|
a | vec3 |
b | vec3 |
Returns
void
project()
project(
onto):vec3
Returns the projection of this vector onto the given vector.
Parameters
| Parameter | Type |
|---|---|
onto | vec3 |
Returns
vec3
projectInPlace()
projectInPlace(
onto):void
Projects this vector onto the given vector in place.
Parameters
| Parameter | Type |
|---|---|
onto | vec3 |
Returns
void
projectOnPlane()
projectOnPlane(
planeNormal):vec3
Returns the projection of this vector onto the plane defined by the given normal.
Parameters
| Parameter | Type |
|---|---|
planeNormal | vec3 |
Returns
vec3
projectOnPlaneInPlace()
projectOnPlaneInPlace(
planeNormal):void
Projects this vector onto the plane defined by the given normal, in place.
Parameters
| Parameter | Type |
|---|---|
planeNormal | vec3 |
Returns
void
randomUnitVector()
staticrandomUnitVector():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
| Parameter | Type |
|---|---|
planeNormal | vec3 |
Returns
vec3
reflectInPlace()
reflectInPlace(
planeNormal):void
Reflects this vector off a surface with the given normal, in place.
Parameters
| Parameter | Type |
|---|---|
planeNormal | vec3 |
Returns
void
right()
staticright():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
| Parameter | Type |
|---|---|
target | vec3 |
step | number |
Returns
vec3
rotateTowardsInPlace()
rotateTowardsInPlace(
target,step):void
Rotates this vector toward the target by at most maxAngle radians, in place.
Parameters
| Parameter | Type |
|---|---|
target | vec3 |
step | number |
Returns
void
scale()
scale(
vec):vec3
Returns a new vec3 that is this vector multiplied by the given scalar.
Parameters
| Parameter | Type |
|---|---|
vec | vec3 |
Returns
vec3
scaleInPlace()
scaleInPlace(
vec):void
Multiplies this vector by the given scalar in place.
Parameters
| Parameter | Type |
|---|---|
vec | vec3 |
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
| Parameter | Type |
|---|---|
r? | number |
g? | number |
b? | number |
Returns
void
setXYZ()
setXYZ(
x?,y?,z?):void
Sets the x, y, z components of this vector.
Parameters
| Parameter | Type |
|---|---|
x? | number |
y? | number |
z? | number |
Returns
void
slerp()
staticslerp(from,to,t):vec3
Returns a new vec3 spherically interpolated between two vectors by factor t.
Parameters
| Parameter | Type |
|---|---|
from | vec3 |
to | vec3 |
t | number |
Returns
vec3
slerpInPlace()
slerpInPlace(
target,t):void
Spherically interpolates this vector toward the given target vector by factor t, in place.
Parameters
| Parameter | Type |
|---|---|
target | vec3 |
t | number |
Returns
void
sub()
sub(
vec):vec3
Returns a new vec3 that is this vector minus the given vector.
Parameters
| Parameter | Type |
|---|---|
vec | vec3 |
Returns
vec3
subInPlace()
subInPlace(
vec):void
Subtracts the given vector from this vector in place.
Parameters
| Parameter | Type |
|---|---|
vec | vec3 |
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
| Parameter | Type |
|---|---|
scale | number |
Returns
vec3
uniformScaleInPlace()
uniformScaleInPlace(
scale):void
Multiplies all components of this vector by the given scalar in place.
Parameters
| Parameter | Type |
|---|---|
scale | number |
Returns
void
up()
staticup():vec3
Static vec3 pointing in the positive Y direction (0, 1, 0).
Returns
vec3
zero()
staticzero():vec3
Static vec3 with all components set to 0.
Returns
vec3