Vector4.html 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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 4D [link:https://en.wikipedia.org/wiki/Vector_space vector].
  13. A 4D vector is an ordered quadruplet of numbers (labeled x, y, z,
  14. and w), which can be used to represent a number of things, such as:
  15. </p>
  16. <ul>
  17. <li>A point in 4D space.</li>
  18. <li>
  19. A direction and length in 4D 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, 0)` to `(x, y, z, w)`
  22. and the direction is also measured from `(0, 0, 0, 0)` towards `(x, y,
  23. z, w)`.
  24. </li>
  25. <li>Any arbitrary ordered quadruplet of numbers.</li>
  26. </ul>
  27. <p>
  28. There are other things a 4D vector can be used to represent, however these
  29. are the most common uses in *three.js*.
  30. </p>
  31. <p>
  32. Iterating through a [name] instance will yield its components `(x, y, z,
  33. w)` in the corresponding order.
  34. </p>
  35. <h2>Code Example</h2>
  36. <code>
  37. const a = new THREE.Vector4( 0, 1, 0, 0 );
  38. //no arguments; will be initialised to (0, 0, 0, 1)
  39. const b = new THREE.Vector4( );
  40. const d = a.dot( b );
  41. </code>
  42. <h2>Constructor</h2>
  43. <h3>
  44. [name]( [param:Float x], [param:Float y], [param:Float z], [param:Float w] )
  45. </h3>
  46. <p>
  47. [page:Float x] - the x value of this vector. Default is `0`.<br />
  48. [page:Float y] - the y value of this vector. Default is `0`.<br />
  49. [page:Float z] - the z value of this vector. Default is `0`.<br />
  50. [page:Float w] - the w value of this vector. Default is `1`.<br /><br />
  51. Creates a new [name].
  52. </p>
  53. <h2>Properties</h2>
  54. <h3>[property:Boolean isVector4]</h3>
  55. <p>Read-only flag to check if a given object is of type [name].</p>
  56. <h3>[property:Float x]</h3>
  57. <h3>[property:Float y]</h3>
  58. <h3>[property:Float z]</h3>
  59. <h3>[property:Float w]</h3>
  60. <h3>[property:Float width]</h3>
  61. <p>Alias for [page:.z z].</p>
  62. <h3>[property:Float height]</h3>
  63. <p>Alias for [page:.w w].</p>
  64. <h2>Methods</h2>
  65. <h3>[method:this add]( [param:Vector4 v] )</h3>
  66. <p>Adds [page:Vector4 v] to this vector.</p>
  67. <h3>[method:this addScalar]( [param:Float s] )</h3>
  68. <p>
  69. Adds the scalar value s to this vector's [page:.x x], [page:.y y],
  70. [page:.z z] and [page:.w w] values.
  71. </p>
  72. <h3>[method:this addScaledVector]( [param:Vector4 v], [param:Float s] )</h3>
  73. <p>
  74. Adds the multiple of [page:Vector4 v] and [page:Float s] to this vector.
  75. </p>
  76. <h3>[method:this addVectors]( [param:Vector4 a], [param:Vector4 b] )</h3>
  77. <p>Sets this vector to [page:Vector4 a] + [page:Vector4 b].</p>
  78. <h3>[method:this applyMatrix4]( [param:Matrix4 m] )</h3>
  79. <p>Multiplies this vector by 4 x 4 [page:Matrix4 m].</p>
  80. <h3>[method:this ceil]()</h3>
  81. <p>
  82. The [page:.x x], [page:.y y], [page:.z z] and [page:.w w] components of
  83. this vector are rounded up to the nearest integer value.
  84. </p>
  85. <h3>[method:this clamp]( [param:Vector4 min], [param:Vector4 max] )</h3>
  86. <p>
  87. [page:Vector4 min] - the minimum [page:.x x], [page:.y y], [page:.z z] and
  88. [page:.w w] values.<br />
  89. [page:Vector4 max] - the maximum [page:.x x], [page:.y y], [page:.z z] and
  90. [page:.w w] values in the desired range<br /><br />
  91. If this vector's x, y, z or w value is greater than the max vector's x, y,
  92. z or w value, it is replaced by the corresponding value. <br /><br />
  93. If this vector's x, y, z or w value is less than the min vector's x, y, z
  94. or w value, it is replaced by the corresponding value.
  95. </p>
  96. <h3>[method:this clampLength]( [param:Float min], [param:Float max] )</h3>
  97. <p>
  98. [page:Float min] - the minimum value the length will be clamped to <br />
  99. [page:Float max] - the maximum value the length will be clamped to<br /><br />
  100. If this vector's length is greater than the max value, it is replaced by
  101. the max value. <br /><br />
  102. If this vector's length is less than the min value, it is replaced by the
  103. min value.
  104. </p>
  105. <h3>[method:this clampScalar]( [param:Float min], [param:Float max] )</h3>
  106. <p>
  107. [page:Float min] - the minimum value the components will be clamped to
  108. <br />
  109. [page:Float max] - the maximum value the components will be clamped to<br /><br />
  110. If this vector's x, y, z or w values are greater than the max value, they
  111. are replaced by the max value. <br /><br />
  112. If this vector's x, y, z or w values are less than the min value, they are
  113. replaced by the min value.
  114. </p>
  115. <h3>[method:Vector4 clone]()</h3>
  116. <p>
  117. Returns a new Vector4 with the same [page:.x x], [page:.y y], [page:.z z]
  118. and [page:.w w] values as this one.
  119. </p>
  120. <h3>[method:this copy]( [param:Vector4 v] )</h3>
  121. <p>
  122. Copies the values of the passed Vector4's [page:.x x], [page:.y y],
  123. [page:.z z] and [page:.w w] properties to this Vector4.
  124. </p>
  125. <h3>[method:this divideScalar]( [param:Float s] )</h3>
  126. <p>Divides this vector by scalar [page:Float s].</p>
  127. <h3>[method:Float dot]( [param:Vector4 v] )</h3>
  128. <p>
  129. Calculates the [link:https://en.wikipedia.org/wiki/Dot_product dot product]
  130. of this vector and [page:Vector4 v].
  131. </p>
  132. <h3>[method:Boolean equals]( [param:Vector4 v] )</h3>
  133. <p>
  134. Returns `true` if the components of this vector and [page:Vector4 v] are
  135. strictly equal; `false` otherwise.
  136. </p>
  137. <h3>[method:this floor]()</h3>
  138. <p>
  139. The components of this vector are rounded down to the nearest integer
  140. value.
  141. </p>
  142. <h3>
  143. [method:this fromArray]( [param:Array array], [param:Integer offset] )
  144. </h3>
  145. <p>
  146. [page:Array array] - the source array.<br />
  147. [page:Integer offset] - (optional) offset into the array. Default is `0`.<br /><br />
  148. Sets this vector's [page:.x x] value to be `array[ offset + 0 ]`, [page:.y y]
  149. value to be `array[ offset + 1 ]` [page:.z z] value to be `array[ offset + 2 ]`
  150. and [page:.w w ] value to be `array[ offset + 3 ]`.
  151. </p>
  152. <h3>
  153. [method:this fromBufferAttribute]( [param:BufferAttribute attribute], [param:Integer index] )
  154. </h3>
  155. <p>
  156. [page:BufferAttribute attribute] - the source attribute.<br />
  157. [page:Integer index] - index in the attribute.<br /><br />
  158. Sets this vector's [page:.x x], [page:.y y], [page:.z z] and [page:.w w]
  159. values from the [page:BufferAttribute attribute].
  160. </p>
  161. <h3>[method:Float getComponent]( [param:Integer index] )</h3>
  162. <p>
  163. [page:Integer index] - `0`, `1`, `2` or `3`.<br /><br />
  164. If index equals `0` returns the [page:.x x] value. <br />
  165. If index equals `1` returns the [page:.y y] value. <br />
  166. If index equals `2` returns the [page:.z z] value.<br />
  167. If index equals `3` returns the [page:.w w] value.
  168. </p>
  169. <h3>[method:Float length]()</h3>
  170. <p>
  171. Computes the [link:https://en.wikipedia.org/wiki/Euclidean_distance Euclidean length]
  172. (straight-line length) from `(0, 0, 0, 0)` to `(x, y, z, w)`.
  173. </p>
  174. <h3>[method:Float manhattanLength]()</h3>
  175. <p>
  176. Computes the [link:http://en.wikipedia.org/wiki/Taxicab_geometry Manhattan length] of this vector.
  177. </p>
  178. <h3>[method:Float lengthSq]()</h3>
  179. <p>
  180. Computes the square of the
  181. [link:https://en.wikipedia.org/wiki/Euclidean_distance Euclidean length]
  182. (straight-line length) from `(0, 0, 0, 0)` to `(x, y, z, w)`. If you are
  183. comparing the lengths of vectors, you should compare the length squared
  184. instead as it is slightly more efficient to calculate.
  185. </p>
  186. <h3>[method:this lerp]( [param:Vector4 v], [param:Float alpha] )</h3>
  187. <p>
  188. [page:Vector4 v] - [page:Vector4] to interpolate towards.<br />
  189. [page:Float alpha] - interpolation factor, typically in the closed
  190. interval `[0, 1]`.<br /><br />
  191. Linearly interpolates between this vector and [page:Vector4 v], where
  192. alpha is the percent distance along the line - `alpha = 0` will be this
  193. vector, and `alpha = 1` will be [page:Vector4 v].
  194. </p>
  195. <h3>
  196. [method:this lerpVectors]( [param:Vector4 v1], [param:Vector4 v2], [param:Float alpha] )
  197. </h3>
  198. <p>
  199. [page:Vector4 v1] - the starting [page:Vector4].<br />
  200. [page:Vector4 v2] - [page:Vector4] to interpolate towards.<br />
  201. [page:Float alpha] - interpolation factor, typically in the closed
  202. interval `[0, 1]`.<br /><br />
  203. Sets this vector to be the vector linearly interpolated between
  204. [page:Vector4 v1] and [page:Vector4 v2] where alpha is the percent
  205. distance along the line connecting the two vectors - alpha = 0 will be
  206. [page:Vector4 v1], and alpha = 1 will be [page:Vector4 v2].
  207. </p>
  208. <h3>[method:this negate]()</h3>
  209. <p>Inverts this vector - i.e. sets x = -x, y = -y, z = -z and w = -w.</p>
  210. <h3>[method:this normalize]()</h3>
  211. <p>
  212. Converts this vector to a [link:https://en.wikipedia.org/wiki/Unit_vector unit vector]
  213. - that is, sets it equal to a vector with the same direction
  214. as this one, but [page:.length length] 1.
  215. </p>
  216. <h3>[method:this max]( [param:Vector4 v] )</h3>
  217. <p>
  218. If this vector's x, y, z or w value is less than [page:Vector4 v]'s x, y,
  219. z or w value, replace that value with the corresponding max value.
  220. </p>
  221. <h3>[method:this min]( [param:Vector4 v] )</h3>
  222. <p>
  223. If this vector's x, y, z or w value is greater than [page:Vector4 v]'s x,
  224. y, z or w value, replace that value with the corresponding min value.
  225. </p>
  226. <h3>[method:this multiply]( [param:Vector4 v] )</h3>
  227. <p>Multiplies this vector by [page:Vector4 v].</p>
  228. <h3>[method:this multiplyScalar]( [param:Float s] )</h3>
  229. <p>Multiplies this vector by scalar [page:Float s].</p>
  230. <h3>[method:this round]()</h3>
  231. <p>
  232. The components of this vector are rounded to the nearest integer value.
  233. </p>
  234. <h3>[method:this roundToZero]()</h3>
  235. <p>
  236. The components of this vector are rounded towards zero (up if negative,
  237. down if positive) to an integer value.
  238. </p>
  239. <h3>
  240. [method:this set]( [param:Float x], [param:Float y], [param:Float z], [param:Float w] )
  241. </h3>
  242. <p>
  243. Sets the [page:.x x], [page:.y y], [page:.z z] and [page:.w w] components
  244. of this vector.
  245. </p>
  246. <h3>[method:this setAxisAngleFromQuaternion]( [param:Quaternion q] )</h3>
  247. <p>
  248. [page:Quaternion q] - a normalized [page:Quaternion]<br /><br />
  249. Sets the [page:.x x], [page:.y y] and [page:.z z] components of this
  250. vector to the quaternion's axis and [page:.w w] to the angle.
  251. </p>
  252. <h3>[method:this setAxisAngleFromRotationMatrix]( [param:Matrix4 m] )</h3>
  253. <p>
  254. [page:Matrix4 m] - a [page:Matrix4] of which the upper left 3x3 matrix is
  255. a pure rotation matrix.<br /><br />
  256. Sets the [page:.x x], [page:.y y] and [page:.z z] to the axis of rotation
  257. and [page:.w w] to the angle.
  258. </p>
  259. <h3>[method:this setFromMatrixPosition]( [param:Matrix4 m] )</h3>
  260. <p>
  261. Sets this vector to the position elements of the
  262. [link:https://en.wikipedia.org/wiki/Transformation_matrix transformation matrix] [page:Matrix4 m].
  263. </p>
  264. <h3>
  265. [method:this setComponent]( [param:Integer index], [param:Float value] )
  266. </h3>
  267. <p>
  268. [page:Integer index] - `0`, `1`, `2` or `3`.<br />
  269. [page:Float value] - [page:Float]<br /><br />
  270. If index equals `0` set [page:.x x] to [page:Float value].<br />
  271. If index equals `1` set [page:.y y] to [page:Float value].<br />
  272. If index equals `2` set [page:.z z] to [page:Float value].<br />
  273. If index equals `3` set [page:.w w] to [page:Float value].
  274. </p>
  275. <h3>[method:this setLength]( [param:Float l] )</h3>
  276. <p>
  277. Sets this vector to a vector with the same direction as this one, but
  278. [page:.length length] [page:Float l].
  279. </p>
  280. <h3>[method:this setScalar]( [param:Float scalar] )</h3>
  281. <p>
  282. Sets the [page:.x x], [page:.y y], [page:.z z] and [page:.w w] values of
  283. this vector both equal to [page:Float scalar].
  284. </p>
  285. <h3>[method:this setX]( [param:Float x] )</h3>
  286. <p>Replaces this vector's [page:.x x] value with [page:Float x].</p>
  287. <h3>[method:this setY]( [param:Float y] )</h3>
  288. <p>Replaces this vector's [page:.y y] value with [page:Float y].</p>
  289. <h3>[method:this setZ]( [param:Float z] )</h3>
  290. <p>Replaces this vector's [page:.z z] value with [page:Float z].</p>
  291. <h3>[method:this setW]( [param:Float w] )</h3>
  292. <p>Replaces this vector's [page:.w w] value with [page:Float w].</p>
  293. <h3>[method:this sub]( [param:Vector4 v] )</h3>
  294. <p>Subtracts [page:Vector4 v] from this vector.</p>
  295. <h3>[method:this subScalar]( [param:Float s] )</h3>
  296. <p>
  297. Subtracts [page:Float s] from this vector's [page:.x x], [page:.y y],
  298. [page:.z z] and [page:.w w] components.
  299. </p>
  300. <h3>[method:this subVectors]( [param:Vector4 a], [param:Vector4 b] )</h3>
  301. <p>Sets this vector to [page:Vector4 a] - [page:Vector4 b].</p>
  302. <h3>
  303. [method:Array toArray]( [param:Array array], [param:Integer offset] )
  304. </h3>
  305. <p>
  306. [page:Array array] - (optional) array to store this vector to. If this is
  307. not provided, a new array will be created.<br />
  308. [page:Integer offset] - (optional) optional offset into the array.<br /><br />
  309. Returns an array [x, y, z, w], or copies x, y, z and w into the provided
  310. [page:Array array].
  311. </p>
  312. <h3>[method:this random]()</h3>
  313. <p>
  314. Sets each component of this vector to a pseudo-random value between `0` and
  315. `1`, excluding `1`.
  316. </p>
  317. <h2>Source</h2>
  318. <p>
  319. [link:https://github.com/mrdoob/three.js/blob/master/src/[path].js src/[path].js]
  320. </p>
  321. </body>
  322. </html>