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. return this;
  28. },
  29. add: function ( a, b ) {
  30. this.x = a.x + b.x;
  31. this.y = a.y + b.y;
  32. this.z = a.z + b.z;
  33. this.w = a.w + b.w;
  34. return this;
  35. },
  36. addSelf: function ( v ) {
  37. this.x += v.x;
  38. this.y += v.y;
  39. this.z += v.z;
  40. this.w += v.w;
  41. return this;
  42. },
  43. sub: function ( a, b ) {
  44. this.x = a.x - b.x;
  45. this.y = a.y - b.y;
  46. this.z = a.z - b.z;
  47. this.w = a.w - b.w;
  48. return this;
  49. },
  50. subSelf: function ( v ) {
  51. this.x -= v.x;
  52. this.y -= v.y;
  53. this.z -= v.z;
  54. this.w -= v.w;
  55. return this;
  56. },
  57. multiplyScalar: function ( s ) {
  58. this.x *= s;
  59. this.y *= s;
  60. this.z *= s;
  61. this.w *= s;
  62. return this;
  63. },
  64. divideScalar: function ( s ) {
  65. if ( s ) {
  66. this.x /= s;
  67. this.y /= s;
  68. this.z /= s;
  69. this.w /= s;
  70. } else {
  71. this.x = 0;
  72. this.y = 0;
  73. this.z = 0;
  74. this.w = 1;
  75. }
  76. return this;
  77. },
  78. negate: function() {
  79. return this.multiplyScalar( -1 );
  80. },
  81. dot: function ( v ) {
  82. return this.x * v.x + this.y * v.y + this.z * v.z + this.w * v.w;
  83. },
  84. lengthSq: function () {
  85. return this.dot( this );
  86. },
  87. length: function () {
  88. return Math.sqrt( this.lengthSq() );
  89. },
  90. normalize: function () {
  91. return this.divideScalar( this.length() );
  92. },
  93. setLength: function ( l ) {
  94. return this.normalize().multiplyScalar( l );
  95. },
  96. lerpSelf: function ( v, alpha ) {
  97. this.x += ( v.x - this.x ) * alpha;
  98. this.y += ( v.y - this.y ) * alpha;
  99. this.z += ( v.z - this.z ) * alpha;
  100. this.w += ( v.w - this.w ) * alpha;
  101. return this;
  102. },
  103. clone: function () {
  104. return new THREE.Vector4( this.x, this.y, this.z, this.w );
  105. }
  106. };
粤ICP备19079148号