Euler.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. import { Quaternion } from './Quaternion.js';
  2. import { Matrix4 } from './Matrix4.js';
  3. import { clamp } from './MathUtils.js';
  4. import { warn } from '../utils.js';
  5. const _matrix = /*@__PURE__*/ new Matrix4();
  6. const _quaternion = /*@__PURE__*/ new Quaternion();
  7. /**
  8. * A class representing Euler angles.
  9. *
  10. * Euler angles describe a rotational transformation by rotating an object on
  11. * its various axes in specified amounts per axis, and a specified axis
  12. * order.
  13. *
  14. * Iterating through an instance will yield its components (x, y, z,
  15. * order) in the corresponding order.
  16. *
  17. * ```js
  18. * const a = new THREE.Euler( 0, 1, 1.57, 'XYZ' );
  19. * const b = new THREE.Vector3( 1, 0, 1 );
  20. * b.applyEuler(a);
  21. * ```
  22. */
  23. class Euler {
  24. /**
  25. * Constructs a new euler instance.
  26. *
  27. * @param {number} [x=0] - The angle of the x axis in radians.
  28. * @param {number} [y=0] - The angle of the y axis in radians.
  29. * @param {number} [z=0] - The angle of the z axis in radians.
  30. * @param {string} [order=Euler.DEFAULT_ORDER] - A string representing the order that the rotations are applied.
  31. */
  32. constructor( x = 0, y = 0, z = 0, order = Euler.DEFAULT_ORDER ) {
  33. /**
  34. * This flag can be used for type testing.
  35. *
  36. * @type {boolean}
  37. * @readonly
  38. * @default true
  39. */
  40. this.isEuler = true;
  41. this._x = x;
  42. this._y = y;
  43. this._z = z;
  44. this._order = order;
  45. }
  46. /**
  47. * The angle of the x axis in radians.
  48. *
  49. * @type {number}
  50. * @default 0
  51. */
  52. get x() {
  53. return this._x;
  54. }
  55. set x( value ) {
  56. this._x = value;
  57. this._onChangeCallback();
  58. }
  59. /**
  60. * The angle of the y axis in radians.
  61. *
  62. * @type {number}
  63. * @default 0
  64. */
  65. get y() {
  66. return this._y;
  67. }
  68. set y( value ) {
  69. this._y = value;
  70. this._onChangeCallback();
  71. }
  72. /**
  73. * The angle of the z axis in radians.
  74. *
  75. * @type {number}
  76. * @default 0
  77. */
  78. get z() {
  79. return this._z;
  80. }
  81. set z( value ) {
  82. this._z = value;
  83. this._onChangeCallback();
  84. }
  85. /**
  86. * A string representing the order that the rotations are applied.
  87. *
  88. * @type {string}
  89. * @default 'XYZ'
  90. */
  91. get order() {
  92. return this._order;
  93. }
  94. set order( value ) {
  95. this._order = value;
  96. this._onChangeCallback();
  97. }
  98. /**
  99. * Sets the Euler components.
  100. *
  101. * @param {number} x - The angle of the x axis in radians.
  102. * @param {number} y - The angle of the y axis in radians.
  103. * @param {number} z - The angle of the z axis in radians.
  104. * @param {string} [order] - A string representing the order that the rotations are applied.
  105. * @return {Euler} A reference to this Euler instance.
  106. */
  107. set( x, y, z, order = this._order ) {
  108. this._x = x;
  109. this._y = y;
  110. this._z = z;
  111. this._order = order;
  112. this._onChangeCallback();
  113. return this;
  114. }
  115. /**
  116. * Returns a new Euler instance with copied values from this instance.
  117. *
  118. * @return {Euler} A clone of this instance.
  119. */
  120. clone() {
  121. return new this.constructor( this._x, this._y, this._z, this._order );
  122. }
  123. /**
  124. * Copies the values of the given Euler instance to this instance.
  125. *
  126. * @param {Euler} euler - The Euler instance to copy.
  127. * @return {Euler} A reference to this Euler instance.
  128. */
  129. copy( euler ) {
  130. this._x = euler._x;
  131. this._y = euler._y;
  132. this._z = euler._z;
  133. this._order = euler._order;
  134. this._onChangeCallback();
  135. return this;
  136. }
  137. /**
  138. * Sets the angles of this Euler instance from a pure rotation matrix.
  139. *
  140. * @param {Matrix4} m - A 4x4 matrix of which the upper 3x3 of matrix is a pure rotation matrix (i.e. unscaled).
  141. * @param {string} [order] - A string representing the order that the rotations are applied.
  142. * @param {boolean} [update=true] - Whether the internal `onChange` callback should be executed or not.
  143. * @return {Euler} A reference to this Euler instance.
  144. */
  145. setFromRotationMatrix( m, order = this._order, update = true ) {
  146. const te = m.elements;
  147. const m11 = te[ 0 ], m12 = te[ 4 ], m13 = te[ 8 ];
  148. const m21 = te[ 1 ], m22 = te[ 5 ], m23 = te[ 9 ];
  149. const m31 = te[ 2 ], m32 = te[ 6 ], m33 = te[ 10 ];
  150. switch ( order ) {
  151. case 'XYZ':
  152. this._y = Math.asin( clamp( m13, - 1, 1 ) );
  153. if ( Math.abs( m13 ) < 0.9999999 ) {
  154. this._x = Math.atan2( - m23, m33 );
  155. this._z = Math.atan2( - m12, m11 );
  156. } else {
  157. this._x = Math.atan2( m32, m22 );
  158. this._z = 0;
  159. }
  160. break;
  161. case 'YXZ':
  162. this._x = Math.asin( - clamp( m23, - 1, 1 ) );
  163. if ( Math.abs( m23 ) < 0.9999999 ) {
  164. this._y = Math.atan2( m13, m33 );
  165. this._z = Math.atan2( m21, m22 );
  166. } else {
  167. this._y = Math.atan2( - m31, m11 );
  168. this._z = 0;
  169. }
  170. break;
  171. case 'ZXY':
  172. this._x = Math.asin( clamp( m32, - 1, 1 ) );
  173. if ( Math.abs( m32 ) < 0.9999999 ) {
  174. this._y = Math.atan2( - m31, m33 );
  175. this._z = Math.atan2( - m12, m22 );
  176. } else {
  177. this._y = 0;
  178. this._z = Math.atan2( m21, m11 );
  179. }
  180. break;
  181. case 'ZYX':
  182. this._y = Math.asin( - clamp( m31, - 1, 1 ) );
  183. if ( Math.abs( m31 ) < 0.9999999 ) {
  184. this._x = Math.atan2( m32, m33 );
  185. this._z = Math.atan2( m21, m11 );
  186. } else {
  187. this._x = 0;
  188. this._z = Math.atan2( - m12, m22 );
  189. }
  190. break;
  191. case 'YZX':
  192. this._z = Math.asin( clamp( m21, - 1, 1 ) );
  193. if ( Math.abs( m21 ) < 0.9999999 ) {
  194. this._x = Math.atan2( - m23, m22 );
  195. this._y = Math.atan2( - m31, m11 );
  196. } else {
  197. this._x = 0;
  198. this._y = Math.atan2( m13, m33 );
  199. }
  200. break;
  201. case 'XZY':
  202. this._z = Math.asin( - clamp( m12, - 1, 1 ) );
  203. if ( Math.abs( m12 ) < 0.9999999 ) {
  204. this._x = Math.atan2( m32, m22 );
  205. this._y = Math.atan2( m13, m11 );
  206. } else {
  207. this._x = Math.atan2( - m23, m33 );
  208. this._y = 0;
  209. }
  210. break;
  211. default:
  212. warn( 'Euler: .setFromRotationMatrix() encountered an unknown order: ' + order );
  213. }
  214. this._order = order;
  215. if ( update === true ) this._onChangeCallback();
  216. return this;
  217. }
  218. /**
  219. * Sets the angles of this Euler instance from a normalized quaternion.
  220. *
  221. * @param {Quaternion} q - A normalized Quaternion.
  222. * @param {string} [order] - A string representing the order that the rotations are applied.
  223. * @param {boolean} [update=true] - Whether the internal `onChange` callback should be executed or not.
  224. * @return {Euler} A reference to this Euler instance.
  225. */
  226. setFromQuaternion( q, order, update ) {
  227. _matrix.makeRotationFromQuaternion( q );
  228. return this.setFromRotationMatrix( _matrix, order, update );
  229. }
  230. /**
  231. * Sets the angles of this Euler instance from the given vector.
  232. *
  233. * @param {Vector3} v - The vector.
  234. * @param {string} [order] - A string representing the order that the rotations are applied.
  235. * @return {Euler} A reference to this Euler instance.
  236. */
  237. setFromVector3( v, order = this._order ) {
  238. return this.set( v.x, v.y, v.z, order );
  239. }
  240. /**
  241. * Resets the euler angle with a new order by creating a quaternion from this
  242. * euler angle and then setting this euler angle with the quaternion and the
  243. * new order.
  244. *
  245. * Warning: This discards revolution information.
  246. *
  247. * @param {string} [newOrder] - A string representing the new order that the rotations are applied.
  248. * @return {Euler} A reference to this Euler instance.
  249. */
  250. reorder( newOrder ) {
  251. _quaternion.setFromEuler( this );
  252. return this.setFromQuaternion( _quaternion, newOrder );
  253. }
  254. /**
  255. * Returns `true` if this Euler instance is equal with the given one.
  256. *
  257. * @param {Euler} euler - The Euler instance to test for equality.
  258. * @return {boolean} Whether this Euler instance is equal with the given one.
  259. */
  260. equals( euler ) {
  261. return ( euler._x === this._x ) && ( euler._y === this._y ) && ( euler._z === this._z ) && ( euler._order === this._order );
  262. }
  263. /**
  264. * Sets this Euler instance's components to values from the given array. The first three
  265. * entries of the array are assign to the x,y and z components. An optional fourth entry
  266. * defines the Euler order.
  267. *
  268. * @param {Array<number,number,number,?string>} array - An array holding the Euler component values.
  269. * @return {Euler} A reference to this Euler instance.
  270. */
  271. fromArray( array ) {
  272. this._x = array[ 0 ];
  273. this._y = array[ 1 ];
  274. this._z = array[ 2 ];
  275. if ( array[ 3 ] !== undefined ) this._order = array[ 3 ];
  276. this._onChangeCallback();
  277. return this;
  278. }
  279. /**
  280. * Writes the components of this Euler instance to the given array. If no array is provided,
  281. * the method returns a new instance.
  282. *
  283. * @param {Array<number,number,number,string>} [array=[]] - The target array holding the Euler components.
  284. * @param {number} [offset=0] - Index of the first element in the array.
  285. * @return {Array<number,number,number,string>} The Euler components.
  286. */
  287. toArray( array = [], offset = 0 ) {
  288. array[ offset ] = this._x;
  289. array[ offset + 1 ] = this._y;
  290. array[ offset + 2 ] = this._z;
  291. array[ offset + 3 ] = this._order;
  292. return array;
  293. }
  294. _onChange( callback ) {
  295. this._onChangeCallback = callback;
  296. return this;
  297. }
  298. _onChangeCallback() {}
  299. *[ Symbol.iterator ]() {
  300. yield this._x;
  301. yield this._y;
  302. yield this._z;
  303. yield this._order;
  304. }
  305. }
  306. /**
  307. * The default Euler angle order.
  308. *
  309. * @static
  310. * @type {string}
  311. * @default 'XYZ'
  312. */
  313. Euler.DEFAULT_ORDER = 'XYZ';
  314. export { Euler };
粤ICP备19079148号