Matrix4.html 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8" />
  5. <base href="../../../" />
  6. <script src="page.js"></script>
  7. <link type="text/css" rel="stylesheet" href="page.css" />
  8. </head>
  9. <body>
  10. <h1>[name]</h1>
  11. <p class="desc">
  12. A class representing a 4x4
  13. [link:https://en.wikipedia.org/wiki/Matrix_(mathematics) matrix].<br /><br />
  14. The most common use of a 4x4 matrix in 3D computer graphics is as a
  15. [link:https://en.wikipedia.org/wiki/Transformation_matrix Transformation Matrix].
  16. For an introduction to transformation matrices as used in WebGL,
  17. check out
  18. [link:http://www.opengl-tutorial.org/beginners-tutorials/tutorial-3-matrices this tutorial].<br /><br />
  19. This allows a [page:Vector3] representing a point in 3D space to undergo
  20. transformations such as translation, rotation, shear, scale, reflection,
  21. orthogonal or perspective projection and so on, by being multiplied by the
  22. matrix. This is known as `applying` the matrix to the vector.<br /><br />
  23. Every [page:Object3D] has three associated Matrix4s:
  24. </p>
  25. <ul>
  26. <li>
  27. [page:Object3D.matrix]: This stores the local transform of the object.
  28. This is the object's transformation relative to its parent.
  29. </li>
  30. <li>
  31. [page:Object3D.matrixWorld]: The global or world transform of the
  32. object. If the object has no parent, then this is identical to the local
  33. transform stored in [page:Object3D.matrix matrix].
  34. </li>
  35. <li>
  36. [page:Object3D.modelViewMatrix]: This represents the object's
  37. transformation relative to the camera's coordinate system. An object's
  38. modelViewMatrix is the object's matrixWorld pre-multiplied by the
  39. camera's matrixWorldInverse.
  40. </li>
  41. </ul>
  42. [page:Camera Cameras] have three additional Matrix4s:
  43. <ul>
  44. <li>
  45. [page:Camera.matrixWorldInverse]: The view matrix - the inverse of the
  46. Camera's [page:Object3D.matrixWorld matrixWorld].
  47. </li>
  48. <li>
  49. [page:Camera.projectionMatrix]: Represents the information how to
  50. project the scene to clip space.
  51. </li>
  52. <li>
  53. [page:Camera.projectionMatrixInverse]: The inverse of projectionMatrix.
  54. </li>
  55. </ul>
  56. Note: [page:Object3D.normalMatrix] is not a Matrix4, but a [page:Matrix3].
  57. <h2>A Note on Row-Major and Column-Major Ordering</h2>
  58. <p>
  59. The constructor and [page:set]() method take arguments in
  60. [link:https://en.wikipedia.org/wiki/Row-_and_column-major_order#Column-major_order row-major]
  61. order, while internally they are stored in the [page:.elements elements] array in column-major order.<br /><br />
  62. This means that calling
  63. <code>
  64. const m = new THREE.Matrix4();
  65. m.set( 11, 12, 13, 14,
  66. 21, 22, 23, 24,
  67. 31, 32, 33, 34,
  68. 41, 42, 43, 44 );
  69. </code>
  70. will result in the [page:.elements elements] array containing:
  71. <code>
  72. m.elements = [ 11, 21, 31, 41,
  73. 12, 22, 32, 42,
  74. 13, 23, 33, 43,
  75. 14, 24, 34, 44 ];
  76. </code>
  77. and internally all calculations are performed using column-major ordering.
  78. However, as the actual ordering makes no difference mathematically and
  79. most people are used to thinking about matrices in row-major order, the
  80. three.js documentation shows matrices in row-major order. Just bear in
  81. mind that if you are reading the source code, you'll have to take the
  82. [link:https://en.wikipedia.org/wiki/Transpose transpose] of any matrices
  83. outlined here to make sense of the calculations.
  84. </p>
  85. <h2>Extracting position, rotation and scale</h2>
  86. <p>
  87. There are several options available for extracting position, rotation and
  88. scale from a Matrix4.
  89. </p>
  90. <ul>
  91. <li>
  92. [page:Vector3.setFromMatrixPosition]: can be used to extract the
  93. translation component.
  94. </li>
  95. <li>
  96. [page:Vector3.setFromMatrixScale]: can be used to extract the scale
  97. component.
  98. </li>
  99. <li>
  100. [page:Quaternion.setFromRotationMatrix],
  101. [page:Euler.setFromRotationMatrix] or [page:.extractRotation extractRotation]
  102. can be used to extract the rotation component from a pure (unscaled) matrix.
  103. </li>
  104. <li>
  105. [page:.decompose decompose] can be used to extract position, rotation
  106. and scale all at once.
  107. </li>
  108. </ul>
  109. <h2>Constructor</h2>
  110. <h3>[name]( [param:Number n11], [param:Number n12], [param:Number n13], [param:Number n14],
  111. [param:Number n21], [param:Number n22], [param:Number n23], [param:Number n24],
  112. [param:Number n31], [param:Number n32], [param:Number n33], [param:Number n34],
  113. [param:Number n41], [param:Number n42], [param:Number n43], [param:Number n44] )</h3>
  114. <p>
  115. Creates a 4x4 matrix with the given arguments in row-major order. If no arguments are provided, the constructor initializes
  116. the [name] to the 4x4 [link:https://en.wikipedia.org/wiki/Identity_matrix identity matrix].
  117. </p>
  118. <h2>Properties</h2>
  119. <h3>[property:Array elements]</h3>
  120. <p>
  121. A
  122. [link:https://en.wikipedia.org/wiki/Row-_and_column-major_order#Column-major_order column-major] list of matrix values.
  123. </p>
  124. <h2>Methods</h2>
  125. <h3>[method:Matrix4 clone]()</h3>
  126. <p>
  127. Creates a new Matrix4 with identical [page:.elements elements] to this
  128. one.
  129. </p>
  130. <h3>
  131. [method:this compose]( [param:Vector3 position], [param:Quaternion quaternion], [param:Vector3 scale] )
  132. </h3>
  133. <p>
  134. Sets this matrix to the transformation composed of [page:Vector3 position],
  135. [page:Quaternion quaternion] and [page:Vector3 scale].
  136. </p>
  137. <h3>[method:this copy]( [param:Matrix4 m] )</h3>
  138. <p>
  139. Copies the [page:.elements elements] of matrix [page:Matrix4 m] into this
  140. matrix.
  141. </p>
  142. <h3>[method:this copyPosition]( [param:Matrix4 m] )</h3>
  143. <p>
  144. Copies the translation component of the supplied matrix [page:Matrix4 m]
  145. into this matrix's translation component.
  146. </p>
  147. <h3>
  148. [method:this decompose]( [param:Vector3 position], [param:Quaternion quaternion], [param:Vector3 scale] )
  149. </h3>
  150. <p>
  151. Decomposes this matrix into its [page:Vector3 position], [page:Quaternion quaternion]
  152. and [page:Vector3 scale] components.<br /><br />
  153. Note: Not all matrices are decomposable in this way. For example, if an
  154. object has a non-uniformly scaled parent, then the object's world matrix
  155. may not be decomposable, and this method may not be appropriate.
  156. </p>
  157. <h3>[method:Float determinant]()</h3>
  158. <p>
  159. Computes and returns the [link:https://en.wikipedia.org/wiki/Determinant determinant] of this matrix.<br /><br />
  160. Based on the method outlined
  161. [link:http://www.euclideanspace.com/maths/algebra/matrix/functions/inverse/fourD/index.html here].
  162. </p>
  163. <h3>[method:Boolean equals]( [param:Matrix4 m] )</h3>
  164. <p>Return true if this matrix and [page:Matrix4 m] are equal.</p>
  165. <h3>
  166. [method:this extractBasis]( [param:Vector3 xAxis], [param:Vector3 yAxis], [param:Vector3 zAxis] )
  167. </h3>
  168. <p>
  169. Extracts the [link:https://en.wikipedia.org/wiki/Basis_(linear_algebra) basis]
  170. of this matrix into the three axis vectors provided. If this matrix
  171. is:
  172. <code>
  173. a, b, c, d,
  174. e, f, g, h,
  175. i, j, k, l,
  176. m, n, o, p </code>
  177. then the [page:Vector3 xAxis], [page:Vector3 yAxis], [page:Vector3 zAxis]
  178. will be set to:
  179. <code>
  180. xAxis = (a, e, i)
  181. yAxis = (b, f, j)
  182. zAxis = (c, g, k) </code>
  183. </p>
  184. <h3>[method:this extractRotation]( [param:Matrix4 m] )</h3>
  185. <p>
  186. Extracts the rotation component of the supplied matrix [page:Matrix4 m]
  187. into this matrix's rotation component.
  188. </p>
  189. <h3>
  190. [method:this fromArray]( [param:Array array], [param:Integer offset] )
  191. </h3>
  192. <p>
  193. [page:Array array] - the array to read the elements from.<br />
  194. [page:Integer offset] - ( optional ) offset into the array. Default is
  195. 0.<br /><br />
  196. Sets the elements of this matrix based on an [page:Array array] in
  197. [link:https://en.wikipedia.org/wiki/Row-_and_column-major_order#Column-major_order column-major] format.
  198. </p>
  199. <h3>[method:this invert]()</h3>
  200. <p>
  201. Inverts this matrix, using the
  202. [link:https://en.wikipedia.org/wiki/Invertible_matrix#Analytic_solution analytic method].
  203. You can not invert with a determinant of zero. If you
  204. attempt this, the method produces a zero matrix instead.
  205. </p>
  206. <h3>[method:Float getMaxScaleOnAxis]()</h3>
  207. <p>Gets the maximum scale value of the 3 axes.</p>
  208. <h3>[method:this identity]()</h3>
  209. <p>
  210. Resets this matrix to the
  211. [link:https://en.wikipedia.org/wiki/Identity_matrix identity matrix].
  212. </p>
  213. <h3>
  214. [method:this lookAt]( [param:Vector3 eye], [param:Vector3 target], [param:Vector3 up] )
  215. </h3>
  216. <p>
  217. Constructs a rotation matrix, looking from [page:Vector3 eye] towards
  218. [page:Vector3 target] oriented by the [page:Vector3 up] vector.
  219. </p>
  220. <h3>
  221. [method:this makeRotationAxis]( [param:Vector3 axis], [param:Float theta] )
  222. </h3>
  223. <p>
  224. [page:Vector3 axis] — Rotation axis, should be normalized.<br />
  225. [page:Float theta] — Rotation angle in radians.<br /><br />
  226. Sets this matrix as rotation transform around [page:Vector3 axis] by
  227. [page:Float theta] radians.<br />
  228. This is a somewhat controversial but mathematically sound alternative to
  229. rotating via [page:Quaternion Quaternions]. See the discussion
  230. [link:https://www.gamedev.net/articles/programming/math-and-physics/do-we-really-need-quaternions-r1199 here].
  231. </p>
  232. <h3>
  233. [method:this makeBasis]( [param:Vector3 xAxis], [param:Vector3 yAxis], [param:Vector3 zAxis] )
  234. </h3>
  235. <p>
  236. Set this to the [link:https://en.wikipedia.org/wiki/Basis_(linear_algebra) basis]
  237. matrix consisting of the three provided basis vectors:
  238. <code>
  239. xAxis.x, yAxis.x, zAxis.x, 0,
  240. xAxis.y, yAxis.y, zAxis.y, 0,
  241. xAxis.z, yAxis.z, zAxis.z, 0,
  242. 0, 0, 0, 1
  243. </code>
  244. </p>
  245. <h3>
  246. [method:this makePerspective]( [param:Float left], [param:Float right], [param:Float top], [param:Float bottom], [param:Float near], [param:Float far] )
  247. </h3>
  248. <p>
  249. Creates a
  250. [link:https://en.wikipedia.org/wiki/3D_projection#Perspective_projection perspective projection]
  251. matrix. This is used internally by
  252. [page:PerspectiveCamera.updateProjectionMatrix]()
  253. </p>
  254. <h3>
  255. [method:this makeOrthographic]( [param:Float left], [param:Float right], [param:Float top], [param:Float bottom], [param:Float near], [param:Float far] )
  256. </h3>
  257. <p>
  258. Creates an [link:https://en.wikipedia.org/wiki/Orthographic_projection orthographic projection]
  259. matrix. This is used internally by
  260. [page:OrthographicCamera.updateProjectionMatrix]().
  261. </p>
  262. <h3>[method:this makeRotationFromEuler]( [param:Euler euler] )</h3>
  263. <p>
  264. Sets the rotation component (the upper left 3x3 matrix) of this matrix to
  265. the rotation specified by the given [page:Euler Euler Angle]. The rest of
  266. the matrix is set to the identity. Depending on the [page:Euler.order order]
  267. of the [page:Euler euler], there are six possible outcomes. See
  268. [link:https://en.wikipedia.org/wiki/Euler_angles#Rotation_matrix this page] for a complete list.
  269. </p>
  270. <h3>[method:this makeRotationFromQuaternion]( [param:Quaternion q] )</h3>
  271. <p>
  272. Sets the rotation component of this matrix to the rotation specified by
  273. [page:Quaternion q], as outlined
  274. [link:https://en.wikipedia.org/wiki/Rotation_matrix#Quaternion here]. The
  275. rest of the matrix is set to the identity. So, given [page:Quaternion q] =
  276. w + xi + yj + zk, the resulting matrix will be:
  277. <code>
  278. 1-2y²-2z² 2xy-2zw 2xz+2yw 0
  279. 2xy+2zw 1-2x²-2z² 2yz-2xw 0
  280. 2xz-2yw 2yz+2xw 1-2x²-2y² 0
  281. 0 0 0 1
  282. </code>
  283. </p>
  284. <h3>[method:this makeRotationX]( [param:Float theta] )</h3>
  285. <p>
  286. [page:Float theta] — Rotation angle in radians.<br /><br />
  287. Sets this matrix as a rotational transformation around the X axis by
  288. [page:Float theta] (&theta;) radians. The resulting matrix will be:
  289. <code>
  290. 1 0 0 0
  291. 0 cos(&theta;) -sin(&theta;) 0
  292. 0 sin(&theta;) cos(&theta;) 0
  293. 0 0 0 1
  294. </code>
  295. </p>
  296. <h3>[method:this makeRotationY]( [param:Float theta] )</h3>
  297. <p>
  298. [page:Float theta] — Rotation angle in radians.<br /><br />
  299. Sets this matrix as a rotational transformation around the Y axis by
  300. [page:Float theta] (&theta;) radians. The resulting matrix will be:
  301. <code>
  302. cos(&theta;) 0 sin(&theta;) 0 0 1 0 0 -sin(&theta;) 0 cos(&theta;) 0 0 0
  303. 0 1
  304. </code>
  305. </p>
  306. <h3>[method:this makeRotationZ]( [param:Float theta] )</h3>
  307. <p>
  308. [page:Float theta] — Rotation angle in radians.<br /><br />
  309. Sets this matrix as a rotational transformation around the Z axis by
  310. [page:Float theta] (&theta;) radians. The resulting matrix will be:
  311. <code>
  312. cos(&theta;) -sin(&theta;) 0 0
  313. sin(&theta;) cos(&theta;) 0 0
  314. 0 0 1 0
  315. 0 0 0 1
  316. </code>
  317. </p>
  318. <h3>
  319. [method:this makeScale]( [param:Float x], [param:Float y], [param:Float z] )
  320. </h3>
  321. <p>
  322. [page:Float x] - the amount to scale in the X axis.<br />
  323. [page:Float y] - the amount to scale in the Y axis.<br />
  324. [page:Float z] - the amount to scale in the Z axis.<br /><br />
  325. Sets this matrix as scale transform:
  326. <code>
  327. x, 0, 0, 0,
  328. 0, y, 0, 0,
  329. 0, 0, z, 0,
  330. 0, 0, 0, 1 </code>
  331. </p>
  332. <h3>
  333. [method:this makeShear]( [param:Float xy], [param:Float xz], [param:Float yx],
  334. [param:Float yz], [param:Float zx], [param:Float zy] )
  335. </h3>
  336. <p>
  337. [page:Float xy] - the amount to shear X by Y.<br />
  338. [page:Float xz] - the amount to shear X by Z.<br />
  339. [page:Float yx] - the amount to shear Y by X.<br />
  340. [page:Float yz] - the amount to shear Y by Z.<br />
  341. [page:Float zx] - the amount to shear Z by X.<br />
  342. [page:Float zy] - the amount to shear Z by Y.<br /><br />
  343. Sets this matrix as a shear transform:
  344. <code>
  345. 1, yx, zx, 0,
  346. xy, 1, zy, 0,
  347. xz, yz, 1, 0,
  348. 0, 0, 0, 1 </code>
  349. </p>
  350. <h3>[method:this makeTranslation]( [param:Vector3 v] )</h3>
  351. <h3>
  352. [method:this makeTranslation]( [param:Float x], [param:Float y], [param:Float z] ) // optional API
  353. </h3>
  354. <p>
  355. Sets this matrix as a translation transform from vector [page:Vector3 v], or numbers [page:Float x], [page:Float y] and [page:Float z]:
  356. <code>
  357. 1, 0, 0, x,
  358. 0, 1, 0, y,
  359. 0, 0, 1, z,
  360. 0, 0, 0, 1 </code>
  361. </p>
  362. <h3>[method:this multiply]( [param:Matrix4 m] )</h3>
  363. <p>Post-multiplies this matrix by [page:Matrix4 m].</p>
  364. <h3>
  365. [method:this multiplyMatrices]( [param:Matrix4 a], [param:Matrix4 b] )
  366. </h3>
  367. <p>Sets this matrix to [page:Matrix4 a] x [page:Matrix4 b].</p>
  368. <h3>[method:this multiplyScalar]( [param:Float s] )</h3>
  369. <p>
  370. Multiplies every component of the matrix by a scalar value [page:Float s].
  371. </p>
  372. <h3>[method:this premultiply]( [param:Matrix4 m] )</h3>
  373. <p>Pre-multiplies this matrix by [page:Matrix4 m].</p>
  374. <h3>[method:this scale]( [param:Vector3 v] )</h3>
  375. <p>Multiplies the columns of this matrix by vector [page:Vector3 v].</p>
  376. <h3>
  377. [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] )
  378. </h3>
  379. <p>
  380. Set the [page:.elements elements] of this matrix to the supplied row-major
  381. values [page:Float n11], [page:Float n12], ... [page:Float n44].
  382. </p>
  383. <h3>[method:this setFromMatrix3]( [param:Matrix3 m] )</h3>
  384. <p>
  385. Set the upper 3x3 elements of this matrix to the values of the Matrix3
  386. [page:Matrix3 m].
  387. </p>
  388. <h3>[method:this setPosition]( [param:Vector3 v] )</h3>
  389. <h3>
  390. [method:this setPosition]( [param:Float x], [param:Float y], [param:Float z] ) // optional API
  391. </h3>
  392. <p>
  393. Sets the position component for this matrix from vector [page:Vector3 v],
  394. without affecting the rest of the matrix - i.e. if the matrix is
  395. currently:
  396. <code>
  397. a, b, c, d,
  398. e, f, g, h,
  399. i, j, k, l,
  400. m, n, o, p </code>
  401. This becomes:
  402. <code>
  403. a, b, c, v.x,
  404. e, f, g, v.y,
  405. i, j, k, v.z,
  406. m, n, o, p </code>
  407. </p>
  408. <h3>
  409. [method:Array toArray]( [param:Array array], [param:Integer offset] )
  410. </h3>
  411. <p>
  412. [page:Array array] - (optional) array to store the resulting vector in.<br />
  413. [page:Integer offset] - (optional) offset in the array at which to put the
  414. result.<br /><br />
  415. Writes the elements of this matrix to an array in
  416. [link:https://en.wikipedia.org/wiki/Row-_and_column-major_order#Column-major_order column-major] format.
  417. </p>
  418. <h3>[method:this transpose]()</h3>
  419. <p>
  420. [link:https://en.wikipedia.org/wiki/Transpose Transposes] this matrix.
  421. </p>
  422. <h2>Source</h2>
  423. <p>
  424. [link:https://github.com/mrdoob/three.js/blob/master/src/[path].js src/[path].js]
  425. </p>
  426. </body>
  427. </html>