Quaternion.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /**
  2. * @author mikael emtinger / http://gomo.se/
  3. * @author alteredq / http://alteredqualia.com/
  4. */
  5. THREE.Quaternion = function( x, y, z, w ) {
  6. this.set(
  7. x || 0,
  8. y || 0,
  9. z || 0,
  10. w !== undefined ? w : 1
  11. );
  12. };
  13. THREE.Quaternion.prototype = {
  14. constructor: THREE.Quaternion,
  15. set: function ( x, y, z, w ) {
  16. this.x = x;
  17. this.y = y;
  18. this.z = z;
  19. this.w = w;
  20. return this;
  21. },
  22. copy: function ( q ) {
  23. this.x = q.x;
  24. this.y = q.y;
  25. this.z = q.z;
  26. this.w = q.w;
  27. return this;
  28. },
  29. setFromEuler: function ( vec3 ) {
  30. var c = Math.PI / 360, // 0.5 * Math.PI / 360, // 0.5 is an optimization
  31. x = vec3.x * c,
  32. y = vec3.y * c,
  33. z = vec3.z * c,
  34. c1 = Math.cos( y ),
  35. s1 = Math.sin( y ),
  36. c2 = Math.cos( -z ),
  37. s2 = Math.sin( -z ),
  38. c3 = Math.cos( x ),
  39. s3 = Math.sin( x ),
  40. c1c2 = c1 * c2,
  41. s1s2 = s1 * s2;
  42. this.w = c1c2 * c3 - s1s2 * s3;
  43. this.x = c1c2 * s3 + s1s2 * c3;
  44. this.y = s1 * c2 * c3 + c1 * s2 * s3;
  45. this.z = c1 * s2 * c3 - s1 * c2 * s3;
  46. return this;
  47. },
  48. setFromAxisAngle: function ( axis, angle ) {
  49. // from http://www.euclideanspace.com/maths/geometry/rotations/conversions/angleToQuaternion/index.htm
  50. // axis have to be normalized
  51. var halfAngle = angle / 2,
  52. s = Math.sin( halfAngle );
  53. this.x = axis.x * s;
  54. this.y = axis.y * s;
  55. this.z = axis.z * s;
  56. this.w = Math.cos( halfAngle );
  57. return this;
  58. },
  59. setFromRotationMatrix: function ( m ) {
  60. // Adapted from: http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQuaternion/index.htm
  61. function copySign(a, b) {
  62. return b < 0 ? -Math.abs(a) : Math.abs(a);
  63. }
  64. var absQ = Math.pow(m.determinant(), 1.0 / 3.0);
  65. this.w = Math.sqrt( Math.max( 0, absQ + m.n11 + m.n22 + m.n33 ) ) / 2;
  66. this.x = Math.sqrt( Math.max( 0, absQ + m.n11 - m.n22 - m.n33 ) ) / 2;
  67. this.y = Math.sqrt( Math.max( 0, absQ - m.n11 + m.n22 - m.n33 ) ) / 2;
  68. this.z = Math.sqrt( Math.max( 0, absQ - m.n11 - m.n22 + m.n33 ) ) / 2;
  69. this.x = copySign( this.x, ( m.n32 - m.n23 ) );
  70. this.y = copySign( this.y, ( m.n13 - m.n31 ) );
  71. this.z = copySign( this.z, ( m.n21 - m.n12 ) );
  72. this.normalize();
  73. return this;
  74. },
  75. calculateW : function () {
  76. this.w = - Math.sqrt( Math.abs( 1.0 - this.x * this.x - this.y * this.y - this.z * this.z ) );
  77. return this;
  78. },
  79. inverse: function () {
  80. this.x *= -1;
  81. this.y *= -1;
  82. this.z *= -1;
  83. return this;
  84. },
  85. length: function () {
  86. return Math.sqrt( this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w );
  87. },
  88. normalize: function () {
  89. var l = Math.sqrt( this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w );
  90. if ( l === 0 ) {
  91. this.x = 0;
  92. this.y = 0;
  93. this.z = 0;
  94. this.w = 0;
  95. } else {
  96. l = 1 / l;
  97. this.x = this.x * l;
  98. this.y = this.y * l;
  99. this.z = this.z * l;
  100. this.w = this.w * l;
  101. }
  102. return this;
  103. },
  104. multiplySelf: function ( quat2 ) {
  105. var qax = this.x, qay = this.y, qaz = this.z, qaw = this.w,
  106. qbx = quat2.x, qby = quat2.y, qbz = quat2.z, qbw = quat2.w;
  107. this.x = qax * qbw + qaw * qbx + qay * qbz - qaz * qby;
  108. this.y = qay * qbw + qaw * qby + qaz * qbx - qax * qbz;
  109. this.z = qaz * qbw + qaw * qbz + qax * qby - qay * qbx;
  110. this.w = qaw * qbw - qax * qbx - qay * qby - qaz * qbz;
  111. return this;
  112. },
  113. multiply: function ( q1, q2 ) {
  114. // from http://www.euclideanspace.com/maths/algebra/realNormedAlgebra/quaternions/code/index.htm
  115. this.x = q1.x * q2.w + q1.y * q2.z - q1.z * q2.y + q1.w * q2.x;
  116. this.y = -q1.x * q2.z + q1.y * q2.w + q1.z * q2.x + q1.w * q2.y;
  117. this.z = q1.x * q2.y - q1.y * q2.x + q1.z * q2.w + q1.w * q2.z;
  118. this.w = -q1.x * q2.x - q1.y * q2.y - q1.z * q2.z + q1.w * q2.w;
  119. return this;
  120. },
  121. multiplyVector3: function ( vec, dest ) {
  122. if( !dest ) { dest = vec; }
  123. var x = vec.x, y = vec.y, z = vec.z,
  124. qx = this.x, qy = this.y, qz = this.z, qw = this.w;
  125. // calculate quat * vec
  126. var ix = qw * x + qy * z - qz * y,
  127. iy = qw * y + qz * x - qx * z,
  128. iz = qw * z + qx * y - qy * x,
  129. iw = -qx * x - qy * y - qz * z;
  130. // calculate result * inverse quat
  131. dest.x = ix * qw + iw * -qx + iy * -qz - iz * -qy;
  132. dest.y = iy * qw + iw * -qy + iz * -qx - ix * -qz;
  133. dest.z = iz * qw + iw * -qz + ix * -qy - iy * -qx;
  134. return dest;
  135. }
  136. }
  137. THREE.Quaternion.slerp = function ( qa, qb, qm, t ) {
  138. // http://www.euclideanspace.com/maths/algebra/realNormedAlgebra/quaternions/slerp/
  139. var cosHalfTheta = qa.w * qb.w + qa.x * qb.x + qa.y * qb.y + qa.z * qb.z;
  140. if (cosHalfTheta < 0) {
  141. qm.w = -qb.w; qm.x = -qb.x; qm.y = -qb.y; qm.z = -qb.z;
  142. cosHalfTheta = -cosHalfTheta;
  143. } else {
  144. qm.copy(qb);
  145. }
  146. if ( Math.abs( cosHalfTheta ) >= 1.0 ) {
  147. qm.w = qa.w; qm.x = qa.x; qm.y = qa.y; qm.z = qa.z;
  148. return qm;
  149. }
  150. var halfTheta = Math.acos( cosHalfTheta ),
  151. sinHalfTheta = Math.sqrt( 1.0 - cosHalfTheta * cosHalfTheta );
  152. if ( Math.abs( sinHalfTheta ) < 0.001 ) {
  153. qm.w = 0.5 * ( qa.w + qb.w );
  154. qm.x = 0.5 * ( qa.x + qb.x );
  155. qm.y = 0.5 * ( qa.y + qb.y );
  156. qm.z = 0.5 * ( qa.z + qb.z );
  157. return qm;
  158. }
  159. var ratioA = Math.sin( ( 1 - t ) * halfTheta ) / sinHalfTheta,
  160. ratioB = Math.sin( t * halfTheta ) / sinHalfTheta;
  161. qm.w = ( qa.w * ratioA + qm.w * ratioB );
  162. qm.x = ( qa.x * ratioA + qm.x * ratioB );
  163. qm.y = ( qa.y * ratioA + qm.y * ratioB );
  164. qm.z = ( qa.z * ratioA + qm.z * ratioB );
  165. return qm;
  166. }
粤ICP备19079148号