Vector3.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764
  1. /**
  2. * @author mrdoob / 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. * @author WestLangley / http://github.com/WestLangley
  8. */
  9. THREE.Vector3 = function ( x, y, z ) {
  10. this.x = x || 0;
  11. this.y = y || 0;
  12. this.z = z || 0;
  13. };
  14. THREE.Vector3.prototype = {
  15. constructor: THREE.Vector3,
  16. set: function ( x, y, z ) {
  17. this.x = x;
  18. this.y = y;
  19. this.z = z;
  20. return this;
  21. },
  22. setScalar: function ( scalar ) {
  23. this.x = scalar;
  24. this.y = scalar;
  25. this.z = scalar;
  26. return this;
  27. },
  28. setX: function ( x ) {
  29. this.x = x;
  30. return this;
  31. },
  32. setY: function ( y ) {
  33. this.y = y;
  34. return this;
  35. },
  36. setZ: function ( z ) {
  37. this.z = z;
  38. return this;
  39. },
  40. setComponent: function ( index, value ) {
  41. switch ( index ) {
  42. case 0: this.x = value; break;
  43. case 1: this.y = value; break;
  44. case 2: this.z = value; break;
  45. default: throw new Error( 'index is out of range: ' + index );
  46. }
  47. },
  48. getComponent: function ( index ) {
  49. switch ( index ) {
  50. case 0: return this.x;
  51. case 1: return this.y;
  52. case 2: return this.z;
  53. default: throw new Error( 'index is out of range: ' + index );
  54. }
  55. },
  56. clone: function () {
  57. return new this.constructor( this.x, this.y, this.z );
  58. },
  59. copy: function ( v ) {
  60. this.x = v.x;
  61. this.y = v.y;
  62. this.z = v.z;
  63. return this;
  64. },
  65. add: function ( v, w ) {
  66. if ( w !== undefined ) {
  67. console.warn( 'THREE.Vector3: .add() now only accepts one argument. Use .addVectors( a, b ) instead.' );
  68. return this.addVectors( v, w );
  69. }
  70. this.x += v.x;
  71. this.y += v.y;
  72. this.z += v.z;
  73. return this;
  74. },
  75. addScalar: function ( s ) {
  76. this.x += s;
  77. this.y += s;
  78. this.z += s;
  79. return this;
  80. },
  81. addVectors: function ( a, b ) {
  82. this.x = a.x + b.x;
  83. this.y = a.y + b.y;
  84. this.z = a.z + b.z;
  85. return this;
  86. },
  87. addScaledVector: function ( v, s ) {
  88. this.x += v.x * s;
  89. this.y += v.y * s;
  90. this.z += v.z * s;
  91. return this;
  92. },
  93. sub: function ( v, w ) {
  94. if ( w !== undefined ) {
  95. console.warn( 'THREE.Vector3: .sub() now only accepts one argument. Use .subVectors( a, b ) instead.' );
  96. return this.subVectors( v, w );
  97. }
  98. this.x -= v.x;
  99. this.y -= v.y;
  100. this.z -= v.z;
  101. return this;
  102. },
  103. subScalar: function ( s ) {
  104. this.x -= s;
  105. this.y -= s;
  106. this.z -= s;
  107. return this;
  108. },
  109. subVectors: function ( a, b ) {
  110. this.x = a.x - b.x;
  111. this.y = a.y - b.y;
  112. this.z = a.z - b.z;
  113. return this;
  114. },
  115. multiply: function ( v, w ) {
  116. if ( w !== undefined ) {
  117. console.warn( 'THREE.Vector3: .multiply() now only accepts one argument. Use .multiplyVectors( a, b ) instead.' );
  118. return this.multiplyVectors( v, w );
  119. }
  120. this.x *= v.x;
  121. this.y *= v.y;
  122. this.z *= v.z;
  123. return this;
  124. },
  125. multiplyScalar: function ( scalar ) {
  126. if ( isFinite( scalar ) ) {
  127. this.x *= scalar;
  128. this.y *= scalar;
  129. this.z *= scalar;
  130. } else {
  131. this.x = 0;
  132. this.y = 0;
  133. this.z = 0;
  134. }
  135. return this;
  136. },
  137. multiplyVectors: function ( a, b ) {
  138. this.x = a.x * b.x;
  139. this.y = a.y * b.y;
  140. this.z = a.z * b.z;
  141. return this;
  142. },
  143. applyEuler: function () {
  144. var quaternion;
  145. return function applyEuler( euler ) {
  146. if ( euler instanceof THREE.Euler === false ) {
  147. console.error( 'THREE.Vector3: .applyEuler() now expects an Euler rotation rather than a Vector3 and order.' );
  148. }
  149. if ( quaternion === undefined ) quaternion = new THREE.Quaternion();
  150. return this.applyQuaternion( quaternion.setFromEuler( euler ) );
  151. };
  152. }(),
  153. applyAxisAngle: function () {
  154. var quaternion;
  155. return function applyAxisAngle( axis, angle ) {
  156. if ( quaternion === undefined ) quaternion = new THREE.Quaternion();
  157. return this.applyQuaternion( quaternion.setFromAxisAngle( axis, angle ) );
  158. };
  159. }(),
  160. applyMatrix3: function ( m ) {
  161. var x = this.x, y = this.y, z = this.z;
  162. var e = m.elements;
  163. this.x = e[ 0 ] * x + e[ 3 ] * y + e[ 6 ] * z;
  164. this.y = e[ 1 ] * x + e[ 4 ] * y + e[ 7 ] * z;
  165. this.z = e[ 2 ] * x + e[ 5 ] * y + e[ 8 ] * z;
  166. return this;
  167. },
  168. applyMatrix4: function ( m ) {
  169. // input: THREE.Matrix4 affine matrix
  170. var x = this.x, y = this.y, z = this.z;
  171. var e = m.elements;
  172. this.x = e[ 0 ] * x + e[ 4 ] * y + e[ 8 ] * z + e[ 12 ];
  173. this.y = e[ 1 ] * x + e[ 5 ] * y + e[ 9 ] * z + e[ 13 ];
  174. this.z = e[ 2 ] * x + e[ 6 ] * y + e[ 10 ] * z + e[ 14 ];
  175. return this;
  176. },
  177. applyProjection: function ( m ) {
  178. // input: THREE.Matrix4 projection matrix
  179. var x = this.x, y = this.y, z = this.z;
  180. var e = m.elements;
  181. var d = 1 / ( e[ 3 ] * x + e[ 7 ] * y + e[ 11 ] * z + e[ 15 ] ); // perspective divide
  182. this.x = ( e[ 0 ] * x + e[ 4 ] * y + e[ 8 ] * z + e[ 12 ] ) * d;
  183. this.y = ( e[ 1 ] * x + e[ 5 ] * y + e[ 9 ] * z + e[ 13 ] ) * d;
  184. this.z = ( e[ 2 ] * x + e[ 6 ] * y + e[ 10 ] * z + e[ 14 ] ) * d;
  185. return this;
  186. },
  187. applyQuaternion: function ( q ) {
  188. var x = this.x, y = this.y, z = this.z;
  189. var qx = q.x, qy = q.y, qz = q.z, qw = q.w;
  190. // calculate quat * vector
  191. var ix = qw * x + qy * z - qz * y;
  192. var iy = qw * y + qz * x - qx * z;
  193. var iz = qw * z + qx * y - qy * x;
  194. var iw = - qx * x - qy * y - qz * z;
  195. // calculate result * inverse quat
  196. this.x = ix * qw + iw * - qx + iy * - qz - iz * - qy;
  197. this.y = iy * qw + iw * - qy + iz * - qx - ix * - qz;
  198. this.z = iz * qw + iw * - qz + ix * - qy - iy * - qx;
  199. return this;
  200. },
  201. project: function () {
  202. var matrix;
  203. return function project( camera ) {
  204. if ( matrix === undefined ) matrix = new THREE.Matrix4();
  205. matrix.multiplyMatrices( camera.projectionMatrix, matrix.getInverse( camera.matrixWorld ) );
  206. return this.applyProjection( matrix );
  207. };
  208. }(),
  209. unproject: function () {
  210. var matrix;
  211. return function unproject( camera ) {
  212. if ( matrix === undefined ) matrix = new THREE.Matrix4();
  213. matrix.multiplyMatrices( camera.matrixWorld, matrix.getInverse( camera.projectionMatrix ) );
  214. return this.applyProjection( matrix );
  215. };
  216. }(),
  217. transformDirection: function ( m ) {
  218. // input: THREE.Matrix4 affine matrix
  219. // vector interpreted as a direction
  220. var x = this.x, y = this.y, z = this.z;
  221. var e = m.elements;
  222. this.x = e[ 0 ] * x + e[ 4 ] * y + e[ 8 ] * z;
  223. this.y = e[ 1 ] * x + e[ 5 ] * y + e[ 9 ] * z;
  224. this.z = e[ 2 ] * x + e[ 6 ] * y + e[ 10 ] * z;
  225. return this.normalize();
  226. },
  227. divide: function ( v ) {
  228. this.x /= v.x;
  229. this.y /= v.y;
  230. this.z /= v.z;
  231. return this;
  232. },
  233. divideScalar: function ( scalar ) {
  234. return this.multiplyScalar( 1 / scalar );
  235. },
  236. min: function ( v ) {
  237. this.x = Math.min( this.x, v.x );
  238. this.y = Math.min( this.y, v.y );
  239. this.z = Math.min( this.z, v.z );
  240. return this;
  241. },
  242. max: function ( v ) {
  243. this.x = Math.max( this.x, v.x );
  244. this.y = Math.max( this.y, v.y );
  245. this.z = Math.max( this.z, v.z );
  246. return this;
  247. },
  248. clamp: function ( min, max ) {
  249. // This function assumes min < max, if this assumption isn't true it will not operate correctly
  250. this.x = Math.max( min.x, Math.min( max.x, this.x ) );
  251. this.y = Math.max( min.y, Math.min( max.y, this.y ) );
  252. this.z = Math.max( min.z, Math.min( max.z, this.z ) );
  253. return this;
  254. },
  255. clampScalar: function () {
  256. var min, max;
  257. return function clampScalar( minVal, maxVal ) {
  258. if ( min === undefined ) {
  259. min = new THREE.Vector3();
  260. max = new THREE.Vector3();
  261. }
  262. min.set( minVal, minVal, minVal );
  263. max.set( maxVal, maxVal, maxVal );
  264. return this.clamp( min, max );
  265. };
  266. }(),
  267. clampLength: function ( min, max ) {
  268. var length = this.length();
  269. return this.multiplyScalar( Math.max( min, Math.min( max, length ) ) / length );
  270. },
  271. floor: function () {
  272. this.x = Math.floor( this.x );
  273. this.y = Math.floor( this.y );
  274. this.z = Math.floor( this.z );
  275. return this;
  276. },
  277. ceil: function () {
  278. this.x = Math.ceil( this.x );
  279. this.y = Math.ceil( this.y );
  280. this.z = Math.ceil( this.z );
  281. return this;
  282. },
  283. round: function () {
  284. this.x = Math.round( this.x );
  285. this.y = Math.round( this.y );
  286. this.z = Math.round( this.z );
  287. return this;
  288. },
  289. roundToZero: function () {
  290. this.x = ( this.x < 0 ) ? Math.ceil( this.x ) : Math.floor( this.x );
  291. this.y = ( this.y < 0 ) ? Math.ceil( this.y ) : Math.floor( this.y );
  292. this.z = ( this.z < 0 ) ? Math.ceil( this.z ) : Math.floor( this.z );
  293. return this;
  294. },
  295. negate: function () {
  296. this.x = - this.x;
  297. this.y = - this.y;
  298. this.z = - this.z;
  299. return this;
  300. },
  301. dot: function ( v ) {
  302. return this.x * v.x + this.y * v.y + this.z * v.z;
  303. },
  304. lengthSq: function () {
  305. return this.x * this.x + this.y * this.y + this.z * this.z;
  306. },
  307. length: function () {
  308. return Math.sqrt( this.x * this.x + this.y * this.y + this.z * this.z );
  309. },
  310. lengthManhattan: function () {
  311. return Math.abs( this.x ) + Math.abs( this.y ) + Math.abs( this.z );
  312. },
  313. normalize: function () {
  314. return this.divideScalar( this.length() );
  315. },
  316. setLength: function ( length ) {
  317. return this.multiplyScalar( length / this.length() );
  318. },
  319. lerp: function ( v, alpha ) {
  320. this.x += ( v.x - this.x ) * alpha;
  321. this.y += ( v.y - this.y ) * alpha;
  322. this.z += ( v.z - this.z ) * alpha;
  323. return this;
  324. },
  325. lerpVectors: function ( v1, v2, alpha ) {
  326. return this.subVectors( v2, v1 ).multiplyScalar( alpha ).add( v1 );
  327. },
  328. cross: function ( v, w ) {
  329. if ( w !== undefined ) {
  330. console.warn( 'THREE.Vector3: .cross() now only accepts one argument. Use .crossVectors( a, b ) instead.' );
  331. return this.crossVectors( v, w );
  332. }
  333. var x = this.x, y = this.y, z = this.z;
  334. this.x = y * v.z - z * v.y;
  335. this.y = z * v.x - x * v.z;
  336. this.z = x * v.y - y * v.x;
  337. return this;
  338. },
  339. crossVectors: function ( a, b ) {
  340. var ax = a.x, ay = a.y, az = a.z;
  341. var bx = b.x, by = b.y, bz = b.z;
  342. this.x = ay * bz - az * by;
  343. this.y = az * bx - ax * bz;
  344. this.z = ax * by - ay * bx;
  345. return this;
  346. },
  347. projectOnVector: function () {
  348. var v1, dot;
  349. return function projectOnVector( vector ) {
  350. if ( v1 === undefined ) v1 = new THREE.Vector3();
  351. v1.copy( vector ).normalize();
  352. dot = this.dot( v1 );
  353. return this.copy( v1 ).multiplyScalar( dot );
  354. };
  355. }(),
  356. projectOnPlane: function () {
  357. var v1;
  358. return function projectOnPlane( planeNormal ) {
  359. if ( v1 === undefined ) v1 = new THREE.Vector3();
  360. v1.copy( this ).projectOnVector( planeNormal );
  361. return this.sub( v1 );
  362. };
  363. }(),
  364. reflect: function () {
  365. // reflect incident vector off plane orthogonal to normal
  366. // normal is assumed to have unit length
  367. var v1;
  368. return function reflect( normal ) {
  369. if ( v1 === undefined ) v1 = new THREE.Vector3();
  370. return this.sub( v1.copy( normal ).multiplyScalar( 2 * this.dot( normal ) ) );
  371. };
  372. }(),
  373. angleTo: function ( v ) {
  374. var theta = this.dot( v ) / ( Math.sqrt( this.lengthSq() * v.lengthSq() ) );
  375. // clamp, to handle numerical problems
  376. return Math.acos( THREE.Math.clamp( theta, - 1, 1 ) );
  377. },
  378. distanceTo: function ( v ) {
  379. return Math.sqrt( this.distanceToSquared( v ) );
  380. },
  381. distanceToSquared: function ( v ) {
  382. var dx = this.x - v.x, dy = this.y - v.y, dz = this.z - v.z;
  383. return dx * dx + dy * dy + dz * dz;
  384. },
  385. setFromSpherical: function( s ) {
  386. var sinPhiRadius = Math.sin( s.phi ) * s.radius;
  387. this.x = sinPhiRadius * Math.sin( s.theta );
  388. this.y = Math.cos( s.phi ) * s.radius;
  389. this.z = sinPhiRadius * Math.cos( s.theta );
  390. return this;
  391. },
  392. setFromMatrixPosition: function ( m ) {
  393. return this.setFromMatrixColumn( m, 3 );
  394. },
  395. setFromMatrixScale: function ( m ) {
  396. var sx = this.setFromMatrixColumn( m, 0 ).length();
  397. var sy = this.setFromMatrixColumn( m, 1 ).length();
  398. var sz = this.setFromMatrixColumn( m, 2 ).length();
  399. this.x = sx;
  400. this.y = sy;
  401. this.z = sz;
  402. return this;
  403. },
  404. setFromMatrixColumn: function ( m, index ) {
  405. if ( typeof m === 'number' ) {
  406. console.warn( 'THREE.Vector3: setFromMatrixColumn now expects ( matrix, index ).' );
  407. m = arguments[ 1 ];
  408. index = arguments[ 0 ];
  409. }
  410. return this.fromArray( m.elements, index * 4 );
  411. },
  412. equals: function ( v ) {
  413. return ( ( v.x === this.x ) && ( v.y === this.y ) && ( v.z === this.z ) );
  414. },
  415. fromArray: function ( array, offset ) {
  416. if ( offset === undefined ) offset = 0;
  417. this.x = array[ offset ];
  418. this.y = array[ offset + 1 ];
  419. this.z = array[ offset + 2 ];
  420. return this;
  421. },
  422. toArray: function ( array, offset ) {
  423. if ( array === undefined ) array = [];
  424. if ( offset === undefined ) offset = 0;
  425. array[ offset ] = this.x;
  426. array[ offset + 1 ] = this.y;
  427. array[ offset + 2 ] = this.z;
  428. return array;
  429. },
  430. fromAttribute: function ( attribute, index, offset ) {
  431. if ( offset === undefined ) offset = 0;
  432. index = index * attribute.itemSize + offset;
  433. this.x = attribute.array[ index ];
  434. this.y = attribute.array[ index + 1 ];
  435. this.z = attribute.array[ index + 2 ];
  436. return this;
  437. }
  438. };