Vector3.js 13 KB

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