Vector3.js 11 KB

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