Vector3.js 12 KB

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