Vector4.js 2.1 KB

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