Vector3.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. /**
  2. * @author mr.doob / http://mrdoob.com/
  3. * @author kile / http://kile.stravaganza.org/
  4. * @author philogb / http://blog.thejit.org/
  5. * @author mikael emtinger / http://gomo.se/
  6. * @author egraether / http://egraether.com/
  7. */
  8. THREE.Vector3 = function ( x, y, z ) {
  9. this.x = x || 0;
  10. this.y = y || 0;
  11. this.z = z || 0;
  12. };
  13. THREE.Vector3.prototype = {
  14. constructor: THREE.Vector3,
  15. set: function ( x, y, z ) {
  16. this.x = x;
  17. this.y = y;
  18. this.z = z;
  19. return this;
  20. },
  21. setX: function ( x ) {
  22. this.x = x;
  23. return this;
  24. },
  25. setY: function ( y ) {
  26. this.y = y;
  27. return this;
  28. },
  29. setZ: function ( z ) {
  30. this.z = z;
  31. return this;
  32. },
  33. copy: function ( v ) {
  34. this.x = v.x;
  35. this.y = v.y;
  36. this.z = v.z;
  37. return this;
  38. },
  39. add: function ( a, b ) {
  40. this.x = a.x + b.x;
  41. this.y = a.y + b.y;
  42. this.z = a.z + b.z;
  43. return this;
  44. },
  45. addSelf: function ( v ) {
  46. this.x += v.x;
  47. this.y += v.y;
  48. this.z += v.z;
  49. return this;
  50. },
  51. addScalar: function ( s ) {
  52. this.x += s;
  53. this.y += s;
  54. this.z += s;
  55. return this;
  56. },
  57. sub: function ( a, b ) {
  58. this.x = a.x - b.x;
  59. this.y = a.y - b.y;
  60. this.z = a.z - b.z;
  61. return this;
  62. },
  63. subSelf: function ( v ) {
  64. this.x -= v.x;
  65. this.y -= v.y;
  66. this.z -= v.z;
  67. return this;
  68. },
  69. multiply: function ( a, b ) {
  70. this.x = a.x * b.x;
  71. this.y = a.y * b.y;
  72. this.z = a.z * b.z;
  73. return this;
  74. },
  75. multiplySelf: function ( v ) {
  76. this.x *= v.x;
  77. this.y *= v.y;
  78. this.z *= v.z;
  79. return this;
  80. },
  81. multiplyScalar: function ( s ) {
  82. this.x *= s;
  83. this.y *= s;
  84. this.z *= s;
  85. return this;
  86. },
  87. divideSelf: function ( v ) {
  88. this.x /= v.x;
  89. this.y /= v.y;
  90. this.z /= v.z;
  91. return this;
  92. },
  93. divideScalar: function ( s ) {
  94. if ( s ) {
  95. this.x /= s;
  96. this.y /= s;
  97. this.z /= s;
  98. } else {
  99. this.x = 0;
  100. this.y = 0;
  101. this.z = 0;
  102. }
  103. return this;
  104. },
  105. negate: function() {
  106. return this.multiplyScalar( - 1 );
  107. },
  108. dot: function ( v ) {
  109. return this.x * v.x + this.y * v.y + this.z * v.z;
  110. },
  111. lengthSq: function () {
  112. return this.x * this.x + this.y * this.y + this.z * this.z;
  113. },
  114. length: function () {
  115. return Math.sqrt( this.lengthSq() );
  116. },
  117. lengthManhattan: function () {
  118. return Math.abs( this.x ) + Math.abs( this.y ) + Math.abs( this.z );
  119. },
  120. normalize: function () {
  121. return this.divideScalar( this.length() );
  122. },
  123. setLength: function ( l ) {
  124. return this.normalize().multiplyScalar( l );
  125. },
  126. lerpSelf: function ( v, alpha ) {
  127. this.x += ( v.x - this.x ) * alpha;
  128. this.y += ( v.y - this.y ) * alpha;
  129. this.z += ( v.z - this.z ) * alpha;
  130. return this;
  131. },
  132. cross: function ( a, b ) {
  133. this.x = a.y * b.z - a.z * b.y;
  134. this.y = a.z * b.x - a.x * b.z;
  135. this.z = a.x * b.y - a.y * b.x;
  136. return this;
  137. },
  138. crossSelf: function ( v ) {
  139. var x = this.x, y = this.y, z = this.z;
  140. this.x = y * v.z - z * v.y;
  141. this.y = z * v.x - x * v.z;
  142. this.z = x * v.y - y * v.x;
  143. return this;
  144. },
  145. distanceTo: function ( v ) {
  146. return Math.sqrt( this.distanceToSquared( v ) );
  147. },
  148. distanceToSquared: function ( v ) {
  149. return new THREE.Vector3().sub( this, v ).lengthSq();
  150. },
  151. getPositionFromMatrix: function ( m ) {
  152. this.x = m.elements[12];
  153. this.y = m.elements[13];
  154. this.z = m.elements[14];
  155. return this;
  156. },
  157. getRotationFromMatrix: function ( m, scale ) {
  158. var sx = scale ? scale.x : 1;
  159. var sy = scale ? scale.y : 1;
  160. var sz = scale ? scale.z : 1;
  161. var m11 = m.elements[0] / sx, m12 = m.elements[4] / sy, m13 = m.elements[8] / sz;
  162. var m21 = m.elements[1] / sx, m22 = m.elements[5] / sy, m23 = m.elements[9] / sz;
  163. var m33 = m.elements[10] / sz;
  164. this.y = Math.asin( m13 );
  165. var cosY = Math.cos( this.y );
  166. if ( Math.abs( cosY ) > 0.00001 ) {
  167. this.x = Math.atan2( - m23 / cosY, m33 / cosY );
  168. this.z = Math.atan2( - m12 / cosY, m11 / cosY );
  169. } else {
  170. this.x = 0;
  171. this.z = Math.atan2( m21, m22 );
  172. }
  173. return this;
  174. },
  175. /*
  176. // from http://www.mathworks.com/matlabcentral/fileexchange/20696-function-to-convert-between-dcm-euler-angles-quaternions-and-euler-vectors/content/SpinCalc.m
  177. // order XYZ
  178. getEulerXYZFromQuaternion: function ( q ) {
  179. this.x = Math.atan2( 2 * ( q.x * q.w - q.y * q.z ), ( q.w * q.w - q.x * q.x - q.y * q.y + q.z * q.z ) );
  180. this.y = Math.asin( 2 * ( q.x * q.z + q.y * q.w ) );
  181. this.z = Math.atan2( 2 * ( q.z * q.w - q.x * q.y ), ( q.w * q.w + q.x * q.x - q.y * q.y - q.z * q.z ) );
  182. },
  183. // from http://www.euclideanspace.com/maths/geometry/rotations/conversions/quaternionToEuler/index.htm
  184. // order YZX (assuming heading == y, attitude == z, bank == x)
  185. getEulerYZXFromQuaternion: function ( q ) {
  186. var sqw = q.w * q.w;
  187. var sqx = q.x * q.x;
  188. var sqy = q.y * q.y;
  189. var sqz = q.z * q.z;
  190. var unit = sqx + sqy + sqz + sqw; // if normalised is one, otherwise is correction factor
  191. var test = q.x * q.y + q.z * q.w;
  192. if ( test > 0.499 * unit ) { // singularity at north pole
  193. this.y = 2 * Math.atan2( q.x, q.w );
  194. this.z = Math.PI / 2;
  195. this.x = 0;
  196. return;
  197. }
  198. if ( test < -0.499 * unit ) { // singularity at south pole
  199. this.y = -2 * Math.atan2( q.x, q.w );
  200. this.z = -Math.PI / 2;
  201. this.x = 0;
  202. return;
  203. }
  204. this.y = Math.atan2( 2 * q.y * q.w - 2 * q.x * q.z, sqx - sqy - sqz + sqw );
  205. this.z = Math.asin( 2 * test / unit );
  206. this.x = Math.atan2( 2 * q.x * q.w - 2 * q.y * q.z, -sqx + sqy - sqz + sqw );
  207. },
  208. */
  209. getScaleFromMatrix: function ( m ) {
  210. var sx = this.set( m.elements[0], m.elements[1], m.elements[2] ).length();
  211. var sy = this.set( m.elements[4], m.elements[5], m.elements[6] ).length();
  212. var sz = this.set( m.elements[8], m.elements[9], m.elements[10] ).length();
  213. this.x = sx;
  214. this.y = sy;
  215. this.z = sz;
  216. },
  217. equals: function ( v ) {
  218. return ( ( v.x === this.x ) && ( v.y === this.y ) && ( v.z === this.z ) );
  219. },
  220. isZero: function () {
  221. return ( this.lengthSq() < 0.0001 /* almostZero */ );
  222. },
  223. clone: function () {
  224. return new THREE.Vector3( this.x, this.y, this.z );
  225. }
  226. };
粤ICP备19079148号