Class: Vec4

Vec4

Four-element vector of 32-bit floats. May reflect an ESSL vec4 uniform variable. Low-performance convenience methods reproduce operator behaviour.
operatormethod
+=add
-=sub
*=mul
/=div
+plus
-minus
*times
/over
·dot

a = b + c can be computed as var a = b.plus(c), when a does not yet exist, and performance does not matter. It is not required that c is a Vec4: it can be a vector of different length, an object literal, or its coordinates given as separate arguments.
a.set(b).add(c) is about three times faster. Variable a needs to exist, and be a Vec4. Neither b nor c are required to be Vec4s: they can be vectors of different length, object literals, or its coordinates given as separate arguments.
If a, b and c are Vec4 instances, a.setSum(b, c) can be used for optimum performance. It is seven times faster than a.set(b).add(c), or twenty times faster than a = b.plus(c).
It is recommended to use optimized methods for time-critical per-frame tasks, while programmer-friendly interfaces are useful for one-time initializations, e.g. when constructing a scene.

new Vec4( [u] [, v] [, s] [, t])

Without parameters, initializes the vector to (0, 0, 0, 1).
Parameters:
Name Type Argument Default Description
u Vec4 | Vec3 | Vec2 | Object | Number <optional>
0 Any object (properties x, y, z, w are interpreted as coordinates, if given), or a numerical value for coordinate x.
v Number <optional>
0 Ignored if u.y is defined. Otherwise, the value for coordinate y.
s Number <optional>
0 Ignored if u.z is defined. Otherwise, the value for coordinate z.
t Number <optional>
1 Ignored if u.w is defined. Otherwise, the value for coordinate w.

Members


storage :Float32Array

4-element typed array for coordinate storage.
Type:
  • Float32Array

w :Number

Alias for storage[3];
Type:
  • Number

x :Number

Alias for storage[0];
Type:
  • Number

y :Number

Alias for storage[1];
Type:
  • Number

z :Number

Alias for storage[2];
Type:
  • Number

Methods


add( [u] [, v] [, s] [, t])

Simulates operator +=. Adds another vector to this vector, overwriting the contents with the result.
Parameters:
Name Type Argument Default Description
u Vec4 | Vec3 | Vec2 | Object | Number <optional>
0 Any object (properties x, y, z, w are interpreted as coordinates, if given), or a numerical value for coordinate x.
v Number <optional>
0 Ignored if u.y is defined. Otherwise, the value for coordinate y.
s Number <optional>
0 Ignored if u.z is defined. Otherwise, the value for coordinate z.
t Number <optional>
0 Ignored if u.w is defined. Otherwise, the value for coordinate w.
Returns:
this
Type
Vec4

addScaled(dt [, u] [, v] [, s] [, t])

Simulates += dt *. Adds another vector, scaled by `dt`, to this vector, overwriting the contents with the result.
Parameters:
Name Type Argument Default Description
dt Number Scaling factor.
u Vec4 | Object | Number <optional>
0 Any object (property x), or a numerical value.
v Number <optional>
0 Ignored if u.y is defined. Otherwise, the value for coordinate y.
s Number <optional>
0 Ignored if u.z is defined. Otherwise, the value for coordinate z.
t Number <optional>
0 Ignored if u.w is defined. Otherwise, the value for coordinate w.
Returns:
this
Type
Vec4

clamp( [minVal] [, maxVal])

Constrains the value of this vector to lie between two further values, elementwise, overwriting the contents with the result.
Parameters:
Name Type Argument Default Description
minVal Vec4 | Vec3 | Vec2 | Object | Number <optional>
0 Specifies the lower end of the range into which to constrain the elements. If a scalar is given, it applies to all channels.
maxVal Vec4 | Vec3 | Vec2 | Object | Number <optional>
1 Specifies the upper end of the range into which to constrain the elements. If a scalar is given, it applies to all channels.
Returns:
this
Type
Vec4

clone()

