Vector3.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775
  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. this.applyQuaternion( quaternion.setFromEuler( euler ) );
  151. return this;
  152. };
  153. }(),
  154. applyAxisAngle: function () {
  155. var quaternion;
  156. return function applyAxisAngle( axis, angle ) {
  157. if ( quaternion === undefined ) quaternion = new THREE.Quaternion();
  158. this.applyQuaternion( quaternion.setFromAxisAngle( axis, angle ) );
  159. return this;
  160. };
  161. }(),
  162. applyMatrix3: function ( m ) {
  163. var x = this.x;
  164. var y = this.y;
  165. var z = this.z;
  166. var e = m.elements;
  167. this.x = e[ 0 ] * x + e[ 3 ] * y + e[ 6 ] * z;
  168. this.y = e[ 1 ] * x + e[ 4 ] * y + e[ 7 ] * z;
  169. this.z = e[ 2 ] * x + e[ 5 ] * y + e[ 8 ] * z;
  170. return this;
  171. },
  172. applyMatrix4: function ( m ) {
  173. // input: THREE.Matrix4 affine matrix
  174. var x = this.x, y = this.y, z = this.z;
  175. var e = m.elements;
  176. this.x = e[ 0 ] * x + e[ 4 ] * y + e[ 8 ] * z + e[ 12 ];
  177. this.y = e[ 1 ] * x + e[ 5 ] * y + e[ 9 ] * z + e[ 13 ];
  178. this.z = e[ 2 ] * x + e[ 6 ] * y + e[ 10 ] * z + e[ 14 ];
  179. return this;
  180. },
  181. applyProjection: function ( m ) {
  182. // input: THREE.Matrix4 projection matrix
  183. var x = this.x, y = this.y, z = this.z;
  184. var e = m.elements;
  185. var d = 1 / ( e[ 3 ] * x + e[ 7 ] * y + e[ 11 ] * z + e[ 15 ] ); // perspective divide
  186. this.x = ( e[ 0 ] * x + e[ 4 ] * y + e[ 8 ] * z + e[ 12 ] ) * d;
  187. this.y = ( e[ 1 ] * x + e[ 5 ] * y + e[ 9 ] * z + e[ 13 ] ) * d;
  188. this.z = ( e[ 2 ] * x + e[ 6 ] * y + e[ 10 ] * z + e[ 14 ] ) * d;
  189. return this;
  190. },
  191. applyQuaternion: function ( q ) {
  192. var x = this.x;
  193. var y = this.y;
  194. var z = this.z;
  195. var qx = q.x;
  196. var qy = q.y;
  197. var qz = q.z;
  198. var qw = q.w;
  199. // calculate quat * vector
  200. var ix = qw * x + qy * z - qz * y;
  201. var iy = qw * y + qz * x - qx * z;
  202. var iz = qw * z + qx * y - qy * x;
  203. var iw = - qx * x - qy * y - qz * z;
  204. // calculate result * inverse quat
  205. this.x = ix * qw + iw * - qx + iy * - qz - iz * - qy;
  206. this.y = iy * qw + iw * - qy + iz * - qx - ix * - qz;
  207. this.z = iz * qw + iw * - qz + ix * - qy - iy * - qx;
  208. return this;
  209. },
  210. project: function () {
  211. var matrix;
  212. return function project( camera ) {
  213. if ( matrix === undefined ) matrix = new THREE.Matrix4();
  214. matrix.multiplyMatrices( camera.projectionMatrix, matrix.getInverse( camera.matrixWorld ) );
  215. return this.applyProjection( matrix );
  216. };
  217. }(),
  218. unproject: function () {
  219. var matrix;
  220. return function unproject( camera ) {
  221. if ( matrix === undefined ) matrix = new THREE.Matrix4();
  222. matrix.multiplyMatrices( camera.matrixWorld, matrix.getInverse( camera.projectionMatrix ) );
  223. return this.applyProjection( matrix );
  224. };
  225. }(),
  226. transformDirection: function ( m ) {
  227. // input: THREE.Matrix4 affine matrix
  228. // vector interpreted as a direction
  229. var x = this.x, y = this.y, z = this.z;
  230. var e = m.elements;
  231. this.x = e[ 0 ] * x + e[ 4 ] * y + e[ 8 ] * z;
  232. this.y = e[ 1 ] * x + e[ 5 ] * y + e[ 9 ] * z;
  233. this.z = e[ 2 ] * x + e[ 6 ] * y + e[ 10 ] * z;
  234. this.normalize();
  235. return this;
  236. },
  237. divide: function ( v ) {
  238. this.x /= v.x;
  239. this.y /= v.y;
  240. this.z /= v.z;
  241. return this;
  242. },
  243. divideScalar: function ( scalar ) {
  244. return this.multiplyScalar( 1 / scalar );
  245. },
  246. min: function ( v ) {
  247. this.x = Math.min( this.x, v.x );
  248. this.y = Math.min( this.y, v.y );
  249. this.z = Math.min( this.z, v.z );
  250. return this;
  251. },
  252. max: function ( v ) {
  253. this.x = Math.max( this.x, v.x );
  254. this.y = Math.max( this.y, v.y );
  255. this.z = Math.max( this.z, v.z );
  256. return this;
  257. },
  258. clamp: function ( min, max ) {
  259. // This function assumes min < max, if this assumption isn't true it will not operate correctly
  260. this.x = Math.max( min.x, Math.min( max.x, this.x ) );
  261. this.y = Math.max( min.y, Math.min( max.y, this.y ) );
  262. this.z = Math.max( min.z, Math.min( max.z, this.z ) );
  263. return this;
  264. },
  265. clampScalar: function () {
  266. var min, max;
  267. return function clampScalar( minVal, maxVal ) {
  268. if ( min === undefined ) {
  269. min = new THREE.Vector3();
  270. max = new THREE.Vector3();
  271. }
  272. min.set( minVal, minVal, minVal );
  273. max.set( maxVal, maxVal, maxVal );
  274. return this.clamp( min, max );
  275. };
  276. }(),
  277. clampLength: function ( min, max ) {
  278. var length = this.length();
  279. this.multiplyScalar( Math.max( min, Math.min( max, length ) ) / length );
  280. return this;
  281. },
  282. floor: function () {
  283. this.x = Math.floor( this.x );
  284. this.y = Math.floor( this.y );
  285. this.z = Math.floor( this.z );
  286. return this;
  287. },
  288. ceil: function () {
  289. this.x = Math.ceil( this.x );
  290. this.y = Math.ceil( this.y );
  291. this.z = Math.ceil( this.z );
  292. return this;
  293. },
  294. round: function () {
  295. this.x = Math.round( this.x );
  296. this.y = Math.round( this.y );
  297. this.z = Math.round( this.z );
  298. return this;
  299. },
  300. roundToZero: function () {
  301. this.x = ( this.x < 0 ) ? Math.ceil( this.x ) : Math.floor( this.x );
  302. this.y = ( this.y < 0 ) ? Math.ceil( this.y ) : Math.floor( this.y );
  303. this.z = ( this.z < 0 ) ? Math.ceil( this.z ) : Math.floor( this.z );
  304. return this;
  305. },
  306. negate: function () {
  307. this.x = - this.x;
  308. this.y = - this.y;
  309. this.z = - this.z;
  310. return this;
  311. },
  312. dot: function ( v ) {
  313. return this.x * v.x + this.y * v.y + this.z * v.z;
  314. },
  315. lengthSq: function () {
  316. return this.x * this.x + this.y * this.y + this.z * this.z;
  317. },
  318. length: function () {
  319. return Math.sqrt( this.x * this.x + this.y * this.y + this.z * this.z );
  320. },
  321. lengthManhattan: function () {
  322. return Math.abs( this.x ) + Math.abs( this.y ) + Math.abs( this.z );
  323. },
  324. normalize: function () {
  325. return this.divideScalar( this.length() );
  326. },
  327. setLength: function ( length ) {
  328. return this.multiplyScalar( length / this.length() );
  329. },
  330. lerp: function ( v, alpha ) {
  331. this.x += ( v.x - this.x ) * alpha;
  332. this.y += ( v.y - this.y ) * alpha;
  333. this.z += ( v.z - this.z ) * alpha;
  334. return this;
  335. },
  336. lerpVectors: function ( v1, v2, alpha ) {
  337. this.subVectors( v2, v1 ).multiplyScalar( alpha ).add( v1 );
  338. return this;
  339. },
  340. cross: function ( v, w ) {
  341. if ( w !== undefined ) {
  342. console.warn( 'THREE.Vector3: .cross() now only accepts one argument. Use .crossVectors( a, b ) instead.' );
  343. return this.crossVectors( v, w );
  344. }
  345. var x = this.x, y = this.y, z = this.z;
  346. this.x = y * v.z - z * v.y;
  347. this.y = z * v.x - x * v.z;
  348. this.z = x * v.y - y * v.x;
  349. return this;
  350. },
  351. crossVectors: function ( a, b ) {
  352. var ax = a.x, ay = a.y, az = a.z;
  353. var bx = b.x, by = b.y, bz = b.z;
  354. this.x = ay * bz - az * by;
  355. this.y = az * bx - ax * bz;
  356. this.z = ax * by - ay * bx;
  357. return this;
  358. },
  359. projectOnVector: function () {
  360. var v1, dot;
  361. return function projectOnVector( vector ) {
  362. if ( v1 === undefined ) v1 = new THREE.Vector3();
  363. v1.copy( vector ).normalize();
  364. dot = this.dot( v1 );
  365. return this.copy( v1 ).multiplyScalar( dot );
  366. };
  367. }(),
  368. projectOnPlane: function () {
  369. var v1;
  370. return function projectOnPlane( planeNormal ) {
  371. if ( v1 === undefined ) v1 = new THREE.Vector3();
  372. v1.copy( this ).projectOnVector( planeNormal );
  373. return this.sub( v1 );
  374. };
  375. }(),
  376. reflect: function () {
  377. // reflect incident vector off plane orthogonal to normal
  378. // normal is assumed to have unit length
  379. var v1;
  380. return function reflect( normal ) {
  381. if ( v1 === undefined ) v1 = new THREE.Vector3();
  382. return this.sub( v1.copy( normal ).multiplyScalar( 2 * this.dot( normal ) ) );
  383. };
  384. }(),
  385. angleTo: function ( v ) {
  386. var theta = this.dot( v ) / ( Math.sqrt( this.lengthSq() * v.lengthSq() ) );
  387. // clamp, to handle numerical problems
  388. return Math.acos( THREE.Math.clamp( theta, - 1, 1 ) );
  389. },
  390. distanceTo: function ( v ) {
  391. return Math.sqrt( this.distanceToSquared( v ) );
  392. },
  393. distanceToSquared: function ( v ) {
  394. var dx = this.x - v.x;
  395. var dy = this.y - v.y;
  396. var dz = this.z - v.z;
  397. return dx * dx + dy * dy + dz * dz;
  398. },
  399. setFromSpherical: function( s ) {
  400. var sinPhiRadius = Math.sin( s.phi ) * s.radius;
  401. this.x = sinPhiRadius * Math.sin( s.theta );
  402. this.y = Math.cos( s.phi ) * s.radius;
  403. this.z = sinPhiRadius * Math.cos( s.theta );
  404. return this;
  405. },
  406. setFromMatrixPosition: function ( m ) {
  407. return this.setFromMatrixColumn( m, 3 );
  408. },
  409. setFromMatrixScale: function ( m ) {
  410. this.x = this.setFromMatrixColumn( m, 0 ).length();
  411. this.y = this.setFromMatrixColumn( m, 1 ).length();
  412. this.z = this.setFromMatrixColumn( m, 2 ).length();
  413. return this;
  414. },
  415. setFromMatrixColumn: function ( m, index ) {
  416. return this.fromArray( m.elements, index * 4 );
  417. },
  418. equals: function ( v ) {
  419. return ( ( v.x === this.x ) && ( v.y === this.y ) && ( v.z === this.z ) );
  420. },
  421. fromArray: function ( array, offset ) {
  422. if ( offset === undefined ) offset = 0;
  423. this.x = array[ offset ];
  424. this.y = array[ offset + 1 ];
  425. this.z = array[ offset + 2 ];
  426. return this;
  427. },
  428. toArray: function ( array, offset ) {
  429. if ( array === undefined ) array = [];
  430. if ( offset === undefined ) offset = 0;
  431. array[ offset ] = this.x;
  432. array[ offset + 1 ] = this.y;
  433. array[ offset + 2 ] = this.z;
  434. return array;
  435. },
  436. fromAttribute: function ( attribute, index, offset ) {
  437. if ( offset === undefined ) offset = 0;
  438. index = index * attribute.itemSize + offset;
  439. this.x = attribute.array[ index ];
  440. this.y = attribute.array[ index + 1 ];
  441. this.z = attribute.array[ index + 2 ];
  442. return this;
  443. }
  444. };