Euler.js 5.4 KB

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