Vector3.js 12 KB

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