Euler.js 5.0 KB

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