Vector3.js 13 KB

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