Vector3.html 15.8 KB
Newer Older
1 2 3
<!DOCTYPE html>
<html lang="en">
	<head>
G
gero3 已提交
4
		<meta charset="utf-8" />
M
Mr.doob 已提交
5 6 7 8
		<base href="../../" />
		<script src="list.js"></script>
		<script src="page.js"></script>
		<link type="text/css" rel="stylesheet" href="page.css" />
9 10 11
	</head>
	<body>
		<h1>[name]</h1>
M
Mr.doob 已提交
12

13
		<p class="desc">Class representing a 3D [link:https://en.wikipedia.org/wiki/Vector_space vector].
L
Lewy Blue 已提交
14 15 16

		A 3D vector is an ordered triplet of numbers (labeled x, y, and z), which can be used to
		represent a number of things, such as:
17
		</p>
L
Lewy Blue 已提交
18 19 20 21 22 23

		<ul>
			<li>
				A point in 3D space.
			</li>
			<li>
L
Lewy Blue 已提交
24
				A direction and length in 3D space. In three.js the length will always be the
L
Lewy Blue 已提交
25 26 27 28 29 30 31 32 33
				[link:https://en.wikipedia.org/wiki/Euclidean_distance Euclidean distance]
				(straight-line distance) from (0, 0, 0) to (x, y, z) and the direction is also
				measured from (0, 0, 0) towards (x, y, z).
			</li>
			<li>
				Any arbitrary ordered triplet of numbers.
			</li>
		</ul>

34
		<p>
L
Lewy Blue 已提交
35
		There are other things a 3D vector can be used to represent, such as momentum
L
Lewy Blue 已提交
36
		vectors and so on, however these are the most common uses in three.js.
37
		</p>
M
Mr.doob 已提交
38 39


40
		<h2>Example</h2>
M
Mr.doob 已提交
41

L
Lewy Blue 已提交
42
		<code>
L
Lewy Blue 已提交
43
var a = new THREE.Vector3( 0, 1, 0 );
L
Lewy Blue 已提交
44

L
Lewy Blue 已提交
45 46
//no arguments; will be initialised to (0, 0, 0)
var b = new THREE.Vector3( );
M
Mr.doob 已提交
47

L
Lewy Blue 已提交
48
var d = a.distanceTo( b );
49
		</code>
M
Mr.doob 已提交
50 51


52
		<h2>Constructor</h2>
M
Mr.doob 已提交
53

54
		<h3>[name]( [param:Float x], [param:Float y], [param:Float z] )</h3>
55
		<p>
L
Lewy Blue 已提交
56 57
		[page:Float x] - the x value of the vector. Default is *0*.<br />
		[page:Float y] -  the y value of the vector. Default is *0*.<br />
L
Lewy Blue 已提交
58
		[page:Float z] - the z value of the vector. Default is *0*.<br /><br />
L
Lewy Blue 已提交
59

L
Lewy Blue 已提交
60
		Creates a new [name].
61
		</p>
M
Mr.doob 已提交
62 63


64
		<h2>Properties</h2>
M
Mr.doob 已提交
65

L
Lewy Blue 已提交
66
		<h3>[property:Boolean isVector3]</h3>
67
		<p>
L
Lewy Blue 已提交
68 69
			Used to check whether this or derived classes are Vector3s. Default is *true*.<br /><br />

J
Jing Ma 已提交
70
			You should not change this, as it is used internally for optimisation.
71
		</p>
L
Lewy Blue 已提交
72

73
		<h3>[property:Float x]</h3>
M
Mr.doob 已提交
74

75
		<h3>[property:Float y]</h3>
M
Mr.doob 已提交
76

77
		<h3>[property:Float z]</h3>
M
Mr.doob 已提交
78 79


80
		<h2>Methods</h2>
M
Mr.doob 已提交
81

82
		<h3>[method:this add]( [param:Vector3 v] )</h3>
83
		<p>Adds [page:Vector3 v] to this vector.</p>
M
Mr.doob 已提交
84

85
		<h3>[method:this addScalar]( [param:Float s] )</h3>
86
		<p>Adds the scalar value s to this vector's [page:.x x], [page:.y y] and [page:.z z] values.</p>
M
Mr.doob 已提交
87

88
		<h3>[method:this addScaledVector]( [param:Vector3 v], [param:Float s] )</h3>
89
		<p>Adds the multiple of [page:Vector3 v] and [page:Float s] to this vector.</p>
M
Mr.doob 已提交
90

91
		<h3>[method:this addVectors]( [param:Vector3 a], [param:Vector3 b] )</h3>
92
		<p>Sets this vector to [page:Vector3 a] + [page:Vector3 b].</p>
93

94
		<h3>[method:this applyAxisAngle]( [param:Vector3 axis], [param:Float angle] )</h3>
95
		<p>
L
Lewy Blue 已提交
96 97
		[page:Vector3 axis] - A normalized [page:Vector3].<br />
		[page:Float angle] - An angle in radians.<br /><br />
A
aardgoose 已提交
98

L
Lewy Blue 已提交
99
		Applies a rotation specified by an axis and an angle to this vector.
100
		</p>
101

102
		<h3>[method:this applyEuler]( [param:Euler euler] )</h3>
103
		<p>
L
Lewy Blue 已提交
104 105
		Applies euler transform to this vector by converting the [page:Euler] object to a
		[page:Quaternion] and applying.
106
		</p>
T
tschw 已提交
107

108
		<h3>[method:this applyMatrix3]( [param:Matrix3 m] )</h3>
109
		<p>Multiplies this vector by [page:Matrix3 m]</p>
110

111
		<h3>[method:this applyMatrix4]( [param:Matrix4 m] )</h3>
112
		<p>
113
		Multiplies this vector (with an implicit 1 in the 4th dimension) and m, and divides by perspective.
114
		</p>
G
gero3 已提交
115

116
		<h3>[method:this applyQuaternion]( [param:Quaternion quaternion] )</h3>
117
		<p>
L
Lewy Blue 已提交
118
		Applies a [page:Quaternion] transform to this vector.
119
		</p>
120

L
Lewy Blue 已提交
121

122
		<h3>[method:Float angleTo]( [param:Vector3 v] )</h3>
123
		<p>
L
Lewy Blue 已提交
124
		Returns the angle between this vector and vector [page:Vector3 v] in radians.
125
		</p>
126

127
		<h3>[method:this ceil]()</h3>
128
		<p>
L
Lewy Blue 已提交
129
		The [page:.x x], [page:.y y] and [page:.z z] components of the vector are rounded up to the nearest integer value.
130
		</p>
M
Mr.doob 已提交
131

132
		<h3>[method:this clamp]( [param:Vector3 min], [param:Vector3 max] )</h3>
133
		<p>
L
Lewy Blue 已提交
134 135 136 137 138
		[page:Vector3 min] - the minimum [page:.x x], [page:.y y] and [page:.z z] values.<br />
		[page:Vector3 max] - the maximum [page:.x x], [page:.y y] and [page:.z z] values in the desired range<br /><br />

		If this vector's x, y or z value is greater than the max vector's x, y or z value, it is replaced by the corresponding value. <br /><br />
		If this vector's x, y or z value is less than the min vector's x, y or z value, it is replaced by the corresponding value.
139
		</p>
140

141
		<h3>[method:this clampLength]( [param:Float min], [param:Float max] )</h3>
142
		<p>
L
Lewy Blue 已提交
143 144 145 146 147
		[page:Float min] - the minimum value the length will be clamped to <br />
		[page:Float max] - the maximum value the length will be clamped to<br /><br />

		If this vector's length is greater than the max value, it is replaced by the max value. <br /><br />
		If this vector's length is less than the min value, it is replaced by the min value.
148
		</p>
149

150
		<h3>[method:this clampScalar]( [param:Float min], [param:Float max] )</h3>
151
		<p>
L
Lewy Blue 已提交
152 153 154 155 156
		[page:Float min] - the minimum value the components will be clamped to <br />
		[page:Float max] - the maximum value the components will be clamped to<br /><br />

		If this vector's x, y or z values are greater than the max value, they are replaced by the max value. <br /><br />
		If this vector's x, y or z values are less than the min value, they are replaced by the min value.
157
		</p>
M
Mr.doob 已提交
158

L
Lewy Blue 已提交
159
		<h3>[method:Vector3 clone]()</h3>
160
		<p>
L
Lewy Blue 已提交
161
		Returns a new vector3 with the same [page:.x x], [page:.y y] and [page:.z z] values as this one.
162
		</p>
M
Mr.doob 已提交
163

164
		<h3>[method:this copy]( [param:Vector3 v] )</h3>
165
		<p>
L
Lewy Blue 已提交
166 167
			Copies the values of the passed vector3's [page:.x x], [page:.y y] and [page:.z z]
			properties to this vector3.
168
		</p>
M
Mr.doob 已提交
169

170
		<h3>[method:this cross]( [param:Vector3 v] )</h3>
171
		<p>
L
Lewy Blue 已提交
172
		Sets this vector to [link:https://en.wikipedia.org/wiki/Cross_product cross product] of itself and [page:Vector3 v].
173
		</p>
174

175
		<h3>[method:this crossVectors]( [param:Vector3 a], [param:Vector3 b] )</h3>
176
		<p>
L
Lewy Blue 已提交
177
		Sets this vector to [link:https://en.wikipedia.org/wiki/Cross_product cross product] of [page:Vector3 a] and [page:Vector3 b].
178
		</p>
179

180
		<h3>[method:Float distanceTo]( [param:Vector3 v] )</h3>
181
		<p>Computes the distance from this vector to [page:Vector3 v].</p>
L
Lewy Blue 已提交
182

183
		<h3>[method:Float manhattanDistanceTo]( [param:Vector3 v] )</h3>
184
		<p>
L
Lewy Blue 已提交
185
		Computes the [link:https://en.wikipedia.org/wiki/Taxicab_geometry Manhattan distance] from this vector to [page:Vector3 v].
186
		</p>
187

188
		<h3>[method:Float distanceToSquared]( [param:Vector3 v] )</h3>
189
		<p>
L
Lewy Blue 已提交
190 191 192
		Computes the squared distance from this vector to [page:Vector3 v]. If you are just
		comparing the distance with another distance, you should compare the distance squared instead
		as it is slightly more efficient to calculate.
193
		</p>
W
WestLangley 已提交
194

195
		<h3>[method:this divide]( [param:Vector3 v] )</h3>
196
		<p>Divides this vector by [page:Vector3 v].</p>
197

198
		<h3>[method:this divideScalar]( [param:Float s] )</h3>
199
		<p>
L
Lewy Blue 已提交
200
		Divides this vector by scalar [page:Float s].<br />
201
		Sets vector to *( 0, 0, 0 )* if *[page:Float s] = 0*.
202
		</p>
203

204
		<h3>[method:Float dot]( [param:Vector3 v] )</h3>
205
		<p>
L
Lewy Blue 已提交
206 207
		Calculate the [link:https://en.wikipedia.org/wiki/Dot_product dot product] of this
		vector and [page:Vector3 v].
208
		</p>
209

210
		<h3>[method:Boolean equals]( [param:Vector3 v] )</h3>
211
		<p>Checks for strict equality of this vector and [page:Vector3 v].</p>
212

213
		<h3>[method:this floor]()</h3>
214
		<p>The components of the vector are rounded down to the nearest integer value.</p>
215

216
		<h3>[method:this fromArray]( [param:Array array], [param:Integer offset] )</h3>
217
		<p>
L
Lewy Blue 已提交
218 219
		[page:Array array] - the source array.<br />
		[page:Integer offset] - ( optional) offset into the array. Default is 0.<br /><br />
220

L
Lewy Blue 已提交
221 222
		Sets this vector's [page:.x x] value to be array[ offset + 0 ], [page:.y y] value to be array[ offset + 1 ]
		and [page:.z z] value to be array[ offset + 2 ].
223
		</p>
A
aardgoose 已提交
224

225
		<h3>[method:this fromBufferAttribute]( [param:BufferAttribute attribute], [param:Integer index] )</h3>
226
		<p>
L
Lewy Blue 已提交
227 228
		[page:BufferAttribute attribute] - the source attribute.<br />
		[page:Integer index] - index in the attribute.<br /><br />
M
Mugen87 已提交
229

L
Lewy Blue 已提交
230
		Sets this vector's [page:.x x], [page:.y y] and [page:.z z] values from the [page:BufferAttribute attribute].
231
		</p>
C
cjshannon 已提交
232

233
		<h3>[method:Float getComponent]( [param:Integer index] )</h3>
234
		<p>
L
Lewy Blue 已提交
235
		[page:Integer index] - 0, 1 or 2.<br /><br />
236

L
Lewy Blue 已提交
237 238 239
		If index equals 0 returns the [page:.x x] value. <br />
		If index equals 1 returns the [page:.y y] value. <br />
		If index equals 2 returns the [page:.z z] value.
240
		</p>
241

L
Lewy Blue 已提交
242
		<h3>[method:Float length]()</h3>
243 244
		<p>Computes the [link:https://en.wikipedia.org/wiki/Euclidean_distance Euclidean length]
		(straight-line length) from (0, 0, 0) to (x, y, z).</p>
245

246
		<h3>[method:Float manhattanLength]()</h3>
247
		<p>
L
Lewy Blue 已提交
248
		Computes the [link:http://en.wikipedia.org/wiki/Taxicab_geometry Manhattan length] of this vector.
249
		</p>
250

L
Lewy Blue 已提交
251
		<h3>[method:Float lengthSq]()</h3>
252
		<p>
L
Lewy Blue 已提交
253 254 255
		Computes the square of the [link:https://en.wikipedia.org/wiki/Euclidean_distance Euclidean length]
		(straight-line length) from (0, 0, 0) to (x, y, z). If you are 	comparing the lengths of
		vectors, you should compare the length squared instead as it is slightly more efficient to calculate.
256
		</p>
257

258
		<h3>[method:this lerp]( [param:Vector3 v], [param:Float alpha] )</h3>
259
		<p>
L
Lewy Blue 已提交
260
		[page:Vector3 v] - [page:Vector3] to interpolate towards.<br />
L
Lewy Blue 已提交
261
		alpha - interpolation factor in the closed interval [0, 1].<br /><br />
262

L
Lewy Blue 已提交
263 264
		Linearly interpolate between this vector and [page:Vector3 v], where alpha is the
		distance along the line - alpha = 0 will be this vector, and alpha = 1 will be [page:Vector3 v].
265
		</p>
C
cjshannon 已提交
266

267
		<h3>[method:this lerpVectors]( [param:Vector3 v1], [param:Vector3 v2], [param:Float alpha] )</h3>
268
		<p>
L
Lewy Blue 已提交
269 270
		[page:Vector3 v1] - the starting [page:Vector3].<br />
		[page:Vector3 v2] - [page:Vector3] to interpolate towards.<br />
L
Lewy Blue 已提交
271
		[page:Float alpha] - interpolation factor in the closed interval [0, 1].<br /><br />
C
cjshannon 已提交
272

L
Lewy Blue 已提交
273 274 275
		Sets this vector to be the vector linearly interpolated between [page:Vector3 v1] and
		[page:Vector3 v2] where alpha is the distance along the line connecting the two vectors
		- alpha = 0 will be [page:Vector3 v1], and alpha = 1 will be [page:Vector3 v2].
276
		</p>
C
cjshannon 已提交
277

278
		<h3>[method:this max]( [param:Vector3 v] )</h3>
279
		<p>
J
Jing Ma 已提交
280
		If this vector's x, y or z value is less than [page:Vector3 v]'s x, y or z value, replace
L
Lewy Blue 已提交
281
		that value with the corresponding max value.
282
		</p>
C
cjshannon 已提交
283

284
		<h3>[method:this min]( [param:Vector3 v] )</h3>
285
		<p>
J
Jing Ma 已提交
286
		If this vector's x, y or z value is greater than [page:Vector3 v]'s x, y or z value, replace
L
Lewy Blue 已提交
287
		that value with the corresponding min value.
288
		</p>
C
cjshannon 已提交
289

290
		<h3>[method:this multiply]( [param:Vector3 v] )</h3>
291
		<p>Multiplies this vector by [page:Vector3 v].</p>
G
Greg Tatum 已提交
292

293
		<h3>[method:this multiplyScalar]( [param:Float s] )</h3>
294
		<p>Multiplies this vector by scalar [page:Float s].</p>
C
cjshannon 已提交
295

296
		<h3>[method:this multiplyVectors]( [param:Vector3 a], [param:Vector3 b] )</h3>
297
		<p>Sets this vector equal to [page:Vector3 a] * [page:Vector3 b], component-wise.</p>
C
cjshannon 已提交
298

299
		<h3>[method:this negate]()</h3>
300
		<p>Inverts this vector - i.e. sets x = -x, y = -y and z = -z.</p>
301

302
		<h3>[method:this normalize]()</h3>
303
		<p>
304 305
		Convert this vector to a [link:https://en.wikipedia.org/wiki/Unit_vector unit vector] - that is, sets it equal to the vector with the same direction
		as this one, but [page:.length length] 1.
306
		</p>
307

308
		<h3>[method:this project]( [param:Camera camera] )</h3>
309
		<p>
L
Lewy Blue 已提交
310
		[page:Camera camera] — camera to use in the projection.<br /><br />
C
cjshannon 已提交
311

L
Lewy Blue 已提交
312
		[link:https://en.wikipedia.org/wiki/Vector_projection Projects] the vector with the camera.
313
		</p>
M
Mr.doob 已提交
314

315
		<h3>[method:this projectOnPlane]( [param:Vector3 planeNormal] )</h3>
316
		<p>
L
Lewy Blue 已提交
317
		[page:Vector3 planeNormal] - A vector representing a plane normal.<br /><br />
M
Mr.doob 已提交
318

L
Lewy Blue 已提交
319 320
		[link:https://en.wikipedia.org/wiki/Vector_projection Projects] this vector onto a plane by subtracting this vector projected onto the plane's
		normal from this vector.
321
		</p>
C
cjshannon 已提交
322

323
		<h3>[method:this projectOnVector]( [param:Vector3] )</h3>
324
		<p>[link:https://en.wikipedia.org/wiki/Vector_projection Projects] this vector onto another vector.</p>
C
cjshannon 已提交
325

326
		<h3>[method:this reflect]( [param:Vector3 normal] )</h3>
327
		<p>
L
Lewy Blue 已提交
328
		[page:Vector3 normal] - the normal to the reflecting plane<br /><br />
C
cjshannon 已提交
329

L
Lewy Blue 已提交
330 331
		Reflect the vector off of plane orthogonal to [page:Vector3 normal]. Normal is assumed to
		have unit length.
332
		</p>
W
WestLangley 已提交
333

334
		<h3>[method:this round]()</h3>
335
		<p>The components of the vector are rounded to the nearest integer value.</p>
C
cjshannon 已提交
336

337
		<h3>[method:this roundToZero]()</h3>
338
		<p>
L
Lewy Blue 已提交
339
		The components of the vector are rounded towards zero (up if negative, down if positive) to an integer value.
340
		</p>
C
cjshannon 已提交
341

342
		<h3>[method:this set]( [param:Float x], [param:Float y], [param:Float z] )</h3>
343
		<p>Sets the [page:.x x], [page:.y y] and [page:.z z] components of this vector.</p>
C
cjshannon 已提交
344

345
		<h3>[method:null setComponent]( [param:Integer index], [param:Float value] )</h3>
346
		<p>
L
Lewy Blue 已提交
347 348
		[page:Integer index] - 0, 1 or 2.<br />
		[page:Float value] - [page:Float]<br /><br />
C
cjshannon 已提交
349

L
Lewy Blue 已提交
350 351 352
		If index equals 0 set [page:.x x] to [page:Float value].<br />
		If index equals 1 set [page:.y y] to [page:Float value].<br />
		If index equals 2 set [page:.z z] to [page:Float value]
353
		</p>
C
cjshannon 已提交
354

355
		<h3>[method:this setFromCylindrical]( [param:Cylindrical c] )</h3>
356
		<p>
L
Lewy Blue 已提交
357
		Sets this vector from the cylindrical coordinates [page:Cylindrical c].
358
		</p>
C
cjshannon 已提交
359

360
		<h3>[method:this setFromMatrixColumn]( [param:Matrix4 matrix], [param:Integer index] )</h3>
361
		<p>
L
Lewy Blue 已提交
362 363
		Sets this vector's [page:.x x], [page:.y y] and [page:.z z] equal to the column of
		the [page:Matrix4 matrix] specified by the [page:Integer index].
364
		</p>
C
cjshannon 已提交
365

366
		<h3>[method:this setFromMatrixPosition]( [param:Matrix4 m] )</h3>
367
		<p>
L
Lewy Blue 已提交
368 369
		Sets this vector to the position elements of the
		[link:https://en.wikipedia.org/wiki/Transformation_matrix transformation matrix] [page:Matrix4 m].
370
		</p>
L
Lewy Blue 已提交
371

372
		<h3>[method:this setFromMatrixScale]( [param:Matrix4 m] )</h3>
373
		<p>
L
Lewy Blue 已提交
374 375
		Sets this vector to the scale elements of the
		[link:https://en.wikipedia.org/wiki/Transformation_matrix transformation matrix] [page:Matrix4 m].
376
		</p>
M
Mr.doob 已提交
377

378
		<h3>[method:this setFromSpherical]( [param:Spherical s] )</h3>
379
		<p>
L
Lewy Blue 已提交
380
		Sets this vector from the spherical coordinates [page:Spherical s].
381
		</p>
L
Lewy Blue 已提交
382

383
		<h3>[method:this setLength]( [param:Float l] )</h3>
384
		<p>
L
Lewy Blue 已提交
385 386
		Set this vector to the vector with the same direction as this one, but [page:.length length]
		[page:Float l].
387
		</p>
M
Mr.doob 已提交
388

389
		<h3>[method:this setScalar]( [param:Float scalar] )</h3>
390
		<p>
L
Lewy Blue 已提交
391
		Set the [page:.x x], [page:.y y] and [page:.z z] values of this vector both equal to [page:Float scalar].
392
		</p>
T
tschw 已提交
393

394
		<h3>[method:this setX]( [param:Float x] )</h3>
395
		<p>Replace this vector's [page:.x x] value with [page:Float x].</p>
L
Lewy Blue 已提交
396

397
		<h3>[method:this setY]( [param:Float y] )</h3>
398
		<p>Replace this vector's [page:.y y] value with [page:Float y].</p>
L
Lewy Blue 已提交
399

400
		<h3>[method:this setZ]( [param:Float z] )</h3>
401
		<p>Replace this vector's [page:.z z] value with [page:Float z].</p>
L
Lewy Blue 已提交
402

403
		<h3>[method:this sub]( [param:Vector3 v] )</h3>
404
		<p>Subtracts [page:Vector3 v] from this vector.</p>
L
Lewy Blue 已提交
405

406
		<h3>[method:this subScalar]( [param:Float s] )</h3>
407
		<p>Subtracts [page:Float s]  from this vector's [page:.x x], [page:.y y] and [page:.z z] compnents.</p>
L
Lewy Blue 已提交
408

409
		<h3>[method:this subVectors]( [param:Vector3 a], [param:Vector3 b] )</h3>
410
		<p>Sets this vector to [page:Vector3 a] - [page:Vector3 b].</p>
L
Lewy Blue 已提交
411

412
		<h3>[method:Array toArray]( [param:Array array], [param:Integer offset] )</h3>
413
		<p>
L
Lewy Blue 已提交
414 415 416 417 418
		[page:Array array] - (optional) array to store the vector to. If this is not provided
		a new array will be created.<br />
		[page:Integer offset] - (optional) optional offset into the array.<br /><br />

		Returns an array [x, y, z], or copies x, y and z into the provided [page:Array array].
419
		</p>
T
tschw 已提交
420

421
		<h3>[method:this transformDirection]( [param:Matrix4 m] )</h3>
422
		<p>
L
Lewy Blue 已提交
423 424
		Transforms the direction of this vector by a matrix (the upper left 3 x 3 subset of a [page:Matrix4 m])
		and then [page:.normalize normalizes] the result.
425
		</p>
L
Lewy Blue 已提交
426

427
		<h3>[method:this unproject]( [param:Camera camera] )</h3>
428
		<p>
L
Lewy Blue 已提交
429 430 431 432
		[page:Camera camera] — camera to use in the projection.<br /><br />

		[link:https://en.wikipedia.org/wiki/Vector_projection Unprojects] the vector with the
		camera's projection matrix.
433
		</p>
T
tschw 已提交
434

L
Lewy Blue 已提交
435

436 437 438 439 440
		<h2>Source</h2>

		[link:https://github.com/mrdoob/three.js/blob/master/src/[path].js src/[path].js]
	</body>
</html>