vec4
ClassFour-component float vector used for XYZW coordinates and RGBA color values.
Index
Properties
Methods
- add
- addInPlace
- angleTo
- clampLength
- clampLengthInPlace
- clone
- copyFrom
- distance
- distanceSquared
- div
- divInPlace
- dot
- equal
- fill
- lerp
- lerpInPlace
- max
- min
- moveTowards
- moveTowardsInPlace
- mult
- multInPlace
- normalize
- normalizeInPlace
- one
- project
- projectInPlace
- projectOnPlane
- projectOnPlaneInPlace
- reflect
- reflectInPlace
- scale
- scaleInPlace
- setRGBA
- setXYZW
- sub
- subInPlace
- toString
- uniformScale
- uniformScaleInPlace
- zero
Example
const a = new vec4(1, 2, 3, 4);
const b = new vec4(5, 6, 7, 8);
const sum = a.add(b); // (6, 8, 10, 12)
const diff = a.sub(b); // (-4, -4, -4, -4)
const scaled = a.uniformScale(2); // (2, 4, 6, 8)
const len = a.length; // 5.477...
const dp = a.dot(b); // 70
Constructors
Constructor
new vec4(
x,y,z,w):vec4
Constructs a vec4 with the given x, y, z, and w components.
Parameters
| Parameter | Type |
|---|---|
x | number |
y | number |
z | number |
w | number |
Returns
vec4
Properties
a
a:
number
Alpha component of the vector, equivalent to w.
b
b:
number
Blue component of the vector, equivalent to z.
g
g:
number
Green component of the vector, equivalent to y.
length
length:
number
Magnitude of the vector.
lengthSquared
lengthSquared:
number
Squared magnitude of the vector.
r
r:
number
Red component of the vector, equivalent to x.
w
w:
number
Fourth component of the vector.
x
x:
number
First component of the vector.
y
y:
number
Second component of the vector.
z
z:
number
Third component of the vector.
Methods
add()
add(
vec):vec4
Returns a new vec4 that is the sum of this vector and the given vector.
Parameters
| Parameter | Type |
|---|---|
vec | vec4 |
Returns
vec4
addInPlace()
addInPlace(
vec):void
Adds the given vector to this vector in place.
Parameters
| Parameter | Type |
|---|---|
vec | vec4 |
Returns
void
angleTo()
angleTo(
vec):number
Returns the angle in radians between this vector and the given vector.
Parameters
| Parameter | Type |
|---|---|
vec | vec4 |
Returns
number
clampLength()
clampLength(
length):vec4
Returns a new vec4 with its length clamped to the given maximum.
Parameters
| Parameter | Type |
|---|---|
length | number |
Returns
vec4
clampLengthInPlace()
clampLengthInPlace(
length):void
Clamps the length of this vector to the given maximum in place.
Parameters
| Parameter | Type |
|---|---|
length | number |
Returns
void
clone()
clone():
vec4
Returns a new vec4 with the same component values.
Returns
vec4
copyFrom()
copyFrom(
source):void
Copies component values from the given vec4 into this vector.
Parameters
Returns
void
distance()
distance(
vec):number
Returns the distance between this vector and the given vector.
Parameters
| Parameter | Type |
|---|---|
vec | vec4 |
Returns
number
distanceSquared()
distanceSquared(
vec):number
Returns the squared distance between this vector and the given vector.
Parameters
| Parameter | Type |
|---|---|
vec | vec4 |
Returns
number
div()
div(
vec):vec4
Returns a new vec4 that is this vector divided component-wise by the given vector.
Parameters
| Parameter | Type |
|---|---|
vec | vec4 |
Returns
vec4
divInPlace()
divInPlace(
vec):void
Divides this vector component-wise by the given vector in place.
Parameters
| Parameter | Type |
|---|---|
vec | vec4 |
Returns
void
dot()
dot(
vec):number
Returns the dot product of this vector and the given vector.
Parameters
| Parameter | Type |
|---|---|
vec | vec4 |
Returns
number
equal()
equal(
vec):boolean
Returns true if this vector and the given vector have equal components.
Parameters
| Parameter | Type |
|---|---|
vec | vec4 |
Returns
boolean
fill()
fill(
scalar):void
Sets all components of this vector to the given value.
Parameters
| Parameter | Type |
|---|---|
scalar | number |
Returns
void
lerp()
staticlerp(from,to,t):vec4
Returns a new vec4 linearly interpolated between two vectors by the given factor.
Parameters
| Parameter | Type |
|---|---|
from | vec4 |
to | vec4 |
t | number |
Returns
vec4
lerpInPlace()
lerpInPlace(
target,t):void
Linearly interpolates this vector toward the given vector by the given factor in place.
Parameters
| Parameter | Type |
|---|---|
target | vec4 |
t | number |
Returns
void
max()
staticmax(a,b):vec4
Returns a new vec4 with the component-wise maximum of two vectors.
Parameters
| Parameter | Type |
|---|---|
a | vec4 |
b | vec4 |
Returns
vec4
min()
staticmin(a,b):vec4
Returns a new vec4 with the component-wise minimum of two vectors.
Parameters
| Parameter | Type |
|---|---|
a | vec4 |
b | vec4 |
Returns
vec4
moveTowards()
moveTowards(
target,step):vec4
Returns a new vec4 moved toward the target by at most the given max delta.
Parameters
| Parameter | Type |
|---|---|
target | vec4 |
step | number |
Returns
vec4
moveTowardsInPlace()
moveTowardsInPlace(
point,magnitude):void
Moves this vector toward the target by at most the given max delta in place.
Parameters
| Parameter | Type |
|---|---|
point | vec4 |
magnitude | number |
Returns
void
mult()
mult(
vec):vec4
Returns a new vec4 that is this vector multiplied component-wise by the given vector.
Parameters
| Parameter | Type |
|---|---|
vec | vec4 |
Returns
vec4
multInPlace()
multInPlace(
vec):void
Multiplies this vector component-wise by the given vector in place.
Parameters
| Parameter | Type |
|---|---|
vec | vec4 |
Returns
void
normalize()
normalize():
vec4
Returns a new vec4 with unit length in the same direction.
Returns
vec4
normalizeInPlace()
normalizeInPlace():
void
Normalizes this vector to unit length in place.
Returns
void
one()
staticone():vec4
Returns a vec4 with all components set to 1.
Returns
vec4
project()
project(
onto):vec4
Returns a new vec4 projected onto the given vector.
Parameters
| Parameter | Type |
|---|---|
onto | vec4 |
Returns
vec4
projectInPlace()
projectInPlace(
onto):void
Projects this vector onto the given vector in place.
Parameters
| Parameter | Type |
|---|---|
onto | vec4 |
Returns
void
projectOnPlane()
projectOnPlane(
planeNormal):vec4
Returns a new vec4 projected onto the plane defined by the given normal.
Parameters
| Parameter | Type |
|---|---|
planeNormal | vec4 |
Returns
vec4
projectOnPlaneInPlace()
projectOnPlaneInPlace(
planeNormal):void
Projects this vector onto the plane defined by the given normal in place.
Parameters
| Parameter | Type |
|---|---|
planeNormal | vec4 |
Returns
void
reflect()
reflect(
planeNormal):vec4
Returns a new vec4 reflected off the plane defined by the given normal.
Parameters
| Parameter | Type |
|---|---|
planeNormal | vec4 |
Returns
vec4
reflectInPlace()
reflectInPlace(
planeNormal):void
Reflects this vector off the plane defined by the given normal in place.
Parameters
| Parameter | Type |
|---|---|
planeNormal | vec4 |
Returns
void
scale()
scale(
vec):vec4
Returns a new vec4 scaled by the given scalar.
Parameters
| Parameter | Type |
|---|---|
vec | vec4 |
Returns
vec4
scaleInPlace()
scaleInPlace(
vec):void
Scales this vector by the given scalar in place.
Parameters
| Parameter | Type |
|---|---|
vec | vec4 |
Returns
void
setRGBA()
setRGBA(
r?,g?,b?,a?):void
Sets the r, g, b, and a components of this vector.
Parameters
| Parameter | Type |
|---|---|
r? | number |
g? | number |
b? | number |
a? | number |
Returns
void
setXYZW()
setXYZW(
x?,y?,z?,w?):void
Sets the x, y, z, and w components of this vector.
Parameters
| Parameter | Type |
|---|---|
x? | number |
y? | number |
z? | number |
w? | number |
Returns
void
sub()
sub(
vec):vec4
Returns a new vec4 that is this vector minus the given vector.
Parameters
| Parameter | Type |
|---|---|
vec | vec4 |
Returns
vec4
subInPlace()
subInPlace(
vec):void
Subtracts the given vector from this vector in place.
Parameters
| Parameter | Type |
|---|---|
vec | vec4 |
Returns
void
toString()
toString():
string
Returns a string representation of this vector.
Returns
string
uniformScale()
uniformScale(
scale):vec4
Returns a new vec4 with all components multiplied by the given scalar.
Parameters
| Parameter | Type |
|---|---|
scale | number |
Returns
vec4
uniformScaleInPlace()
uniformScaleInPlace(
scale):void
Multiplies all components of this vector by the given scalar in place.
Parameters
| Parameter | Type |
|---|---|
scale | number |
Returns
void
zero()
staticzero():vec4
Returns a vec4 with all components set to 0.
Returns
vec4