Vector3.js 13 KB

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