Vector3.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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. */
  7. THREE.Vector3 = function ( x, y, z ) {
  8. this.set(
  9. x || 0,
  10. y || 0,
  11. z || 0
  12. );
  13. };
  14. THREE.Vector3.prototype = {
  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.set(
  23. v.x,
  24. v.y,
  25. v.z
  26. );
  27. return this;
  28. },
  29. add : function ( a, b ) {
  30. this.set(
  31. a.x + b.x,
  32. a.y + b.y,
  33. a.z + b.z
  34. );
  35. return this;
  36. },
  37. addSelf : function ( v ) {
  38. this.set(
  39. this.x + v.x,
  40. this.y + v.y,
  41. this.z + v.z
  42. );
  43. return this;
  44. },
  45. addScalar : function ( s ) {
  46. this.set(
  47. this.x + s,
  48. this.y + s,
  49. this.z + s
  50. );
  51. return this;
  52. },
  53. sub : function ( a, b ) {
  54. this.set(
  55. a.x - b.x,
  56. a.y - b.y,
  57. a.z - b.z
  58. );
  59. return this;
  60. },
  61. subSelf : function ( v ) {
  62. this.set(
  63. this.x - v.x,
  64. this.y - v.y,
  65. this.z - v.z
  66. );
  67. return this;
  68. },
  69. cross : function ( a, b ) {
  70. this.set(
  71. a.y * b.z - a.z * b.y,
  72. a.z * b.x - a.x * b.z,
  73. a.x * b.y - a.y * b.x
  74. );
  75. return this;
  76. },
  77. crossSelf : function ( v ) {
  78. var tx = this.x, ty = this.y, tz = this.z;
  79. this.set(
  80. ty * v.z - tz * v.y,
  81. tz * v.x - tx * v.z,
  82. tx * v.y - ty * v.x
  83. );
  84. return this;
  85. },
  86. multiply : function ( a, b ) {
  87. this.set(
  88. a.x * b.x,
  89. a.y * b.y,
  90. a.z * b.z
  91. );
  92. return this;
  93. },
  94. multiplySelf : function ( v ) {
  95. this.set(
  96. this.x * v.x,
  97. this.y * v.y,
  98. this.z * v.z
  99. );
  100. return this;
  101. },
  102. multiplyScalar : function ( s ) {
  103. this.set(
  104. this.x * s,
  105. this.y * s,
  106. this.z * s
  107. );
  108. return this;
  109. },
  110. divideSelf : function ( v ) {
  111. this.set(
  112. this.x / v.x,
  113. this.y / v.y,
  114. this.z / v.z
  115. );
  116. return this;
  117. },
  118. divideScalar : function ( s ) {
  119. this.set(
  120. this.x / s,
  121. this.y / s,
  122. this.z / s
  123. );
  124. return this;
  125. },
  126. negate : function () {
  127. this.set(
  128. - this.x,
  129. - this.y,
  130. - this.z
  131. );
  132. return this;
  133. },
  134. dot : function ( v ) {
  135. return this.x * v.x + this.y * v.y + this.z * v.z;
  136. },
  137. distanceTo : function ( v ) {
  138. return Math.sqrt( this.distanceToSquared( v ) );
  139. },
  140. distanceToSquared : function ( v ) {
  141. var dx = this.x - v.x, dy = this.y - v.y, dz = this.z - v.z;
  142. return dx * dx + dy * dy + dz * dz;
  143. },
  144. length : function () {
  145. return Math.sqrt( this.lengthSq() );
  146. },
  147. lengthSq : function () {
  148. return this.x * this.x + this.y * this.y + this.z * this.z;
  149. },
  150. lengthManhattan : function () {
  151. return this.x + this.y + this.z;
  152. },
  153. normalize : function () {
  154. var l = this.length();
  155. l > 0 ? this.multiplyScalar( 1 / l ) : this.set( 0, 0, 0 );
  156. return this;
  157. },
  158. setPositionFromMatrix : function ( m ) {
  159. this.x = m.n14;
  160. this.y = m.n24;
  161. this.z = m.n34;
  162. },
  163. setRotationFromMatrix : function ( m ) {
  164. this.y = Math.asin( m.n13 );
  165. var cosY = Math.cos( this.y );
  166. if ( Math.abs( cosY ) > 0.00001 ) {
  167. this.x = Math.atan2( - m.n23 / cosY, m.n33 / cosY );
  168. this.z = Math.atan2( - m.n13 / cosY, m.n11 / cosY );
  169. } else {
  170. this.x = 0;
  171. this.z = Math.atan2( m.n21, m.n22 );
  172. }
  173. },
  174. setLength : function ( l ) {
  175. return this.normalize().multiplyScalar( l );
  176. },
  177. isZero : function () {
  178. var almostZero = 0.0001;
  179. return ( Math.abs( this.x ) < almostZero ) && ( Math.abs( this.y ) < almostZero ) && ( Math.abs( this.z ) < almostZero );
  180. },
  181. clone : function () {
  182. return new THREE.Vector3( this.x, this.y, this.z );
  183. }
  184. };