Matrix4.rst 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. Matrix4 - A 4x4 Matrix
  2. ----------------------
  3. .. ...............................................................................
  4. .. rubric:: Constructor
  5. .. ...............................................................................
  6. .. class:: Matrix4( n11, n12, n13, n14, n21, n22, n23, n24, n31, n32, n33, n34, n41, n42, n43, n44 )
  7. A 4x4 Matrix
  8. .. ...............................................................................
  9. .. rubric:: Attributes
  10. .. ...............................................................................
  11. .. attribute:: Matrix4.n11
  12. .. attribute:: Matrix4.n12
  13. .. attribute:: Matrix4.n13
  14. .. attribute:: Matrix4.n14
  15. .. attribute:: Matrix4.n21
  16. .. attribute:: Matrix4.n22
  17. .. attribute:: Matrix4.n23
  18. .. attribute:: Matrix4.n24
  19. .. attribute:: Matrix4.n31
  20. .. attribute:: Matrix4.n32
  21. .. attribute:: Matrix4.n33
  22. .. attribute:: Matrix4.n34
  23. .. attribute:: Matrix4.n41
  24. .. attribute:: Matrix4.n42
  25. .. attribute:: Matrix4.n43
  26. .. attribute:: Matrix4.n44
  27. .. ...............................................................................
  28. .. rubric:: Methods
  29. .. ...............................................................................
  30. .. function:: Matrix4.clone( )
  31. Clones this matrix
  32. :returns: New instance identical to this matrix
  33. :rtype: :class:`Matrix4`
  34. .. function:: Matrix4.set( n11, n12, n13, n14, n21, n22, n23, n24, n31, n32, n33, n34, n41, n42, n43, n44 )
  35. Sets all fields of this matrix
  36. :returns: This matrix
  37. :rtype: :class:`Matrix4`
  38. .. function:: Matrix4.identity()
  39. Resets this matrix to identity
  40. :returns: This matrix
  41. :rtype: :class:`Matrix4`
  42. .. function:: Matrix4.copy( m )
  43. Copies a matrix ``m`` into this matrix
  44. :param Matrix4 m: Matrix to be copied
  45. :returns: This matrix
  46. :rtype: :class:`Matrix4`
  47. .. function:: Matrix4.lookAt( eye, center, up )
  48. Constructs rotation matrix, looking from ``eye`` towards ``center`` with defined ``up`` vector
  49. :param Vector3 eye: vector
  50. :param Vector3 center: vector
  51. :param Vector3 up: vector
  52. :returns: This matrix
  53. :rtype: :class:`Matrix4`
  54. .. function:: Matrix4.multiply( a, b )
  55. Sets this matrix to ``a * b``
  56. :param Matrix4 a: source matrix A
  57. :param Matrix4 b: source matrix B
  58. :returns: This matrix
  59. :rtype: :class:`Matrix4`
  60. .. function:: Matrix4.multiplyToArray( a, b, r )
  61. Sets this matrix to ``a * b`` and sets result into flat array ``r``
  62. Destination array can be regular JS array or Typed Array
  63. :param Matrix4 a: source matrix A
  64. :param Matrix4 b: source matrix B
  65. :param array r: destination array
  66. :returns: This matrix
  67. :rtype: :class:`Matrix4`
  68. .. function:: Matrix4.multiplySelf( a )
  69. Multiplies this matrix by ``a``
  70. :param Matrix4 a: matrix
  71. :returns: This matrix
  72. :rtype: :class:`Matrix4`
  73. .. function:: Matrix4.multiplyScalar( s )
  74. Multiplies this matrix by ``s``
  75. :param float a: number
  76. :returns: This matrix
  77. :rtype: :class:`Matrix4`
  78. .. function:: Matrix4.multiplyVector3( v )
  79. Applies this matrix to a :class:`Vector3`
  80. :param Vector3 v: vector
  81. :returns: Multiplied vector
  82. :rtype: :class:`Vector3`
  83. .. function:: Matrix4.multiplyVector4( v )
  84. Applies this matrix to a :class:`Vector4`
  85. :param Vector4 v: vector
  86. :returns: Multiplied vector
  87. :rtype: :class:`Vector4`
  88. .. function:: Matrix4.rotateAxis( v )
  89. Applies rotation submatrix of this matrix to vector ``v`` and then normalizes it
  90. :param Vector3 v: vector
  91. :returns: Rotated vector
  92. :rtype: :class:`Vector3`
  93. .. function:: Matrix4.crossVector( a )
  94. //todo:description
  95. :param Vector4 a: vector
  96. :rtype: :class:`Vector4`
  97. .. function:: Matrix4.determinant()
  98. Computes determinant of this matrix
  99. Based on http://www.euclideanspace.com/maths/algebra/matrix/functions/inverse/fourD/index.htm
  100. :returns: Determinant
  101. :rtype: float
  102. .. function:: Matrix4.transpose()
  103. Transposes this matrix
  104. :returns: This matrix
  105. :rtype: :class:`Matrix4`
  106. .. function:: Matrix4.flatten()
  107. Flattens this matrix into internal :attr:`Matrix4.flat` array
  108. :returns: Flat array with this matrix values
  109. :rtype: array
  110. .. function:: Matrix4.flattenToArray( flat )
  111. Flattens this matrix into supplied ``flat`` array
  112. :param array flat: array
  113. :returns: Flat array with this matrix values
  114. :rtype: array
  115. .. function:: Matrix4.flattenToArrayOffset( flat, offset )
  116. Flattens this matrix into supplied ``flat`` array starting from ``offset`` position in the array
  117. :param array flat: array
  118. :param integer offset: offset
  119. :returns: Flat array with this matrix values
  120. :rtype: array
  121. .. function:: Matrix4.setTranslation( x, y, z )
  122. Sets this matrix as translation transform
  123. :param float x: x-translation
  124. :param float y: y-translation
  125. :param float z: z-translation
  126. :returns: This matrix
  127. :rtype: :class:`Matrix4`
  128. .. function:: Matrix4.setScale( x, y, z )
  129. Sets this matrix as scale transform
  130. :param float x: x-scale
  131. :param float y: y-scale
  132. :param float z: z-scale
  133. :returns: This matrix
  134. :rtype: :class:`Matrix4`
  135. .. function:: Matrix4.setRotationX( theta )
  136. Sets this matrix as rotation transform around x-axis by ``theta`` radians
  137. :param float theta: Rotation angle in radians
  138. :returns: This matrix
  139. :rtype: :class:`Matrix4`
  140. .. function:: Matrix4.setRotationY( theta )
  141. Sets this matrix as rotation transform around y-axis by ``theta`` radians
  142. :param float theta: Rotation angle in radians
  143. :returns: This matrix
  144. :rtype: :class:`Matrix4`
  145. .. function:: Matrix4.setRotationZ( theta )
  146. Sets this matrix as rotation transform around z-axis by ``theta`` radians
  147. :param float theta: Rotation angle in radians
  148. :returns: This matrix
  149. :rtype: :class:`Matrix4`
  150. .. function:: Matrix4.setRotationAxis( axis, angle )
  151. Sets this matrix as rotation transform around ``axis`` by ``angle`` radians
  152. Based on http://www.gamedev.net/reference/articles/article1199.asp
  153. :param Vector3 axis: Rotation axis
  154. :param float angle: Rotation angle in radians
  155. :returns: This matrix
  156. :rtype: :class:`Matrix4`
  157. .. function:: Matrix4.setPosition( v )
  158. Sets just position component for this matrix from vector ``v``
  159. :param Vector3 v: position vector
  160. :returns: This matrix
  161. :rtype: :class:`Matrix4`
  162. .. function:: Matrix4.getPosition()
  163. Returns position component from this matrix
  164. Note: this method returns a reference to internal class vector, make copy or clone if you don't use it right away.
  165. :returns: Vector with position
  166. :rtype: :class:`Vector3`
  167. .. function:: Matrix4.getColumnX()
  168. Returns x-column component from this matrix
  169. Note: this method returns a reference to internal class vector, make copy or clone if you don't use it right away.
  170. :returns: Vector with x-column
  171. :rtype: :class:`Vector3`
  172. .. function:: Matrix4.getColumnY()
  173. Returns y-column component from this matrix
  174. Note: this method returns a reference to internal class vector, make copy or clone if you don't use it right away.
  175. :returns: Vector with y-column
  176. :rtype: :class:`Vector3`
  177. .. function:: Matrix4.getColumnZ()
  178. Returns z-column component from this matrix
  179. Note: this method returns a reference to internal class vector, make copy or clone if you don't use it right away.
  180. :returns: Vector with z-column
  181. :rtype: :class:`Vector3`
  182. .. function:: Matrix4.getInverse( m )
  183. Sets this matrix to inverse of matrix ``m``
  184. Based on http://www.euclideanspace.com/maths/algebra/matrix/functions/inverse/fourD/index.htm
  185. :param Matrix4 m: source matrix
  186. :returns: This matrix
  187. :rtype: :class:`Matrix4`
  188. .. function:: Matrix4.setRotationFromEuler( v, order )
  189. Sets rotation submatrix of this matrix to rotation specified by Euler angles
  190. Default order ``XYZ``
  191. :param Vector3 v: Vector3 with all the rotations
  192. :param string order: The order of rotations eg. 'XYZ'
  193. :returns: This matrix
  194. :rtype: :class:`Matrix4`
  195. .. function:: Matrix4.setRotationFromQuaternion( q )
  196. Sets rotation submatrix of this matrix to rotation specified by quaternion
  197. :param Quaternion q: rotation
  198. :returns: This matrix
  199. :rtype: :class:`Matrix4`
  200. .. function:: Matrix4.scale( v )
  201. Multiplies columns of this matrix by vector ``v``
  202. :param Vector3 v: scale vector
  203. :returns: This matrix
  204. :rtype: :class:`Matrix4`
  205. .. function:: Matrix4.compose( translation, rotation, scale )
  206. Sets this matrix to transform composed of ``translation``, ``rotation`` and ``scale``
  207. :param Vector3 translation: vector
  208. :param Quaternion rotation: quaternion
  209. :param Vector3 scale: vector
  210. :returns: This matrix
  211. :rtype: :class:`Matrix4`
  212. .. function:: Matrix4.decompose( translation, rotation, scale )
  213. Decomposes this matrix into `translation``, ``rotation`` and ``scale`` components
  214. If parameters are not supplied, new instances will be created
  215. :param Vector3 translation: destination translation vector
  216. :param Quaternion rotation: destination rotation quaternion
  217. :param Vector3 scale: destination scale vector
  218. :returns: Array [ translation, rotation, scale ]
  219. :rtype: Array
  220. .. function:: Matrix4.extractPosition( m )
  221. Copies translation component of supplied matrix ``m`` into this matrix translation
  222. :param Matrix4 m: source matrix
  223. :returns: This matrix
  224. :rtype: :class:`Matrix4`
  225. .. function:: Matrix4.extractRotation( m )
  226. Copies rotation component of supplied matrix ``m`` into this matrix rotation
  227. :param Matrix4 m: source matrix
  228. :returns: This matrix
  229. :rtype: :class:`Matrix4`
  230. .. function:: Matrix4.rotateByAxis( axis, angle )
  231. Rotates this matrix around supplied ``axis`` by ``angle``
  232. :param Vector3 axis: rotation axis
  233. :param float angle: rotation angle in radians
  234. :returns: This matrix
  235. :rtype: :class:`Matrix4`
  236. .. function:: Matrix4.rotateX( angle )
  237. Rotates this matrix around x-axis by ``angle``
  238. :param float angle: rotation angle in radians
  239. :returns: This matrix
  240. :rtype: :class:`Matrix4`
  241. .. function:: Matrix4.rotateY( angle )
  242. Rotates this matrix around y-axis by ``angle``
  243. :param float angle: rotation angle in radians
  244. :returns: This matrix
  245. :rtype: :class:`Matrix4`
  246. .. function:: Matrix4.rotateZ( angle )
  247. Rotates this matrix around z-axis by ``angle``
  248. :param float angle: rotation angle in radians
  249. :returns: This matrix
  250. :rtype: :class:`Matrix4`
  251. .. function:: Matrix4.translate( v )
  252. Translates this matrix by vector ``v``
  253. :param Vector3 v: translation vector
  254. :returns: This matrix
  255. :rtype: :class:`Matrix4`
  256. .. ...............................................................................
  257. .. rubric:: Static methods
  258. .. ...............................................................................
  259. .. function:: Matrix4.makeInvert3x3( m )
  260. Inverts just rotation submatrix of matrix ``m``
  261. Note: this method returns a reference to internal 3x3 matrix, make copy or clone if you don't use it right away.
  262. Based on http://code.google.com/p/webgl-mjs/
  263. :param Matrix4 m: source matrix
  264. :returns: inverted submatrix
  265. :rtype: :class:`Matrix3`
  266. .. function:: Matrix4.makeFrustum( left, right, bottom, top, near, far )
  267. Creates frustum matrix
  268. :param float left: left
  269. :param float right: right
  270. :param float bottom: bottom
  271. :param float top: top
  272. :param float near: near
  273. :param float far: far
  274. :returns: New instance of frustum matrix
  275. :rtype: :class:`Matrix4`
  276. .. function:: Matrix4.makePerspective( fov, aspect, near, far )
  277. Creates perspective projection matrix
  278. :param float fov: vertical field of view in degrees
  279. :param float aspect: aspect ratio
  280. :param float near: near plane
  281. :param float far: far plane
  282. :returns: New instance of projection matrix
  283. :rtype: :class:`Matrix4`
  284. .. function:: Matrix4.makeOrtho( left, right, top, bottom, near, far )
  285. Creates orthographic projection matrix
  286. :param float left: left
  287. :param float right: right
  288. :param float top: top
  289. :param float bottom: bottom
  290. :param float near: near plane
  291. :param float far: far plane
  292. :returns: New instance of projection matrix
  293. :rtype: :class:`Matrix4`
  294. .. ...............................................................................
  295. .. rubric:: Example
  296. .. ...............................................................................
  297. ::
  298. // simple rig for rotation around 3 axes
  299. var m = new THREE.Matrix4();
  300. var m1 = new THREE.Matrix4();
  301. var m2 = new THREE.Matrix4();
  302. var m3 = new THREE.Matrix4();
  303. var alpha = 0;
  304. var beta = Math.PI;
  305. var gamma = Math.PI/2;
  306. m1.setRotationX( alpha );
  307. m2.setRotationY( beta );
  308. m3.setRotationZ( gamma );
  309. m.multiply( m1, m2 );
  310. m.multiplySelf( m3 );