Vector3.js 12 KB

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