Vector3.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. /**
  2. * @author mr.doob / 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. copy: function ( v ) {
  35. this.x = v.x;
  36. this.y = v.y;
  37. this.z = v.z;
  38. return this;
  39. },
  40. add: function ( a, b ) {
  41. this.x = a.x + b.x;
  42. this.y = a.y + b.y;
  43. this.z = a.z + b.z;
  44. return this;
  45. },
  46. addSelf: function ( v ) {
  47. this.x += v.x;
  48. this.y += v.y;
  49. this.z += v.z;
  50. return this;
  51. },
  52. addScalar: function ( s ) {
  53. this.x += s;
  54. this.y += s;
  55. this.z += s;
  56. return this;
  57. },
  58. sub: function ( a, b ) {
  59. this.x = a.x - b.x;
  60. this.y = a.y - b.y;
  61. this.z = a.z - b.z;
  62. return this;
  63. },
  64. subSelf: function ( v ) {
  65. this.x -= v.x;
  66. this.y -= v.y;
  67. this.z -= v.z;
  68. return this;
  69. },
  70. multiply: function ( a, b ) {
  71. this.x = a.x * b.x;
  72. this.y = a.y * b.y;
  73. this.z = a.z * b.z;
  74. return this;
  75. },
  76. multiplySelf: function ( v ) {
  77. this.x *= v.x;
  78. this.y *= v.y;
  79. this.z *= v.z;
  80. return this;
  81. },
  82. multiplyScalar: function ( s ) {
  83. this.x *= s;
  84. this.y *= s;
  85. this.z *= s;
  86. return this;
  87. },
  88. divideSelf: function ( v ) {
  89. this.x /= v.x;
  90. this.y /= v.y;
  91. this.z /= v.z;
  92. return this;
  93. },
  94. divideScalar: function ( s ) {
  95. if ( s ) {
  96. this.x /= s;
  97. this.y /= s;
  98. this.z /= s;
  99. } else {
  100. this.x = 0;
  101. this.y = 0;
  102. this.z = 0;
  103. }
  104. return this;
  105. },
  106. negate: function() {
  107. return this.multiplyScalar( - 1 );
  108. },
  109. dot: function ( v ) {
  110. return this.x * v.x + this.y * v.y + this.z * v.z;
  111. },
  112. lengthSq: function () {
  113. return this.x * this.x + this.y * this.y + this.z * this.z;
  114. },
  115. length: function () {
  116. return Math.sqrt( this.lengthSq() );
  117. },
  118. lengthManhattan: function () {
  119. return Math.abs( this.x ) + Math.abs( this.y ) + Math.abs( this.z );
  120. },
  121. normalize: function () {
  122. return this.divideScalar( this.length() );
  123. },
  124. setLength: function ( l ) {
  125. return this.normalize().multiplyScalar( l );
  126. },
  127. lerpSelf: function ( v, alpha ) {
  128. this.x += ( v.x - this.x ) * alpha;
  129. this.y += ( v.y - this.y ) * alpha;
  130. this.z += ( v.z - this.z ) * alpha;
  131. return this;
  132. },
  133. cross: function ( a, b ) {
  134. this.x = a.y * b.z - a.z * b.y;
  135. this.y = a.z * b.x - a.x * b.z;
  136. this.z = a.x * b.y - a.y * b.x;
  137. return this;
  138. },
  139. crossSelf: function ( v ) {
  140. var x = this.x, y = this.y, z = this.z;
  141. this.x = y * v.z - z * v.y;
  142. this.y = z * v.x - x * v.z;
  143. this.z = x * v.y - y * v.x;
  144. return this;
  145. },
  146. distanceTo: function ( v ) {
  147. return Math.sqrt( this.distanceToSquared( v ) );
  148. },
  149. distanceToSquared: function ( v ) {
  150. return new THREE.Vector3().sub( this, v ).lengthSq();
  151. },
  152. getPositionFromMatrix: function ( m ) {
  153. this.x = m.elements[12];
  154. this.y = m.elements[13];
  155. this.z = m.elements[14];
  156. return this;
  157. },
  158. setEulerFromRotationMatrix: function ( m, order ) {
  159. // assumes the upper 3x3 of m is a pure rotation matrix (i.e, unscaled)
  160. // clamp, to handle numerical problems
  161. function clamp( x ) {
  162. return Math.min( Math.max( x, -1 ), 1 );
  163. }
  164. var _order = order || 'XYZ',
  165. te = m.elements,
  166. m11 = te[0], m12 = te[4], m13 = te[8],
  167. m21 = te[1], m22 = te[5], m23 = te[9],
  168. m31 = te[2], m32 = te[6], m33 = te[10];
  169. switch ( _order ) {
  170. case 'YXZ':
  171. this.x = Math.asin( - clamp( m23 ) );
  172. if ( Math.abs( m23 ) < 0.99999 ) {
  173. this.y = Math.atan2( m13, m33 );
  174. this.z = Math.atan2( m21, m22 );
  175. } else {
  176. this.y = Math.atan2( - m31, m11 );
  177. this.z = 0;
  178. }
  179. break;
  180. case 'ZXY':
  181. this.x = Math.asin( clamp( m32 ) );
  182. if ( Math.abs( m32 ) < 0.99999 ) {
  183. this.y = Math.atan2( - m31, m33 );
  184. this.z = Math.atan2( - m12, m22 );
  185. } else {
  186. this.y = 0;
  187. this.z = Math.atan2( m13, m11 );
  188. }
  189. break;
  190. case 'ZYX':
  191. this.y = Math.asin( - clamp( m31 ) );
  192. if ( Math.abs( m31 ) < 0.99999 ) {
  193. this.x = Math.atan2( m32, m33 );
  194. this.z = Math.atan2( m21, m11 );
  195. } else {
  196. this.x = 0;
  197. this.z = Math.atan2( - m12, m22 );
  198. }
  199. break;
  200. case 'YZX':
  201. this.z = Math.asin( clamp( m21 ) );
  202. if ( Math.abs( m21 ) < 0.99999 ) {
  203. this.x = Math.atan2( - m23, m22 );
  204. this.y = Math.atan2( - m31, m11 );
  205. } else {
  206. this.x = 0;
  207. this.y = Math.atan2( m31, m33 );
  208. }
  209. break;
  210. case 'XZY':
  211. this.z = Math.asin( - clamp( m12 ) );
  212. if ( Math.abs( m12 ) < 0.99999 ) {
  213. this.x = Math.atan2( m32, m22 );
  214. this.y = Math.atan2( m13, m11 );
  215. } else {
  216. this.x = Math.atan2( - m13, m33 );
  217. this.y = 0;
  218. }
  219. break;
  220. default: // 'XYZ'
  221. this.y = Math.asin( clamp( m13 ) );
  222. if ( Math.abs( m13 ) < 0.99999 ) {
  223. this.x = Math.atan2( - m23, m33 );
  224. this.z = Math.atan2( - m12, m11 );
  225. } else {
  226. this.x = Math.atan2( m21, m22 );
  227. this.z = 0;
  228. }
  229. break;
  230. }
  231. return this;
  232. },
  233. setEulerFromQuaternion: function ( q, order ) {
  234. // q is assumed to be normalized
  235. // clamp, to handle numerical problems
  236. function clamp( x ) {
  237. return Math.min( Math.max( x, -1 ), 1 );
  238. }
  239. // http://www.mathworks.com/matlabcentral/fileexchange/20696-function-to-convert-between-dcm-euler-angles-quaternions-and-euler-vectors/content/SpinCalc.m
  240. var sqx = q.x * q.x;
  241. var sqy = q.y * q.y;
  242. var sqz = q.z * q.z;
  243. var sqw = q.w * q.w;
  244. switch ( order ) {
  245. case 'YXZ':
  246. this.x = Math.asin( clamp( 2 * ( q.x * q.w - q.y * q.z ) ) );
  247. this.y = Math.atan2( 2 * ( q.x * q.z + q.y * q.w ), ( sqw - sqx - sqy + sqz ) );
  248. this.z = Math.atan2( 2 * ( q.x * q.y + q.z * q.w ), ( sqw - sqx + sqy - sqz ) );
  249. break;
  250. case 'ZXY':
  251. this.x = Math.asin( clamp( 2 * ( q.x * q.w + q.y * q.z ) ) );
  252. this.y = Math.atan2( 2 * ( q.y * q.w - q.z * q.x ), ( sqw - sqx - sqy + sqz ) );
  253. this.z = Math.atan2( 2 * ( q.z * q.w - q.x * q.y ), ( sqw - sqx + sqy - sqz ) );
  254. break;
  255. case 'ZYX':
  256. this.x = Math.atan2( 2 * ( q.x * q.w + q.z * q.y ), ( sqw - sqx - sqy + sqz ) );
  257. this.y = Math.asin( clamp( 2 * ( q.y * q.w - q.x * q.z ) ) );
  258. this.z = Math.atan2( 2 * ( q.x * q.y + q.z * q.w ), ( sqw + sqx - sqy - sqz ) );
  259. break;
  260. case 'YZX':
  261. this.x = Math.atan2( 2 * ( q.x * q.w - q.z * q.y ), ( sqw - sqx + sqy - sqz ) );
  262. this.y = Math.atan2( 2 * ( q.y * q.w - q.x * q.z ), ( sqw + sqx - sqy - sqz ) );
  263. this.z = Math.asin( clamp( 2 * ( q.x * q.y + q.z * q.w ) ) );
  264. break;
  265. case 'XZY':
  266. this.x = Math.atan2( 2 * ( q.x * q.w + q.y * q.z ), ( sqw - sqx + sqy - sqz ) );
  267. this.y = Math.atan2( 2 * ( q.x * q.z + q.y * q.w ), ( sqw + sqx - sqy - sqz ) );
  268. this.z = Math.asin( clamp( 2 * ( q.z * q.w - q.x * q.y ) ) );
  269. break;
  270. default: // 'XYZ'
  271. this.x = Math.atan2( 2 * ( q.x * q.w - q.y * q.z ), ( sqw - sqx - sqy + sqz ) );
  272. this.y = Math.asin( clamp( 2 * ( q.x * q.z + q.y * q.w ) ) );
  273. this.z = Math.atan2( 2 * ( q.z * q.w - q.x * q.y ), ( sqw + sqx - sqy - sqz ) );
  274. }
  275. return this;
  276. },
  277. getScaleFromMatrix: function ( m ) {
  278. var sx = this.set( m.elements[0], m.elements[1], m.elements[2] ).length();
  279. var sy = this.set( m.elements[4], m.elements[5], m.elements[6] ).length();
  280. var sz = this.set( m.elements[8], m.elements[9], m.elements[10] ).length();
  281. this.x = sx;
  282. this.y = sy;
  283. this.z = sz;
  284. return this;
  285. },
  286. equals: function ( v ) {
  287. return ( ( v.x === this.x ) && ( v.y === this.y ) && ( v.z === this.z ) );
  288. },
  289. isZero: function () {
  290. return ( this.lengthSq() < 0.0001 /* almostZero */ );
  291. },
  292. clone: function () {
  293. return new THREE.Vector3( this.x, this.y, this.z );
  294. }
  295. };
粤ICP备19079148号