Euler.js 5.1 KB

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