Vector4.js 10.0 KB

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