Creates a copy.
Returns:
A new instance with identical contents.
Type
Vec4

commit(gl, uniformLocation)

Sets the value of the vector to a WebGL vec4 uniform variable.
Parameters:
Name Type Description
gl WebGLRenderingContext rendering context
uniformLocation WebGLUniformLocation location of the uniform variable in the currently used WebGL program

direction()

Scales the vector by the inverse of its length, and returns the result in a new instance.
Returns:
A unit length vector with the same direction as this.
Type
Vec4

div( [u] [, v] [, s] [, t])

Simulates operator /=. Divides, elementwise, this vector with another vector, or scalar, overwriting the contents with the result.
Parameters:
Name Type Argument Default Description
u Vec4 | Vec3 | Vec2 | Object | Number <optional>
1 Any object (properties x, y, z, w are interpreted as coordinates, if given), or a numerical value for coordinate x.
v Number <optional>
1 Ignored if u.y is defined. Otherwise, the value for coordinate y. Defaults to the value of parameter u, if it is a number.
s Number <optional>
1 Ignored if u.z is defined. Otherwise, the value for coordinate z. Defaults to the value of parameter u, if it is a number.
t Number <optional>
1 Ignored if u.w is defined. Otherwise, the value for coordinate w. Defaults to the value of parameter u, if it is a number.
Returns:
this
Type
Vec4

dot( [u] [, v] [, s] [, t])

Computes the dot product with another vector.
Parameters:
Name Type Argument Default Description
u Vec4 | Vec3 | Vec2 | Object | Number <optional>
0 Any object (properties x, y, z, w are interpreted as coordinates, if given), or a numerical value for coordinate x.
v Number <optional>
0 Ignored if u.y is defined. Otherwise, the value for coordinate y.
s Number <optional>
0 Ignored if u.z is defined. Otherwise, the value for coordinate z.
t Number <optional>
0 Ignored if u.w is defined. Otherwise, the value for coordinate w.
Returns:
Type
Number

length()

Computes the vector length.
Returns:
Type
Number

length2()

Computes the length squared.
Returns:
x*x + y*y + z*z + w*w
Type
Number

minus( [u] [, v] [, s] [, t])

Simulates operator -. Subtracts the parameter vector from this vector, and returns the result in a new instance.
Parameters:
Name Type Argument Default Description
u Vec4 | Vec3 | Vec2 | Object | Number <optional>
0 Any object (properties x, y, z, w are interpreted as coordinates, if given), or a numerical value for coordinate x.
v Number <optional>
0 Ignored if u.y is defined. Otherwise, the value for coordinate y.
s Number <optional>
0 Ignored if u.z is defined. Otherwise, the value for coordinate z.
t Number <optional>
0 Ignored if u.w is defined. Otherwise, the value for coordinate w.
Returns:
the difference of the two vectors
Type
Vec4

mul( [u] [, v] [, s] [, t])

Simulates operator *=. Multiplies, this vector with another vector elementwise, matrix, or scalar, from the right, overwriting the contents with the result.
Parameters:
Name Type Argument Default Description
u Mat4 | Vec4 | Vec3 | Vec2 | Object | Number <optional>
1 A 4x4 matrix, or any object (properties x, y, z, w are interpreted as coordinates, if given), or a numerical value for coordinate x.
v Number <optional>
1 Ignored if u is a matrix, or u.y is defined. Otherwise, the value for coordinate y. Defaults to the value of parameter u, if it is a number.
s Number <optional>
1 Ignored if u is a matrix, or u.z is defined. Otherwise, the value for coordinate z. Defaults to the value of parameter u, if it is a number.
t Number <optional>
1 Ignored if u is a matrix, or u.w is defined. Otherwise, the value for coordinate w. Defaults to the value of parameter u, if it is a number.
Returns:
this
Type
Vec4

normalize()

Scales the vector by the inverse of its length, overwriting the contents with the result.
Returns:
this
Type
Vec4

over( [u] [, v] [, s] [, t])

