Quaternion.html 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. <!DOCTYPE html>
  2. <html lang="zh">
  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. 该类实现了 [link:http://en.wikipedia.org/wiki/Quaternion quaternion] 。<br/>
  14. 四元数在three.js中用于表示 [link:https://en.wikipedia.org/wiki/Quaternions_and_spatial_rotation rotation] (旋转)。
  15. </p>
  16. <h2>代码示例</h2>
  17. <code>
  18. const quaternion = new THREE.Quaternion();
  19. quaternion.setFromAxisAngle( new THREE.Vector3( 0, 1, 0 ), Math.PI / 2 );
  20. const vector = new THREE.Vector3( 1, 0, 0 );
  21. vector.applyQuaternion( quaternion );
  22. </code>
  23. <h2>构造函数</h2>
  24. <h3>[name]( [param:Float x], [param:Float y], [param:Float z], [param:Float w] )</h3>
  25. <p>
  26. [page:Float x] - x 坐标<br />
  27. [page:Float y] - y 坐标<br />
  28. [page:Float z] - z 坐标<br />
  29. [page:Float w] - w 坐标
  30. </p>
  31. <h2>属性</h2>
  32. <h3>[property:Float x]</h3>
  33. <h3>[property:Float y]</h3>
  34. <h3>[property:Float z]</h3>
  35. <h3>[property:Float w]</h3>
  36. <h2>方法</h2>
  37. <h3>[method:Float angleTo]( [param:Quaternion q] )</h3>
  38. <p>
  39. 以弧度返回该四元数与四元数 [page:Quaternion q] 之间的夹角。
  40. </p>
  41. <h3>[method:Quaternion clone]()</h3>
  42. <p>
  43. 创建一个与该四元数具有相同[page:.x x]、[page:.y y]、[page:.z z]和[page:.w w]
  44. 属性的四元数。
  45. </p>
  46. <h3>[method:Quaternion conjugate]()</h3>
  47. <p>
  48. 返回该四元数的旋转共轭。
  49. 四元数的共轭表示的是,围绕旋转轴在相反方向上的相同旋转。
  50. </p>
  51. <h3>[method:Quaternion copy]( [param:Quaternion q] )</h3>
  52. <p>
  53. 复制四元数 [page:Quaternion q] 的 [page:.x x]、[page:.y y]、[page:.z z] 和 [page:.w w]
  54. 属性到该四元数中。
  55. </p>
  56. <h3>[method:Boolean equals]( [param:Quaternion v] )</h3>
  57. <p>
  58. [page:Quaternion v] - 用于进行比较的四元数。<br /><br />
  59. 将四元数 [page:Quaternion v] 的 [page:.x x]、 [page:.y y]、 [page:.z z] 和 [page:.w w] 的属性
  60. 与当前四元数的对应属性相比较,以确定它们是否表示相同的旋转。
  61. </p>
  62. <h3>[method:Float dot]( [param:Quaternion v] )</h3>
  63. <p>
  64. 计算四元数 [page:Quaternion v] 与当前四元数的[link:https://en.wikipedia.org/wiki/Dot_product dot product](点积)。
  65. </p>
  66. <h3>[method:Quaternion fromArray]( [param:Array array], [param:Integer offset] )</h3>
  67. <p>
  68. [page:Array array] - 用于构造四元数的形如(x, y, z, w)的数组。<br />
  69. [page:Integer offset] - (可选)数组的偏移量。(译者注:使用数组中从第offset元素算起的四个元素)<br /><br />
  70. 从一个数组来设置四元数的 [page:.x x]、 [page:.y y]、[page:.z z] 和 [page:.w w] 的属性。
  71. </p>
  72. <h3>[method:Quaternion identity]()</h3>
  73. <p>
  74. 设置该四元数为 identity 四元数,即表示“不旋转”的四元数。
  75. </p>
  76. <h3>[method:Quaternion inverse]()</h3>
  77. <p>
  78. 翻转该四元数 —— 计算 [page:.conjugate conjugate] 。假定该四元数具有单位长度。
  79. </p>
  80. <h3>[method:Float length]()</h3>
  81. <p>计算四元数的 [link:https://en.wikipedia.org/wiki/Euclidean_distance Euclidean length]
  82. (欧几里得长度,直线长度),视为一个四维向量。</p>
  83. <h3>[method:Float lengthSq]()</h3>
  84. <p>
  85. 计算四元数 [link:https://en.wikipedia.org/wiki/Euclidean_distance Euclidean length]
  86. (欧几里得长度,直线长度)的平方,视为一个四维向量。
  87. 如果要比较两个四元数的长度,这可能会十分有用,
  88. 因为这比 [page:.length length]() 的效率稍高一些。
  89. </p>
  90. <h3>[method:Quaternion normalize]()</h3>
  91. <p>
  92. [link:https://en.wikipedia.org/wiki/Normalized_vector Normalizes](归一化)四元数 ——
  93. 即计算与该四元数具有相同旋转、但长度为*1*的四元数。
  94. </p>
  95. <h3>[method:Quaternion multiply]( [param:Quaternion q] )</h3>
  96. <p>将该四元数与[page:Quaternion q]相乘。</p>
  97. <h3>[method:Quaternion multiplyQuaternions]( [param:Quaternion a], [param:Quaternion b] )</h3>
  98. <p>
  99. 将该四元数设为 [page:Quaternion a] x [page:Quaternion b] 。<br />
  100. 改编自 [link:http://www.euclideanspace.com/maths/algebra/realNormedAlgebra/quaternions/code/index.htm here] 所概述的方法。
  101. </p>
  102. <h3>[method:Quaternion premultiply]( [param:Quaternion q] )</h3>
  103. <p>Pre-multiplies this quaternion by [page:Quaternion q].</p>
  104. <h3>[method:Quaternion rotateTowards]( [param:Quaternion q], [param:Float step] )</h3>
  105. <p>
  106. [page:Quaternion q] - The target quaternion.<br />
  107. [page:float step] - The angular step in radians.<br /><br />
  108. Rotates this quaternion by a given angular step to the defined quaternion *q*.
  109. The method ensures that the final quaternion will not overshoot *q*.
  110. </p>
  111. <h3>[method:Quaternion slerp]( [param:Quaternion qb], [param:float t] )</h3>
  112. <p>
  113. [page:Quaternion qb] - The other quaternion rotation<br />
  114. [page:float t] - interpolation factor in the closed interval [0, 1].<br /><br />
  115. Handles the spherical linear interpolation between quaternions. [page:float t] represents the
  116. amount of rotation between this quaternion (where [page:float t] is 0) and [page:Quaternion qb] (where
  117. [page:float t] is 1). This quaternion is set to the result. Also see the static version of the
  118. *slerp* below.
  119. <code>
  120. // rotate a mesh towards a target quaternion
  121. mesh.quaternion.slerp( endQuaternion, 0.01 );
  122. </code>
  123. </p>
  124. <h3>[method:Quaternion set]( [param:Float x], [param:Float y], [param:Float z], [param:Float w] )</h3>
  125. <p>设置该四元数的 [page:.x x]、[page:.y y]、[page:.z z]和[page:.w w]属性。</p>
  126. <h3>[method:Quaternion setFromAxisAngle]( [param:Vector3 axis], [param:Float angle] )</h3>
  127. <p>
  128. 从由 [page:Vector3 axis](轴) 和 [page:Float angle](角度)所给定的旋转来设置该四元数。<br />
  129. 改编自 [link:http://www.euclideanspace.com/maths/geometry/rotations/conversions/angleToQuaternion/index.htm here] 所述的方法。<br />
  130. 假定*Axis*已被归一化,*angle*以弧度来表示。
  131. </p>
  132. <h3>[method:Quaternion setFromEuler]( [param:Euler euler] )</h3>
  133. <p>从由 [page:Euler] 角所给定的旋转来设置该四元数。</p>
  134. <h3>[method:Quaternion setFromRotationMatrix]( [param:Matrix4 m] )</h3>
  135. <p>
  136. 从[page:Matrix4 m]的旋转分量中来设置该四元数。<br />
  137. 改编自 [link:http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQuaternion/index.htm here] 所概述的方法。
  138. </p>
  139. <h3>[method:Quaternion setFromUnitVectors]( [param:Vector3 vFrom], [param:Vector3 vTo] )</h3>
  140. <p>
  141. Sets this quaternion to the rotation required to rotate direction vector [page:Vector3 vFrom] to
  142. direction vector [page:Vector3 vTo].<br />
  143. Adapted from the method [link:http://lolengine.net/blog/2013/09/18/beautiful-maths-quaternion-from-vectors here].<br />
  144. [page:Vector3 vFrom] and [page:Vector3 vTo] are assumed to be normalized.
  145. </p>
  146. <h3>[method:Array toArray]( [param:Array array], [param:Integer offset] )</h3>
  147. <p>
  148. [page:Array array] - (可选)存储该四元数的数组。若未指定该参数,则将创建一个新数组。<br/>
  149. [page:Integer offset] - (可选)若指定了该值,结果将会被拷贝到该
  150. [page:Array]。<br /><br />
  151. 在形如[x, y, z, w]的数组中,返回四元数中的数字元素。
  152. </p>
  153. <h3>[method:this fromBufferAttribute]( [param:BufferAttribute attribute], [param:Integer index] )</h3>
  154. <p>
  155. [page:BufferAttribute attribute] - 源 attribute。<br />
  156. [page:Integer index] - attribute 中的索引。<br /><br />
  157. 从 [page:BufferAttribute attribute] 中设置该四元数的[page:.x x]、 [page:.y y]、 [page:.z z]、 [page:.w w]属性。
  158. </p>
  159. <h2>静态方法</h2>
  160. <p>
  161. 静态方法(相对于实例方法)被设计用来直接从类进行调用,而非从特定的实例上进行调用。
  162. 要使用静态版本,需要按照如下方式来调用:
  163. <code>
  164. THREE.Quaternion.slerp( qStart, qEnd, qTarget, t );
  165. </code>
  166. 作为对比,要调用“正常”或实例的 slerp 方法,则进行如下操作:
  167. <code>
  168. //instantiate a quaternion with default values
  169. const q = new THREE.Quaternion();
  170. //call the instanced slerp method
  171. q.slerp( qb, t )
  172. </code>
  173. </p>
  174. <h3>[method:Quaternion slerp]( [param:Quaternion qStart], [param:Quaternion qEnd], [param:Quaternion qTarget], [param:Float t] )</h3>
  175. <p>
  176. [page:Quaternion qStart] - The starting quaternion (where [page:Float t] is 0)<br />
  177. [page:Quaternion qEnd] - The ending quaternion (where [page:Float t] is 1)<br />
  178. [page:Quaternion qTarget] - The target quaternion that gets set with the result<br />
  179. [page:float t] - interpolation factor in the closed interval [0, 1].<br /><br />
  180. Unlike the normal method, the static version of slerp sets a target quaternion to the result of the slerp operation.
  181. <code>
  182. // Code setup
  183. const startQuaternion = new THREE.Quaternion().set( 0, 0, 0, 1 ).normalize();
  184. const endQuaternion = new THREE.Quaternion().set( 1, 1, 1, 1 ).normalize();
  185. let t = 0;
  186. // Update a mesh's rotation in the loop
  187. t = ( t + 0.01 ) % 1; // constant angular momentum
  188. THREE.Quaternion.slerp( startQuaternion, endQuaternion, mesh.quaternion, t );
  189. </code>
  190. </p>
  191. <h3>[method:null slerpFlat]( [param:Array dst], [param:Integer dstOffset], [param:Array src0], [param:Integer srcOffset0], [param:Array src1], [param:Integer srcOffset1], [param:Float t] )</h3>
  192. <p>
  193. [page:Array dst] - The output array.<br />
  194. [page:Integer dstOffset] - An offset into the output array.<br />
  195. [page:Array src0] - The source array of the starting quaternion.<br />
  196. [page:Integer srcOffset0] - An offset into the array *src0*.<br />
  197. [page:Array src1] - The source array of the target quatnerion.<br />
  198. [page:Integer srcOffset1] - An offset into the array *src1*.<br />
  199. [page:float t] - Normalized interpolation factor (between 0 and 1).<br /><br />
  200. </p>
  201. <p>
  202. Like the static *slerp* method above, but operates directly on flat arrays of numbers.
  203. </p>
  204. <!-- Note: Do not add non-static methods to the bottom of this page. Put them above the <h2>Static Methods</h2> -->
  205. <h2>源码</h2>
  206. <p>
  207. [link:https://github.com/mrdoob/three.js/blob/master/src/[path].js src/[path].js]
  208. </p>
  209. </body>
  210. </html>