Vector3.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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. 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 te = m.elements;
  165. var m11 = te[0], m12 = te[4], m13 = te[8];
  166. var m21 = te[1], m22 = te[5], m23 = te[9];
  167. var m31 = te[2], m32 = te[6], m33 = te[10];
  168. if ( order === undefined || order === 'XYZ' ) {
  169. this.y = Math.asin( clamp( m13 ) );
  170. if ( Math.abs( m13 ) < 0.99999 ) {
  171. this.x = Math.atan2( - m23, m33 );
  172. this.z = Math.atan2( - m12, m11 );
  173. } else {
  174. this.x = Math.atan2( m32, m22 );
  175. this.z = 0;
  176. }
  177. } else if ( order === 'YXZ' ) {
  178. this.x = Math.asin( - clamp( m23 ) );
  179. if ( Math.abs( m23 ) < 0.99999 ) {
  180. this.y = Math.atan2( m13, m33 );
  181. this.z = Math.atan2( m21, m22 );
  182. } else {
  183. this.y = Math.atan2( - m31, m11 );
  184. this.z = 0;
  185. }
  186. } else if ( order === 'ZXY' ) {
  187. this.x = Math.asin( clamp( m32 ) );
  188. if ( Math.abs( m32 ) < 0.99999 ) {
  189. this.y = Math.atan2( - m31, m33 );
  190. this.z = Math.atan2( - m12, m22 );
  191. } else {
  192. this.y = 0;
  193. this.z = Math.atan2( m21, m11 );
  194. }
  195. } else if ( order === 'ZYX' ) {
  196. this.y = Math.asin( - clamp( m31 ) );
  197. if ( Math.abs( m31 ) < 0.99999 ) {
  198. this.x = Math.atan2( m32, m33 );
  199. this.z = Math.atan2( m21, m11 );
  200. } else {
  201. this.x = 0;
  202. this.z = Math.atan2( - m12, m22 );
  203. }
  204. } else if ( order === 'YZX' ) {
  205. this.z = Math.asin( clamp( m21 ) );
  206. if ( Math.abs( m21 ) < 0.99999 ) {
  207. this.x = Math.atan2( - m23, m22 );
  208. this.y = Math.atan2( - m31, m11 );
  209. } else {
  210. this.x = 0;
  211. this.y = Math.atan2( m13, m33 );
  212. }
  213. } else if ( order === 'XZY' ) {
  214. this.z = Math.asin( - clamp( m12 ) );
  215. if ( Math.abs( m12 ) < 0.99999 ) {
  216. this.x = Math.atan2( m32, m22 );
  217. this.y = Math.atan2( m13, m11 );
  218. } else {
  219. this.x = Math.atan2( - m23, m33 );
  220. this.y = 0;
  221. }
  222. }
  223. return this;
  224. },
  225. setEulerFromQuaternion: function ( q, order ) {
  226. // q is assumed to be normalized
  227. // clamp, to handle numerical problems
  228. function clamp( x ) {
  229. return Math.min( Math.max( x, -1 ), 1 );
  230. }
  231. // http://www.mathworks.com/matlabcentral/fileexchange/20696-function-to-convert-between-dcm-euler-angles-quaternions-and-euler-vectors/content/SpinCalc.m
  232. var sqx = q.x * q.x;
  233. var sqy = q.y * q.y;
  234. var sqz = q.z * q.z;
  235. var sqw = q.w * q.w;
  236. if ( order === undefined || order === 'XYZ' ) {
  237. this.x = Math.atan2( 2 * ( q.x * q.w - q.y * q.z ), ( sqw - sqx - sqy + sqz ) );
  238. this.y = Math.asin( clamp( 2 * ( q.x * q.z + q.y * q.w ) ) );
  239. this.z = Math.atan2( 2 * ( q.z * q.w - q.x * q.y ), ( sqw + sqx - sqy - sqz ) );
  240. } else if ( order === 'YXZ' ) {
  241. this.x = Math.asin( clamp( 2 * ( q.x * q.w - q.y * q.z ) ) );
  242. this.y = Math.atan2( 2 * ( q.x * q.z + q.y * q.w ), ( sqw - sqx - sqy + sqz ) );
  243. this.z = Math.atan2( 2 * ( q.x * q.y + q.z * q.w ), ( sqw - sqx + sqy - sqz ) );
  244. } else if ( order === 'ZXY' ) {
  245. this.x = Math.asin( clamp( 2 * ( q.x * q.w + q.y * q.z ) ) );
  246. this.y = Math.atan2( 2 * ( q.y * q.w - q.z * q.x ), ( sqw - sqx - sqy + sqz ) );
  247. this.z = Math.atan2( 2 * ( q.z * q.w - q.x * q.y ), ( sqw - sqx + sqy - sqz ) );
  248. } else if ( order === 'ZYX' ) {
  249. this.x = Math.atan2( 2 * ( q.x * q.w + q.z * q.y ), ( sqw - sqx - sqy + sqz ) );
  250. this.y = Math.asin( clamp( 2 * ( q.y * q.w - q.x * q.z ) ) );
  251. this.z = Math.atan2( 2 * ( q.x * q.y + q.z * q.w ), ( sqw + sqx - sqy - sqz ) );
  252. } else if ( order === 'YZX' ) {
  253. this.x = Math.atan2( 2 * ( q.x * q.w - q.z * q.y ), ( sqw - sqx + sqy - sqz ) );
  254. this.y = Math.atan2( 2 * ( q.y * q.w - q.x * q.z ), ( sqw + sqx - sqy - sqz ) );
  255. this.z = Math.asin( clamp( 2 * ( q.x * q.y + q.z * q.w ) ) );
  256. } else if ( order === 'XZY' ) {
  257. this.x = Math.atan2( 2 * ( q.x * q.w + q.y * q.z ), ( sqw - sqx + sqy - sqz ) );
  258. this.y = Math.atan2( 2 * ( q.x * q.z + q.y * q.w ), ( sqw + sqx - sqy - sqz ) );
  259. this.z = Math.asin( clamp( 2 * ( q.z * q.w - q.x * q.y ) ) );
  260. }
  261. return this;
  262. },
  263. getScaleFromMatrix: function ( m ) {
  264. var sx = this.set( m.elements[0], m.elements[1], m.elements[2] ).length();
  265. var sy = this.set( m.elements[4], m.elements[5], m.elements[6] ).length();
  266. var sz = this.set( m.elements[8], m.elements[9], m.elements[10] ).length();
  267. this.x = sx;
  268. this.y = sy;
  269. this.z = sz;
  270. return this;
  271. },
  272. equals: function ( v ) {
  273. return ( ( v.x === this.x ) && ( v.y === this.y ) && ( v.z === this.z ) );
  274. },
  275. isZero: function () {
  276. return ( this.lengthSq() < 0.0001 /* almostZero */ );
  277. },
  278. clone: function () {
  279. return new THREE.Vector3( this.x, this.y, this.z );
  280. }
  281. };
粤ICP备19079148号