Vector3.js 14 KB

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