Vector3.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676
  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. copy: function ( v ) {
  51. this.x = v.x;
  52. this.y = v.y;
  53. this.z = v.z;
  54. return this;
  55. },
  56. add: function ( v ) {
  57. if ( arguments.length > 1 ) debugger;
  58. this.x += v.x;
  59. this.y += v.y;
  60. this.z += v.z;
  61. return this;
  62. },
  63. addScalar: function ( s ) {
  64. this.x += s;
  65. this.y += s;
  66. this.z += s;
  67. return this;
  68. },
  69. addVectors: function ( a, b ) {
  70. this.x = a.x + b.x;
  71. this.y = a.y + b.y;
  72. this.z = a.z + b.z;
  73. return this;
  74. },
  75. sub: function ( v ) {
  76. if ( arguments.length > 1 ) debugger;
  77. this.x -= v.x;
  78. this.y -= v.y;
  79. this.z -= v.z;
  80. return this;
  81. },
  82. subVectors: function ( a, b ) {
  83. this.x = a.x - b.x;
  84. this.y = a.y - b.y;
  85. this.z = a.z - b.z;
  86. return this;
  87. },
  88. multiply: function ( v ) {
  89. if ( arguments.length > 1 ) debugger;
  90. this.x *= v.x;
  91. this.y *= v.y;
  92. this.z *= v.z;
  93. return this;
  94. },
  95. multiplyScalar: function ( s ) {
  96. this.x *= s;
  97. this.y *= s;
  98. this.z *= s;
  99. return this;
  100. },
  101. multiplyVectors: function ( a, b ) {
  102. this.x = a.x * b.x;
  103. this.y = a.y * b.y;
  104. this.z = a.z * b.z;
  105. return this;
  106. },
  107. applyMatrix3: function ( m ) {
  108. var x = this.x;
  109. var y = this.y;
  110. var z = this.z;
  111. var e = m.elements;
  112. this.x = e[0] * x + e[3] * y + e[6] * z;
  113. this.y = e[1] * x + e[4] * y + e[7] * z;
  114. this.z = e[2] * x + e[5] * y + e[8] * z;
  115. return this;
  116. },
  117. applyMatrix4: function ( m ) {
  118. var x = this.x;
  119. var y = this.y;
  120. var z = this.z;
  121. var e = m.elements;
  122. var d = 1 / ( e[3] * x + e[7] * y + e[11] * z + e[15] );
  123. this.x = ( e[0] * x + e[4] * y + e[8] * z + e[12] ) * d;
  124. this.y = ( e[1] * x + e[5] * y + e[9] * z + e[13] ) * d;
  125. this.z = ( e[2] * x + e[6] * y + e[10] * z + e[14] ) * d;
  126. return this;
  127. },
  128. applyQuaternion: function ( q ) {
  129. var x = this.x;
  130. var y = this.y;
  131. var z = this.z;
  132. var qx = q.x;
  133. var qy = q.y;
  134. var qz = q.z;
  135. var qw = q.w;
  136. // calculate quat * vector
  137. var ix = qw * x + qy * z - qz * y;
  138. var iy = qw * y + qz * x - qx * z;
  139. var iz = qw * z + qx * y - qy * x;
  140. var iw = -qx * x - qy * y - qz * z;
  141. // calculate result * inverse quat
  142. this.x = ix * qw + iw * -qx + iy * -qz - iz * -qy;
  143. this.y = iy * qw + iw * -qy + iz * -qx - ix * -qz;
  144. this.z = iz * qw + iw * -qz + ix * -qy - iy * -qx;
  145. return this;
  146. },
  147. divide: function ( v ) {
  148. this.x /= v.x;
  149. this.y /= v.y;
  150. this.z /= v.z;
  151. return this;
  152. },
  153. divideScalar: function ( s ) {
  154. if ( s !== 0 ) {
  155. this.x /= s;
  156. this.y /= s;
  157. this.z /= s;
  158. } else {
  159. this.x = 0;
  160. this.y = 0;
  161. this.z = 0;
  162. }
  163. return this;
  164. },
  165. min: function ( v ) {
  166. if ( this.x > v.x ) {
  167. this.x = v.x;
  168. }
  169. if ( this.y > v.y ) {
  170. this.y = v.y;
  171. }
  172. if ( this.z > v.z ) {
  173. this.z = v.z;
  174. }
  175. return this;
  176. },
  177. max: function ( v ) {
  178. if ( this.x < v.x ) {
  179. this.x = v.x;
  180. }
  181. if ( this.y < v.y ) {
  182. this.y = v.y;
  183. }
  184. if ( this.z < v.z ) {
  185. this.z = v.z;
  186. }
  187. return this;
  188. },
  189. clamp: function ( min, max ) {
  190. // This function assumes min < max, if this assumption isn't true it will not operate correctly
  191. if ( this.x < min.x ) {
  192. this.x = min.x;
  193. } else if ( this.x > max.x ) {
  194. this.x = max.x;
  195. }
  196. if ( this.y < min.y ) {
  197. this.y = min.y;
  198. } else if ( this.y > max.y ) {
  199. this.y = max.y;
  200. }
  201. if ( this.z < min.z ) {
  202. this.z = min.z;
  203. } else if ( this.z > max.z ) {
  204. this.z = max.z;
  205. }
  206. return this;
  207. },
  208. negate: function() {
  209. return this.multiplyScalar( - 1 );
  210. },
  211. dot: function ( v ) {
  212. return this.x * v.x + this.y * v.y + this.z * v.z;
  213. },
  214. lengthSq: function () {
  215. return this.x * this.x + this.y * this.y + this.z * this.z;
  216. },
  217. length: function () {
  218. return Math.sqrt( this.x * this.x + this.y * this.y + this.z * this.z );
  219. },
  220. lengthManhattan: function () {
  221. return Math.abs( this.x ) + Math.abs( this.y ) + Math.abs( this.z );
  222. },
  223. normalize: function () {
  224. return this.divideScalar( this.length() );
  225. },
  226. setLength: function ( l ) {
  227. var oldLength = this.length();
  228. if ( oldLength !== 0 && l !== oldLength ) {
  229. this.multiplyScalar( l / oldLength );
  230. }
  231. return this;
  232. },
  233. lerp: function ( v, alpha ) {
  234. this.x += ( v.x - this.x ) * alpha;
  235. this.y += ( v.y - this.y ) * alpha;
  236. this.z += ( v.z - this.z ) * alpha;
  237. return this;
  238. },
  239. cross: function ( v ) {
  240. if ( arguments.length > 1 ) debugger;
  241. var x = this.x, y = this.y, z = this.z;
  242. this.x = y * v.z - z * v.y;
  243. this.y = z * v.x - x * v.z;
  244. this.z = x * v.y - y * v.x;
  245. return this;
  246. },
  247. crossVectors: function ( a, b ) {
  248. this.x = a.y * b.z - a.z * b.y;
  249. this.y = a.z * b.x - a.x * b.z;
  250. this.z = a.x * b.y - a.y * b.x;
  251. return this;
  252. },
  253. angleTo: function ( v ) {
  254. return Math.acos( this.dot( v ) / this.length() / v.length() );
  255. },
  256. distanceTo: function ( v ) {
  257. return Math.sqrt( this.distanceToSquared( v ) );
  258. },
  259. distanceToSquared: function ( v ) {
  260. var dx = this.x - v.x;
  261. var dy = this.y - v.y;
  262. var dz = this.z - v.z;
  263. return dx * dx + dy * dy + dz * dz;
  264. },
  265. getPositionFromMatrix: function ( m ) {
  266. this.x = m.elements[12];
  267. this.y = m.elements[13];
  268. this.z = m.elements[14];
  269. return this;
  270. },
  271. setEulerFromRotationMatrix: function ( m, order ) {
  272. // assumes the upper 3x3 of m is a pure rotation matrix (i.e, unscaled)
  273. // clamp, to handle numerical problems
  274. function clamp( x ) {
  275. return Math.min( Math.max( x, -1 ), 1 );
  276. }
  277. var te = m.elements;
  278. var m11 = te[0], m12 = te[4], m13 = te[8];
  279. var m21 = te[1], m22 = te[5], m23 = te[9];
  280. var m31 = te[2], m32 = te[6], m33 = te[10];
  281. if ( order === undefined || order === 'XYZ' ) {
  282. this.y = Math.asin( clamp( m13 ) );
  283. if ( Math.abs( m13 ) < 0.99999 ) {
  284. this.x = Math.atan2( - m23, m33 );
  285. this.z = Math.atan2( - m12, m11 );
  286. } else {
  287. this.x = Math.atan2( m32, m22 );
  288. this.z = 0;
  289. }
  290. } else if ( order === 'YXZ' ) {
  291. this.x = Math.asin( - clamp( m23 ) );
  292. if ( Math.abs( m23 ) < 0.99999 ) {
  293. this.y = Math.atan2( m13, m33 );
  294. this.z = Math.atan2( m21, m22 );
  295. } else {
  296. this.y = Math.atan2( - m31, m11 );
  297. this.z = 0;
  298. }
  299. } else if ( order === 'ZXY' ) {
  300. this.x = Math.asin( clamp( m32 ) );
  301. if ( Math.abs( m32 ) < 0.99999 ) {
  302. this.y = Math.atan2( - m31, m33 );
  303. this.z = Math.atan2( - m12, m22 );
  304. } else {
  305. this.y = 0;
  306. this.z = Math.atan2( m21, m11 );
  307. }
  308. } else if ( order === 'ZYX' ) {
  309. this.y = Math.asin( - clamp( m31 ) );
  310. if ( Math.abs( m31 ) < 0.99999 ) {
  311. this.x = Math.atan2( m32, m33 );
  312. this.z = Math.atan2( m21, m11 );
  313. } else {
  314. this.x = 0;
  315. this.z = Math.atan2( - m12, m22 );
  316. }
  317. } else if ( order === 'YZX' ) {
  318. this.z = Math.asin( clamp( m21 ) );
  319. if ( Math.abs( m21 ) < 0.99999 ) {
  320. this.x = Math.atan2( - m23, m22 );
  321. this.y = Math.atan2( - m31, m11 );
  322. } else {
  323. this.x = 0;
  324. this.y = Math.atan2( m13, m33 );
  325. }
  326. } else if ( order === 'XZY' ) {
  327. this.z = Math.asin( - clamp( m12 ) );
  328. if ( Math.abs( m12 ) < 0.99999 ) {
  329. this.x = Math.atan2( m32, m22 );
  330. this.y = Math.atan2( m13, m11 );
  331. } else {
  332. this.x = Math.atan2( - m23, m33 );
  333. this.y = 0;
  334. }
  335. }
  336. return this;
  337. },
  338. setEulerFromQuaternion: function ( q, order ) {
  339. // q is assumed to be normalized
  340. // clamp, to handle numerical problems
  341. function clamp( x ) {
  342. return Math.min( Math.max( x, -1 ), 1 );
  343. }
  344. // http://www.mathworks.com/matlabcentral/fileexchange/20696-function-to-convert-between-dcm-euler-angles-quaternions-and-euler-vectors/content/SpinCalc.m
  345. var sqx = q.x * q.x;
  346. var sqy = q.y * q.y;
  347. var sqz = q.z * q.z;
  348. var sqw = q.w * q.w;
  349. if ( order === undefined || order === 'XYZ' ) {
  350. this.x = Math.atan2( 2 * ( q.x * q.w - q.y * q.z ), ( sqw - sqx - sqy + sqz ) );
  351. this.y = Math.asin( clamp( 2 * ( q.x * q.z + q.y * q.w ) ) );
  352. this.z = Math.atan2( 2 * ( q.z * q.w - q.x * q.y ), ( sqw + sqx - sqy - sqz ) );
  353. } else if ( order === 'YXZ' ) {
  354. this.x = Math.asin( clamp( 2 * ( q.x * q.w - q.y * q.z ) ) );
  355. this.y = Math.atan2( 2 * ( q.x * q.z + q.y * q.w ), ( sqw - sqx - sqy + sqz ) );
  356. this.z = Math.atan2( 2 * ( q.x * q.y + q.z * q.w ), ( sqw - sqx + sqy - sqz ) );
  357. } else if ( order === 'ZXY' ) {
  358. this.x = Math.asin( clamp( 2 * ( q.x * q.w + q.y * q.z ) ) );
  359. this.y = Math.atan2( 2 * ( q.y * q.w - q.z * q.x ), ( sqw - sqx - sqy + sqz ) );
  360. this.z = Math.atan2( 2 * ( q.z * q.w - q.x * q.y ), ( sqw - sqx + sqy - sqz ) );
  361. } else if ( order === 'ZYX' ) {
  362. this.x = Math.atan2( 2 * ( q.x * q.w + q.z * q.y ), ( sqw - sqx - sqy + sqz ) );
  363. this.y = Math.asin( clamp( 2 * ( q.y * q.w - q.x * q.z ) ) );
  364. this.z = Math.atan2( 2 * ( q.x * q.y + q.z * q.w ), ( sqw + sqx - sqy - sqz ) );
  365. } else if ( order === 'YZX' ) {
  366. this.x = Math.atan2( 2 * ( q.x * q.w - q.z * q.y ), ( sqw - sqx + sqy - sqz ) );
  367. this.y = Math.atan2( 2 * ( q.y * q.w - q.x * q.z ), ( sqw + sqx - sqy - sqz ) );
  368. this.z = Math.asin( clamp( 2 * ( q.x * q.y + q.z * q.w ) ) );
  369. } else if ( order === 'XZY' ) {
  370. this.x = Math.atan2( 2 * ( q.x * q.w + q.y * q.z ), ( sqw - sqx + sqy - sqz ) );
  371. this.y = Math.atan2( 2 * ( q.x * q.z + q.y * q.w ), ( sqw + sqx - sqy - sqz ) );
  372. this.z = Math.asin( clamp( 2 * ( q.z * q.w - q.x * q.y ) ) );
  373. }
  374. return this;
  375. },
  376. getScaleFromMatrix: function ( m ) {
  377. var sx = this.set( m.elements[0], m.elements[1], m.elements[2] ).length();
  378. var sy = this.set( m.elements[4], m.elements[5], m.elements[6] ).length();
  379. var sz = this.set( m.elements[8], m.elements[9], m.elements[10] ).length();
  380. this.x = sx;
  381. this.y = sy;
  382. this.z = sz;
  383. return this;
  384. },
  385. equals: function ( v ) {
  386. return ( ( v.x === this.x ) && ( v.y === this.y ) && ( v.z === this.z ) );
  387. },
  388. clone: function () {
  389. return new THREE.Vector3( this.x, this.y, this.z );
  390. }
  391. };
粤ICP备19079148号