Vector4.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. */
  7. THREE.Vector4 = function ( x, y, z, w ) {
  8. this.x = x || 0;
  9. this.y = y || 0;
  10. this.z = z || 0;
  11. this.w = ( w !== undefined ) ? w : 1;
  12. };
  13. THREE.Vector4.prototype = {
  14. constructor: THREE.Vector4,
  15. set: function ( x, y, z, w ) {
  16. this.x = x;
  17. this.y = y;
  18. this.z = z;
  19. this.w = w;
  20. return this;
  21. },
  22. copy: function ( v ) {
  23. this.x = v.x;
  24. this.y = v.y;
  25. this.z = v.z;
  26. this.w = ( v.w !== undefined ) ? v.w : 1;
  27. },
  28. clone: function () {
  29. return new THREE.Vector4( this.x, this.y, this.z, this.w );
  30. },
  31. add: function ( v1, v2 ) {
  32. this.x = v1.x + v2.x;
  33. this.y = v1.y + v2.y;
  34. this.z = v1.z + v2.z;
  35. this.w = v1.w + v2.w;
  36. return this;
  37. },
  38. addSelf: function ( v ) {
  39. this.x += v.x;
  40. this.y += v.y;
  41. this.z += v.z;
  42. this.w += v.w;
  43. return this;
  44. },
  45. sub: function ( v1, v2 ) {
  46. this.x = v1.x - v2.x;
  47. this.y = v1.y - v2.y;
  48. this.z = v1.z - v2.z;
  49. this.w = v1.w - v2.w;
  50. return this;
  51. },
  52. subSelf: function ( v ) {
  53. this.x -= v.x;
  54. this.y -= v.y;
  55. this.z -= v.z;
  56. this.w -= v.w;
  57. return this;
  58. },
  59. multiplyScalar: function ( s ) {
  60. this.x *= s;
  61. this.y *= s;
  62. this.z *= s;
  63. this.w *= s;
  64. return this;
  65. },
  66. divideScalar: function ( s ) {
  67. if ( s ) {
  68. this.x /= s;
  69. this.y /= s;
  70. this.z /= s;
  71. this.w /= s;
  72. } else {
  73. this.x = 0;
  74. this.y = 0;
  75. this.z = 0;
  76. this.w = 1;
  77. }
  78. return this;
  79. },
  80. negate: function() {
  81. return this.multiplyScalar( -1 );
  82. },
  83. dot: function ( v ) {
  84. return this.x * v.x + this.y * v.y + this.z * v.z + this.w * v.w;
  85. },
  86. lengthSq: function () {
  87. return this.dot( this );
  88. },
  89. length: function () {
  90. return Math.sqrt( this.lengthSq() );
  91. },
  92. normalize: function () {
  93. return this.divideScalar( this.length() );
  94. },
  95. setLength: function ( l ) {
  96. return this.normalize().multiplyScalar( l );
  97. },
  98. lerpSelf: function ( v, alpha ) {
  99. this.x += ( v.x - this.x ) * alpha;
  100. this.y += ( v.y - this.y ) * alpha;
  101. this.z += ( v.z - this.z ) * alpha;
  102. this.w += ( v.w - this.w ) * alpha;
  103. return this;
  104. }
  105. };