Euler.js 4.7 KB

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