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