Euler.js 5.4 KB

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