Quaternion.html 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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. 这个类是对[link:http://en.wikipedia.org/wiki/Quaternion quaternion](四元数)的实现。
  14. 它用于排除万向锁([link:http://en.wikipedia.org/wiki/Gimbal_lock gimbal lock])问题,而对物体进行旋转([link:https://en.wikipedia.org/wiki/Quaternions_and_spatial_rotation rotating things]),
  15. 同时它还具有其它优点。
  16. </p>
  17. <h2>示例</h2>
  18. <code>
  19. var quaternion = new THREE.Quaternion();
  20. quaternion.setFromAxisAngle( new THREE.Vector3( 0, 1, 0 ), Math.PI / 2 );
  21. var vector = new THREE.Vector3( 1, 0, 0 );
  22. vector.applyQuaternion( quaternion );
  23. </code>
  24. <h2>构造函数</h2>
  25. <h3>[name]( [param:Float x], [param:Float y], [param:Float z], [param:Float w] )</h3>
  26. <p>
  27. [page:Float x] - x coordinate<br />
  28. [page:Float y] - y coordinate<br />
  29. [page:Float z] - z coordinate<br />
  30. [page:Float w] - w coordinate
  31. </p>
  32. <h2>属性</h2>
  33. <h3>[property:Float x]</h3>
  34. <p>改变这一属性将会导致[page:.onChangeCallback onChangeCallback]被调用。</p>
  35. <h3>[property:Float y]</h3>
  36. <p>改变这一属性将会导致[page:.onChangeCallback onChangeCallback]被调用。</p>
  37. <h3>[property:Float z]</h3>
  38. <p>改变这一属性将会导致[page:.onChangeCallback onChangeCallback]被调用。</p>
  39. <h3>[property:Float w]</h3>
  40. <p>改变这一属性将会导致[page:.onChangeCallback onChangeCallback]被调用。</p>
  41. <h2>方法</h2>
  42. <h3>[method:Float angleTo]( [param:Quaternion q] )</h3>
  43. <p>
  44. 以弧度的形式返回这一四元数与四元数[page:Quaternion q]之间的夹角。
  45. </p>
  46. <h3>[method:Quaternion clone]()</h3>
  47. <p>
  48. 以和这一四元数相同的[page:.x x]、[page:.y y]、[page:.z z]和[page:.w w]属性来创建一个新的四元数。
  49. </p>
  50. <h3>[method:Quaternion conjugate]()</h3>
  51. <p>
  52. 返回该四元数的旋转共轭。四元数
  53. Returns the rotational conjugate of this quaternion. The conjugate of a quaternion
  54. represents the same rotation in the opposite direction about the rotational axis.
  55. </p>
  56. <h3>[method:Quaternion copy]( [param:Quaternion q] )</h3>
  57. <p>
  58. Copies the [page:.x x], [page:.y y], [page:.z z] and [page:.w w] properties
  59. of [page:Quaternion q] into this quaternion.
  60. </p>
  61. <h3>[method:Boolean equals]( [param:Quaternion v] )</h3>
  62. <p>
  63. [page:Quaternion v] - Quaternion that this quaternion will be compared to.<br /><br />
  64. Compares the [page:.x x], [page:.y y], [page:.z z] and [page:.w w] properties of
  65. [page:Quaternion v] to the equivalent properties of this quaternion to determine if they
  66. represent the same rotation.
  67. </p>
  68. <h3>[method:Float dot]( [param:Quaternion v] )</h3>
  69. <p>
  70. Calculates the [link:https://en.wikipedia.org/wiki/Dot_product dot product] of
  71. quaternions [page:Quaternion v] and this one.
  72. </p>
  73. <h3>[method:Quaternion fromArray]( [param:Array array], [param:Integer offset] )</h3>
  74. <p>
  75. [page:Array array] - array of format (x, y, z, w) used to construct the quaternion.<br />
  76. [page:Integer offset] - (optional) an offset into the array.<br /><br />
  77. Sets this quaternion's [page:.x x], [page:.y y], [page:.z z] and [page:.w w] properties
  78. from an array.
  79. </p>
  80. <h3>[method:Quaternion inverse]()</h3>
  81. <p>
  82. Inverts this quaternion - calculate the [page:.conjugate conjugate] and then
  83. [page:.normalize normalizes] the result.
  84. </p>
  85. <h3>[method:Float length]()</h3>
  86. <p>Computes the [link:https://en.wikipedia.org/wiki/Euclidean_distance Euclidean length]
  87. (straight-line length) of this quaternion, considered as a 4 dimensional vector.</p>
  88. <h3>[method:Float lengthSq]()</h3>
  89. <p>
  90. Computes the [link:https://en.wikipedia.org/wiki/Euclidean_distance Euclidean length]
  91. (straight-line length) of this quaternion, considered as a 4 dimensional
  92. vector. This can be useful if you are comparing the lengths of two quaternions,
  93. as this is a slightly more efficient calculation than [page:.length length]().
  94. </p>
  95. <h3>[method:Quaternion normalize]()</h3>
  96. <p>
  97. [link:https://en.wikipedia.org/wiki/Normalized_vector Normalizes] this quaternion - that is,
  98. calculated the quaternion that performs the same rotation as this one, but has [page:.length length]
  99. equal to *1*.
  100. </p>
  101. <h3>[method:Quaternion multiply]( [param:Quaternion q] )</h3>
  102. <p>Multiplies this quaternion by [page:Quaternion q].</p>
  103. <h3>[method:Quaternion multiplyQuaternions]( [param:Quaternion a], [param:Quaternion b] )</h3>
  104. <p>
  105. Sets this quaternion to [page:Quaternion a] x [page:Quaternion b].<br />
  106. Adapted from the method outlined [link:http://www.euclideanspace.com/maths/algebra/realNormedAlgebra/quaternions/code/index.htm here].
  107. </p>
  108. <h3>[method:Quaternion onChange]( [param:Function onChangeCallback] )</h3>
  109. <p>Sets the [page:.onChangeCallback onChangeCallback]() method.</p>
  110. <h3>[method:Quaternion onChangeCallback]( )</h3>
  111. <p>
  112. This function is called whenever any of the following occurs:
  113. <ul>
  114. <li>
  115. The [page:.x x], [page:.y y], [page:.z z] or
  116. [page:.w w] properties are changed.
  117. </li>
  118. <li>
  119. The [page:.set set](), [page:.copy copy](), [page:.clone clone](),
  120. [page:.setFromAxisAngle setFromAxisAngle](), [page:.setFromRotationMatrix setFromRotationMatrix](),
  121. [page:.conjugate conjugate](), [page:.normalize normalize](),
  122. [page:.multiplyQuaternions multiplyQuaternions](), [page:.slerp slerp]() or [page:.fromArray fromArray]()
  123. functions are called.
  124. </li>
  125. <li>
  126. [page:.setFromEuler setFromEuler]() function is called with its *update* argument set to true.
  127. </li>
  128. </ul>
  129. By default it is the empty function, however you can change it if needed using [page:.onChange onChange]( [page:Function onChangeCallback] ).
  130. </p>
  131. <h3>[method:Quaternion premultiply]( [param:Quaternion q] )</h3>
  132. <p>Pre-multiplies this quaternion by [page:Quaternion q].</p>
  133. <h3>[method:Quaternion rotateTowards]( [param:Quaternion q], [param:Float step] )</h3>
  134. <p>
  135. [page:Quaternion q] - The target quaternion.<br />
  136. [page:float step] - The angular step in radians.<br /><br />
  137. Rotates this quaternion by a given angular step to the defined quaternion *q*.
  138. The method ensures that the final quaternion will not overshoot *q*.
  139. </p>
  140. <h3>[method:Quaternion slerp]( [param:Quaternion qb], [param:float t] )</h3>
  141. <p>
  142. [page:Quaternion qb] - The other quaternion rotation<br />
  143. [page:float t] - interpolation factor in the closed interval [0, 1].<br /><br />
  144. Handles the spherical linear interpolation between quaternions. [page:float t] represents the
  145. amount of rotation between this quaternion (where [page:float t] is 0) and [page:Quaternion qb] (where
  146. [page:float t] is 1). This quaternion is set to the result. Also see the static version of the
  147. *slerp* below.
  148. <code>
  149. // rotate a mesh towards a target quaternion
  150. mesh.quaternion.slerp( endQuaternion, 0.01 );
  151. </code>
  152. </p>
  153. <h3>[method:Quaternion set]( [param:Float x], [param:Float y], [param:Float z], [param:Float w] )</h3>
  154. <p>Sets [page:.x x], [page:.y y], [page:.z z], [page:.w w] properties of this quaternion.</p>
  155. <h3>[method:Quaternion setFromAxisAngle]( [param:Vector3 axis], [param:Float angle] )</h3>
  156. <p>
  157. Sets this quaternion from rotation specified by [page:Vector3 axis] and [page:Float angle].<br />
  158. Adapted from the method [link:http://www.euclideanspace.com/maths/geometry/rotations/conversions/angleToQuaternion/index.htm here].<br />
  159. *Axis* is assumed to be normalized, *angle* is in radians.
  160. </p>
  161. <h3>[method:Quaternion setFromEuler]( [param:Euler euler] )</h3>
  162. <p>Sets this quaternion from the rotation specified by [page:Euler] angle.</p>
  163. <h3>[method:Quaternion setFromRotationMatrix]( [param:Matrix4 m] )</h3>
  164. <p>
  165. Sets this quaternion from rotation component of [page:Matrix4 m].<br />
  166. Adapted from the method [link:http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQuaternion/index.htm here].
  167. </p>
  168. <h3>[method:Quaternion setFromUnitVectors]( [param:Vector3 vFrom], [param:Vector3 vTo] )</h3>
  169. <p>
  170. Sets this quaternion to the rotation required to rotate direction vector [page:Vector3 vFrom] to
  171. direction vector [page:Vector3 vTo].<br />
  172. Adapted from the method [link:http://lolengine.net/blog/2013/09/18/beautiful-maths-quaternion-from-vectors here].<br />
  173. [page:Vector3 vFrom] and [page:Vector3 vTo] are assumed to be normalized.
  174. </p>
  175. <h3>[method:Array toArray]( [param:Array array], [param:Integer offset] )</h3>
  176. <p>
  177. [page:Array array] - An optional array to store the quaternion. If not specified, a new array will be created.<br/>
  178. [page:Integer offset] - (optional) if specified, the result will be copied
  179. into this [page:Array].<br /><br />
  180. Returns the numerical elements of this quaternion in an array of format [x, y, z, w].
  181. </p>
  182. <h2>Static Methods</h2>
  183. <p>
  184. Static methods (as opposed to instance methods) are designed to be called directly from the class,
  185. rather than from a specific instance. So to use the static version of, call it like so:
  186. <code>
  187. THREE.Quaternion.slerp( qStart, qEnd, qTarget, t );
  188. </code>
  189. By contrast, to call the 'normal' or instanced slerp method, you would do the following:
  190. <code>
  191. //instantiate a quaternion with default values
  192. var q = new THREE.Quaternion();
  193. //call the instanced slerp method
  194. q.slerp( qb, t )
  195. </code>
  196. </p>
  197. <h3>[method:Quaternion slerp]( [param:Quaternion qStart], [param:Quaternion qEnd], [param:Quaternion qTarget], [param:Float t] )</h3>
  198. <p>
  199. [page:Quaternion qStart] - The starting quaternion (where [page:Float t] is 0)<br />
  200. [page:Quaternion qEnd] - The ending quaternion (where [page:Float t] is 1)<br />
  201. [page:Quaternion qTarget] - The target quaternion that gets set with the result<br />
  202. [page:float t] - interpolation factor in the closed interval [0, 1].<br /><br />
  203. Unlike the normal method, the static version of slerp sets a target quaternion to the result of the slerp operation.
  204. <code>
  205. // Code setup
  206. var startQuaternion = new THREE.Quaternion().set( 0, 0, 0, 1 ).normalize();
  207. var endQuaternion = new THREE.Quaternion().set( 1, 1, 1, 1 ).normalize();
  208. var t = 0;
  209. // Update a mesh's rotation in the loop
  210. t = ( t + 0.01 ) % 1; // constant angular momentum
  211. THREE.Quaternion.slerp( startQuaternion, endQuaternion, mesh.quaternion, t );
  212. </code>
  213. </p>
  214. <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>
  215. <p>
  216. [page:Array dst] - The output array.<br />
  217. [page:Integer dstOffset] - An offset into the output array.<br />
  218. [page:Array src0] - The source array of the starting quaternion.<br />
  219. [page:Integer srcOffset0] - An offset into the array *src0*.<br />
  220. [page:Array src1] - The source array of the target quatnerion.<br />
  221. [page:Integer srcOffset1] - An offset into the array *src1*.<br />
  222. [page:float t] - Normalized interpolation factor (between 0 and 1).<br /><br />
  223. </p>
  224. <p>
  225. Like the static *slerp* method above, but operates directly on flat arrays of numbers.
  226. </p>
  227. <!-- Note: Do not add non-static methods to the bottom of this page. Put them above the <h2>Static Methods</h2> -->
  228. <h2>Source</h2>
  229. [link:https://github.com/mrdoob/three.js/blob/master/src/[path].js src/[path].js]
  230. </body>
  231. </html>