Quaternion.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /*
  2. * @author mikael emtinger / http://gomo.se/
  3. */
  4. THREE.Quaternion = function( _x, _y, _z, _w ) {
  5. this.x = _x || 0;
  6. this.y = _y || 0;
  7. this.z = _z || 0;
  8. this.w = _w !== undefined ? _w : 1;
  9. this.api = {
  10. isDirty: false,
  11. that: this,
  12. get x() { return this.that.x; },
  13. get y() { return this.that.y; },
  14. get z() { return this.that.z; },
  15. get w() { return this.that.w; },
  16. set x( value ) { this.that.x = value; this.isDirty = true; },
  17. set y( value ) { this.that.y = value; this.isDirty = true; },
  18. set z( value ) { this.that.z = value; this.isDirty = true; },
  19. set w( value ) { this.that.w = value; this.isDirty = true; },
  20. };
  21. this.api.__proto__ = THREE.Quaternion.prototype;
  22. return this.api;
  23. };
  24. THREE.Quaternion.prototype.set = function( x, y, z, w ) {
  25. var quat = this.that;
  26. quat.x = x;
  27. quat.y = y;
  28. quat.z = z;
  29. quat.w = w;
  30. this.isDirty = true;
  31. return this;
  32. };
  33. THREE.Quaternion.prototype.setFromEuler = function( vec3 ) {
  34. var c = 0.5 * Math.PI / 360, // 0.5 is an optimization
  35. x = vec3.x * c,
  36. y = vec3.y * c,
  37. z = vec3.z * c,
  38. c1 = Math.cos( y ),
  39. s1 = Math.sin( y ),
  40. c2 = Math.cos( -z ),
  41. s2 = Math.sin( -z ),
  42. c3 = Math.cos( x ),
  43. s3 = Math.sin( x ),
  44. c1c2 = c1 * c2,
  45. s1s2 = s1 * s2,
  46. quat = this.that;
  47. quat.w = c1c2 * c3 - s1s2 * s3;
  48. quat.x = c1c2 * s3 + s1s2 * c3;
  49. quat.y = s1 * c2 * c3 + c1 * s2 * s3;
  50. quat.z = c1 * s2 * c3 - s1 * c2 * s3;
  51. this.isDirty = true;
  52. return this;
  53. };
  54. THREE.Quaternion.prototype.calculateW = function() {
  55. var quat = this.that,
  56. x = quat.x,
  57. y = quat.y,
  58. z = quat.z;
  59. quat.w = -Math.sqrt( Math.abs( 1.0 - x*x - y*y - z*z ));
  60. this.isDirty = true;
  61. return this;
  62. };
  63. THREE.Quaternion.prototype.inverse = function() {
  64. var quat = this.that;
  65. quat.x *= -1;
  66. quat.y *= -1;
  67. quat.z *= -1;
  68. this.isDirty = true;
  69. return this;
  70. };
  71. THREE.Quaternion.prototype.length = function() {
  72. var quat = this.that;
  73. return Math.sqrt( quat.x * quat.x + quat.y * quat.y + quat.z * quat.z + quat.w * quat.w );
  74. };
  75. THREE.Quaternion.prototype.normalize = function() {
  76. var quat = this.that,
  77. x = quat.x,
  78. y = quat.y,
  79. z = quat.z,
  80. w = quat.w;
  81. var len = Math.sqrt( x*x + y*y + z*z + w*w );
  82. if ( len == 0 ) {
  83. quat.x = 0;
  84. quat.y = 0;
  85. quat.z = 0;
  86. quat.w = 0;
  87. this.isDirty = true;
  88. return this;
  89. }
  90. len = 1 / len;
  91. quat.x = x * len;
  92. quat.y = y * len;
  93. quat.z = z * len;
  94. quat.w = w * len;
  95. this.isDirty = true;
  96. return this;
  97. };
  98. THREE.Quaternion.prototype.multiplySelf = function( quat2 ) {
  99. var quat = this.that;
  100. qax = quat.x, qay = quat.y, qaz = quat.z, qaw = quat.w,
  101. qbx = quat2.x, qby = quat2.y, qbz = quat2.z, qbw = quat2.w;
  102. quat.x = qax * qbw + qaw * qbx + qay * qbz - qaz * qby;
  103. quat.y = qay * qbw + qaw * qby + qaz * qbx - qax * qbz;
  104. quat.z = qaz * qbw + qaw * qbz + qax * qby - qay * qbx;
  105. quat.w = qaw * qbw - qax * qbx - qay * qby - qaz * qbz;
  106. this.isDirty = true;
  107. return this;
  108. };
  109. THREE.Quaternion.prototype.multiplyVector3 = function( vec, dest ) {
  110. if( !dest ) { dest = vec; }
  111. var quat = this.that,
  112. x = vec.x, y = vec.y, z = vec.z,
  113. qx = quat.x, qy = quat.y, qz = quat.z, qw = quat.w;
  114. // calculate quat * vec
  115. var ix = qw * x + qy * z - qz * y,
  116. iy = qw * y + qz * x - qx * z,
  117. iz = qw * z + qx * y - qy * x,
  118. iw = -qx * x - qy * y - qz * z;
  119. // calculate result * inverse quat
  120. dest.x = ix * qw + iw * -qx + iy * -qz - iz * -qy;
  121. dest.y = iy * qw + iw * -qy + iz * -qx - ix * -qz;
  122. dest.z = iz * qw + iw * -qz + ix * -qy - iy * -qx;
  123. return dest;
  124. };
  125. THREE.Quaternion.prototype.toMatrix3 = function( matrix3 ) {
  126. /* todo: create a proper Matrix3 first
  127. var x = quat[0], y = quat[1], z = quat[2], w = quat[3];
  128. var x2 = x + x;
  129. var y2 = y + y;
  130. var z2 = z + z;
  131. var xx = x*x2;
  132. var xy = x*y2;
  133. var xz = x*z2;
  134. var yy = y*y2;
  135. var yz = y*z2;
  136. var zz = z*z2;
  137. var wx = w*x2;
  138. var wy = w*y2;
  139. var wz = w*z2;
  140. dest[0] = 1 - (yy + zz);
  141. dest[1] = xy - wz;
  142. dest[2] = xz + wy;
  143. dest[3] = xy + wz;
  144. dest[4] = 1 - (xx + zz);
  145. dest[5] = yz - wx;
  146. dest[6] = xz - wy;
  147. dest[7] = yz + wx;
  148. dest[8] = 1 - (xx + yy);
  149. return dest;*/
  150. };
  151. THREE.Quaternion.prototype.toMatrix4 = function( matrix4 ) {
  152. /* todo: this is implemented in Matrix4.setFromQuaternion
  153. var x = quat[0], y = quat[1], z = quat[2], w = quat[3];
  154. var x2 = x + x;
  155. var y2 = y + y;
  156. var z2 = z + z;
  157. var xx = x*x2;
  158. var xy = x*y2;
  159. var xz = x*z2;
  160. var yy = y*y2;
  161. var yz = y*z2;
  162. var zz = z*z2;
  163. var wx = w*x2;
  164. var wy = w*y2;
  165. var wz = w*z2;
  166. dest[0] = 1 - (yy + zz);
  167. dest[1] = xy - wz;
  168. dest[2] = xz + wy;
  169. dest[3] = 0;
  170. dest[4] = xy + wz;
  171. dest[5] = 1 - (xx + zz);
  172. dest[6] = yz - wx;
  173. dest[7] = 0;
  174. dest[8] = xz - wy;
  175. dest[9] = yz + wx;
  176. dest[10] = 1 - (xx + yy);
  177. dest[11] = 0;
  178. dest[12] = 0;
  179. dest[13] = 0;
  180. dest[14] = 0;
  181. dest[15] = 1;
  182. */
  183. };
  184. THREE.Quaternion.slerp = function( qa, qb, qm, t ) {
  185. var cosHalfTheta = qa.w * qb.w + qa.x * qb.x + qa.y * qb.y + qa.z * qb.z;
  186. if( Math.abs( cosHalfTheta ) >= 1.0) {
  187. qm.w = qa.w; qm.x = qa.x; qm.y = qa.y; qm.z = qa.z;
  188. return qm;
  189. }
  190. var halfTheta = Math.acos( cosHalfTheta ),
  191. sinHalfTheta = Math.sqrt( 1.0 - cosHalfTheta * cosHalfTheta );
  192. if( Math.abs( sinHalfTheta ) < 0.001 ) {
  193. qm.w = 0.5 * ( qa.w + qb.w );
  194. qm.x = 0.5 * ( qa.x + qb.x );
  195. qm.y = 0.5 * ( qa.y + qb.y );
  196. qm.z = 0.5 * ( qa.z + qb.z );
  197. return qm;
  198. }
  199. var ratioA = Math.sin( (1 - t) * halfTheta ) / sinHalfTheta,
  200. ratioB = Math.sin( t * halfTheta ) / sinHalfTheta;
  201. qm.w = ( qa.w * ratioA + qb.w * ratioB );
  202. qm.x = ( qa.x * ratioA + qb.x * ratioB );
  203. qm.y = ( qa.y * ratioA + qb.y * ratioB );
  204. qm.z = ( qa.z * ratioA + qb.z * ratioB );
  205. return qm;
  206. };