Matrix4.html 15.3 KB
Newer Older
1 2 3
<!DOCTYPE html>
<html lang="en">
	<head>
4
		<meta charset="utf-8" />
M
Mr.doob 已提交
5
		<base href="../../../" />
M
Mr.doob 已提交
6 7
		<script src="page.js"></script>
		<link type="text/css" rel="stylesheet" href="page.css" />
8 9 10
	</head>
	<body>
		<h1>[name]</h1>
M
Mr.doob 已提交
11

12
		<p class="desc">
L
looeee 已提交
13 14 15 16 17 18 19 20 21
			A class representing a 4x4 [link:https://en.wikipedia.org/wiki/Matrix_(mathematics) matrix].<br /><br />

			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].<br /><br />

			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
L
Lewy Blue 已提交
22 23
			and so on, by being multiplied by the matrix. This is known as	<em>applying</em>
			the matrix to the vector.<br /><br />
L
looeee 已提交
24 25 26 27

			Every [page:Object3D] has three associated Matrix4s:
			<ul>
				<li>
28
					[page:Object3D.matrix]: This stores the local transform of the object. This is the object's transformation relative to its parent.
L
looeee 已提交
29 30
				</li>
				<li>
31
					[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].
L
looeee 已提交
32 33
				</li>
				<li>
