Vector3.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. set : function ( x, y, z ) {
  17. this.x = x;
  18. this.y = y;
  19. this.z = z;
  20. return this;
  21. },
  22. copy : function ( v ) {
  23. this.x = v.x;
  24. this.y = v.y;
  25. this.z = v.z;
  26. return this;
  27. },
  28. clone : function () {
  29. return new THREE.Vector3( this.x, this.y, this.z );
  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. return this;
  36. },
  37. addSelf : function ( v ) {
  38. this.x += v.x;
  39. this.y += v.y;
  40. this.z += v.z;
  41. return this;
  42. },
  43. addScalar : function ( s ) {
  44. this.x += s;
  45. this.y += s;
  46. this.z += s;
  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. return this;
  54. },
  55. subSelf : function ( v ) {
  56. this.x -= v.x;
  57. this.y -= v.y;
  58. this.z -= v.z;
  59. return this;
  60. },
  61. multiply : function ( a, b ) {
  62. this.x = a.x * b.x;
  63. this.y = a.y * b.y;
  64. this.z = a.z * b.z;
  65. return this;
  66. },
  67. multiplySelf : function ( v ) {
  68. this.x *= v.x;
  69. this.y *= v.y;
  70. this.z *= v.y;
  71. return this;
  72. },
  73. multiplyScalar : function ( s ) {
  74. this.x *= s;
  75. this.y *= s;
  76. this.z *= s;
  77. return this;
  78. },
  79. divideSelf : function ( v ) {
  80. return this.divide( this, v );
  81. },
  82. divideScalar : function ( s ) {
  83. if ( s ) {
  84. this.x /= s;
  85. this.y /= s;
  86. this.z /= s;
  87. } else {
  88. this.set( 0, 0, 0 );
  89. }
  90. return this;
  91. },
  92. negate : function() {
  93. return this.multiplyScalar( -1 );
  94. },
  95. dot : function ( v ) {
  96. return this.x * v.x + this.y * v.y + this.z * v.z;
  97. },
  98. lengthSq : function () {
  99. return this.x * this.x + this.y * this.y + this.z * this.z;
  100. },
  101. length : function () {
  102. return Math.sqrt( this.lengthSq() );
  103. },
  104. lengthManhattan : function () {
  105. // correct version
  106. // return Math.abs( this.x ) + Math.abs( this.y ) + Math.abs( this.z );
  107. return this.x + this.y + this.z;
  108. },
  109. normalize : function () {
  110. return this.divideScalar( this.length() );
  111. },
  112. setLength : function ( l ) {
  113. return this.normalize().multiplyScalar( l );
  114. },
  115. cross : function ( a, b ) {
  116. this.x = a.y * b.z - a.z * b.y;
  117. this.y = a.z * b.x - a.x * b.z;
  118. this.z = a.x * b.y - a.y * b.x;
  119. return this;
  120. },
  121. crossSelf : function ( v ) {
  122. return this.set(
  123. this.y * v.z - this.z * v.y,
  124. this.z * v.x - this.x * v.z,
  125. this.x * v.y - this.y * v.x
  126. );
  127. },
  128. distanceTo : function ( v ) {
  129. return Math.sqrt( this.distanceToSquared( v ) );
  130. },
  131. distanceToSquared : function ( v ) {
  132. return new THREE.Vector3().sub( this, v ).lengthSq();
  133. },
  134. setPositionFromMatrix : function ( m ) {
  135. this.x = m.n14;
  136. this.y = m.n24;
  137. this.z = m.n34;
  138. },
  139. setRotationFromMatrix : function ( m ) {
  140. var cosY = Math.cos( this.y );
  141. this.y = Math.asin( m.n13 );
  142. if ( Math.abs( cosY ) > 0.00001 ) {
  143. this.x = Math.atan2( - m.n23 / cosY, m.n33 / cosY );
  144. this.z = Math.atan2( - m.n12 / cosY, m.n11 / cosY );
  145. } else {
  146. this.x = 0;
  147. this.z = Math.atan2( m.n21, m.n22 );
  148. }
  149. },
  150. isZero : function () {
  151. return ( this.lengthSq() < 0.0001 /* almostZero */ );
  152. }
  153. };
粤ICP备19079148号