Vector3.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. "use strict";
  2. function Vector3(x, y, z)
  3. {
  4. this.x = x || 0;
  5. this.y = y || 0;
  6. this.z = z || 0;
  7. }
  8. Object.assign(Vector3.prototype,
  9. {
  10. set: function(x, y, z)
  11. {
  12. this.x = x;
  13. this.y = y;
  14. this.z = z;
  15. },
  16. setScalar: function(scalar)
  17. {
  18. this.x = scalar;
  19. this.y = scalar;
  20. this.z = scalar;
  21. },
  22. clone: function()
  23. {
  24. return new Vector3(this.x, this.y, this.z);
  25. },
  26. copy: function(v)
  27. {
  28. this.x = v.x;
  29. this.y = v.y;
  30. this.z = v.z;
  31. },
  32. add: function(v, w)
  33. {
  34. this.x += v.x;
  35. this.y += v.y;
  36. this.z += v.z;
  37. },
  38. addScalar: function(s)
  39. {
  40. this.x += s;
  41. this.y += s;
  42. this.z += s;
  43. },
  44. addVectors: function(a, b)
  45. {
  46. this.x = a.x + b.x;
  47. this.y = a.y + b.y;
  48. this.z = a.z + b.z;
  49. },
  50. addScaledVector: function(v, s)
  51. {
  52. this.x += v.x * s;
  53. this.y += v.y * s;
  54. this.z += v.z * s;
  55. },
  56. sub: function(v, w)
  57. {
  58. this.x -= v.x;
  59. this.y -= v.y;
  60. this.z -= v.z;
  61. },
  62. subScalar: function(s)
  63. {
  64. this.x -= s;
  65. this.y -= s;
  66. this.z -= s;
  67. },
  68. subVectors: function(a, b)
  69. {
  70. this.x = a.x - b.x;
  71. this.y = a.y - b.y;
  72. this.z = a.z - b.z;
  73. },
  74. multiply: function(v, w)
  75. {
  76. this.x *= v.x;
  77. this.y *= v.y;
  78. this.z *= v.z;
  79. },
  80. multiplyScalar: function(scalar)
  81. {
  82. this.x *= scalar;
  83. this.y *= scalar;
  84. this.z *= scalar;
  85. },
  86. multiplyVectors: function(a, b)
  87. {
  88. this.x = a.x * b.x;
  89. this.y = a.y * b.y;
  90. this.z = a.z * b.z;
  91. },
  92. divide: function(v)
  93. {
  94. this.x /= v.x;
  95. this.y /= v.y;
  96. this.z /= v.z;
  97. },
  98. divideScalar: function(scalar)
  99. {
  100. return this.multiplyScalar(1 / scalar);
  101. },
  102. min: function(v)
  103. {
  104. this.x = Math.min(this.x, v.x);
  105. this.y = Math.min(this.y, v.y);
  106. this.z = Math.min(this.z, v.z);
  107. },
  108. max: function(v)
  109. {
  110. this.x = Math.max(this.x, v.x);
  111. this.y = Math.max(this.y, v.y);
  112. this.z = Math.max(this.z, v.z);
  113. },
  114. clamp: function(min, max)
  115. {
  116. // assumes min < max, componentwise
  117. this.x = Math.max(min.x, Math.min(max.x, this.x));
  118. this.y = Math.max(min.y, Math.min(max.y, this.y));
  119. this.z = Math.max(min.z, Math.min(max.z, this.z));
  120. },
  121. clampScalar: function(minVal, maxVal)
  122. {
  123. this.x = Math.max(minVal, Math.min(maxVal, this.x));
  124. this.y = Math.max(minVal, Math.min(maxVal, this.y));
  125. this.z = Math.max(minVal, Math.min(maxVal, this.z));
  126. },
  127. clampLength: function(min, max)
  128. {
  129. var length = this.length();
  130. return this.divideScalar(length || 1).multiplyScalar(Math.max(min, Math.min(max, length)));
  131. },
  132. floor: function()
  133. {
  134. this.x = Math.floor(this.x);
  135. this.y = Math.floor(this.y);
  136. this.z = Math.floor(this.z);
  137. },
  138. ceil: function()
  139. {
  140. this.x = Math.ceil(this.x);
  141. this.y = Math.ceil(this.y);
  142. this.z = Math.ceil(this.z);
  143. },
  144. round: function()
  145. {
  146. this.x = Math.round(this.x);
  147. this.y = Math.round(this.y);
  148. this.z = Math.round(this.z);
  149. },
  150. roundToZero: function()
  151. {
  152. this.x = (this.x < 0) ? Math.ceil(this.x) : Math.floor(this.x);
  153. this.y = (this.y < 0) ? Math.ceil(this.y) : Math.floor(this.y);
  154. this.z = (this.z < 0) ? Math.ceil(this.z) : Math.floor(this.z);
  155. },
  156. negate: function()
  157. {
  158. this.x = -this.x;
  159. this.y = -this.y;
  160. this.z = -this.z;
  161. },
  162. dot: function(v)
  163. {
  164. return this.x * v.x + this.y * v.y + this.z * v.z;
  165. },
  166. lengthSquared: function()
  167. {
  168. return this.x * this.x + this.y * this.y + this.z * this.z;
  169. },
  170. length: function()
  171. {
  172. return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z);
  173. },
  174. manhattanLength: function()
  175. {
  176. return Math.abs(this.x) + Math.abs(this.y) + Math.abs(this.z);
  177. },
  178. normalize: function()
  179. {
  180. return this.divideScalar(this.length() || 1);
  181. },
  182. setLength: function(length)
  183. {
  184. return this.normalize().multiplyScalar(length);
  185. },
  186. lerp: function(v, alpha)
  187. {
  188. this.x += (v.x - this.x) * alpha;
  189. this.y += (v.y - this.y) * alpha;
  190. this.z += (v.z - this.z) * alpha;
  191. },
  192. lerpVectors: function(v1, v2, alpha)
  193. {
  194. return this.subVectors(v2, v1).multiplyScalar(alpha).add(v1);
  195. },
  196. cross: function(v, w)
  197. {
  198. return this.crossVectors(this, v);
  199. },
  200. crossVectors: function(a, b)
  201. {
  202. var ax = a.x,
  203. ay = a.y,
  204. az = a.z;
  205. var bx = b.x,
  206. by = b.y,
  207. bz = b.z;
  208. this.x = ay * bz - az * by;
  209. this.y = az * bx - ax * bz;
  210. this.z = ax * by - ay * bx;
  211. },
  212. projectOnVector: function(vector)
  213. {
  214. var scalar = vector.dot(this) / vector.lengthSq();
  215. return this.copy(vector).multiplyScalar(scalar);
  216. },
  217. /**
  218. * Reflect incident vector off plane orthogonal to normal, normal is assumed to have unit length.
  219. */
  220. reflect: function()
  221. {
  222. var v1 = new Vector3();
  223. return function reflect(normal)
  224. {
  225. return this.sub(v1.copy(normal).multiplyScalar(2 * this.dot(normal)));
  226. };
  227. }(),
  228. angleTo: function(v)
  229. {
  230. var theta = this.dot(v) / (Math.sqrt(this.lengthSq() * v.lengthSq()));
  231. return Math.acos(theta);
  232. },
  233. distanceTo: function(v)
  234. {
  235. return Math.sqrt(this.distanceToSquared(v));
  236. },
  237. distanceToSquared: function(v)
  238. {
  239. var dx = this.x - v.x,
  240. dy = this.y - v.y,
  241. dz = this.z - v.z;
  242. return dx * dx + dy * dy + dz * dz;
  243. },
  244. manhattanDistanceTo: function(v)
  245. {
  246. return Math.abs(this.x - v.x) + Math.abs(this.y - v.y) + Math.abs(this.z - v.z);
  247. },
  248. setFromSpherical: function(s)
  249. {
  250. return this.setFromSphericalCoords(s.radius, s.phi, s.theta);
  251. },
  252. setFromSphericalCoords: function(radius, phi, theta)
  253. {
  254. var sinPhiRadius = Math.sin(phi) * radius;
  255. this.x = sinPhiRadius * Math.sin(theta);
  256. this.y = Math.cos(phi) * radius;
  257. this.z = sinPhiRadius * Math.cos(theta);
  258. },
  259. setFromCylindrical: function(c)
  260. {
  261. return this.setFromCylindricalCoords(c.radius, c.theta, c.y);
  262. },
  263. setFromCylindricalCoords: function(radius, theta, y)
  264. {
  265. this.x = radius * Math.sin(theta);
  266. this.y = y;
  267. this.z = radius * Math.cos(theta);
  268. },
  269. setFromMatrixPosition: function(m)
  270. {
  271. var e = m.elements;
  272. this.x = e[12];
  273. this.y = e[13];
  274. this.z = e[14];
  275. },
  276. equals: function(v)
  277. {
  278. return ((v.x === this.x) && (v.y === this.y) && (v.z === this.z));
  279. },
  280. fromArray: function(array, offset)
  281. {
  282. if(offset === undefined) offset = 0;
  283. this.x = array[offset];
  284. this.y = array[offset + 1];
  285. this.z = array[offset + 2];
  286. },
  287. toArray: function(array, offset)
  288. {
  289. if(array === undefined) array = [];
  290. if(offset === undefined) offset = 0;
  291. array[offset] = this.x;
  292. array[offset + 1] = this.y;
  293. array[offset + 2] = this.z;
  294. return array;
  295. }
  296. });
  297. //export {Vector3};