J
Jost Schmithals 已提交
34 35
					[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.
L
looeee 已提交
36 37 38
				</li>
			</ul>

J
Jost Schmithals 已提交
39
			[page:Camera Cameras] have two additional Matrix4s:
L
looeee 已提交
40 41
			<ul>
				<li>
J
Jost Schmithals 已提交
42
					[page:Camera.matrixWorldInverse]: The view matrix - the inverse of the Camera's [page:Object3D.matrixWorld matrixWorld].
L
looeee 已提交
43 44
				</li>
				<li>
J
Jost Schmithals 已提交
45
					[page:Camera.projectionMatrix]: Represents the information how to project the scene to clip space.
L
looeee 已提交
46 47
				</li>
			</ul>
48
			Note: [page:Object3D.normalMatrix] is not a Matrix4, but a [page:Matrix3].
49
		</p>
M
Mr.doob 已提交
50

L
looeee 已提交
51
		<h2>A Note on Row-Major and Column-Major Ordering</h2>
52
		<p>
L
looeee 已提交
53
			The [page:set]() method takes arguments in [link:https://en.wikipedia.org/wiki/Row-_and_column-major_order#Column-major_order row-major]
L
looeee 已提交
54
			order, while internally they are stored in the [page:.elements elements] array in column-major order.<br /><br />
M
Mr.doob 已提交
55

L
looeee 已提交
56 57
			This means that calling
		<code>
M
Mugen87 已提交
58
const m = new THREE.Matrix4();
L
Lewy Blue 已提交
59

L
looeee 已提交
60 61
m.set( 11, 12, 13, 14,
       21, 22, 23, 24,
L
Lewy Blue 已提交
62 63
       31, 32, 33, 34,
       41, 42, 43, 44 );
M
Mr.doob 已提交
64

65
		</code>
L
looeee 已提交
66
		will result in the [page:.elements elements] array containing:
L
looeee 已提交
67 68 69
		<code>
m.elements = [ 11, 21, 31, 41,
               12, 22, 32, 42,
L
Lewy Blue 已提交
70 71
               13, 23, 33, 43,
               14, 24, 34, 44 ];
L
looeee 已提交
72 73 74
		</code>
		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,
L
Lewy Blue 已提交
75
		the three.js documentation shows matrices in row-major order. Just bear in mind that if you are reading the source
L
Lewy Blue 已提交
76
		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.
77
		</p>
M
Mr.doob 已提交
78

79 80 81 82 83 84 85 86 87 88 89
		<h2>Extracting position, rotation and scale</h2>
		<p>
			There are several options available for extracting position, rotation and scale from a Matrix4.
			<ul>
				<li>
					[page:Vector3.setFromMatrixPosition]: can be used to extract the translation component.
				</li>
				<li>
					[page:Vector3.setFromMatrixScale]: can be used to extract the scale component.
				</li>
				<li>
F
FlorentMasson 已提交
90
					[page:Quaternion.setFromRotationMatrix], [page:Euler.setFromRotationMatrix] or [page:.extractRotation extractRotation] can be used to extract the rotation component from a pure (unscaled) matrix.
91 92 93 94 95 96
				</li>
				<li>
					[page:.decompose decompose] can be used to extract position, rotation and scale all at once.
				</li>
			</ul>
		</p>
M
Mr.doob 已提交
97

98
		<h2>Constructor</h2>
M
Mr.doob 已提交
99

C
cjshannon 已提交
100

101
		<h3>[name]()</h3>
M
Mr.doob 已提交
102

103
		<p>
L
looeee 已提交
104 105
			Creates and initializes the [name] to the 4x4
			[link:https://en.wikipedia.org/wiki/Identity_matrix identity matrix].
106
	</p>
M
Mr.doob 已提交
107

108
		<h2>Properties</h2>
M
Mr.doob 已提交
109

110
		<h3>[property:Array elements]</h3>
111
		<p>
L
looeee 已提交
112 113
		A [link:https://en.wikipedia.org/wiki/Row-_and_column-major_order#Column-major_order column-major]
		 list of matrix values.
114
		</p>
M
Mr.doob 已提交
115

L
looeee 已提交
116 117 118 119 120 121



		<h2>Methods</h2>

		<h3>[method:Matrix4 clone]()</h3>
122
		<p>Creates a new Matrix4 with identical [page:.elements elements] to this one.</p>
L
looeee 已提交
123

124
		<h3>[method:this compose]( [param:Vector3 position], [param:Quaternion quaternion], [param:Vector3 scale] )</h3>
125
		<p>
L
looeee 已提交
126
		Sets this matrix to the transformation composed of [page:Vector3 position],
127
		[page:Quaternion quaternion] and [page:Vector3 scale].
128
		</p>
129

130
		<h3>[method:this copy]( [param:Matrix4 m] )</h3>
131
		<p>Copies the [page:.elements elements] of matrix [page:Matrix4 m] into this matrix.</p>
L
looeee 已提交
132

133
		<h3>[method:this copyPosition]( [param:Matrix4 m] )</h3>
134
		<p>
L
looeee 已提交
135 136
		Copies the translation component of the supplied matrix [page:Matrix4 m] into this
		matrix's translation component.
137
		</p>
138

139
		<h3>[method:null decompose]( [param:Vector3 position], [param:Quaternion quaternion], [param:Vector3 scale] )</h3>
140
		<p>
M
Michael Herzog 已提交
141
		Decomposes this matrix into its [page:Vector3 position], [page:Quaternion quaternion] and [page:Vector3 scale] components.<br/><br/>
142
		Note: Not all matrices are decomposable in this way. For example, if an object has a non-uniformly scaled parent, then the object's world matrix may not be decomposable, and this method may not be appropriate.
143
		</p>
144

L
looeee 已提交
145
		<h3>[method:Float determinant]()</h3>
146
		<p>
L
looeee 已提交
147 148 149
		Computes and returns the
		[link:https://en.wikipedia.org/wiki/Determinant determinant] of this matrix.<br /><br />

L
Lewy Blue 已提交
150
		Based on the method outlined [link:http://www.euclideanspace.com/maths/algebra/matrix/functions/inverse/fourD/index.htm here].
151
		</p>
152

153
		<h3>[method:Boolean equals]( [param:Matrix4 m] )</h3>
154
		<p>Return true if this matrix and [page:Matrix4 m] are equal.</p>
L
looeee 已提交
155

156
		<h3>[method:this extractBasis]( [param:Vector3 xAxis], [param:Vector3 yAxis], [param:Vector3 zAxis] )</h3>
157
		<p>
L
looeee 已提交
158
		Extracts the [link:https://en.wikipedia.org/wiki/Basis_(linear_algebra) basis] of this
L
looeee 已提交
159 160 161 162 163 164 165 166 167 168
		matrix into the three axis vectors provided. If this matrix is:
		<code>
a, b, c, d,
e, f, g, h,
i, j, k, l,
m, n, o, p
		</code>
		then the [page:Vector3 xAxis], [page:Vector3 yAxis], [page:Vector3 zAxis] will be set to:
		<code>
xAxis = (a, e, i)
169
yAxis = (b, f, j)
L
looeee 已提交
170 171
zAxis = (c, g, k)
		</code>
172
		</p>
173

174
		<h3>[method:this extractRotation]( [param:Matrix4 m] )</h3>
175
		<p>
L
looeee 已提交
176 177
		Extracts the rotation component of the supplied matrix [page:Matrix4 m] into this matrix's
		rotation component.
178
		</p>
179

180
		<h3>[method:this fromArray]( [param:Array array], [param:Integer offset] )</h3>
181
		<p>
L
looeee 已提交
182 183 184 185 186
		[page:Array array] - the array to read the elements from.<br />
		[page:Integer offset] - ( optional ) offset into the array. Default is 0.<br /><br />

		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.
187
		</p>
188

M
Maciek Jurczyk 已提交
189
		<h3>[method:this invert]()</h3>
190
		<p>
M
Mugen87 已提交
191
		Inverts this matrix, using the [link:https://en.wikipedia.org/wiki/Invertible_matrix#Analytic_solution analytic method].
L
looeee 已提交
192

M
Mugen87 已提交
193
		You can not invert with a determinant of zero. If you attempt this, the method produces a zero matrix instead.
194
		</p>
195

L
looeee 已提交
196
		<h3>[method:Float getMaxScaleOnAxis]()</h3>
197
		<p>Gets the maximum scale value of the 3 axes.</p>
198

199
		<h3>[method:this identity]()</h3>
200
		<p>Resets this matrix to the [link:https://en.wikipedia.org/wiki/Identity_matrix identity matrix].</p>
201

202
		<h3>[method:this lookAt]( [param:Vector3 eye], [param:Vector3 center], [param:Vector3 up], )</h3>
203
		<p>
L
looeee 已提交
204 205
			Constructs a rotation matrix, looking from [page:Vector3 eye] towards [page:Vector3 center]
			oriented by the [page:Vector3 up] vector.
206
		</p>
207

208
		<h3>[method:this makeRotationAxis]( [param:Vector3 axis], [param:Float theta] )</h3>
209
		<p>
L
looeee 已提交
210 211 212 213 214 215
		[page:Vector3 axis] — Rotation axis, should be normalized.<br />
		[page:Float theta] — Rotation angle in radians.<br /><br />

		Sets this matrix as rotation transform around [page:Vector3 axis] by [page:Float theta] radians.<br />

		This is a somewhat controversial but mathematically sound alternative to rotating via [page:Quaternions].
M
makc 已提交
216
		See the discussion [link:https://www.gamedev.net/articles/programming/math-and-physics/do-we-really-need-quaternions-r1199 here].
217
		</p>
218

219
		<h3>[method:this makeBasis]( [param:Vector3 xAxis], [param:Vector3 yAxis], [param:Vector3 zAxis] )</h3>
220
		<p>
L
looeee 已提交
221 222 223 224 225 226 227 228
		Set this to the [link:https://en.wikipedia.org/wiki/Basis_(linear_algebra) basis] matrix consisting
		of the three provided basis vectors:
		<code>
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
		</code>
229
		</p>
L
looeee 已提交
230

231
		<h3>[method:this makePerspective]( [param:Float left], [param:Float right], [param:Float top], [param:Float bottom], [param:Float near], [param:Float far] )</h3>
232
		<p>
233
			Creates a [link:https://en.wikipedia.org/wiki/3D_projection#Perspective_projection perspective projection] matrix.
L
looeee 已提交
234
			This is used internally by [page:PerspectiveCamera.updateProjectionMatrix]()
235
		</p>
236

237
		<h3>[method:this makeOrthographic]( [param:Float left], [param:Float right], [param:Float top], [param:Float bottom], [param:Float near], [param:Float far] )</h3>
238
		<p>
L
looeee 已提交
239 240
		Creates an [link:https://en.wikipedia.org/wiki/Orthographic_projection orthographic projection] matrix.
		This is used internally by [page:OrthographicCamera.updateProjectionMatrix]().
241
		</p>
242

243
		<h3>[method:this makeRotationFromEuler]( [param:Euler euler] )</h3>
244
		<p>
L
looeee 已提交
245 246 247
		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.
248
		</p>
249

250
		<h3>[method:this makeRotationFromQuaternion]( [param:Quaternion q] )</h3>
251
		<p>
L
looeee 已提交
252 253 254 255
		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:
		<code>
C
Calvin Huang 已提交
256
1-2y²-2z²    2xy-2zw    2xz+2yw    0
L
looeee 已提交
257 258 259 260
2xy+2zw      1-2x²-2z²  2yz-2xw    0
2xz-2yw      2yz+2xw    1-2x²-2y²  0
0            0          0          1
		</code>
261
		</p>
262

263
		<h3>[method:this makeRotationX]( [param:Float theta] )</h3>
264
		<p>
L
looeee 已提交
265 266 267 268 269 270 271 272 273 274
		[page:Float theta] — Rotation angle in radians.<br /><br />

		Sets this matrix as a rotational transformation around the X axis by [page:Float theta] (&theta;) radians.
		The resulting matrix will be:
		<code>
1 0      0        0
0 cos(&theta;) -sin(&theta;)  0
0 sin(&theta;) cos(&theta;)   0
0 0      0        1
		</code>
275
		</p>
276

277
		<h3>[method:this makeRotationY]( [param:Float theta] )</h3>
278
		<p>
L
looeee 已提交
279 280 281 282 283 284 285 286 287 288
		[page:Float theta] — Rotation angle in radians.<br /><br />

		Sets this matrix as a rotational transformation around the Y axis by [page:Float theta] (&theta;) radians.
		The resulting matrix will be:
		<code>
cos(&theta;)  0 sin(&theta;) 0
0       1 0      0
-sin(&theta;) 0 cos(&theta;) 0
0       0 0      1
		</code>
289
		</p>
290

291
		<h3>[method:this makeRotationZ]( [param:Float theta] )</h3>
292
		<p>
L
looeee 已提交
293 294 295 296 297 298 299 300 301 302
		[page:Float theta] — Rotation angle in radians.<br /><br />

		Sets this matrix as a rotational transformation around the Z axis by [page:Float theta] (&theta;) radians.
		The resulting matrix will be:
		<code>
cos(&theta;) -sin(&theta;) 0 0
sin(&theta;) cos(&theta;)  0 0
0      0       1 0
0      0       0 1
		</code>
303
		</p>
M
Mr.doob 已提交
304

305
		<h3>[method:this makeScale]( [param:Float x], [param:Float y], [param:Float z] )</h3>
306
		<p>
L
looeee 已提交
307 308 309 310 311 312 313 314 315 316 317
			[page:Float x] - the amount to scale in the X axis.<br />
			[page:Float y] - the amount to scale in the Y axis.<br />
			[page:Float z] - the amount to scale in the Z axis.<br /><br />

			Sets this matrix as scale transform:
			<code>
x, 0, 0, 0,
0, y, 0, 0,
0, 0, z, 0,
0, 0, 0, 1
			</code>
318
		</p>
L
looeee 已提交
319

320
		<h3>[method:this makeShear]( [param:Float x], [param:Float y], [param:Float z] )</h3>
321
		<p>
L
looeee 已提交
322 323 324 325 326 327 328 329 330 331 332
		[page:Float x] - the amount to shear in the X axis.<br />
		[page:Float y] - the amount to shear in the Y axis.<br />
		[page:Float z] - the amount to shear in the Z axis.<br /><br />

		Sets this matrix as a shear transform:
<code>
1, y, z, 0,
x, 1, z, 0,
x, y, 1, 0,
0, 0, 0, 1
</code>
333
		</p>
M
Mr.doob 已提交
334

335
		<h3>[method:this makeTranslation]( [param:Float x], [param:Float y], [param:Float z] )</h3>
336
		<p>
L
looeee 已提交
337 338 339 340 341 342 343 344 345 346 347
			[page:Float x] - the amount to translate in the X axis.<br />
			[page:Float y] - the amount to translate in the Y axis.<br />
			[page:Float z] - the amount to translate in the Z axis.<br /><br />

		Sets this matrix as a translation transform:
		<code>
1, 0, 0, x,
0, 1, 0, y,
0, 0, 1, z,
0, 0, 0, 1
		</code>
348
		</p>
349

350
		<h3>[method:this multiply]( [param:Matrix4 m] )</h3>
351
		<p>Post-multiplies this matrix by [page:Matrix4 m].</p>
352

353
		<h3>[method:this multiplyMatrices]( [param:Matrix4 a], [param:Matrix4 b] )</h3>
354
		<p>Sets this matrix to [page:Matrix4 a] x [page:Matrix4 b].</p>
355

356
		<h3>[method:this multiplyScalar]( [param:Float s] )</h3>
357
		<p>Multiplies every component of the matrix by a scalar value [page:Float s].</p>
L
looeee 已提交
358

359
		<h3>[method:this premultiply]( [param:Matrix4 m] )</h3>
360
		<p>Pre-multiplies this matrix by [page:Matrix4 m].</p>
L
looeee 已提交
361

362
		<h3>[method:this scale]( [param:Vector3 v] )</h3>
363
		<p>Multiplies the columns of this matrix by vector [page:Vector3 v].</p>
C
cjshannon 已提交
364

365
		<h3>[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] )</h3>
366
		<p>
L
looeee 已提交
367 368
			Set the [page:.elements elements] of this matrix to the supplied row-major values [page:Float n11],
			[page:Float n12], ... [page:Float n44].
369
		</p>
C
cjshannon 已提交
370

G
Garrett Johnson 已提交
371 372 373
		<h3>[method:this setFromMatrix3]( [param:Matrix3 m] )</h3>
		<p>Set the upper 3x3 elements of this matrix to the values of the Matrix3 [page:Matrix3 m].</p>

374
		<h3>[method:this setPosition]( [param:Vector3 v] )</h3>
375
		<h3>[method:this setPosition]( [param:Float x], [param:Float y], [param:Float z] ) // optional API</h3>
376
		<p>
L
looeee 已提交
377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
			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:
<code>
a, b, c, d,
e, f, g, h,
i, j, k, l,
m, n, o, p
</code>
This becomes:
<code>
a, b, c, v.x,
e, f, g, v.y,
i, j, k, v.z,
m, n, o, p
</code>
392
		</p>
T
tschw 已提交
393

394
		<h3>[method:Array toArray]( [param:Array array], [param:Integer offset] )</h3>
395
		<p>
L
looeee 已提交
396 397 398 399 400
		[page:Array array] - (optional) array to store the resulting vector in.<br />
		[page:Integer offset] - (optional) offset in the array at which to put the result.<br /><br />

		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.
401
		</p>
T
tschw 已提交
402

403
		<h3>[method:this transpose]()</h3>
404
		<p>[link:https://en.wikipedia.org/wiki/Transpose Transposes] this matrix.</p>
L
looeee 已提交
405

406 407
		<h2>Source</h2>

M
Mugen87 已提交
408 409 410
		<p>
			[link:https://github.com/mrdoob/three.js/blob/master/src/[path].js src/[path].js]
		</p>
411 412
	</body>
</html>