Vector4.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691
  1. /**
  2. * @author supereggbert / http://www.paulbrunt.co.uk/
  3. * @author philogb / http://blog.thejit.org/
  4. * @author mikael emtinger / http://gomo.se/
  5. * @author egraether / http://egraether.com/
  6. * @author WestLangley / http://github.com/WestLangley
  7. */
  8. THREE.Vector4 = function ( x, y, z, w ) {
  9. this.x = x || 0;
  10. this.y = y || 0;
  11. this.z = z || 0;
  12. this.w = ( w !== undefined ) ? w : 1;
  13. };
  14. THREE.Vector4.prototype = {
  15. constructor: THREE.Vector4,
  16. set: function ( x, y, z, w ) {
  17. this.x = x;
  18. this.y = y;
  19. this.z = z;
  20. this.w = w;
  21. return this;
  22. },
  23. setX: function ( x ) {
  24. this.x = x;
  25. return this;
  26. },
  27. setY: function ( y ) {
  28. this.y = y;
  29. return this;
  30. },
  31. setZ: function ( z ) {
  32. this.z = z;
  33. return this;
  34. },
  35. setW: function ( w ) {
  36. this.w = w;
  37. return this;
  38. },
  39. setComponent: function ( index, value ) {
  40. switch ( index ) {
  41. case 0: this.x = value; break;
  42. case 1: this.y = value; break;
  43. case 2: this.z = value; break;
  44. case 3: this.w = value; break;
  45. default: throw new Error( 'index is out of range: ' + index );
  46. }
  47. },
  48. getComponent: function ( index ) {
  49. switch ( index ) {
  50. case 0: return this.x;
  51. case 1: return this.y;
  52. case 2: return this.z;
  53. case 3: return this.w;
  54. default: throw new Error( 'index is out of range: ' + index );
  55. }
  56. },
  57. copy: function ( v ) {
  58. this.x = v.x;
  59. this.y = v.y;
  60. this.z = v.z;
  61. this.w = ( v.w !== undefined ) ? v.w : 1;
  62. return this;
  63. },
  64. add: function ( v, w ) {
  65. if ( w !== undefined ) {
  66. console.warn( 'THREE.Vector4: .add() now only accepts one argument. Use .addVectors( a, b ) instead.' );
  67. return this.addVectors( v, w );
  68. }
  69. this.x += v.x;
  70. this.y += v.y;
  71. this.z += v.z;
  72. this.w += v.w;
  73. return this;
  74. },
  75. addScalar: function ( s ) {
  76. this.x += s;
  77. this.y += s;
  78. this.z += s;
  79. this.w += s;
  80. return this;
  81. },
  82. addVectors: 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. this.w = a.w + b.w;
  87. return this;
  88. },
  89. sub: function ( v, w ) {
  90. if ( w !== undefined ) {
  91. console.warn( 'THREE.Vector4: .sub() now only accepts one argument. Use .subVectors( a, b ) instead.' );
  92. return this.subVectors( v, w );
  93. }
  94. this.x -= v.x;
  95. this.y -= v.y;
  96. this.z -= v.z;
  97. this.w -= v.w;
  98. return this;
  99. },
  100. subScalar: function ( s ) {
  101. this.x -= s;
  102. this.y -= s;
  103. this.z -= s;
  104. this.w -= s;
  105. return this;
  106. },
  107. subVectors: function ( a, b ) {
  108. this.x = a.x - b.x;
  109. this.y = a.y - b.y;
  110. this.z = a.z - b.z;
  111. this.w = a.w - b.w;
  112. return this;
  113. },
  114. multiplyScalar: function ( scalar ) {
  115. this.x *= scalar;
  116. this.y *= scalar;
  117. this.z *= scalar;
  118. this.w *= scalar;
  119. return this;
  120. },
  121. applyMatrix4: function ( m ) {
  122. var x = this.x;
  123. var y = this.y;
  124. var z = this.z;
  125. var w = this.w;
  126. var e = m.elements;
  127. this.x = e[ 0 ] * x + e[ 4 ] * y + e[ 8 ] * z + e[ 12 ] * w;
  128. this.y = e[ 1 ] * x + e[ 5 ] * y + e[ 9 ] * z + e[ 13 ] * w;
  129. this.z = e[ 2 ] * x + e[ 6 ] * y + e[ 10 ] * z + e[ 14 ] * w;
  130. this.w = e[ 3 ] * x + e[ 7 ] * y + e[ 11 ] * z + e[ 15 ] * w;
  131. return this;
  132. },
  133. divideScalar: function ( scalar ) {
  134. if ( scalar !== 0 ) {
  135. var invScalar = 1 / scalar;
  136. this.x *= invScalar;
  137. this.y *= invScalar;
  138. this.z *= invScalar;
  139. this.w *= invScalar;
  140. } else {
  141. this.x = 0;
  142. this.y = 0;
  143. this.z = 0;
  144. this.w = 1;
  145. }
  146. return this;
  147. },
  148. setAxisAngleFromQuaternion: function ( q ) {
  149. // http://www.euclideanspace.com/maths/geometry/rotations/conversions/quaternionToAngle/index.htm
  150. // q is assumed to be normalized
  151. this.w = 2 * Math.acos( q.w );
  152. var s = Math.sqrt( 1 - q.w * q.w );
  153. if ( s < 0.0001 ) {
  154. this.x = 1;
  155. this.y = 0;
  156. this.z = 0;
  157. } else {
  158. this.x = q.x / s;
  159. this.y = q.y / s;
  160. this.z = q.z / s;
  161. }
  162. return this;
  163. },
  164. setAxisAngleFromRotationMatrix: function ( m ) {
  165. // http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToAngle/index.htm
  166. // assumes the upper 3x3 of m is a pure rotation matrix (i.e, unscaled)
  167. var angle, x, y, z, // variables for result
  168. epsilon = 0.01, // margin to allow for rounding errors
  169. epsilon2 = 0.1, // margin to distinguish between 0 and 180 degrees
  170. te = m.elements,
  171. m11 = te[ 0 ], m12 = te[ 4 ], m13 = te[ 8 ],
  172. m21 = te[ 1 ], m22 = te[ 5 ], m23 = te[ 9 ],
  173. m31 = te[ 2 ], m32 = te[ 6 ], m33 = te[ 10 ];
  174. if ( ( Math.abs( m12 - m21 ) < epsilon )
  175. && ( Math.abs( m13 - m31 ) < epsilon )
  176. && ( Math.abs( m23 - m32 ) < epsilon ) ) {
  177. // singularity found
  178. // first check for identity matrix which must have +1 for all terms
  179. // in leading diagonal and zero in other terms
  180. if ( ( Math.abs( m12 + m21 ) < epsilon2 )
  181. && ( Math.abs( m13 + m31 ) < epsilon2 )
  182. && ( Math.abs( m23 + m32 ) < epsilon2 )
  183. && ( Math.abs( m11 + m22 + m33 - 3 ) < epsilon2 ) ) {
  184. // this singularity is identity matrix so angle = 0
  185. this.set( 1, 0, 0, 0 );
  186. return this; // zero angle, arbitrary axis
  187. }
  188. // otherwise this singularity is angle = 180
  189. angle = Math.PI;
  190. var xx = ( m11 + 1 ) / 2;
  191. var yy = ( m22 + 1 ) / 2;
  192. var zz = ( m33 + 1 ) / 2;
  193. var xy = ( m12 + m21 ) / 4;
  194. var xz = ( m13 + m31 ) / 4;
  195. var yz = ( m23 + m32 ) / 4;
  196. if ( ( xx > yy ) && ( xx > zz ) ) { // m11 is the largest diagonal term
  197. if ( xx < epsilon ) {
  198. x = 0;
  199. y = 0.707106781;
  200. z = 0.707106781;
  201. } else {
  202. x = Math.sqrt( xx );
  203. y = xy / x;
  204. z = xz / x;
  205. }
  206. } else if ( yy > zz ) { // m22 is the largest diagonal term
  207. if ( yy < epsilon ) {
  208. x = 0.707106781;
  209. y = 0;
  210. z = 0.707106781;
  211. } else {
  212. y = Math.sqrt( yy );
  213. x = xy / y;
  214. z = yz / y;
  215. }
  216. } else { // m33 is the largest diagonal term so base result on this
  217. if ( zz < epsilon ) {
  218. x = 0.707106781;
  219. y = 0.707106781;
  220. z = 0;
  221. } else {
  222. z = Math.sqrt( zz );
  223. x = xz / z;
  224. y = yz / z;
  225. }
  226. }
  227. this.set( x, y, z, angle );
  228. return this; // return 180 deg rotation
  229. }
  230. // as we have reached here there are no singularities so we can handle normally
  231. var s = Math.sqrt( ( m32 - m23 ) * ( m32 - m23 )
  232. + ( m13 - m31 ) * ( m13 - m31 )
  233. + ( m21 - m12 ) * ( m21 - m12 ) ); // used to normalize
  234. if ( Math.abs( s ) < 0.001 ) s = 1;
  235. // prevent divide by zero, should not happen if matrix is orthogonal and should be
  236. // caught by singularity test above, but I've left it in just in case
  237. this.x = ( m32 - m23 ) / s;
  238. this.y = ( m13 - m31 ) / s;
  239. this.z = ( m21 - m12 ) / s;
  240. this.w = Math.acos( ( m11 + m22 + m33 - 1 ) / 2 );
  241. return this;
  242. },
  243. min: function ( v ) {
  244. if ( this.x > v.x ) {
  245. this.x = v.x;
  246. }
  247. if ( this.y > v.y ) {
  248. this.y = v.y;
  249. }
  250. if ( this.z > v.z ) {
  251. this.z = v.z;
  252. }
  253. if ( this.w > v.w ) {
  254. this.w = v.w;
  255. }
  256. return this;
  257. },
  258. max: function ( v ) {
  259. if ( this.x < v.x ) {
  260. this.x = v.x;
  261. }
  262. if ( this.y < v.y ) {
  263. this.y = v.y;
  264. }
  265. if ( this.z < v.z ) {
  266. this.z = v.z;
  267. }
  268. if ( this.w < v.w ) {
  269. this.w = v.w;
  270. }
  271. return this;
  272. },
  273. clamp: function ( min, max ) {
  274. // This function assumes min < max, if this assumption isn't true it will not operate correctly
  275. if ( this.x < min.x ) {
  276. this.x = min.x;
  277. } else if ( this.x > max.x ) {
  278. this.x = max.x;
  279. }
  280. if ( this.y < min.y ) {
  281. this.y = min.y;
  282. } else if ( this.y > max.y ) {
  283. this.y = max.y;
  284. }
  285. if ( this.z < min.z ) {
  286. this.z = min.z;
  287. } else if ( this.z > max.z ) {
  288. this.z = max.z;
  289. }
  290. if ( this.w < min.w ) {
  291. this.w = min.w;
  292. } else if ( this.w > max.w ) {
  293. this.w = max.w;
  294. }
  295. return this;
  296. },
  297. clampScalar: ( function () {
  298. var min, max;
  299. return function ( minVal, maxVal ) {
  300. if ( min === undefined ) {
  301. min = new THREE.Vector4();
  302. max = new THREE.Vector4();
  303. }
  304. min.set( minVal, minVal, minVal, minVal );
  305. max.set( maxVal, maxVal, maxVal, maxVal );
  306. return this.clamp( min, max );
  307. };
  308. } )(),
  309. floor: function () {
  310. this.x = Math.floor( this.x );
  311. this.y = Math.floor( this.y );
  312. this.z = Math.floor( this.z );
  313. this.w = Math.floor( this.w );
  314. return this;
  315. },
  316. ceil: function () {
  317. this.x = Math.ceil( this.x );
  318. this.y = Math.ceil( this.y );
  319. this.z = Math.ceil( this.z );
  320. this.w = Math.ceil( this.w );
  321. return this;
  322. },
  323. round: function () {
  324. this.x = Math.round( this.x );
  325. this.y = Math.round( this.y );
  326. this.z = Math.round( this.z );
  327. this.w = Math.round( this.w );
  328. return this;
  329. },
  330. roundToZero: function () {
  331. this.x = ( this.x < 0 ) ? Math.ceil( this.x ) : Math.floor( this.x );
  332. this.y = ( this.y < 0 ) ? Math.ceil( this.y ) : Math.floor( this.y );
  333. this.z = ( this.z < 0 ) ? Math.ceil( this.z ) : Math.floor( this.z );
  334. this.w = ( this.w < 0 ) ? Math.ceil( this.w ) : Math.floor( this.w );
  335. return this;
  336. },
  337. negate: function () {
  338. this.x = - this.x;
  339. this.y = - this.y;
  340. this.z = - this.z;
  341. this.w = - this.w;
  342. return this;
  343. },
  344. dot: function ( v ) {
  345. return this.x * v.x + this.y * v.y + this.z * v.z + this.w * v.w;
  346. },
  347. lengthSq: function () {
  348. return this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w;
  349. },
  350. length: function () {
  351. return Math.sqrt( this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w );
  352. },
  353. lengthManhattan: function () {
  354. return Math.abs( this.x ) + Math.abs( this.y ) + Math.abs( this.z ) + Math.abs( this.w );
  355. },
  356. normalize: function () {
  357. return this.divideScalar( this.length() );
  358. },
  359. setLength: function ( l ) {
  360. var oldLength = this.length();
  361. if ( oldLength !== 0 && l !== oldLength ) {
  362. this.multiplyScalar( l / oldLength );
  363. }
  364. return this;
  365. },
  366. lerp: function ( v, alpha ) {
  367. this.x += ( v.x - this.x ) * alpha;
  368. this.y += ( v.y - this.y ) * alpha;
  369. this.z += ( v.z - this.z ) * alpha;
  370. this.w += ( v.w - this.w ) * alpha;
  371. return this;
  372. },
  373. lerpVectors: function ( v1, v2, alpha ) {
  374. this.subVectors( v2, v1 ).multiplyScalar( alpha ).add( v1 );
  375. return this;
  376. },
  377. equals: function ( v ) {
  378. return ( ( v.x === this.x ) && ( v.y === this.y ) && ( v.z === this.z ) && ( v.w === this.w ) );
  379. },
  380. fromArray: function ( array, offset ) {
  381. if ( offset === undefined ) offset = 0;
  382. this.x = array[ offset ];
  383. this.y = array[ offset + 1 ];
  384. this.z = array[ offset + 2 ];
  385. this.w = array[ offset + 3 ];
  386. return this;
  387. },
  388. toArray: function ( array, offset ) {
  389. if ( array === undefined ) array = [];
  390. if ( offset === undefined ) offset = 0;
  391. array[ offset ] = this.x;
  392. array[ offset + 1 ] = this.y;
  393. array[ offset + 2 ] = this.z;
  394. array[ offset + 3 ] = this.w;
  395. return array;
  396. },
  397. fromAttribute: function ( attribute, index, offset ) {
  398. if ( offset === undefined ) offset = 0;
  399. index = index * attribute.itemSize + offset;
  400. this.x = attribute.array[ index ];
  401. this.y = attribute.array[ index + 1 ];
  402. this.z = attribute.array[ index + 2 ];
  403. this.w = attribute.array[ index + 3 ];
  404. return this;
  405. },
  406. clone: function () {
  407. return new THREE.Vector4( this.x, this.y, this.z, this.w );
  408. }
  409. };
粤ICP备19079148号