Euler.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. import { Quaternion } from './Quaternion.js';
  2. import { Matrix4 } from './Matrix4.js';
  3. import { clamp } from './MathUtils.js';
  4. const _matrix = /*@__PURE__*/ new Matrix4();
  5. const _quaternion = /*@__PURE__*/ new Quaternion();
  6. class Euler {
  7. constructor( x = 0, y = 0, z = 0, order = Euler.DEFAULT_ORDER ) {
  8. this.isEuler = true;
  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 = this._order ) {
  43. this._x = x;
  44. this._y = y;
  45. this._z = z;
  46. this._order = 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 = this._order, update = true ) {
  62. // assumes the upper 3x3 of m is a pure rotation matrix (i.e, unscaled)
  63. const te = m.elements;
  64. const m11 = te[ 0 ], m12 = te[ 4 ], m13 = te[ 8 ];
  65. const m21 = te[ 1 ], m22 = te[ 5 ], m23 = te[ 9 ];
  66. const m31 = te[ 2 ], m32 = te[ 6 ], m33 = te[ 10 ];
  67. switch ( order ) {
  68. case 'XYZ':
  69. this._y = Math.asin( clamp( m13, - 1, 1 ) );
  70. if ( Math.abs( m13 ) < 0.9999999 ) {
  71. this._x = Math.atan2( - m23, m33 );
  72. this._z = Math.atan2( - m12, m11 );
  73. } else {
  74. this._x = Math.atan2( m32, m22 );
  75. this._z = 0;
  76. }
  77. break;
  78. case 'YXZ':
  79. this._x = Math.asin( - clamp( m23, - 1, 1 ) );
  80. if ( Math.abs( m23 ) < 0.9999999 ) {
  81. this._y = Math.atan2( m13, m33 );
  82. this._z = Math.atan2( m21, m22 );
  83. } else {
  84. this._y = Math.atan2( - m31, m11 );
  85. this._z = 0;
  86. }
  87. break;
  88. case 'ZXY':
  89. this._x = Math.asin( clamp( m32, - 1, 1 ) );
  90. if ( Math.abs( m32 ) < 0.9999999 ) {
  91. this._y = Math.atan2( - m31, m33 );
  92. this._z = Math.atan2( - m12, m22 );
  93. } else {
  94. this._y = 0;
  95. this._z = Math.atan2( m21, m11 );
  96. }
  97. break;
  98. case 'ZYX':
  99. this._y = Math.asin( - clamp( m31, - 1, 1 ) );
  100. if ( Math.abs( m31 ) < 0.9999999 ) {
  101. this._x = Math.atan2( m32, m33 );
  102. this._z = Math.atan2( m21, m11 );
  103. } else {
  104. this._x = 0;
  105. this._z = Math.atan2( - m12, m22 );
  106. }
  107. break;
  108. case 'YZX':
  109. this._z = Math.asin( clamp( m21, - 1, 1 ) );
  110. if ( Math.abs( m21 ) < 0.9999999 ) {
  111. this._x = Math.atan2( - m23, m22 );
  112. this._y = Math.atan2( - m31, m11 );
  113. } else {
  114. this._x = 0;
  115. this._y = Math.atan2( m13, m33 );
  116. }
  117. break;
  118. case 'XZY':
  119. this._z = Math.asin( - clamp( m12, - 1, 1 ) );
  120. if ( Math.abs( m12 ) < 0.9999999 ) {
  121. this._x = Math.atan2( m32, m22 );
  122. this._y = Math.atan2( m13, m11 );
  123. } else {
  124. this._x = Math.atan2( - m23, m33 );
  125. this._y = 0;
  126. }
  127. break;
  128. default:
  129. console.warn( 'THREE.Euler: .setFromRotationMatrix() encountered an unknown order: ' + order );
  130. }
  131. this._order = order;
  132. if ( update === true ) this._onChangeCallback();
  133. return this;
  134. }
  135. setFromQuaternion( q, order, update ) {
  136. _matrix.makeRotationFromQuaternion( q );
  137. return this.setFromRotationMatrix( _matrix, order, update );
  138. }
  139. setFromVector3( v, order = this._order ) {
  140. return this.set( v.x, v.y, v.z, order );
  141. }
  142. reorder( newOrder ) {
  143. // WARNING: this discards revolution information -bhouston
  144. _quaternion.setFromEuler( this );
  145. return this.setFromQuaternion( _quaternion, newOrder );
  146. }
  147. equals( euler ) {
  148. return ( euler._x === this._x ) && ( euler._y === this._y ) && ( euler._z === this._z ) && ( euler._order === this._order );
  149. }
  150. fromArray( array ) {
  151. this._x = array[ 0 ];
  152. this._y = array[ 1 ];
  153. this._z = array[ 2 ];
  154. if ( array[ 3 ] !== undefined ) this._order = array[ 3 ];
  155. this._onChangeCallback();
  156. return this;
  157. }
  158. toArray( array = [], offset = 0 ) {
  159. array[ offset ] = this._x;
  160. array[ offset + 1 ] = this._y;
  161. array[ offset + 2 ] = this._z;
  162. array[ offset + 3 ] = this._order;
  163. return array;
  164. }
  165. _onChange( callback ) {
  166. this._onChangeCallback = callback;
  167. return this;
  168. }
  169. _onChangeCallback() {}
  170. *[ Symbol.iterator ]() {
  171. yield this._x;
  172. yield this._y;
  173. yield this._z;
  174. yield this._order;
  175. }
  176. }
  177. Euler.DEFAULT_ORDER = 'XYZ';
  178. export { Euler };