Vector3.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /**
  2. * @author mr.doob / http://mrdoob.com/
  3. * @author kile / http://kile.stravaganza.org/
  4. * @author philogb / http://blog.thejit.org/
  5. * @author mikael emtinger / http://gomo.se/
  6. * @author egraether / http://egraether.com/
  7. */
  8. THREE.Vector3 = function ( x, y, z ) {
  9. this.set(
  10. x || 0,
  11. y || 0,
  12. z || 0
  13. );
  14. };
  15. THREE.Vector3.prototype = {
  16. constructor: THREE.Vector3,
  17. set: function ( x, y, z ) {
  18. this.x = x;
  19. this.y = y;
  20. this.z = z;
  21. return this;
  22. },
  23. copy: function ( v ) {
  24. this.x = v.x;
  25. this.y = v.y;
  26. this.z = v.z;
  27. return this;
  28. },
  29. clone: function () {
  30. return new THREE.Vector3( this.x, this.y, this.z );
  31. },
  32. add: function ( v1, v2 ) {
  33. this.x = v1.x + v2.x;
  34. this.y = v1.y + v2.y;
  35. this.z = v1.z + v2.z;
  36. return this;
  37. },
  38. addSelf: function ( v ) {
  39. this.x += v.x;
  40. this.y += v.y;
  41. this.z += v.z;
  42. return this;
  43. },
  44. addScalar: function ( s ) {
  45. this.x += s;
  46. this.y += s;
  47. this.z += s;
  48. return this;
  49. },
  50. sub: function ( v1, v2 ) {
  51. this.x = v1.x - v2.x;
  52. this.y = v1.y - v2.y;
  53. this.z = v1.z - v2.z;
  54. return this;
  55. },
  56. subSelf: function ( v ) {
  57. this.x -= v.x;
  58. this.y -= v.y;
  59. this.z -= v.z;
  60. return this;
  61. },
  62. multiply: function ( a, b ) {
  63. this.x = a.x * b.x;
  64. this.y = a.y * b.y;
  65. this.z = a.z * b.z;
  66. return this;
  67. },
  68. multiplySelf: function ( v ) {
  69. this.x *= v.x;
  70. this.y *= v.y;
  71. this.z *= v.z;
  72. return this;
  73. },
  74. multiplyScalar: function ( s ) {
  75. this.x *= s;
  76. this.y *= s;
  77. this.z *= s;
  78. return this;
  79. },
  80. divideSelf: function ( v ) {
  81. return this.divide( this, v );
  82. },
  83. divideScalar: function ( s ) {
  84. if ( s ) {
  85. this.x /= s;
  86. this.y /= s;
  87. this.z /= s;
  88. } else {
  89. this.set( 0, 0, 0 );
  90. }
  91. return this;
  92. },
  93. negate: function() {
  94. return this.multiplyScalar( -1 );
  95. },
  96. dot: function ( v ) {
  97. return this.x * v.x + this.y * v.y + this.z * v.z;
  98. },
  99. lengthSq: function () {
  100. return this.x * this.x + this.y * this.y + this.z * this.z;
  101. },
  102. length: function () {
  103. return Math.sqrt( this.lengthSq() );
  104. },
  105. lengthManhattan: function () {
  106. // correct version
  107. // return Math.abs( this.x ) + Math.abs( this.y ) + Math.abs( this.z );
  108. return this.x + this.y + this.z;
  109. },
  110. normalize: function () {
  111. return this.divideScalar( this.length() );
  112. },
  113. setLength: function ( l ) {
  114. return this.normalize().multiplyScalar( l );
  115. },
  116. cross: function ( a, b ) {
  117. this.x = a.y * b.z - a.z * b.y;
  118. this.y = a.z * b.x - a.x * b.z;
  119. this.z = a.x * b.y - a.y * b.x;
  120. return this;
  121. },
  122. crossSelf: function ( v ) {
  123. return this.set(
  124. this.y * v.z - this.z * v.y,
  125. this.z * v.x - this.x * v.z,
  126. this.x * v.y - this.y * v.x
  127. );
  128. },
  129. distanceTo: function ( v ) {
  130. return Math.sqrt( this.distanceToSquared( v ) );
  131. },
  132. distanceToSquared: function ( v ) {
  133. return new THREE.Vector3().sub( this, v ).lengthSq();
  134. },
  135. setPositionFromMatrix: function ( m ) {
  136. this.x = m.n14;
  137. this.y = m.n24;
  138. this.z = m.n34;
  139. },
  140. setRotationFromMatrix: function ( m ) {
  141. var cosY = Math.cos( this.y );
  142. this.y = Math.asin( m.n13 );
  143. if ( Math.abs( cosY ) > 0.00001 ) {
  144. this.x = Math.atan2( - m.n23 / cosY, m.n33 / cosY );
  145. this.z = Math.atan2( - m.n12 / cosY, m.n11 / cosY );
  146. } else {
  147. this.x = 0;
  148. this.z = Math.atan2( m.n21, m.n22 );
  149. }
  150. },
  151. isZero: function () {
  152. return ( this.lengthSq() < 0.0001 /* almostZero */ );
  153. }
  154. };
粤ICP备19079148号