Math3D. Matrix4

4x4 transform matrix for moving, rotating and scaling points in 3D

  • Static builders like Matrix4.translation return a new matrix
  • Methods on a matrix change it in place and return it, so calls can chain
  • a.multiply(b) means b happens first, then a
  • Stored the way WebGL wants it, so it can be sent to a shader as is

Constructor

new Matrix4(mopt)

Create a matrix, identity by default

Parameters:
NameTypeAttributesDescription
mFloat32Array | Array.<number><optional>

16 column major values

Example
const m = buildMatrix(vec3(0, 1, 0), vec3(0, PI/2, 0)); // rotate then move up
const p = m.transformPoint(vec3(1, 0, 0));

Members

m

Properties
TypeDescription
Float32Array

The 16 column major values

Methods

copy() → {Matrix4}

Returns a new matrix that is a copy of this

Returns:
Type: 
Matrix4

getTranslation() → {Vector3}

Returns the translation part of this matrix

Returns:
Type: 
Vector3

invert() → {Matrix4}

Flip this matrix so it undoes itself, returns this and does nothing if it cannot be inverted

Returns:
Type: 
Matrix4

multiply(matrix) → {Matrix4}

Multiply this matrix by another and return this, the other happens first

Parameters:
NameTypeDescription
matrixMatrix4
Returns:
Type: 
Matrix4

rotate(euler) → {Matrix4}

Append a rotation, returns self

Parameters:
NameTypeDescription
eulerVector3

vec3(pitch, yaw, roll) in radians

Returns:
Type: 
Matrix4

scale(v) → {Matrix4}

Append a scale, returns self

Parameters:
NameTypeDescription
vVector3
Returns:
Type: 
Matrix4

toString() → {string}

Returns a string representation of this matrix for debugging

Returns:
Type: 
string

transformDirection(v) → {Vector3}

Transform a direction, rotation and scale only

Parameters:
NameTypeDescription
vVector3
Returns:
Type: 
Vector3

transformPoint(v) → {Vector3}

Transform a point, translation included

Parameters:
NameTypeDescription
vVector3
Returns:
Type: 
Vector3

translate(v) → {Matrix4}

Append a translation, returns self

Parameters:
NameTypeDescription
vVector3
Returns:
Type: 
Matrix4

transpose() → {Matrix4}

Transpose this matrix in place, returns self

Returns:
Type: 
Matrix4

(static) identity() → {Matrix4}

Returns a new identity matrix

Returns:
Type: 
Matrix4

(static) lookAt(eye, target, upopt) → {Matrix4}

Returns the transform of something at eye turned to face target

  • Invert it to get a view matrix for a camera there
Parameters:
NameTypeAttributesDescription
eyeVector3
targetVector3
upVector3<optional>
Returns:
Type: 
Matrix4

(static) orthographic(left, right, bottom, top, near, far) → {Matrix4}

Returns a new orthographic projection, camera looks down -Z

Parameters:
NameTypeDescription
leftnumber

Edge of the visible box

rightnumber

Edge of the visible box

bottomnumber

Edge of the visible box

topnumber

Edge of the visible box

nearnumber

Closest visible distance

farnumber

Furthest visible distance, Infinity is not allowed here

Returns:
Type: 
Matrix4

(static) perspective(fov, aspect, near, far) → {Matrix4}

Returns a new perspective projection, camera looks down -Z

Parameters:
NameTypeDescription
fovnumber

Vertical field of view in radians

aspectnumber

Width divided by height

nearnumber

Closest visible distance

farnumber

Furthest visible distance, Infinity is allowed

Returns:
Type: 
Matrix4

(static) rotation(euler) → {Matrix4}

Returns a new rotation matrix, rolled first, then pitched, then yawed

Parameters:
NameTypeDescription
eulerVector3

vec3(pitch, yaw, roll) in radians

Returns:
Type: 
Matrix4

(static) scaling(v) → {Matrix4}

Returns a new scale matrix

Parameters:
NameTypeDescription
vVector3
Returns:
Type: 
Matrix4

(static) translation(v) → {Matrix4}

Returns a new translation matrix

Parameters:
NameTypeDescription
vVector3
Returns:
Type: 
Matrix4