Vector3.js 12 KB

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