Euler.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. import { Quaternion } from './Quaternion.js';
  2. import { Vector3 } from './Vector3.js';
  3. import { Matrix4 } from './Matrix4.js';
  4. import { MathUtils } from './MathUtils.js';
  5. const _matrix = /*@__PURE__*/ new Matrix4();
  6. const _quaternion = /*@__PURE__*/ new Quaternion();
  7. class Euler {
  8. constructor( x = 0, y = 0, z = 0, order = Euler.DefaultOrder ) {
  9. this._x = x;
  10. this._y = y;
  11. this._z = z;
  12. this._order = order;
  13. }
  14. get x() {
  15. return this._x;
  16. }
  17. set x( value ) {
  18. this._x = value;
  19. this._onChangeCallback();
  20. }
  21. get y() {
  22. return this._y;
  23. }
  24. set y( value ) {
  25. this._y = value;
  26. this._onChangeCallback();
  27. }
  28. get z() {
  29. return this._z;
  30. }
  31. set z( value ) {
  32. this._z = value;
  33. this._onChangeCallback();
  34. }
  35. get order() {
  36. return this._order;
  37. }
  38. set order( value ) {
  39. this._order = value;
  40. this._onChangeCallback();
  41. }
  42. set( x, y, z, order ) {
  43. this._x = x;
  44. this._y = y;
  45. this._z = z;
  46. this._order = order || this._order;
  47. this._onChangeCallback();
  48. return this;
  49. }
  50. clone() {
  51. return new this.constructor( this._x, this._y, this._z, this._order );
  52. }
  53. copy( euler ) {
  54. this._x = euler._x;
  55. this._y = euler._y;
  56. this._z = euler._z;
  57. this._order = euler._order;
  58. this._onChangeCallback();
  59. return this;
  60. }
  61. setFromRotationMatrix( m, order, update ) {
  62. const clamp = MathUtils.clamp;
  63. // assumes the upper 3x3 of m is a pure rotation matrix (i.e, unscaled)
  64. const te = m.elements;
  65. const m11 = te[ 0 ], m12 = te[ 4 ], m13 = te[ 8 ];
  66. const m21 = te[ 1 ], m22 = te[ 5 ], m23 = te[ 9 ];
  67. const m31 = te[ 2 ], m32 = te[ 6 ], m33 = te[ 10 ];
  68. order = order || this._order;
  69. switch ( order ) {
  70. case 'XYZ':
  71. this._y = Math.asin( clamp( m13, - 1, 1 ) );
  72. if ( Math.abs( m13 ) < 0.9999999 ) {
  73. this._x = Math.atan2( - m23, m33 );
  74. this._z = Math.atan2( - m12, m11 );
  75. } else {
  76. this._x = Math.atan2( m32, m22 );
  77. this._z = 0;
  78. }
  79. break;
  80. case 'YXZ':
  81. this._x = Math.asin( - clamp( m23, - 1, 1 ) );
  82. if ( Math.abs( m23 ) < 0.9999999 ) {
  83. this._y = Math.atan2( m13, m33 );
  84. this._z = Math.atan2( m21, m22 );
  85. } else {
  86. this._y = Math.atan2( - m31, m11 );
  87. this._z = 0;
  88. }
  89. break;
  90. case 'ZXY':
  91. this._x = Math.asin( clamp( m32, - 1, 1 ) );
  92. if ( Math.abs( m32 ) < 0.9999999 ) {
  93. this._y = Math.atan2( - m31, m33 );
  94. this._z = Math.atan2( - m12, m22 );
  95. } else {
  96. this._y = 0;
  97. this._z = Math.atan2( m21, m11 );
  98. }
  99. break;
  100. case 'ZYX':
  101. this._y = Math.asin( - clamp( m31, - 1, 1 ) );
  102. if ( Math.abs( m31 ) < 0.9999999 ) {
  103. this._x = Math.atan2( m32, m33 );
  104. this._z = Math.atan2( m21, m11 );
  105. } else {
  106. this._x = 0;
  107. this._z = Math.atan2( - m12, m22 );
  108. }
  109. break;
  110. case 'YZX':
  111. this._z = Math.asin( clamp( m21, - 1, 1 ) );
  112. if ( Math.abs( m21 ) < 0.9999999 ) {
  113. this._x = Math.atan2( - m23, m22 );
  114. this._y = Math.atan2( - m31, m11 );
  115. } else {
  116. this._x = 0;
  117. this._y = Math.atan2( m13, m33 );
  118. }
  119. break;
  120. case 'XZY':
  121. this._z = Math.asin( - clamp( m12, - 1, 1 ) );
  122. if ( Math.abs( m12 ) < 0.9999999 ) {
  123. this._x = Math.atan2( m32, m22 );
  124. this._y = Math.atan2( m13, m11 );
  125. } else {
  126. this._x = Math.atan2( - m23, m33 );
  127. this._y = 0;
  128. }
  129. break;
  130. default:
  131. console.warn( 'THREE.Euler: .setFromRotationMatrix() encountered an unknown order: ' + order );
  132. }
  133. this._order = order;
  134. if ( update !== false ) this._onChangeCallback();
  135. return this;
  136. }
  137. setFromQuaternion( q, order, update ) {
  138. _matrix.makeRotationFromQuaternion( q );
  139. return this.setFromRotationMatrix( _matrix, order, update );
  140. }
  141. setFromVector3( v, order ) {
  142. return this.set( v.x, v.y, v.z, order || this._order );
  143. }
  144. reorder( newOrder ) {
  145. // WARNING: this discards revolution information -bhouston
  146. _quaternion.setFromEuler( this );
  147. return this.setFromQuaternion( _quaternion, newOrder );
  148. }
  149. equals( euler ) {
  150. return ( euler._x === this._x ) && ( euler._y === this._y ) && ( euler._z === this._z ) && ( euler._order === this._order );
  151. }
  152. fromArray( array ) {
  153. this._x = array[ 0 ];
  154. this._y = array[ 1 ];
  155. this._z = array[ 2 ];
  156. if ( array[ 3 ] !== undefined ) this._order = array[ 3 ];
  157. this._onChangeCallback();
  158. return this;
  159. }
  160. toArray( array = [], offset = 0 ) {
  161. array[ offset ] = this._x;
  162. array[ offset + 1 ] = this._y;
  163. array[ offset + 2 ] = this._z;
  164. array[ offset + 3 ] = this._order;
  165. return array;
  166. }
  167. toVector3( optionalResult ) {
  168. if ( optionalResult ) {
  169. return optionalResult.set( this._x, this._y, this._z );
  170. } else {
  171. return new Vector3( this._x, this._y, this._z );
  172. }
  173. }
  174. _onChange( callback ) {
  175. this._onChangeCallback = callback;
  176. return this;
  177. }
  178. _onChangeCallback() {}
  179. }
  180. Euler.prototype.isEuler = true;
  181. Euler.DefaultOrder = 'XYZ';
  182. Euler.RotationOrders = [ 'XYZ', 'YZX', 'ZXY', 'XZY', 'YXZ', 'ZYX' ];
  183. export { Euler };