Vector3.js 12 KB

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