Simulates operator /. Divides, elementwise, this vector with another vector, or scalar, and returns the result in a new instance.
Parameters:
Name Type Argument Default Description
u Vec4 | Vec3 | Vec2 | Object | Number <optional>
1 Any object (properties x, y, z, w are interpreted as coordinates, if given), or a numerical value for coordinate x.
v Number <optional>
1 Ignored if u.y is defined. Otherwise, the value for coordinate y. Defaults to the value of parameter u, if it is a number.
s Number <optional>
1 Ignored if u.z is defined. Otherwise, the value for coordinate z. Defaults to the value of parameter u, if it is a number.
t Number <optional>
1 Ignored if u.w is defined. Otherwise, the value for coordinate w. Defaults to the value of parameter u, if it is a number.
Returns:
the elementwise product of the two vectors
Type
Vec4

plus( [u] [, v] [, s] [, t])

Simulates operator +. Adds this vector and the parameter vector, and returns the result in a new instance.
Parameters:
Name Type Argument Default Description
u Vec4 | Vec3 | Vec2 | Object | Number <optional>
0 Any object (properties x, y, z, w are interpreted as coordinates, if given), or a numerical value for coordinate x.
v Number <optional>
0 Ignored if u.y is defined. Otherwise, the value for coordinate y.
s Number <optional>
0 Ignored if u.z is defined. Otherwise, the value for coordinate z.
t Number <optional>
0 Ignored if u.w is defined. Otherwise, the value for coordinate w.
Returns:
the sum of the two vectors
Type
Vec4

set( [u] [, v] [, s] [, t])

Simulates operator =. Sets the coordinates from another vector, or number values. Without parameters, sets (0, 0, 0, 1).
Parameters:
Name Type Argument Default Description
u Vec4 | Vec3 | Vec2 | Object | Number <optional>
0 Any object (properties x, y, z, w are interpreted as coordinates, if given), or a numerical value for coordinate x.
v Number <optional>
0 Ignored if u.y is defined. Otherwise, the value for coordinate y.
s Number <optional>
0 Ignored if u.z is defined. Otherwise, the value for coordinate z.
t Number <optional>
1 Ignored if u.w is defined. Otherwise, the value for coordinate w.
Returns:
this
Type
Vec4

setClamped(b [, minVal] [, maxVal])

Fast. Constrains a value to lie between two further values, elementwise, storing the result in this vector.
Parameters:
Name Type Argument Default Description
b Vec4 The value to constrain.
minVal Vec4 | Vec3 | Vec2 | Object | Number <optional>
0 Specifies the lower end of the range into which to constrain the elements. If a scalar is given, it applies to all channels.
maxVal Vec4 | Vec3 | Vec2 | Object | Number <optional>
1 Specifies the upper end of the range into which to constrain the elements. If a scalar is given, it applies to all channels.
Returns:
this
Type
Vec4

setDifference(b, c)

Fast. Subtracts the second argument vector from the first one, storing the result in this vector.
Parameters:
Name Type Description
b Vec4 Minuend.
c Vec4 Subtrahend.
Returns:
this
Type
Vec4

setNormalized(b)

Scales the argmument vector by the inverse of its length, storing the result in this vector.
Parameters:
Name Type Description
b Vec4 The vector to normalize.
Returns:
this
Type
Vec4

setProduct(b, c)

Fast. Multiplies, elementwise, the two argument vectors, storing the result in this vector.
Parameters:
Name Type Description
b Vec4 Factor 1.
c Vec4 Factor 2.
Returns:
this
Type
Vec4

setQuotient(b, c)

Fast. Divides, elementwise, the two argument vectors, storing the result in this vector.
Parameters:
Name Type Description
b Vec4 Dividend.
c Vec4 Divisor.
Returns:
this
Type
Vec4

setRandom( [minVal] [, maxVal])

