2
0

Quaternion.html 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. Implementation of a [link:http://en.wikipedia.org/wiki/Quaternion quaternion].<br/>
  13. Quaternions are used in three.js to represent [link:https://en.wikipedia.org/wiki/Quaternions_and_spatial_rotation rotations].
  14. </p>
  15. <h2>Code Example</h2>
  16. <code>
  17. const quaternion = new THREE.Quaternion();
  18. quaternion.setFromAxisAngle( new THREE.Vector3( 0, 1, 0 ), Math.PI / 2 );
  19. const vector = new THREE.Vector3( 1, 0, 0 );
  20. vector.applyQuaternion( quaternion );
  21. </code>
  22. <h2>Constructor</h2>
  23. <h3>[name]( [param:Float x], [param:Float y], [param:Float z], [param:Float w] )</h3>
  24. <p>
  25. [page:Float x] - x coordinate<br />
  26. [page:Float y] - y coordinate<br />
  27. [page:Float z] - z coordinate<br />
  28. [page:Float w] - w coordinate
  29. </p>
  30. <h2>Properties</h2>
  31. <h3>[property:Float x]</h3>
  32. <h3>[property:Float y]</h3>
  33. <h3>[property:Float z]</h3>
  34. <h3>[property:Float w]</h3>
  35. <h2>Methods</h2>
  36. <h3>[method:Float angleTo]( [param:Quaternion q] )</h3>
  37. <p>
  38. Returns the angle between this quaternion and quaternion [page:Quaternion q] in radians.
  39. </p>
  40. <h3>[method:Quaternion clone]()</h3>
  41. <p>
  42. Creates a new Quaternion with identical [page:.x x], [page:.y y],
  43. [page:.z z] and [page:.w w] properties to this one.
  44. </p>
  45. <h3>[method:Quaternion conjugate]()</h3>
  46. <p>
  47. Returns the rotational conjugate of this quaternion. The conjugate of a quaternion
  48. represents the same rotation in the opposite direction about the rotational axis.
  49. </p>
  50. <h3>[method:Quaternion copy]( [param:Quaternion q] )</h3>
  51. <p>
  52. Copies the [page:.x x], [page:.y y], [page:.z z] and [page:.w w] properties
  53. of [page:Quaternion q] into this quaternion.
  54. </p>
  55. <h3>[method:Boolean equals]( [param:Quaternion v] )</h3>
  56. <p>
  57. [page:Quaternion v] - Quaternion that this quaternion will be compared to.<br /><br />
  58. Compares the [page:.x x], [page:.y y], [page:.z z] and [page:.w w] properties of
  59. [page:Quaternion v] to the equivalent properties of this quaternion to determine if they
  60. represent the same rotation.
  61. </p>
  62. <h3>[method:Float dot]( [param:Quaternion v] )</h3>
  63. <p>
  64. Calculates the [link:https://en.wikipedia.org/wiki/Dot_product dot product] of
  65. quaternions [page:Quaternion v] and this one.
  66. </p>
  67. <h3>[method:Quaternion fromArray]( [param:Array array], [param:Integer offset] )</h3>
  68. <p>
  69. [page:Array array] - array of format (x, y, z, w) used to construct the quaternion.<br />
  70. [page:Integer offset] - (optional) an offset into the array.<br /><br />
  71. Sets this quaternion's [page:.x x], [page:.y y], [page:.z z] and [page:.w w] properties
  72. from an array.
  73. </p>
  74. <h3>[method:Quaternion identity]()</h3>
  75. <p>
  76. Sets this quaternion to the identity quaternion; that is, to the quaternion that represents "no rotation".
  77. </p>
  78. <h3>[method:Quaternion invert]()</h3>
  79. <p>
  80. Inverts this quaternion - calculates the [page:.conjugate conjugate]. The quaternion is assumed to have unit length.
  81. </p>
  82. <h3>[method:Float length]()</h3>
  83. <p>Computes the [link:https://en.wikipedia.org/wiki/Euclidean_distance Euclidean length]
  84. (straight-line length) of this quaternion, considered as a 4 dimensional vector.</p>
  85. <h3>[method:Float lengthSq]()</h3>
  86. <p>
  87. Computes the squared [link:https://en.wikipedia.org/wiki/Euclidean_distance Euclidean length]
  88. (straight-line length) of this quaternion, considered as a 4 dimensional
  89. vector. This can be useful if you are comparing the lengths of two quaternions,
  90. as this is a slightly more efficient calculation than [page:.length length]().
  91. </p>
  92. <h3>[method:Quaternion normalize]()</h3>
  93. <p>
  94. [link:https://en.wikipedia.org/wiki/Normalized_vector Normalizes] this quaternion - that is,
  95. calculated the quaternion that performs the same rotation as this one, but has [page:.length length]
  96. equal to *1*.
  97. </p>
  98. <h3>[method:Quaternion multiply]( [param:Quaternion q] )</h3>
  99. <p>Multiplies this quaternion by [page:Quaternion q].</p>
  100. <h3>[method:Quaternion multiplyQuaternions]( [param:Quaternion a], [param:Quaternion b] )</h3>
  101. <p>
  102. Sets this quaternion to [page:Quaternion a] x [page:Quaternion b].<br />
  103. Adapted from the method outlined [link:http://www.euclideanspace.com/maths/algebra/realNormedAlgebra/quaternions/code/index.htm here].
  104. </p>
  105. <h3>[method:Quaternion premultiply]( [param:Quaternion q] )</h3>
  106. <p>Pre-multiplies this quaternion by [page:Quaternion q].</p>
  107. <h3>[method:Quaternion rotateTowards]( [param:Quaternion q], [param:Float step] )</h3>
  108. <p>
  109. [page:Quaternion q] - The target quaternion.<br />
  110. [page:Float step] - The angular step in radians.<br /><br />
  111. Rotates this quaternion by a given angular step to the defined quaternion *q*.
  112. The method ensures that the final quaternion will not overshoot *q*.
  113. </p>
  114. <h3>[method:Quaternion slerp]( [param:Quaternion qb], [param:Float t] )</h3>
  115. <p>
  116. [page:Quaternion qb] - The other quaternion rotation<br />
  117. [page:Float t] - interpolation factor in the closed interval [0, 1].<br /><br />
  118. Handles the spherical linear interpolation between quaternions. [page:Float t] represents the
  119. amount of rotation between this quaternion (where [page:Float t] is 0) and [page:Quaternion qb] (where
  120. [page:Float t] is 1). This quaternion is set to the result. Also see the static version of the
  121. *slerp* below.
  122. <code>
  123. // rotate a mesh towards a target quaternion
  124. mesh.quaternion.slerp( endQuaternion, 0.01 );
  125. </code>
  126. </p>
  127. <h3>[method:Quaternion set]( [param:Float x], [param:Float y], [param:Float z], [param:Float w] )</h3>
  128. <p>Sets [page:.x x], [page:.y y], [page:.z z], [page:.w w] properties of this quaternion.</p>
  129. <h3>[method:Quaternion setFromAxisAngle]( [param:Vector3 axis], [param:Float angle] )</h3>
  130. <p>
  131. Sets this quaternion from rotation specified by [page:Vector3 axis] and [page:Float angle].<br />
  132. Adapted from the method [link:http://www.euclideanspace.com/maths/geometry/rotations/conversions/angleToQuaternion/index.htm here].<br />
  133. *Axis* is assumed to be normalized, *angle* is in radians.
  134. </p>
  135. <h3>[method:Quaternion setFromEuler]( [param:Euler euler] )</h3>
  136. <p>Sets this quaternion from the rotation specified by [page:Euler] angle.</p>
  137. <h3>[method:Quaternion setFromRotationMatrix]( [param:Matrix4 m] )</h3>
  138. <p>
  139. [page:Matrix4 m] - a [page:Matrix4] of which the upper 3x3 of matrix is a pure
  140. [link:https://en.wikipedia.org/wiki/Rotation_matrix rotation matrix] (i.e. unscaled).<br />
  141. Sets this quaternion from rotation component of [page:Matrix4 m].<br />
  142. Adapted from the method [link:http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQuaternion/index.htm here].
  143. </p>
  144. <h3>[method:Quaternion setFromUnitVectors]( [param:Vector3 vFrom], [param:Vector3 vTo] )</h3>
  145. <p>
  146. Sets this quaternion to the rotation required to rotate direction vector [page:Vector3 vFrom] to
  147. direction vector [page:Vector3 vTo].<br />
  148. Adapted from the method [link:http://lolengine.net/blog/2013/09/18/beautiful-maths-quaternion-from-vectors here].<br />
  149. [page:Vector3 vFrom] and [page:Vector3 vTo] are assumed to be normalized.
  150. </p>
  151. <h3>[method:Array toArray]( [param:Array array], [param:Integer offset] )</h3>
  152. <p>
  153. [page:Array array] - An optional array to store the quaternion. If not specified, a new array will be created.<br/>
  154. [page:Integer offset] - (optional) if specified, the result will be copied
  155. into this [page:Array].<br /><br />
  156. Returns the numerical elements of this quaternion in an array of format [x, y, z, w].
  157. </p>
  158. <h3>[method:this fromBufferAttribute]( [param:BufferAttribute attribute], [param:Integer index] )</h3>
  159. <p>
  160. [page:BufferAttribute attribute] - the source attribute.<br />
  161. [page:Integer index] - index in the attribute.<br /><br />
  162. Sets [page:.x x], [page:.y y], [page:.z z], [page:.w w] properties of this quaternion from the [page:BufferAttribute attribute].
  163. </p>
  164. <h2>Static Methods</h2>
  165. <p>
  166. Static methods (as opposed to instance methods) are designed to be called directly from the class,
  167. rather than from a specific instance. So to use the static version of, call it like so:
  168. <code>
  169. THREE.Quaternion.slerp( qStart, qEnd, qTarget, t );
  170. </code>
  171. By contrast, to call the 'normal' or instanced slerp method, you would do the following:
  172. <code>
  173. //instantiate a quaternion with default values
  174. const q = new THREE.Quaternion();
  175. //call the instanced slerp method
  176. q.slerp( qb, t )
  177. </code>
  178. </p>
  179. <h3>[method:Quaternion slerp]( [param:Quaternion qStart], [param:Quaternion qEnd], [param:Quaternion qTarget], [param:Float t] )</h3>
  180. <p>
  181. [page:Quaternion qStart] - The starting quaternion (where [page:Float t] is 0)<br />
  182. [page:Quaternion qEnd] - The ending quaternion (where [page:Float t] is 1)<br />
  183. [page:Quaternion qTarget] - The target quaternion that gets set with the result<br />
  184. [page:Float t] - interpolation factor in the closed interval [0, 1].<br /><br />
  185. Unlike the normal method, the static version of slerp sets a target quaternion to the result of the slerp operation.
  186. <code>
  187. // Code setup
  188. const startQuaternion = new THREE.Quaternion().set( 0, 0, 0, 1 ).normalize();
  189. const endQuaternion = new THREE.Quaternion().set( 1, 1, 1, 1 ).normalize();
  190. let t = 0;
  191. // Update a mesh's rotation in the loop
  192. t = ( t + 0.01 ) % 1; // constant angular momentum
  193. THREE.Quaternion.slerp( startQuaternion, endQuaternion, mesh.quaternion, t );
  194. </code>
  195. </p>
  196. <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>
  197. <p>
  198. [page:Array dst] - The output array.<br />
  199. [page:Integer dstOffset] - An offset into the output array.<br />
  200. [page:Array src0] - The source array of the starting quaternion.<br />
  201. [page:Integer srcOffset0] - An offset into the array *src0*.<br />
  202. [page:Array src1] - The source array of the target quatnerion.<br />
  203. [page:Integer srcOffset1] - An offset into the array *src1*.<br />
  204. [page:Float t] - Normalized interpolation factor (between 0 and 1).<br /><br />
  205. </p>
  206. <p>
  207. Like the static *slerp* method above, but operates directly on flat arrays of numbers.
  208. </p>
  209. <!-- Note: Do not add non-static methods to the bottom of this page. Put them above the <h2>Static Methods</h2> -->
  210. <h2>Source</h2>
  211. <p>
  212. [link:https://github.com/mrdoob/three.js/blob/master/src/[path].js src/[path].js]
  213. </p>
  214. </body>
  215. </html>