Vector3.js 12 KB

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