Fill the vector with random values that to lie between two further values, elementwise.
Parameters:
Name Type Argument Default Description
minVal Vec4 | Vec3 | Vec2 | Object | Number <optional>
0 Specifies the lower end of the random range. If a scalar is given, it applies to all channels.
maxVal Vec4 | Vec3 | Vec2 | Object | Number <optional>
1 Specifies the upper end of the random range. If a scalar is given, it applies to all channels.
Returns:
this
Type
Vec4

setScaled(a, s)

Fast. Scales the vector by a scalar.
Parameters:
Name Type Description
a Vec4 Vector to scale.
s Number Scale factor.
Returns:
this
Type
Vec4

setScaledByInverse(a, s)

Fast. Scales the vector by the reciprocal of scalar.
Parameters:
Name Type Description
a Vec4 Vector to scale.
s Number Scale factor inverse.
Returns:
this
Type
Vec4

setSum(b, c)

Fast. Adds the two argument vectors, storing the result in this vector.
Parameters:
Name Type Description
b Vec4 Term 1.
c Vec4 Term 2.
Returns:
this
Type
Vec4

setTransformed(m)

Multiplies the argument vector (considering it a row vector) with the argument matrix, from the right. The contents of this are overwritten with the transformed vector with the result. See Vec4.prototype#times for a version creating a new vector instance.
Parameters:
Name Type Description
m Mat4 The 4x4 linear homogeneous transformation matrix using column-major representation.
Returns:
this
Type
Vec4

sub( [u] [, v] [, s] [, t])

Simulates operator -=. Subtracts another vector from this vector, overwriting the contents with the result.
Parameters:
Name Type Argument Default Description
u Vec4 | Vec3 | Vec2 | Object | Number <optional>
0 Any object (properties x, y, z, w are interpreted as coordinates, if given), or a numerical value for coordinate x.
v Number <optional>
0 Ignored if u.y is defined. Otherwise, the value for coordinate y.
s Number <optional>
0 Ignored if u.z is defined. Otherwise, the value for coordinate z.
t Number <optional>
0 Ignored if u.w is defined. Otherwise, the value for coordinate w.
Returns:
this
Type
Vec4

times( [u] [, v] [, s] [, t])

Simulates operator *. Multiplies, this vector with another vector elementwise, matrix, or scalar, from the right, and returns the result in a new instance.
Parameters:
Name Type Argument Default Description
u Mat4 | Vec4 | Vec3 | Vec2 | Object | Number <optional>
1 A 4x4 matrix, or any object (properties x, y, z, w are interpreted as coordinates, if given), or a numerical value for coordinate x.
v Number <optional>
1 Ignored if u is a matrix, or u.y is defined. Otherwise, the value for coordinate y. Defaults to the value of parameter u, if it is a number.
s Number <optional>
1 Ignored if u is a matrix, or u.z is defined. Otherwise, the value for coordinate z. Defaults to the value of parameter u, if it is a number.
t Number <optional>
1 Ignored if u is a matrix, or u.w is defined. Otherwise, the value for coordinate w. Defaults to the value of parameter u, if it is a number.
Returns:
the vector transformed by the matrix, or the elementwise product of the two vectors, or the scaled vector
Type
Vec4

transform(m)

Multiplies the vector (considering it a row vector) with a matrix, from the right. The contents of this are overwritten with the transformed vector with the result. See Vec4.prototype#times for a version creating a new vector instance.
Parameters:
Name Type Description
m Mat4 The 4x4 linear homogeneous transformation matrix using column-major representation.
Returns:
this
Type
Vec4

<static> random( [minVal] [, maxVal])

Return a new Vec4 with random values that to lie between two values, elementwise.
Parameters:
Name Type Argument Default Description
minVal Vec4 | Vec3 | Vec2 | Object | Number <optional>
0 Specifies the lower end of the random range. If a scalar is given, it applies to all channels.
maxVal Vec4 | Vec3 | Vec2 | Object | Number <optional>
1 Specifies the upper end of the random range. If a scalar is given, it applies to all channels.
Returns:
this
Type
Vec4