Matrix4.html 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8" />
  5. <base href="../../../" />
  6. <script src="list.js"></script>
  7. <script src="page.js"></script>
  8. <link type="text/css" rel="stylesheet" href="page.css" />
  9. </head>
  10. <body>
  11. <h1>四维矩阵([name])</h1>
  12. <p class="desc">
  13. 表示为一个 4x4 [link:https://en.wikipedia.org/wiki/Matrix_(mathematics) matrix].<br /><br />
  14. 在3D计算机图形学中,4x4矩阵最常用的用法是作为一个变换矩阵[link:https://en.wikipedia.org/wiki/Transformation_matrix Transformation Matrix]。
  15. 有关WebGL中使用的变换矩阵的介绍,请参阅本教程[link:http://www.opengl-tutorial.org/beginners-tutorials/tutorial-3-matrices this tutorial]。<br /><br />
  16. 这使得表示三维空间中的一个点的向量[page:Vector3]通过乘以矩阵来进行转换,如平移、旋转、剪切、缩放、反射、正交或透视投影等。这就是把矩阵<em>应用</em>到向量上。<br /><br />
  17. 任何3D物体[page:Object3D]都有三个关联的矩阵:
  18. <ul>
  19. <li>
  20. [page:Object3D.matrix]: 存储物体的本地变换。 这是对象相对于其父对象的变换。
  21. </li>
  22. <li>
  23. [page:Object3D.matrixWorld]: 对象的全局或世界变换。如果对象没有父对象,那么这与存储在矩阵[page:Object3D.matrix matrix]中的本地变换相同。
  24. </li>
  25. <li>
  26. [page:Object3D.modelViewMatrix]: 表示对象相坐标相对于摄像机空间坐标转换,
  27. 一个对象的 modelViewMatrix 是物体世界变换矩阵乘以摄像机相对于世界空间变换矩阵的逆矩阵。
  28. </li>
  29. </ul>
  30. 摄像机[page:Camera Cameras] 有两个额外的四维矩阵:
  31. <ul>
  32. <li>
  33. [page:Camera.matrixWorldInverse]: 视矩阵 - 摄像机世界坐标变换的逆矩阵。
  34. </li>
  35. <li>
  36. [page:Camera.projectionMatrix]: 表示将场景中的信息投影到裁剪空间。
  37. </li>
  38. </ul>
  39. 注意:物体的正规矩阵 [page:Object3D.normalMatrix] 并不是一个4维矩阵,而是一个三维矩阵[page:Matrix3]。
  40. </p>
  41. <h2>注意行优先列优先的顺序。</h2>
  42. <p>
  43. 设置[page:set]()方法参数采用行优先[link:https://en.wikipedia.org/wiki/Row-_and_column-major_order#Column-major_order row-major],
  44. 而它们在内部是用列优先[link:https://en.wikipedia.org/wiki/Row-_and_column-major_order column-major]顺序存储在数组当中。<br /><br />
  45. 这意味着
  46. <code>
  47. var m = new THREE.Matrix4();
  48. m.set( 11, 12, 13, 14,
  49. 21, 22, 23, 24,
  50. 31, 32, 33, 34,
  51. 41, 42, 43, 44 );
  52. </code>
  53. 元素数组[page:.elements elements]将存储为:
  54. <code>
  55. m.elements = [ 11, 21, 31, 41,
  56. 12, 22, 32, 42,
  57. 13, 23, 33, 43,
  58. 14, 24, 34, 44 ];
  59. </code>
  60. 在内部,所有的计算都是使用列优先顺序进行的。然而,由于实际的排序在数学上没有什么不同,
  61. 而且大多数人习惯于以行优先顺序考虑矩阵,所以three.js文档以行为主的顺序显示矩阵。
  62. 请记住,如果您正在阅读源代码,您必须对这里列出的任何矩阵进行转置[link:https://en.wikipedia.org/wiki/Transpose transpose],以理解计算。
  63. </p>
  64. <h2>Extracting position, rotation and scale</h2>
  65. <p>
  66. There are several options available for extracting position, rotation and scale from a Matrix4.
  67. <ul>
  68. <li>
  69. [page:Vector3.setFromMatrixPosition]: can be used to extract the translation component.
  70. </li>
  71. <li>
  72. [page:Vector3.setFromMatrixScale]: can be used to extract the scale component.
  73. </li>
  74. <li>
  75. [page:Quaternion.setFromRotationMatrix], [page:Euler.setFromRotationMatrix] or [page:.extractRotation extractRotation] can be used to extract the rotation component.
  76. </li>
  77. <li>
  78. [page:.decompose decompose] can be used to extract position, rotation and scale all at once.
  79. </li>
  80. </ul>
  81. </p>
  82. <h2>构造器(Constructor)</h2>
  83. <h3>[name]()</h3>
  84. <p>
  85. 创建并初始化一个4X4的单位矩阵[link:https://en.wikipedia.org/wiki/Identity_matrix identity matrix].
  86. </p>
  87. <h2>属性(Properties)</h2>
  88. <h3>[property:Array elements]</h3>
  89. <p>
  90. 矩阵列优先[link:https://en.wikipedia.org/wiki/Row-_and_column-major_order column-major]列表。
  91. </p>
  92. <h2>方法(Methods)</h2>
  93. <h3>[method:Matrix4 clone]()</h3>
  94. <p>创建一个新的矩阵,元素[page:.elements elements]与该矩阵相同。</p>
  95. <h3>[method:this compose]( [param:Vector3 position], [param:Quaternion quaternion], [param:Vector3 scale] )</h3>
  96. <p>
  97. 设置将该对象由位置[page:Vector3 position],四元数[page:Quaternion quaternion] 和 缩放[page:Vector3 scale]
  98. 组合变换的矩阵。内部先调用[page:.makeRotationFromQuaternion makeRotationFromQuaternion]( [page:Quaternion quaternion] )
  99. 再调用缩放[page:.scale scale]( [page:Vector3 scale] )最后是平移[page:.setPosition setPosition]( [page:Vector3 position] )。
  100. </p>
  101. <h3>[method:this copy]( [param:Matrix4 m] )</h3>
  102. <p>将矩阵[page:Matrix3 m]的元素[page:.elements elements]复制到当前矩阵中。</p>
  103. <h3>[method:this copyPosition]( [param:Matrix4 m] )</h3>
  104. <p>
  105. 将给定矩阵[param:Matrix4 m] 的平移分量拷贝到当前矩阵中。
  106. </p>
  107. <h3>[method:null decompose]( [param:Vector3 position], [param:Quaternion quaternion], [param:Vector3 scale] )</h3>
  108. <p>
  109. 将矩阵分解到给定的平移[page:Vector3 position] ,旋转 [page:Quaternion quaternion],缩放[page:Vector3 scale]分量中。
  110. </p>
  111. <h3>[method:Float determinant]()</h3>
  112. <p>
  113. 计算并返回矩阵的行列式[link:https://en.wikipedia.org/wiki/Determinant determinant] 。<br /><br />
  114. 基于这个的方法概述[link:http://www.euclideanspace.com/maths/algebra/matrix/functions/inverse/fourD/index.htm here]。
  115. </p>
  116. <h3>[method:Boolean equals]( [param:Matrix4 m] )</h3>
  117. <p>如果矩阵[page:Matrix3 m] 与当前矩阵所有对应元素相同则返回true。</p>
  118. <h3>[method:this extractBasis]( [param:Vector3 xAxis], [param:Vector3 yAxis], [param:Vector3 zAxis] )</h3>
  119. <p>
  120. 将矩阵的基向量[link:https://en.wikipedia.org/wiki/Basis_(linear_algebra) basis]提取到指定的3个轴向量中。
  121. 如果矩阵如下:
  122. <code>
  123. a, b, c, d,
  124. e, f, g, h,
  125. i, j, k, l,
  126. m, n, o, p
  127. </code>
  128. 然后x轴y轴z轴被设为:
  129. <code>
  130. xAxis = (a, e, i)
  131. yAxis = (b, f, j)
  132. zAxis = (c, g, k)
  133. </code>
  134. </p>
  135. <h3>[method:this extractRotation]( [param:Matrix4 m] )</h3>
  136. <p>
  137. 将给定矩阵[page:Matrix4 m]的旋转分量提取到该矩阵的旋转分量中。
  138. </p>
  139. <h3>[method:this fromArray]( [param:Array array], [param:Integer offset] )</h3>
  140. <p>
  141. [page:Array array] - 用来存储设置元素数据的数组<br />
  142. [page:Integer offset] - (可选参数) 数组的偏移量,默认值为 0。<br /><br />
  143. 使用基于列优先格式[link:https://en.wikipedia.org/wiki/Row-_and_column-major_order#Column-major_order column-major]的数组来设置该矩阵。
  144. </p>
  145. <h3>[method:this getInverse]( [param:Matrix4 m], [param:Boolean throwOnDegenerate] )</h3>
  146. <p>
  147. [page:Matrix3 m] - 取逆的矩阵。<br />
  148. [page:Boolean throwOnDegenerate] - (optional) 如果设置为true,如果矩阵是退化的(如果不可逆的话),则会抛出一个错误。<br /><br />
  149. 使用逆矩阵计算方法[link:https://en.wikipedia.org/wiki/Invertible_matrix#Analytic_solution analytic method],
  150. 将当前矩阵设置为给定矩阵的逆矩阵[link:https://en.wikipedia.org/wiki/Invertible_matrix inverse],如果[page:Boolean throwOnDegenerate]
  151. 参数没有设置且给定矩阵不可逆,那么将当前矩阵设置为3X3单位矩阵。
  152. </p>
  153. <h3>[method:Float getMaxScaleOnAxis]()</h3>
  154. <p>获取3个轴方向的最大缩放值。</p>
  155. <h3>[method:this identity]()</h3>
  156. <p>将当前矩阵重置为单位矩阵[link:https://en.wikipedia.org/wiki/Identity_matrix identity matrix]。</p>
  157. <h3>[method:this lookAt]( [param:Vector3 eye], [param:Vector3 center], [param:Vector3 up], )</h3>
  158. <p>
  159. 构造一个旋转矩阵,从[page:Vector3 eye] 指向 [page:Vector3 center],由向量 [param:Vector3 up] 定向。
  160. <!-- Constructs a rotation matrix, looking from [page:Vector3 eye] towards [page:Vector3 center]
  161. oriented by the [page:Vector3 up] vector.-->
  162. </p>
  163. <h3>[method:this makeRotationAxis]( [param:Vector3 axis], [param:Float theta] )</h3>
  164. <p>
  165. [page:Vector3 axis] — 旋转轴,需要被归一化。<br />
  166. [page:Float theta] — 旋转量(弧度)。<br /><br />
  167. 设置当前矩阵为围绕轴 [page:Vector3 axis] 旋转量为 [page:Float theta]弧度。<br />
  168. 这是一种有点争议但在数学上可以替代通过四元数[page:Quaternions]旋转的办法。 请参阅此处[link:https://www.gamedev.net/articles/programming/math-and-physics/do-we-really-need-quaternions-r1199 here]的讨论。
  169. </p>
  170. <h3>[method:this makeBasis]( [param:Vector3 xAxis], [param:Vector3 yAxis], [param:Vector3 zAxis] )</h3>
  171. <p>
  172. 通过给定的三个向量设置该矩阵为基矩阵[link:https://en.wikipedia.org/wiki/Basis_(linear_algebra) basis]:
  173. <code>
  174. xAxis.x, yAxis.x, zAxis.x, 0,
  175. xAxis.y, yAxis.y, zAxis.y, 0,
  176. xAxis.z, yAxis.z, zAxis.z, 0,
  177. 0, 0, 0, 1
  178. </code>
  179. </p>
  180. <h3>[method:this makePerspective]( [param:Float left], [param:Float right], [param:Float top], [param:Float bottom], [param:Float near], [param:Float far] )</h3>
  181. <p>
  182. 创建一个透视投影矩阵[link:https://en.wikipedia.org/wiki/3D_projection#Perspective_projection perspective projection]。
  183. 在引擎内部由[page:PerspectiveCamera.updateProjectionMatrix]()使用。
  184. </p>
  185. <h3>[method:this makeOrthographic]( [param:Float left], [param:Float right], [param:Float top], [param:Float bottom], [param:Float near], [param:Float far] )</h3>
  186. <p>
  187. 创建一个正交投影矩阵[link:https://en.wikipedia.org/wiki/Orthographic_projection orthographic projection]。
  188. 在引擎内部由[page:OrthographicCamera.updateProjectionMatrix]()使用。
  189. </p>
  190. <h3>[method:this makeRotationFromEuler]( [param:Euler euler] )</h3>
  191. <p>
  192. 将传入的欧拉角转换为该矩阵的旋转分量(左上角的3x3矩阵)。
  193. 矩阵的其余部分被设为单位矩阵。根据欧拉角[page:Euler euler]的旋转顺序[page:Euler.order order],总共有六种可能的结果。
  194. 详细信息,请参阅本页[link:https://en.wikipedia.org/wiki/Euler_angles#Rotation_matrix this page]。
  195. </p>
  196. <h3>[method:this makeRotationFromQuaternion]( [param:Quaternion q] )</h3>
  197. <p>
  198. 将这个矩阵的旋转分量设置为四元数[page:Quaternion q]指定的旋转,如下链接所诉[link:https://en.wikipedia.org/wiki/Rotation_matrix#Quaternion here]。
  199. 矩阵的其余部分被设为单位矩阵。因此,给定四元数[page:Quaternion q] = w + xi + yj + zk,得到的矩阵为:
  200. <code>
  201. 1-2y²-2z² 2xy-2zw 2xz+2yw 0
  202. 2xy+2zw 1-2x²-2z² 2yz-2xw 0
  203. 2xz-2yw 2yz+2xw 1-2x²-2y² 0
  204. 0 0 0 1
  205. </code>
  206. </p>
  207. <h3>[method:this makeRotationX]( [param:Float theta] )</h3>
  208. <p>
  209. [page:Float theta] — Rotation angle in radians.<br /><br />
  210. 把该矩阵设置为绕x轴旋转弧度[page:Float theta] (&theta;)大小的矩阵。
  211. 结果如下:
  212. <code>
  213. 1 0 0 0
  214. 0 cos(&theta;) -sin(&theta;) 0
  215. 0 sin(&theta;) cos(&theta;) 0
  216. 0 0 0 1
  217. </code>
  218. </p>
  219. <h3>[method:this makeRotationY]( [param:Float theta] )</h3>
  220. <p>
  221. [page:Float theta] — Rotation angle in radians.<br /><br />
  222. 把该矩阵设置为绕Y轴旋转弧度[page:Float theta] (&theta;)大小的矩阵。
  223. 结果如下:
  224. <code>
  225. cos(&theta;) 0 sin(&theta;) 0
  226. 0 1 0 0
  227. -sin(&theta;) 0 cos(&theta;) 0
  228. 0 0 0 1
  229. </code>
  230. </p>
  231. <h3>[method:this makeRotationZ]( [param:Float theta] )</h3>
  232. <p>
  233. [page:Float theta] — Rotation angle in radians.<br /><br />
  234. 把该矩阵设置为绕z轴旋转弧度[page:Float theta] (&theta;)大小的矩阵。
  235. 结果如下:
  236. <code>
  237. cos(&theta;) -sin(&theta;) 0 0
  238. sin(&theta;) cos(&theta;) 0 0
  239. 0 0 1 0
  240. 0 0 0 1
  241. </code>
  242. </p>
  243. <h3>[method:this makeScale]( [param:Float x], [param:Float y], [param:Float z] )</h3>
  244. <p>
  245. [page:Float x] - 在X轴方向的缩放比。<br />
  246. [page:Float y] - 在Y轴方向的缩放比。<br />
  247. [page:Float z] - 在Z轴方向的缩放比。<br /><br />
  248. 将这个矩阵设置为缩放变换:
  249. <code>
  250. x, 0, 0, 0,
  251. 0, y, 0, 0,
  252. 0, 0, z, 0,
  253. 0, 0, 0, 1
  254. </code>
  255. </p>
  256. <h3>[method:this makeShear]( [param:Float x], [param:Float y], [param:Float z] )</h3>
  257. <p>
  258. [page:Float x] - 在X轴上剪切的量。<br />
  259. [page:Float y] - 在Y轴上剪切的量。<br />
  260. [page:Float z] - 在Z轴上剪切的量。<br /><br />
  261. 将此矩阵设置为剪切变换:
  262. <code>
  263. 1, y, z, 0,
  264. x, 1, z, 0,
  265. x, y, 1, 0,
  266. 0, 0, 0, 1
  267. </code>
  268. </p>
  269. <h3>[method:this makeTranslation]( [param:Float x], [param:Float y], [param:Float z] )</h3>
  270. <p>
  271. [page:Float x] - 在X轴上的平移量。<br />
  272. [page:Float y] - 在Y轴上的平移量。<br />
  273. [page:Float z] - 在Z轴上的平移量。<br /><br />
  274. 设置该矩阵为平移变换:
  275. <code>
  276. 1, 0, 0, x,
  277. 0, 1, 0, y,
  278. 0, 0, 1, z,
  279. 0, 0, 0, 1
  280. </code>
  281. </p>
  282. <h3>[method:this multiply]( [param:Matrix4 m] )</h3>
  283. <p>将当前矩阵乘以矩阵[page:Matrix4 m]。</p>
  284. <h3>[method:this multiplyMatrices]( [param:Matrix4 a], [param:Matrix4 b] )</h3>
  285. <p>设置当前矩阵为矩阵[page:Matrix4 a] x 矩阵[page:Matrix4 b]。</p>
  286. <h3>[method:this multiplyScalar]( [param:Float s] )</h3>
  287. <p>当前矩阵所有的元素乘以该缩放值*s*</p>
  288. <h3>[method:this premultiply]( [param:Matrix4 m] )</h3>
  289. <p>将矩阵[page:Matrix4 m]乘以当前矩阵。</p>
  290. <h3>[method:this scale]( [param:Vector3 v] )</h3>
  291. <p>将该矩阵的列向量乘以对应向量[page:Vector3 v]的分量。</p>
  292. <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>
  293. <p>
  294. 以行优先的格式将传入的数值设置给该矩阵中的元素[page:.elements elements]。
  295. </p>
  296. <h3>[method:this setPosition]( [param:Vector3 v] )</h3>
  297. <h3>[method:this setPosition]( [param:Float x], [param:Float y], [param:Float z] ) // optional API</h3>
  298. <p>
  299. 取传入参数[param:Vector3 v]中值设置该矩阵的位置分量,不影响该矩阵的其余部分——即,如果该矩阵当前为:
  300. <code>
  301. a, b, c, d,
  302. e, f, g, h,
  303. i, j, k, l,
  304. m, n, o, p
  305. </code>
  306. 变成:
  307. <code>
  308. a, b, c, v.x,
  309. e, f, g, v.y,
  310. i, j, k, v.z,
  311. m, n, o, p
  312. </code>
  313. </p>
  314. <h3>[method:Array toArray]( [param:Array array], [param:Integer offset] )</h3>
  315. <p>
  316. [page:Array array] - (可选参数) 存储矩阵元素的数组,如果未指定会创建一个新的数组。<br />
  317. [page:Integer offset] - (可选参数) 存放矩阵元素数组的偏移量。<br /><br />
  318. 使用列优先[link:https://en.wikipedia.org/wiki/Row-_and_column-major_order#Column-major_order column-major]格式将此矩阵的元素写入数组中。
  319. </p>
  320. <h3>[method:this transpose]()</h3>
  321. <p>将该矩阵转置[link:https://en.wikipedia.org/wiki/Transpose Transposes]。</p>
  322. <h2>源码(Source)</h2>
  323. <p>
  324. [link:https://github.com/mrdoob/three.js/blob/master/src/[path].js src/[path].js]
  325. </p>
  326. </body>
  327. </html>