2
0

Vector3.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  1. import { MathUtils } from './MathUtils.js';
  2. import { Quaternion } from './Quaternion.js';
  3. /**
  4. * @author mrdoob / http://mrdoob.com/
  5. * @author kile / http://kile.stravaganza.org/
  6. * @author philogb / http://blog.thejit.org/
  7. * @author mikael emtinger / http://gomo.se/
  8. * @author egraether / http://egraether.com/
  9. * @author WestLangley / http://github.com/WestLangley
  10. */
  11. const _vector = new Vector3();
  12. const _quaternion = new Quaternion();
  13. function Vector3( x = 0, y = 0, z = 0 ) {
  14. this.x = x;
  15. this.y = y;
  16. this.z = z;
  17. }
  18. Object.assign( Vector3.prototype, {
  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. this.x *= scalar;
  132. this.y *= scalar;
  133. this.z *= scalar;
  134. return this;
  135. },
  136. multiplyVectors: function ( a, b ) {
  137. this.x = a.x * b.x;
  138. this.y = a.y * b.y;
  139. this.z = a.z * b.z;
  140. return this;
  141. },
  142. applyEuler: function ( euler ) {
  143. if ( ! ( euler && euler.isEuler ) ) {
  144. console.error( 'THREE.Vector3: .applyEuler() now expects an Euler rotation rather than a Vector3 and order.' );
  145. }
  146. return this.applyQuaternion( _quaternion.setFromEuler( euler ) );
  147. },
  148. applyAxisAngle: function ( axis, angle ) {
  149. return this.applyQuaternion( _quaternion.setFromAxisAngle( axis, angle ) );
  150. },
  151. applyMatrix3: function ( m ) {
  152. const x = this.x, y = this.y, z = this.z;
  153. const e = m.elements;
  154. this.x = e[ 0 ] * x + e[ 3 ] * y + e[ 6 ] * z;
  155. this.y = e[ 1 ] * x + e[ 4 ] * y + e[ 7 ] * z;
  156. this.z = e[ 2 ] * x + e[ 5 ] * y + e[ 8 ] * z;
  157. return this;
  158. },
  159. applyNormalMatrix: function ( m ) {
  160. return this.applyMatrix3( m ).normalize();
  161. },
  162. applyMatrix4: function ( m ) {
  163. const x = this.x, y = this.y, z = this.z;
  164. const e = m.elements;
  165. const w = 1 / ( e[ 3 ] * x + e[ 7 ] * y + e[ 11 ] * z + e[ 15 ] );
  166. this.x = ( e[ 0 ] * x + e[ 4 ] * y + e[ 8 ] * z + e[ 12 ] ) * w;
  167. this.y = ( e[ 1 ] * x + e[ 5 ] * y + e[ 9 ] * z + e[ 13 ] ) * w;
  168. this.z = ( e[ 2 ] * x + e[ 6 ] * y + e[ 10 ] * z + e[ 14 ] ) * w;
  169. return this;
  170. },
  171. applyQuaternion: function ( q ) {
  172. const x = this.x, y = this.y, z = this.z;
  173. const qx = q.x, qy = q.y, qz = q.z, qw = q.w;
  174. // calculate quat * vector
  175. const ix = qw * x + qy * z - qz * y;
  176. const iy = qw * y + qz * x - qx * z;
  177. const iz = qw * z + qx * y - qy * x;
  178. const iw = - qx * x - qy * y - qz * z;
  179. // calculate result * inverse quat
  180. this.x = ix * qw + iw * - qx + iy * - qz - iz * - qy;
  181. this.y = iy * qw + iw * - qy + iz * - qx - ix * - qz;
  182. this.z = iz * qw + iw * - qz + ix * - qy - iy * - qx;
  183. return this;
  184. },
  185. project: function ( camera ) {
  186. return this.applyMatrix4( camera.matrixWorldInverse ).applyMatrix4( camera.projectionMatrix );
  187. },
  188. unproject: function ( camera ) {
  189. return this.applyMatrix4( camera.projectionMatrixInverse ).applyMatrix4( camera.matrixWorld );
  190. },
  191. transformDirection: function ( m ) {
  192. // input: THREE.Matrix4 affine matrix
  193. // vector interpreted as a direction
  194. const x = this.x, y = this.y, z = this.z;
  195. const e = m.elements;
  196. this.x = e[ 0 ] * x + e[ 4 ] * y + e[ 8 ] * z;
  197. this.y = e[ 1 ] * x + e[ 5 ] * y + e[ 9 ] * z;
  198. this.z = e[ 2 ] * x + e[ 6 ] * y + e[ 10 ] * z;
  199. return this.normalize();
  200. },
  201. divide: function ( v ) {
  202. this.x /= v.x;
  203. this.y /= v.y;
  204. this.z /= v.z;
  205. return this;
  206. },
  207. divideScalar: function ( scalar ) {
  208. return this.multiplyScalar( 1 / scalar );
  209. },
  210. min: function ( v ) {
  211. this.x = Math.min( this.x, v.x );
  212. this.y = Math.min( this.y, v.y );
  213. this.z = Math.min( this.z, v.z );
  214. return this;
  215. },
  216. max: function ( v ) {
  217. this.x = Math.max( this.x, v.x );
  218. this.y = Math.max( this.y, v.y );
  219. this.z = Math.max( this.z, v.z );
  220. return this;
  221. },
  222. clamp: function ( min, max ) {
  223. // assumes min < max, componentwise
  224. this.x = Math.max( min.x, Math.min( max.x, this.x ) );
  225. this.y = Math.max( min.y, Math.min( max.y, this.y ) );
  226. this.z = Math.max( min.z, Math.min( max.z, this.z ) );
  227. return this;
  228. },
  229. clampScalar: function ( minVal, maxVal ) {
  230. this.x = Math.max( minVal, Math.min( maxVal, this.x ) );
  231. this.y = Math.max( minVal, Math.min( maxVal, this.y ) );
  232. this.z = Math.max( minVal, Math.min( maxVal, this.z ) );
  233. return this;
  234. },
  235. clampLength: function ( min, max ) {
  236. const length = this.length();
  237. return this.divideScalar( length || 1 ).multiplyScalar( Math.max( min, Math.min( max, length ) ) );
  238. },
  239. floor: function () {
  240. this.x = Math.floor( this.x );
  241. this.y = Math.floor( this.y );
  242. this.z = Math.floor( this.z );
  243. return this;
  244. },
  245. ceil: function () {
  246. this.x = Math.ceil( this.x );
  247. this.y = Math.ceil( this.y );
  248. this.z = Math.ceil( this.z );
  249. return this;
  250. },
  251. round: function () {
  252. this.x = Math.round( this.x );
  253. this.y = Math.round( this.y );
  254. this.z = Math.round( this.z );
  255. return this;
  256. },
  257. roundToZero: function () {
  258. this.x = ( this.x < 0 ) ? Math.ceil( this.x ) : Math.floor( this.x );
  259. this.y = ( this.y < 0 ) ? Math.ceil( this.y ) : Math.floor( this.y );
  260. this.z = ( this.z < 0 ) ? Math.ceil( this.z ) : Math.floor( this.z );
  261. return this;
  262. },
  263. negate: function () {
  264. this.x = - this.x;
  265. this.y = - this.y;
  266. this.z = - this.z;
  267. return this;
  268. },
  269. dot: function ( v ) {
  270. return this.x * v.x + this.y * v.y + this.z * v.z;
  271. },
  272. // TODO lengthSquared?
  273. lengthSq: function () {
  274. return this.x * this.x + this.y * this.y + this.z * this.z;
  275. },
  276. length: function () {
  277. return Math.sqrt( this.x * this.x + this.y * this.y + this.z * this.z );
  278. },
  279. manhattanLength: function () {
  280. return Math.abs( this.x ) + Math.abs( this.y ) + Math.abs( this.z );
  281. },
  282. normalize: function () {
  283. return this.divideScalar( this.length() || 1 );
  284. },
  285. setLength: function ( length ) {
  286. return this.normalize().multiplyScalar( length );
  287. },
  288. lerp: function ( v, alpha ) {
  289. this.x += ( v.x - this.x ) * alpha;
  290. this.y += ( v.y - this.y ) * alpha;
  291. this.z += ( v.z - this.z ) * alpha;
  292. return this;
  293. },
  294. lerpVectors: function ( v1, v2, alpha ) {
  295. this.x = v1.x + ( v2.x - v1.x ) * alpha;
  296. this.y = v1.y + ( v2.y - v1.y ) * alpha;
  297. this.z = v1.z + ( v2.z - v1.z ) * alpha;
  298. return this;
  299. },
  300. cross: function ( v, w ) {
  301. if ( w !== undefined ) {
  302. console.warn( 'THREE.Vector3: .cross() now only accepts one argument. Use .crossVectors( a, b ) instead.' );
  303. return this.crossVectors( v, w );
  304. }
  305. return this.crossVectors( this, v );
  306. },
  307. crossVectors: function ( a, b ) {
  308. const ax = a.x, ay = a.y, az = a.z;
  309. const bx = b.x, by = b.y, bz = b.z;
  310. this.x = ay * bz - az * by;
  311. this.y = az * bx - ax * bz;
  312. this.z = ax * by - ay * bx;
  313. return this;
  314. },
  315. projectOnVector: function ( v ) {
  316. const denominator = v.lengthSq();
  317. if ( denominator === 0 ) return this.set( 0, 0, 0 );
  318. const scalar = v.dot( this ) / denominator;
  319. return this.copy( v ).multiplyScalar( scalar );
  320. },
  321. projectOnPlane: function ( planeNormal ) {
  322. _vector.copy( this ).projectOnVector( planeNormal );
  323. return this.sub( _vector );
  324. },
  325. reflect: function ( normal ) {
  326. // reflect incident vector off plane orthogonal to normal
  327. // normal is assumed to have unit length
  328. return this.sub( _vector.copy( normal ).multiplyScalar( 2 * this.dot( normal ) ) );
  329. },
  330. angleTo: function ( v ) {
  331. const denominator = Math.sqrt( this.lengthSq() * v.lengthSq() );
  332. if ( denominator === 0 ) return Math.PI / 2;
  333. const theta = this.dot( v ) / denominator;
  334. // clamp, to handle numerical problems
  335. return Math.acos( MathUtils.clamp( theta, - 1, 1 ) );
  336. },
  337. distanceTo: function ( v ) {
  338. return Math.sqrt( this.distanceToSquared( v ) );
  339. },
  340. distanceToSquared: function ( v ) {
  341. const dx = this.x - v.x, dy = this.y - v.y, dz = this.z - v.z;
  342. return dx * dx + dy * dy + dz * dz;
  343. },
  344. manhattanDistanceTo: function ( v ) {
  345. return Math.abs( this.x - v.x ) + Math.abs( this.y - v.y ) + Math.abs( this.z - v.z );
  346. },
  347. setFromSpherical: function ( s ) {
  348. return this.setFromSphericalCoords( s.radius, s.phi, s.theta );
  349. },
  350. setFromSphericalCoords: function ( radius, phi, theta ) {
  351. const sinPhiRadius = Math.sin( phi ) * radius;
  352. this.x = sinPhiRadius * Math.sin( theta );
  353. this.y = Math.cos( phi ) * radius;
  354. this.z = sinPhiRadius * Math.cos( theta );
  355. return this;
  356. },
  357. setFromCylindrical: function ( c ) {
  358. return this.setFromCylindricalCoords( c.radius, c.theta, c.y );
  359. },
  360. setFromCylindricalCoords: function ( radius, theta, y ) {
  361. this.x = radius * Math.sin( theta );
  362. this.y = y;
  363. this.z = radius * Math.cos( theta );
  364. return this;
  365. },
  366. setFromMatrixPosition: function ( m ) {
  367. const e = m.elements;
  368. this.x = e[ 12 ];
  369. this.y = e[ 13 ];
  370. this.z = e[ 14 ];
  371. return this;
  372. },
  373. setFromMatrixScale: function ( m ) {
  374. const sx = this.setFromMatrixColumn( m, 0 ).length();
  375. const sy = this.setFromMatrixColumn( m, 1 ).length();
  376. const sz = this.setFromMatrixColumn( m, 2 ).length();
  377. this.x = sx;
  378. this.y = sy;
  379. this.z = sz;
  380. return this;
  381. },
  382. setFromMatrixColumn: function ( m, index ) {
  383. return this.fromArray( m.elements, index * 4 );
  384. },
  385. setFromMatrix3Column: function ( m, index ) {
  386. return this.fromArray( m.elements, index * 3 );
  387. },
  388. equals: function ( v ) {
  389. return ( ( v.x === this.x ) && ( v.y === this.y ) && ( v.z === this.z ) );
  390. },
  391. fromArray: function ( array, offset ) {
  392. if ( offset === undefined ) offset = 0;
  393. this.x = array[ offset ];
  394. this.y = array[ offset + 1 ];
  395. this.z = array[ offset + 2 ];
  396. return this;
  397. },
  398. toArray: function ( array, offset ) {
  399. if ( array === undefined ) array = [];
  400. if ( offset === undefined ) offset = 0;
  401. array[ offset ] = this.x;
  402. array[ offset + 1 ] = this.y;
  403. array[ offset + 2 ] = this.z;
  404. return array;
  405. },
  406. fromBufferAttribute: function ( attribute, index, offset ) {
  407. if ( offset !== undefined ) {
  408. console.warn( 'THREE.Vector3: offset has been removed from .fromBufferAttribute().' );
  409. }
  410. this.x = attribute.getX( index );
  411. this.y = attribute.getY( index );
  412. this.z = attribute.getZ( index );
  413. return this;
  414. },
  415. random: function () {
  416. this.x = Math.random();
  417. this.y = Math.random();
  418. this.z = Math.random();
  419. return this;
  420. }
  421. } );
  422. export { Vector3 };