Vector3.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /**
  2. * @author mr.doob / http://mrdoob.com/
  3. * @author kile / http://kile.stravaganza.org/
  4. * @author philogb / http://blog.thejit.org/
  5. */
  6. THREE.Vector3 = function ( x, y, z ) {
  7. this.x = x || 0;
  8. this.y = y || 0;
  9. this.z = z || 0;
  10. };
  11. THREE.Vector3.prototype = {
  12. set: function ( x, y, z ) {
  13. this.x = x;
  14. this.y = y;
  15. this.z = z;
  16. return this;
  17. },
  18. copy: function ( v ) {
  19. this.x = v.x;
  20. this.y = v.y;
  21. this.z = v.z;
  22. return this;
  23. },
  24. add: function ( a, b ) {
  25. this.x = a.x + b.x;
  26. this.y = a.y + b.y;
  27. this.z = a.z + b.z;
  28. return this;
  29. },
  30. addSelf: function ( v ) {
  31. this.x += v.x;
  32. this.y += v.y;
  33. this.z += v.z;
  34. return this;
  35. },
  36. addScalar: function ( s ) {
  37. this.x += s;
  38. this.y += s;
  39. this.z += s;
  40. return this;
  41. },
  42. sub: function( a, b ) {
  43. this.x = a.x - b.x;
  44. this.y = a.y - b.y;
  45. this.z = a.z - b.z;
  46. return this;
  47. },
  48. subSelf: function ( v ) {
  49. this.x -= v.x;
  50. this.y -= v.y;
  51. this.z -= v.z;
  52. return this;
  53. },
  54. cross: function ( a, b ) {
  55. this.x = a.y * b.z - a.z * b.y;
  56. this.y = a.z * b.x - a.x * b.z;
  57. this.z = a.x * b.y - a.y * b.x;
  58. return this;
  59. },
  60. crossSelf: function ( v ) {
  61. var tx = this.x, ty = this.y, tz = this.z;
  62. this.x = ty * v.z - tz * v.y;
  63. this.y = tz * v.x - tx * v.z;
  64. this.z = tx * v.y - ty * v.x;
  65. return this;
  66. },
  67. multiply: function ( a, b ) {
  68. this.x = a.x * b.x;
  69. this.y = a.y * b.y;
  70. this.z = a.z * b.z;
  71. return this;
  72. },
  73. multiplySelf: function ( v ) {
  74. this.x *= v.x;
  75. this.y *= v.y;
  76. this.z *= v.z;
  77. return this;
  78. },
  79. multiplyScalar: function ( s ) {
  80. this.x *= s;
  81. this.y *= s;
  82. this.z *= s;
  83. return this;
  84. },
  85. divideSelf: function ( v ) {
  86. this.x /= v.x;
  87. this.y /= v.y;
  88. this.z /= v.z;
  89. return this;
  90. },
  91. divideScalar: function ( s ) {
  92. this.x /= s;
  93. this.y /= s;
  94. this.z /= s;
  95. return this;
  96. },
  97. dot: function ( v ) {
  98. return this.x * v.x + this.y * v.y + this.z * v.z;
  99. },
  100. distanceTo: function ( v ) {
  101. var dx = this.x - v.x, dy = this.y - v.y, dz = this.z - v.z;
  102. return Math.sqrt( dx * dx + dy * dy + dz * dz );
  103. },
  104. distanceToSquared: function ( v ) {
  105. var dx = this.x - v.x, dy = this.y - v.y, dz = this.z - v.z;
  106. return dx * dx + dy * dy + dz * dz;
  107. },
  108. length: function () {
  109. return Math.sqrt( this.x * this.x + this.y * this.y + this.z * this.z );
  110. },
  111. lengthSq: function () {
  112. return this.x * this.x + this.y * this.y + this.z * this.z;
  113. },
  114. negate: function () {
  115. this.x = - this.x;
  116. this.y = - this.y;
  117. this.z = - this.z;
  118. return this;
  119. },
  120. normalize: function () {
  121. var length = Math.sqrt( this.x * this.x + this.y * this.y + this.z * this.z );
  122. length > 0 ? this.multiplyScalar( 1 / length ) : this.set( 0, 0, 0 );
  123. return this;
  124. },
  125. setLength: function( len ) {
  126. return this.normalize().multiplyScalar( len );
  127. },
  128. isZero: function () {
  129. var almostZero = 0.0001;
  130. return ( Math.abs( this.x ) < almostZero ) && ( Math.abs( this.y ) < almostZero ) && ( Math.abs( this.z ) < almostZero );
  131. },
  132. clone: function () {
  133. return new THREE.Vector3( this.x, this.y, this.z );
  134. },
  135. toString: function () {
  136. return 'THREE.Vector3 ( ' + this.x + ', ' + this.y + ', ' + this.z + ' )';
  137. }
  138. };