1
0

Vector3.html 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8" />
  5. <base href="../../../" />
  6. <script src="page.js"></script>
  7. <link type="text/css" rel="stylesheet" href="page.css" />
  8. </head>
  9. <body>
  10. <h1>[name]</h1>
  11. <p class="desc">
  12. Class representing a 3D [link:https://en.wikipedia.org/wiki/Vector_space vector].
  13. A 3D vector is an ordered triplet of numbers (labeled x, y, and
  14. z), which can be used to represent a number of things, such as:
  15. </p>
  16. <ul>
  17. <li>A point in 3D space.</li>
  18. <li>
  19. A direction and length in 3D space. In three.js the length will always
  20. be the [link:https://en.wikipedia.org/wiki/Euclidean_distance Euclidean distance]
  21. (straight-line distance) from `(0, 0, 0)` to `(x, y, z)` and
  22. the direction is also measured from `(0, 0, 0)` towards `(x, y, z)`.
  23. </li>
  24. <li>Any arbitrary ordered triplet of numbers.</li>
  25. </ul>
  26. <p>
  27. There are other things a 3D vector can be used to represent, such as
  28. momentum vectors and so on, however these are the most common uses in
  29. three.js.
  30. </p>
  31. <p>
  32. Iterating through a [name] instance will yield its components `(x, y, z)`
  33. in the corresponding order.
  34. </p>
  35. <h2>Code Example</h2>
  36. <code>
  37. const a = new THREE.Vector3( 0, 1, 0 );
  38. //no arguments; will be initialised to (0, 0, 0)
  39. const b = new THREE.Vector3( );
  40. const d = a.distanceTo( b );
  41. </code>
  42. <h2>Constructor</h2>
  43. <h3>[name]( [param:Float x], [param:Float y], [param:Float z] )</h3>
  44. <p>
  45. [page:Float x] - the x value of this vector. Default is `0`.<br />
  46. [page:Float y] - the y value of this vector. Default is `0`.<br />
  47. [page:Float z] - the z value of this vector. Default is `0`.<br /><br />
  48. Creates a new [name].
  49. </p>
  50. <h2>Properties</h2>
  51. <h3>[property:Boolean isVector3]</h3>
  52. <p>Read-only flag to check if a given object is of type [name].</p>
  53. <h3>[property:Float x]</h3>
  54. <h3>[property:Float y]</h3>
  55. <h3>[property:Float z]</h3>
  56. <h2>Methods</h2>
  57. <h3>[method:this add]( [param:Vector3 v] )</h3>
  58. <p>Adds [page:Vector3 v] to this vector.</p>
  59. <h3>[method:this addScalar]( [param:Float s] )</h3>
  60. <p>
  61. Adds the scalar value s to this vector's [page:.x x], [page:.y y] and
  62. [page:.z z] values.
  63. </p>
  64. <h3>[method:this addScaledVector]( [param:Vector3 v], [param:Float s] )</h3>
  65. <p>
  66. Adds the multiple of [page:Vector3 v] and [page:Float s] to this vector.
  67. </p>
  68. <h3>[method:this addVectors]( [param:Vector3 a], [param:Vector3 b] )</h3>
  69. <p>Sets this vector to [page:Vector3 a] + [page:Vector3 b].</p>
  70. <h3>
  71. [method:this applyAxisAngle]( [param:Vector3 axis], [param:Float angle] )
  72. </h3>
  73. <p>
  74. [page:Vector3 axis] - A normalized [page:Vector3].<br />
  75. [page:Float angle] - An angle in radians.<br /><br />
  76. Applies a rotation specified by an axis and an angle to this vector.
  77. </p>
  78. <h3>[method:this applyEuler]( [param:Euler euler] )</h3>
  79. <p>
  80. Applies euler transform to this vector by converting the [page:Euler]
  81. object to a [page:Quaternion] and applying.
  82. </p>
  83. <h3>[method:this applyMatrix3]( [param:Matrix3 m] )</h3>
  84. <p>Multiplies this vector by [page:Matrix3 m]</p>
  85. <h3>[method:this applyMatrix4]( [param:Matrix4 m] )</h3>
  86. <p>
  87. Multiplies this vector (with an implicit 1 in the 4th dimension) by m, and
  88. divides by perspective.
  89. </p>
  90. <h3>[method:this applyNormalMatrix]( [param:Matrix3 m] )</h3>
  91. <p>
  92. Multiplies this vector by normal matrix [page:Matrix3 m] and normalizes
  93. the result.
  94. </p>
  95. <h3>[method:this applyQuaternion]( [param:Quaternion quaternion] )</h3>
  96. <p>Applies a [page:Quaternion] transform to this vector.</p>
  97. <h3>[method:Float angleTo]( [param:Vector3 v] )</h3>
  98. <p>
  99. Returns the angle between this vector and vector [page:Vector3 v] in
  100. radians.
  101. </p>
  102. <h3>[method:this ceil]()</h3>
  103. <p>
  104. The [page:.x x], [page:.y y] and [page:.z z] components of this vector are
  105. rounded up to the nearest integer value.
  106. </p>
  107. <h3>[method:this clamp]( [param:Vector3 min], [param:Vector3 max] )</h3>
  108. <p>
  109. [page:Vector3 min] - the minimum [page:.x x], [page:.y y] and [page:.z z]
  110. values.<br />
  111. [page:Vector3 max] - the maximum [page:.x x], [page:.y y] and [page:.z z]
  112. values in the desired range<br /><br />
  113. If this vector's x, y or z value is greater than the max vector's x, y or
  114. z value, it is replaced by the corresponding value. <br /><br />
  115. If this vector's x, y or z value is less than the min vector's x, y or z
  116. value, it is replaced by the corresponding value.
  117. </p>
  118. <h3>[method:this clampLength]( [param:Float min], [param:Float max] )</h3>
  119. <p>
  120. [page:Float min] - the minimum value the length will be clamped to <br />
  121. [page:Float max] - the maximum value the length will be clamped to<br /><br />
  122. If this vector's length is greater than the max value, the vector will be
  123. scaled down so its length is the max value. <br /><br />
  124. If this vector's length is less than the min value, the vector will be
  125. scaled up so its length is the min value.
  126. </p>
  127. <h3>[method:this clampScalar]( [param:Float min], [param:Float max] )</h3>
  128. <p>
  129. [page:Float min] - the minimum value the components will be clamped to
  130. <br />
  131. [page:Float max] - the maximum value the components will be clamped to<br /><br />
  132. If this vector's x, y or z values are greater than the max value, they are
  133. replaced by the max value. <br /><br />
  134. If this vector's x, y or z values are less than the min value, they are
  135. replaced by the min value.
  136. </p>
  137. <h3>[method:Vector3 clone]()</h3>
  138. <p>
  139. Returns a new vector3 with the same [page:.x x], [page:.y y]
  140. and [page:.z z] values as this one.
  141. </p>
  142. <h3>[method:this copy]( [param:Vector3 v] )</h3>
  143. <p>
  144. Copies the values of the passed vector3's [page:.x x], [page:.y y] and
  145. [page:.z z] properties to this vector3.
  146. </p>
  147. <h3>[method:this cross]( [param:Vector3 v] )</h3>
  148. <p>
  149. Sets this vector to [link:https://en.wikipedia.org/wiki/Cross_product cross product]
  150. of itself and [page:Vector3 v].
  151. </p>
  152. <h3>[method:this crossVectors]( [param:Vector3 a], [param:Vector3 b] )</h3>
  153. <p>
  154. Sets this vector to [link:https://en.wikipedia.org/wiki/Cross_product cross product]
  155. of [page:Vector3 a] and [page:Vector3 b].
  156. </p>
  157. <h3>[method:Float distanceTo]( [param:Vector3 v] )</h3>
  158. <p>Computes the distance from this vector to [page:Vector3 v].</p>
  159. <h3>[method:Float manhattanDistanceTo]( [param:Vector3 v] )</h3>
  160. <p>
  161. Computes the [link:https://en.wikipedia.org/wiki/Taxicab_geometry Manhattan distance]
  162. from this vector to [page:Vector3 v].
  163. </p>
  164. <h3>[method:Float distanceToSquared]( [param:Vector3 v] )</h3>
  165. <p>
  166. Computes the squared distance from this vector to [page:Vector3 v]. If you
  167. are just comparing the distance with another distance, you should compare
  168. the distance squared instead as it is slightly more efficient to
  169. calculate.
  170. </p>
  171. <h3>[method:this divide]( [param:Vector3 v] )</h3>
  172. <p>Divides this vector by [page:Vector3 v].</p>
  173. <h3>[method:this divideScalar]( [param:Float s] )</h3>
  174. <p>Divides this vector by scalar [page:Float s].</p>
  175. <h3>[method:Float dot]( [param:Vector3 v] )</h3>
  176. <p>
  177. Calculate the [link:https://en.wikipedia.org/wiki/Dot_product dot product]
  178. of this vector and [page:Vector3 v].
  179. </p>
  180. <h3>[method:Boolean equals]( [param:Vector3 v] )</h3>
  181. <p>
  182. Returns `true` if the components of this vector and [page:Vector3 v] are
  183. strictly equal; `false` otherwise.
  184. </p>
  185. <h3>[method:this floor]()</h3>
  186. <p>
  187. The components of this vector are rounded down to the nearest integer
  188. value.
  189. </p>
  190. <h3>
  191. [method:this fromArray]( [param:Array array], [param:Integer offset] )
  192. </h3>
  193. <p>
  194. [page:Array array] - the source array.<br />
  195. [page:Integer offset] - ( optional) offset into the array. Default is
  196. 0.<br /><br />
  197. Sets this vector's [page:.x x] value to be `array[ offset + 0 ]`, [page:.y y]
  198. value to be `array[ offset + 1 ]` and [page:.z z] value to be `array[ offset + 2 ]`.
  199. </p>
  200. <h3>
  201. [method:this fromBufferAttribute]( [param:BufferAttribute attribute], [param:Integer index] )
  202. </h3>
  203. <p>
  204. [page:BufferAttribute attribute] - the source attribute.<br />
  205. [page:Integer index] - index in the attribute.<br /><br />
  206. Sets this vector's [page:.x x], [page:.y y] and [page:.z z] values from
  207. the [page:BufferAttribute attribute].
  208. </p>
  209. <h3>[method:Float getComponent]( [param:Integer index] )</h3>
  210. <p>
  211. [page:Integer index] - `0`, `1` or `2`.<br /><br />
  212. If index equals `0` returns the [page:.x x] value. <br />
  213. If index equals `1` returns the [page:.y y] value. <br />
  214. If index equals `2` returns the [page:.z z] value.
  215. </p>
  216. <h3>[method:Float length]()</h3>
  217. <p>
  218. Computes the [link:https://en.wikipedia.org/wiki/Euclidean_distance Euclidean length]
  219. (straight-line length) from (0, 0, 0) to (x, y, z).
  220. </p>
  221. <h3>[method:Float manhattanLength]()</h3>
  222. <p>
  223. Computes the [link:http://en.wikipedia.org/wiki/Taxicab_geometry Manhattan length]
  224. of this vector.
  225. </p>
  226. <h3>[method:Float lengthSq]()</h3>
  227. <p>
  228. Computes the square of the
  229. [link:https://en.wikipedia.org/wiki/Euclidean_distance Euclidean length]
  230. (straight-line length) from (0, 0, 0) to (x, y, z). If you are comparing
  231. the lengths of vectors, you should compare the length squared instead as
  232. it is slightly more efficient to calculate.
  233. </p>
  234. <h3>[method:this lerp]( [param:Vector3 v], [param:Float alpha] )</h3>
  235. <p>
  236. [page:Vector3 v] - [page:Vector3] to interpolate towards.<br />
  237. [page:Float alpha] - interpolation factor, typically in the closed
  238. interval `[0, 1]`.<br /><br />
  239. Linearly interpolate between this vector and [page:Vector3 v], where alpha
  240. is the percent distance along the line - alpha = 0 will be this vector,
  241. and alpha = 1 will be [page:Vector3 v].
  242. </p>
  243. <h3>
  244. [method:this lerpVectors]( [param:Vector3 v1], [param:Vector3 v2], [param:Float alpha] )
  245. </h3>
  246. <p>
  247. [page:Vector3 v1] - the starting [page:Vector3].<br />
  248. [page:Vector3 v2] - [page:Vector3] to interpolate towards.<br />
  249. [page:Float alpha] - interpolation factor, typically in the closed
  250. interval `[0, 1]`.<br /><br />
  251. Sets this vector to be the vector linearly interpolated between
  252. [page:Vector3 v1] and [page:Vector3 v2] where alpha is the percent
  253. distance along the line connecting the two vectors - alpha = 0 will be
  254. [page:Vector3 v1], and alpha = 1 will be [page:Vector3 v2].
  255. </p>
  256. <h3>[method:this max]( [param:Vector3 v] )</h3>
  257. <p>
  258. If this vector's x, y or z value is less than [page:Vector3 v]'s x, y or z
  259. value, replace that value with the corresponding max value.
  260. </p>
  261. <h3>[method:this min]( [param:Vector3 v] )</h3>
  262. <p>
  263. If this vector's x, y or z value is greater than [page:Vector3 v]'s x, y
  264. or z value, replace that value with the corresponding min value.
  265. </p>
  266. <h3>[method:this multiply]( [param:Vector3 v] )</h3>
  267. <p>Multiplies this vector by [page:Vector3 v].</p>
  268. <h3>[method:this multiplyScalar]( [param:Float s] )</h3>
  269. <p>Multiplies this vector by scalar [page:Float s].</p>
  270. <h3>
  271. [method:this multiplyVectors]( [param:Vector3 a], [param:Vector3 b] )
  272. </h3>
  273. <p>
  274. Sets this vector equal to [page:Vector3 a] * [page:Vector3 b],
  275. component-wise.
  276. </p>
  277. <h3>[method:this negate]()</h3>
  278. <p>Inverts this vector - i.e. sets x = -x, y = -y and z = -z.</p>
  279. <h3>[method:this normalize]()</h3>
  280. <p>
  281. Convert this vector to a [link:https://en.wikipedia.org/wiki/Unit_vector unit vector]
  282. - that is, sets it equal to a vector with the same direction
  283. as this one, but [page:.length length] 1.
  284. </p>
  285. <h3>[method:this project]( [param:Camera camera] )</h3>
  286. <p>
  287. [page:Camera camera] — camera to use in the projection.<br /><br />
  288. Projects this vector from world space into the camera's normalized device
  289. coordinate (NDC) space.
  290. </p>
  291. <h3>[method:this projectOnPlane]( [param:Vector3 planeNormal] )</h3>
  292. <p>
  293. [page:Vector3 planeNormal] - A vector representing a plane normal.<br /><br />
  294. [link:https://en.wikipedia.org/wiki/Vector_projection Projects] this
  295. vector onto a plane by subtracting this vector projected onto the plane's
  296. normal from this vector.
  297. </p>
  298. <h3>[method:this projectOnVector]( [param:Vector3 v] )</h3>
  299. <p>
  300. [link:https://en.wikipedia.org/wiki/Vector_projection Projects] this
  301. vector onto [page:Vector3 v].
  302. </p>
  303. <h3>[method:this reflect]( [param:Vector3 normal] )</h3>
  304. <p>
  305. [page:Vector3 normal] - the normal to the reflecting plane<br /><br />
  306. Reflect this vector off of plane orthogonal to [page:Vector3 normal].
  307. Normal is assumed to have unit length.
  308. </p>
  309. <h3>[method:this round]()</h3>
  310. <p>
  311. The components of this vector are rounded to the nearest integer value.
  312. </p>
  313. <h3>[method:this roundToZero]()</h3>
  314. <p>
  315. The components of this vector are rounded towards zero (up if negative,
  316. down if positive) to an integer value.
  317. </p>
  318. <h3>
  319. [method:this set]( [param:Float x], [param:Float y], [param:Float z] )
  320. </h3>
  321. <p>
  322. Sets the [page:.x x], [page:.y y] and [page:.z z] components of this
  323. vector.
  324. </p>
  325. <h3>
  326. [method:this setComponent]( [param:Integer index], [param:Float value] )
  327. </h3>
  328. <p>
  329. [page:Integer index] - `0`, `1` or `2`.<br />
  330. [page:Float value] - [page:Float]<br /><br />
  331. If index equals `0` set [page:.x x] to [page:Float value].<br />
  332. If index equals `1` set [page:.y y] to [page:Float value].<br />
  333. If index equals `2` set [page:.z z] to [page:Float value]
  334. </p>
  335. <h3>[method:this setFromColor]( [param:Color color] )</h3>
  336. <p>
  337. Sets this vector's [page:.x x], [page:.y y] and [page:.z z] components
  338. from the r, g, and b components of the specified [page:Color color].
  339. </p>
  340. <h3>[method:this setFromCylindrical]( [param:Cylindrical c] )</h3>
  341. <p>
  342. Sets this vector from the cylindrical coordinates [page:Cylindrical c].
  343. </p>
  344. <h3>
  345. [method:this setFromCylindricalCoords]( [param:Float radius], [param:Float theta], [param:Float y] )
  346. </h3>
  347. <p>
  348. Sets this vector from the cylindrical coordinates [page:Cylindrical radius],
  349. [page:Cylindrical theta] and [page:Cylindrical y].
  350. </p>
  351. <h3>[method:this setFromEuler]( [param:Euler euler] )</h3>
  352. <p>
  353. Sets this vector's [page:.x x], [page:.y y] and [page:.z z] components
  354. from the x, y, and z components of the specified [page:Euler Euler Angle].
  355. </p>
  356. <h3>
  357. [method:this setFromMatrixColumn]( [param:Matrix4 matrix], [param:Integer index] )
  358. </h3>
  359. <p>
  360. Sets this vector's [page:.x x], [page:.y y] and [page:.z z] components
  361. from [page:Integer index] column of [page:Matrix4 matrix].
  362. </p>
  363. <h3>
  364. [method:this setFromMatrix3Column]( [param:Matrix3 matrix], [param:Integer index] )
  365. </h3>
  366. <p>
  367. Sets this vector's [page:.x x], [page:.y y] and [page:.z z] components
  368. from [page:Integer index] column of [page:Matrix3 matrix].
  369. </p>
  370. <h3>[method:this setFromMatrixPosition]( [param:Matrix4 m] )</h3>
  371. <p>
  372. Sets this vector to the position elements of the
  373. [link:https://en.wikipedia.org/wiki/Transformation_matrix transformation matrix] [page:Matrix4 m].
  374. </p>
  375. <h3>[method:this setFromMatrixScale]( [param:Matrix4 m] )</h3>
  376. <p>
  377. Sets this vector to the scale elements of the
  378. [link:https://en.wikipedia.org/wiki/Transformation_matrix transformation matrix] [page:Matrix4 m].
  379. </p>
  380. <h3>[method:this setFromSpherical]( [param:Spherical s] )</h3>
  381. <p>Sets this vector from the spherical coordinates [page:Spherical s].</p>
  382. <h3>
  383. [method:this setFromSphericalCoords]( [param:Float radius], [param:Float phi], [param:Float theta] )
  384. </h3>
  385. <p>
  386. Sets this vector from the spherical coordinates [page:Spherical radius],
  387. [page:Spherical phi] and [page:Spherical theta].
  388. </p>
  389. <h3>[method:this setLength]( [param:Float l] )</h3>
  390. <p>
  391. Set this vector to a vector with the same direction as this one, but
  392. [page:.length length] [page:Float l].
  393. </p>
  394. <h3>[method:this setScalar]( [param:Float scalar] )</h3>
  395. <p>
  396. Set the [page:.x x], [page:.y y] and [page:.z z] values of this vector
  397. both equal to [page:Float scalar].
  398. </p>
  399. <h3>[method:this setX]( [param:Float x] )</h3>
  400. <p>Replace this vector's [page:.x x] value with [page:Float x].</p>
  401. <h3>[method:this setY]( [param:Float y] )</h3>
  402. <p>Replace this vector's [page:.y y] value with [page:Float y].</p>
  403. <h3>[method:this setZ]( [param:Float z] )</h3>
  404. <p>Replace this vector's [page:.z z] value with [page:Float z].</p>
  405. <h3>[method:this sub]( [param:Vector3 v] )</h3>
  406. <p>Subtracts [page:Vector3 v] from this vector.</p>
  407. <h3>[method:this subScalar]( [param:Float s] )</h3>
  408. <p>
  409. Subtracts [page:Float s] from this vector's [page:.x x], [page:.y y] and
  410. [page:.z z] components.
  411. </p>
  412. <h3>[method:this subVectors]( [param:Vector3 a], [param:Vector3 b] )</h3>
  413. <p>Sets this vector to [page:Vector3 a] - [page:Vector3 b].</p>
  414. <h3>
  415. [method:Array toArray]( [param:Array array], [param:Integer offset] )
  416. </h3>
  417. <p>
  418. [page:Array array] - (optional) array to store this vector to. If this is
  419. not provided a new array will be created.<br />
  420. [page:Integer offset] - (optional) optional offset into the array.<br /><br />
  421. Returns an array [x, y, z], or copies x, y and z into the provided
  422. [page:Array array].
  423. </p>
  424. <h3>[method:this transformDirection]( [param:Matrix4 m] )</h3>
  425. <p>
  426. Transforms the direction of this vector by a matrix (the upper left 3 x 3
  427. subset of a [page:Matrix4 m]) and then [page:.normalize normalizes] the
  428. result.
  429. </p>
  430. <h3>[method:this unproject]( [param:Camera camera] )</h3>
  431. <p>
  432. [page:Camera camera] — camera to use in the projection.<br /><br />
  433. Projects this vector from the camera's normalized device coordinate (NDC)
  434. space into world space.
  435. </p>
  436. <h3>[method:this random]()</h3>
  437. <p>
  438. Sets each component of this vector to a pseudo-random value between `0` and
  439. `1`, excluding `1`.
  440. </p>
  441. <h3>[method:this randomDirection]()</h3>
  442. <p>Sets this vector to a uniformly random point on a unit sphere.</p>
  443. <h2>Source</h2>
  444. <p>
  445. [link:https://github.com/mrdoob/three.js/blob/master/src/[path].js src/[path].js]
  446. </p>
  447. </body>
  448. </html>