# [name]

A class representing a 4x4 [link:https://en.wikipedia.org/wiki/Matrix_(mathematics) matrix].
The most common use of a 4x4 matrix in 3D computer graphics is as a [link:https://en.wikipedia.org/wiki/Transformation_matrix Transformation Matrix]. For an introduction to transformation matrices as used in WebGL, check out [link:http://www.opengl-tutorial.org/beginners-tutorials/tutorial-3-matrices this tutorial].
This allows a [page:Vector3] representing a point in 3D space to undergo transformations such as translation, rotation, shear, scale, reflection, orthogonal or perspective projection and so on, by being multiplied by the matrix. This is known as applying the matrix to the vector.
Every [page:Object3D] has three associated Matrix4s:

- [page:Object3D.matrix]: This stores the local transform of the object. This is the object's transformation relative to its parent.
- [page:Object3D.matrixWorld]: The global or world transform of the object. If the object has no parent, then this is identical to the local transform stored in [page:Object3D.matrix matrix].
- [page:Object3D.modelViewMatrix]: This represents the object's transformation relative to the camera's coordinate system. An object's modelViewMatrix is the object's matrixWorld pre-multiplied by the camera's matrixWorldInverse.

[page:Camera Cameras] have two additional Matrix4s:

- [page:Camera.matrixWorldInverse]: The view matrix - the inverse of the Camera's [page:Object3D.matrixWorld matrixWorld].
- [page:Camera.projectionMatrix]: Represents the information how to project the scene to clip space.

Note: [page:Object3D.normalMatrix] is not a Matrix4, but a [page:Matrix3].

## A Note on Row-Major and Column-Major Ordering

The [page:set]() method takes arguments in [link:https://en.wikipedia.org/wiki/Row-_and_column-major_order#Column-major_order row-major] order, while internally they are stored in the [page:.elements elements] array in column-major order.
This means that calling `var m = new THREE.Matrix4(); m.set( 11, 12, 13, 14, 21, 22, 23, 24, 31, 32, 33, 34, 41, 42, 43, 44 );` will result in the [page:.elements elements] array containing: `m.elements = [ 11, 21, 31, 41, 12, 22, 32, 42, 13, 23, 33, 43, 14, 24, 34, 44 ];` and internally all calculations are performed using column-major ordering. However, as the actual ordering makes no difference mathematically and most people are used to thinking about matrices in row-major order, the three.js documentation shows matrices in row-major order. Just bear in mind that if you are reading the source code, you'll have to take the [link:https://en.wikipedia.org/wiki/Transpose transpose] of any matrices outlined here to make sense of the calculations.

## Constructor

### [name]()

Creates and initializes the [name] to the 4x4 [link:https://en.wikipedia.org/wiki/Identity_matrix identity matrix].

## Properties

### [property:Array elements]

A [link:https://en.wikipedia.org/wiki/Row-_and_column-major_order#Column-major_order column-major] list of matrix values.

### [property:Boolean isMatrix4]

Used to check whether this or derived classes are Matrix4s. Default is *true*.
You should not change this, as it used internally for optimisation.

## Methods

### [method:Array applyToBufferAttribute]( [param:BufferAttribute attribute] )

[page:BufferAttribute attribute] - An attribute of floats that represent 3D vectors.
Multiplies (applies) this matrix to every 3D vector in the [page:BufferAttribute attribute].

### [method:Matrix4 clone]()

Creates a new Matrix4 with identical [page:.elements elements] to this one.

### [method:this compose]( [param:Vector3 position], [param:Quaternion quaternion], [param:Vector3 scale] )

Sets this matrix to the transformation composed of [page:Vector3 position], [page:Quaternion quaternion] and [page:Vector3 scale]. Internally this calls [page:.makeRotationFromQuaternion makeRotationFromQuaternion]( [page:Quaternion quaternion] ) followed by [page:.scale scale]( [page:Vector3 scale] ), then finally [page:.setPosition setPosition]( [page:Vector3 position] ).

### [method:this copy]( [param:Matrix4 m] )

Copies the [page:.elements elements] of matrix [page:Matrix4 m] into this matrix.

### [method:this copyPosition]( [param:Matrix4 m] )

Copies the translation component of the supplied matrix [page:Matrix4 m] into this matrix's translation component.

### [method:null decompose]( [param:Vector3 position], [param:Quaternion quaternion], [param:Vector3 scale] )

Decomposes this matrix into it's [page:Vector3 position], [page:Quaternion quaternion] and [page:Vector3 scale] components.

### [method:Float determinant]()

Computes and returns the [link:https://en.wikipedia.org/wiki/Determinant determinant] of this matrix.
Based on the method outlined [link:http://www.euclideanspace.com/maths/algebra/matrix/functions/inverse/fourD/index.htm here].

### [method:Boolean equals]( [param:Matrix4 m] )

Return true if this matrix and [page:Matrix4 m] are equal.

### [method:this extractBasis]( [param:Vector3 xAxis], [param:Vector3 yAxis], [param:Vector3 zAxis] )

Extracts the [link:https://en.wikipedia.org/wiki/Basis_(linear_algebra) basis] of this matrix into the three axis vectors provided. If this matrix is: `a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p` then the [page:Vector3 xAxis], [page:Vector3 yAxis], [page:Vector3 zAxis] will be set to: `xAxis = (a, e, i) yAxis = (b, f, j) zAxis = (c, g, k)`

### [method:this extractRotation]( [param:Matrix4 m] )

Extracts the rotation component of the supplied matrix [page:Matrix4 m] into this matrix's rotation component.

### [method:this fromArray]( [param:Array array], [param:Integer offset] )

[page:Array array] - the array to read the elements from.
[page:Integer offset] - ( optional ) offset into the array. Default is 0.
Sets the elements of this matrix based on an [page:Array array] in [link:https://en.wikipedia.org/wiki/Row-_and_column-major_order#Column-major_order column-major] format.

### [method:this getInverse]( [param:Matrix4 m], [param:Boolean throwOnDegenerate] )

[page:Matrix4 m] - the matrix to take the inverse of.
[page:Boolean throwOnDegenerate] - (optional) If true, throw an error if the matrix is degenerate (not invertible).
Set this matrix to the [link:https://en.wikipedia.org/wiki/Invertible_matrix inverse] of the passed matrix [page:Matrix4 m], using the method outlined [link:http://www.euclideanspace.com/maths/algebra/matrix/functions/inverse/fourD/index.htm here]. If [page:Boolean throwOnDegenerate] is not set and the matrix is not invertible, set this to the 4x4 identity matrix.

### [method:Float getMaxScaleOnAxis]()

Gets the maximum scale value of the 3 axes.

### [method:this identity]()

Resets this matrix to the [link:https://en.wikipedia.org/wiki/Identity_matrix identity matrix].

### [method:this lookAt]( [param:Vector3 eye], [param:Vector3 center], [param:Vector3 up], )

Constructs a rotation matrix, looking from [page:Vector3 eye] towards [page:Vector3 center] oriented by the [page:Vector3 up] vector.

### [method:this makeRotationAxis]( [param:Vector3 axis], [param:Float theta] )

[page:Vector3 axis] — Rotation axis, should be normalized.
[page:Float theta] — Rotation angle in radians.
Sets this matrix as rotation transform around [page:Vector3 axis] by [page:Float theta] radians.
This is a somewhat controversial but mathematically sound alternative to rotating via [page:Quaternions]. See the discussion [link:https://www.gamedev.net/articles/programming/math-and-physics/do-we-really-need-quaternions-r1199 here].

### [method:this makeBasis]( [param:Vector3 xAxis], [param:Vector3 yAxis], [param:Vector3 zAxis] )

Set this to the [link:https://en.wikipedia.org/wiki/Basis_(linear_algebra) basis] matrix consisting of the three provided basis vectors: `xAxis.x, yAxis.x, zAxis.x, 0, xAxis.y, yAxis.y, zAxis.y, 0, xAxis.z, yAxis.z, zAxis.z, 0, 0, 0, 0, 1`

### [method:this makePerspective]( [param:Float left], [param:Float right], [param:Float top], [param:Float bottom], [param:Float near], [param:Float far] )

Creates a [link:https://en.wikipedia.org/wiki/3D_projection#Perspective_projection perspective projection] matrix. This is used internally by [page:PerspectiveCamera.updateProjectionMatrix]()

### [method:this makeOrthographic]( [param:Float left], [param:Float right], [param:Float top], [param:Float bottom], [param:Float near], [param:Float far] )

Creates an [link:https://en.wikipedia.org/wiki/Orthographic_projection orthographic projection] matrix. This is used internally by [page:OrthographicCamera.updateProjectionMatrix]().

### [method:this makeRotationFromEuler]( [param:Euler euler] )

Sets the rotation component (the upper left 3x3 matrix) of this matrix to the rotation specified by the given [page:Euler Euler Angle]. The rest of the matrix is set to the identity. Depending on the [page:Euler.order order] of the [page:Euler euler], there are six possible outcomes. See [link:https://en.wikipedia.org/wiki/Euler_angles#Rotation_matrix this page] for a complete list.

### [method:this makeRotationFromQuaternion]( [param:Quaternion q] )

Sets the rotation component of this matrix to the rotation specified by [page:Quaternion q], as outlined [link:https://en.wikipedia.org/wiki/Rotation_matrix#Quaternion here]. The rest of the matrix is set to the identity. So, given [page:Quaternion q] = w + xi + yj + zk, the resulting matrix will be: `1-2y²-2z² 2xy-2zw 2xz+2yw 0 2xy+2zw 1-2x²-2z² 2yz-2xw 0 2xz-2yw 2yz+2xw 1-2x²-2y² 0 0 0 0 1`

### [method:this makeRotationX]( [param:Float theta] )

[page:Float theta] — Rotation angle in radians.
Sets this matrix as a rotational transformation around the X axis by [page:Float theta] (θ) radians. The resulting matrix will be: `1 0 0 0 0 cos(θ) -sin(θ) 0 0 sin(θ) cos(θ) 0 0 0 0 1`

### [method:this makeRotationY]( [param:Float theta] )

[page:Float theta] — Rotation angle in radians.
Sets this matrix as a rotational transformation around the Y axis by [page:Float theta] (θ) radians. The resulting matrix will be: `cos(θ) 0 sin(θ) 0 0 1 0 0 -sin(θ) 0 cos(θ) 0 0 0 0 1`

### [method:this makeRotationZ]( [param:Float theta] )

[page:Float theta] — Rotation angle in radians.
Sets this matrix as a rotational transformation around the Z axis by [page:Float theta] (θ) radians. The resulting matrix will be: `cos(θ) -sin(θ) 0 0 sin(θ) cos(θ) 0 0 0 0 1 0 0 0 0 1`

### [method:this makeScale]( [param:Float x], [param:Float y], [param:Float z] )

[page:Float x] - the amount to scale in the X axis.
[page:Float y] - the amount to scale in the Y axis.
[page:Float z] - the amount to scale in the Z axis.
Sets this matrix as scale transform: `x, 0, 0, 0, 0, y, 0, 0, 0, 0, z, 0, 0, 0, 0, 1`

### [method:this makeShear]( [param:Float x], [param:Float y], [param:Float z] )

[page:Float x] - the amount to shear in the X axis.
[page:Float y] - the amount to shear in the Y axis.
[page:Float z] - the amount to shear in the Z axis.
Sets this matrix as a shear transform: `1, y, z, 0, x, 1, z, 0, x, y, 1, 0, 0, 0, 0, 1`

### [method:this makeTranslation]( [param:Float x], [param:Float y], [param:Float z] )

[page:Float x] - the amount to translate in the X axis.
[page:Float y] - the amount to translate in the Y axis.
[page:Float z] - the amount to translate in the Z axis.
Sets this matrix as a translation transform: `1, 0, 0, x, 0, 1, 0, y, 0, 0, 1, z, 0, 0, 0, 1`

### [method:this multiply]( [param:Matrix4 m] )

Post-multiplies this matrix by [page:Matrix4 m].

### [method:this multiplyMatrices]( [param:Matrix4 a], [param:Matrix4 b] )

Sets this matrix to [page:Matrix4 a] x [page:Matrix4 b].

### [method:this multiplyScalar]( [param:Float s] )

Multiplies every component of the matrix by a scalar value [page:Float s].

### [method:this premultiply]( [param:Matrix4 m] )

Pre-multiplies this matrix by [page:Matrix4 m].

### [method:this scale]( [param:Vector3 v] )

Multiplies the columns of this matrix by vector [page:Vector3 v].

### [method:this set]( [param:Float n11], [param:Float n12], [param:Float n13], [param:Float n14], [param:Float n21], [param:Float n22], [param:Float n23], [param:Float n24], [param:Float n31], [param:Float n32], [param:Float n33], [param:Float n34], [param:Float n41], [param:Float n42], [param:Float n43], [param:Float n44] )

Set the [page:.elements elements] of this matrix to the supplied row-major values [page:Float n11], [page:Float n12], ... [page:Float n44].

### [method:this setPosition]( [param:Vector3 v] )

### [method:this setPosition]( [param:Float x], [param:Float y], [param:Float z] ) // optional API

Sets the position component for this matrix from vector [page:Vector3 v], without affecting the rest of the matrix - i.e. if the matrix is currently: `a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p` This becomes: `a, b, c, v.x, e, f, g, v.y, i, j, k, v.z, m, n, o, p`

### [method:Array toArray]( [param:Array array], [param:Integer offset] )

[page:Array array] - (optional) array to store the resulting vector in.
[page:Integer offset] - (optional) offset in the array at which to put the result.
Writes the elements of this matrix to an array in [link:https://en.wikipedia.org/wiki/Row-_and_column-major_order#Column-major_order column-major] format.

### [method:this transpose]()

[link:https://en.wikipedia.org/wiki/Transpose Transposes] this matrix.

## Source

[link:https://github.com/mrdoob/three.js/blob/master/src/[path].js src/[path].js]