Vector3.js 4.1 KB

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