Euler.js 5.3 KB

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