Vector3.js 13 KB

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