Euler.js 5.3 KB

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