Vector3.js 13.7 KB
Newer Older
M
Mr.doob 已提交
1
/**
M
Mr.doob 已提交
2
 * @author mrdoob / http://mrdoob.com/
3
 * @author *kile / http://kile.stravaganza.org/
M
Mr.doob 已提交
4
 * @author philogb / http://blog.thejit.org/
5
 * @author mikael emtinger / http://gomo.se/
M
Mr.doob 已提交
6
 * @author egraether / http://egraether.com/
W
WestLangley 已提交
7
 * @author WestLangley / http://github.com/WestLangley
M
Mr.doob 已提交
8 9
 */

A
alteredq 已提交
10
THREE.Vector3 = function ( x, y, z ) {
11

12 13 14
	this.x = x || 0;
	this.y = y || 0;
	this.z = z || 0;
M
Mr.doob 已提交
15

16
};
17

18 19 20
THREE.Vector3.prototype = {

	constructor: THREE.Vector3,
21

22
	set: function ( x, y, z ) {
M
Mr.doob 已提交
23

A
alteredq 已提交
24 25 26
		this.x = x;
		this.y = y;
		this.z = z;
M
Mr.doob 已提交
27

28
		return this;
29

30
	},
31

M
Mr.doob 已提交
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
	setX: function ( x ) {

		this.x = x;

		return this;

	},

	setY: function ( y ) {

		this.y = y;

		return this;

	},

	setZ: function ( z ) {

		this.z = z;

		return this;

	},

M
Mr.doob 已提交
56
	setComponent: function ( index, value ) {
B
Ben Houston 已提交
57

M
Mr.doob 已提交
58
		switch ( index ) {
59

M
Mr.doob 已提交
60 61 62 63
			case 0: this.x = value; break;
			case 1: this.y = value; break;
			case 2: this.z = value; break;
			default: throw new Error( "index is out of range: " + index );
B
Ben Houston 已提交
64

M
Mr.doob 已提交
65
		}
B
Ben Houston 已提交
66

M
Mr.doob 已提交
67
	},
68

M
Mr.doob 已提交
69
	getComponent: function ( index ) {
70

M
Mr.doob 已提交
71
		switch ( index ) {
72

M
Mr.doob 已提交
73 74 75 76
			case 0: return this.x;
			case 1: return this.y;
			case 2: return this.z;
			default: throw new Error( "index is out of range: " + index );
77

M
Mr.doob 已提交
78
		}
79

M
Mr.doob 已提交
80
	},
81

82
	copy: function ( v ) {
M
Mr.doob 已提交
83

M
Mr.doob 已提交
84 85 86
		this.x = v.x;
		this.y = v.y;
		this.z = v.z;
M
Mr.doob 已提交
87

88
		return this;
M
Mr.doob 已提交
89

90
	},
91

92 93 94 95 96 97 98 99
	add: function ( v, w ) {

		if ( w !== undefined ) {

			console.warn( 'DEPRECATED: Vector3\'s .add() now only accepts one argument. Use .addVectors( a, b ) instead.' );
			return this.addVectors( v, w );

		}
M
Mr.doob 已提交
100

M
Mr.doob 已提交
101 102 103
		this.x += v.x;
		this.y += v.y;
		this.z += v.z;
104

B
Ben Nolan 已提交
105 106
		return this;

107
	},
108

109
	addScalar: function ( s ) {
M
Mr.doob 已提交
110

M
Mr.doob 已提交
111 112 113
		this.x += s;
		this.y += s;
		this.z += s;
114

B
Ben Nolan 已提交
115 116
		return this;

117
	},
118

119
	addVectors: function ( a, b ) {
120

121 122 123
		this.x = a.x + b.x;
		this.y = a.y + b.y;
		this.z = a.z + b.z;
124

125
		return this;
M
Mr.doob 已提交
126

127
	},
128

129 130 131 132 133 134 135 136
	sub: function ( v, w ) {

		if ( w !== undefined ) {

			console.warn( 'DEPRECATED: Vector3\'s .sub() now only accepts one argument. Use .subVectors( a, b ) instead.' );
			return this.subVectors( v, w );

		}
137

M
Mr.doob 已提交
138 139 140
		this.x -= v.x;
		this.y -= v.y;
		this.z -= v.z;
141

B
Ben Nolan 已提交
142 143
		return this;

144
	},
145

146
	subVectors: function ( a, b ) {
147

148 149 150
		this.x = a.x - b.x;
		this.y = a.y - b.y;
		this.z = a.z - b.z;
151

152
		return this;
M
Mr.doob 已提交
153

154
	},
155

156 157 158 159 160 161 162 163
	multiply: function ( v, w ) {

		if ( w !== undefined ) {

			console.warn( 'DEPRECATED: Vector3\'s .multiply() now only accepts one argument. Use .multiplyVectors( a, b ) instead.' );
			return this.multiplyVectors( v, w );

		}
164

M
Mr.doob 已提交
165 166
		this.x *= v.x;
		this.y *= v.y;
N
Nico Kruithof 已提交
167
		this.z *= v.z;
M
Mr.doob 已提交
168 169 170 171 172

		return this;

	},

173
	multiplyScalar: function ( s ) {
M
Mr.doob 已提交
174

M
Mr.doob 已提交
175 176 177
		this.x *= s;
		this.y *= s;
		this.z *= s;
178

B
Ben Nolan 已提交
179 180
		return this;

181
	},
182

183 184 185 186 187 188 189 190 191 192
	multiplyVectors: function ( a, b ) {

		this.x = a.x * b.x;
		this.y = a.y * b.y;
		this.z = a.z * b.z;

		return this;

	},

193
	applyMatrix3: function ( m ) {
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208

		var x = this.x;
		var y = this.y;
		var z = this.z;

		var e = m.elements;

		this.x = e[0] * x + e[3] * y + e[6] * z;
		this.y = e[1] * x + e[4] * y + e[7] * z;
		this.z = e[2] * x + e[5] * y + e[8] * z;

		return this;

	},

209
	applyMatrix4: function ( m ) {
210

211 212 213
		// input: THREE.Matrix4 affine matrix

		var x = this.x, y = this.y, z = this.z;
214 215 216

		var e = m.elements;

217 218 219
		this.x = e[0] * x + e[4] * y + e[8]  * z + e[12];
		this.y = e[1] * x + e[5] * y + e[9]  * z + e[13];
		this.z = e[2] * x + e[6] * y + e[10] * z + e[14];
220 221 222 223 224

		return this;

	},

225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
	applyProjection: function ( m ) {

		// input: THREE.Matrix4 projection matrix

		var x = this.x, y = this.y, z = this.z;

		var e = m.elements;
		var d = 1 / ( e[3] * x + e[7] * y + e[11] * z + e[15] ); // perspective divide

		this.x = ( e[0] * x + e[4] * y + e[8]  * z + e[12] ) * d;
		this.y = ( e[1] * x + e[5] * y + e[9]  * z + e[13] ) * d;
		this.z = ( e[2] * x + e[6] * y + e[10] * z + e[14] ) * d;

		return this;

	},

242
	applyQuaternion: function ( q ) {
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269

		var x = this.x;
		var y = this.y;
		var z = this.z;

		var qx = q.x;
		var qy = q.y;
		var qz = q.z;
		var qw = q.w;

		// calculate quat * vector

		var ix =  qw * x + qy * z - qz * y;
		var iy =  qw * y + qz * x - qx * z;
		var iz =  qw * z + qx * y - qy * x;
		var iw = -qx * x - qy * y - qz * z;

		// calculate result * inverse quat

		this.x = ix * qw + iw * -qx + iy * -qz - iz * -qy;
		this.y = iy * qw + iw * -qy + iz * -qx - ix * -qz;
		this.z = iz * qw + iw * -qz + ix * -qy - iy * -qx;

		return this;

	},

270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
	transformDirection: function ( m ) {

		// input: THREE.Matrix4 affine matrix
		// vector interpreted as a direction

		var x = this.x, y = this.y, z = this.z;

		var e = m.elements;

		this.x = e[0] * x + e[4] * y + e[8]  * z;
		this.y = e[1] * x + e[5] * y + e[9]  * z;
		this.z = e[2] * x + e[6] * y + e[10] * z;

		this.normalize();

		return this;

	},

289
	divide: function ( v ) {
290

291 292 293 294 295
		this.x /= v.x;
		this.y /= v.y;
		this.z /= v.z;

		return this;
M
Mr.doob 已提交
296

M
Mr.doob 已提交
297
	},
298

299
	divideScalar: function ( s ) {
300

B
Ben Houston 已提交
301
		if ( s !== 0 ) {
302

M
Mr.doob 已提交
303 304 305
			this.x /= s;
			this.y /= s;
			this.z /= s;
M
Mr.doob 已提交
306

M
Mr.doob 已提交
307
		} else {
M
Mr.doob 已提交
308

309 310 311
			this.x = 0;
			this.y = 0;
			this.z = 0;
312

M
Mr.doob 已提交
313
		}
314

B
Ben Nolan 已提交
315 316
		return this;

317
	},
318

319
	min: function ( v ) {
320

A
alteredq 已提交
321 322
		if ( this.x > v.x ) {

323
			this.x = v.x;
A
alteredq 已提交
324

325
		}
A
alteredq 已提交
326 327 328

		if ( this.y > v.y ) {

329
			this.y = v.y;
A
alteredq 已提交
330

331
		}
A
alteredq 已提交
332 333 334

		if ( this.z > v.z ) {

335
			this.z = v.z;
A
alteredq 已提交
336

337
		}
338 339 340 341 342

		return this;

	},

343
	max: function ( v ) {
344

A
alteredq 已提交
345 346
		if ( this.x < v.x ) {

347
			this.x = v.x;
A
alteredq 已提交
348

349
		}
A
alteredq 已提交
350 351 352

		if ( this.y < v.y ) {

353
			this.y = v.y;
A
alteredq 已提交
354

355
		}
A
alteredq 已提交
356 357 358

		if ( this.z < v.z ) {

359
			this.z = v.z;
A
alteredq 已提交
360

361
		}
362 363 364 365

		return this;

	},
M
Mr.doob 已提交
366

367
	clamp: function ( min, max ) {
368 369 370

		// This function assumes min < max, if this assumption isn't true it will not operate correctly

A
alteredq 已提交
371 372
		if ( this.x < min.x ) {

373
			this.x = min.x;
A
alteredq 已提交
374 375 376

		} else if ( this.x > max.x ) {

377
			this.x = max.x;
A
alteredq 已提交
378

379 380
		}

A
alteredq 已提交
381 382
		if ( this.y < min.y ) {

383
			this.y = min.y;
A
alteredq 已提交
384 385 386

		} else if ( this.y > max.y ) {

387
			this.y = max.y;
A
alteredq 已提交
388

389 390
		}

A
alteredq 已提交
391 392
		if ( this.z < min.z ) {

393
			this.z = min.z;
A
alteredq 已提交
394 395 396

		} else if ( this.z > max.z ) {

397
			this.z = max.z;
A
alteredq 已提交
398

399 400 401
		}

		return this;
A
alteredq 已提交
402

403 404
	},

405
	negate: function () {
M
Mr.doob 已提交
406

M
Mr.doob 已提交
407
		return this.multiplyScalar( - 1 );
B
Ben Nolan 已提交
408

409
	},
410

411
	dot: function ( v ) {
M
Mr.doob 已提交
412

M
Mr.doob 已提交
413
		return this.x * v.x + this.y * v.y + this.z * v.z;
M
Mr.doob 已提交
414

M
Mr.doob 已提交
415
	},
M
Mr.doob 已提交
416

417
	lengthSq: function () {
M
Mr.doob 已提交
418

M
Mr.doob 已提交
419
		return this.x * this.x + this.y * this.y + this.z * this.z;
M
Mr.doob 已提交
420 421 422

	},

423
	length: function () {
M
Mr.doob 已提交
424

425
		return Math.sqrt( this.x * this.x + this.y * this.y + this.z * this.z );
M
Mr.doob 已提交
426

M
Mr.doob 已提交
427
	},
428

429
	lengthManhattan: function () {
430

431
		return Math.abs( this.x ) + Math.abs( this.y ) + Math.abs( this.z );
A
alteredq 已提交
432

433 434
	},

435
	normalize: function () {
436

M
Mr.doob 已提交
437
		return this.divideScalar( this.length() );
438

439
	},
440

441
	setLength: function ( l ) {
442

443
		var oldLength = this.length();
M
Mr.doob 已提交
444

B
Ben Houston 已提交
445
		if ( oldLength !== 0 && l !== oldLength  ) {
446 447 448 449 450

			this.multiplyScalar( l / oldLength );
		}

		return this;
451

452
	},
453

454
	lerp: function ( v, alpha ) {
455 456 457 458 459 460 461 462

		this.x += ( v.x - this.x ) * alpha;
		this.y += ( v.y - this.y ) * alpha;
		this.z += ( v.z - this.z ) * alpha;

		return this;

	},
463

464 465 466 467 468 469 470 471
	cross: function ( v, w ) {

		if ( w !== undefined ) {

			console.warn( 'DEPRECATED: Vector3\'s .cross() now only accepts one argument. Use .crossVectors( a, b ) instead.' );
			return this.crossVectors( v, w );

		}
472

473
		var x = this.x, y = this.y, z = this.z;
474

475 476 477
		this.x = y * v.z - z * v.y;
		this.y = z * v.x - x * v.z;
		this.z = x * v.y - y * v.x;
478

479
		return this;
480 481 482

	},

483 484 485 486 487 488 489 490 491 492
	crossVectors: function ( a, b ) {

		this.x = a.y * b.z - a.z * b.y;
		this.y = a.z * b.x - a.x * b.z;
		this.z = a.x * b.y - a.y * b.x;

		return this;

	},

W
Wilt 已提交
493
	angleTo: function ( v ) {
M
Mr.doob 已提交
494

495 496
		var theta = this.dot( v ) / ( this.length() * v.length() );

497 498
		// clamp, to handle numerical problems

M
Mr.doob 已提交
499
		return Math.acos( THREE.Math.clamp( theta, -1, 1 ) );
M
Mr.doob 已提交
500 501

	},
W
Wilt 已提交
502

503
	distanceTo: function ( v ) {
504

M
Mr.doob 已提交
505
		return Math.sqrt( this.distanceToSquared( v ) );
506

M
Mr.doob 已提交
507 508
	},

509
	distanceToSquared: function ( v ) {
M
Mr.doob 已提交
510

M
Mr.doob 已提交
511 512 513 514
		var dx = this.x - v.x;
		var dy = this.y - v.y;
		var dz = this.z - v.z;

515
		return dx * dx + dy * dy + dz * dz;
M
Mr.doob 已提交
516

517
	},
M
Mr.doob 已提交
518

W
WestLangley 已提交
519 520 521
	setEulerFromRotationMatrix: function ( m, order ) {

		// assumes the upper 3x3 of m is a pure rotation matrix (i.e, unscaled)
M
Mr.doob 已提交
522

W
WestLangley 已提交
523
		// clamp, to handle numerical problems
M
Mr.doob 已提交
524 525 526 527 528 529

		function clamp( x ) {

			return Math.min( Math.max( x, -1 ), 1 );

		}
530 531 532 533 534 535 536 537 538

		var te = m.elements;
		var m11 = te[0], m12 = te[4], m13 = te[8];
		var m21 = te[1], m22 = te[5], m23 = te[9];
		var m31 = te[2], m32 = te[6], m33 = te[10];

		if ( order === undefined || order === 'XYZ' ) {

			this.y = Math.asin( clamp( m13 ) );
M
Mr.doob 已提交
539

540
			if ( Math.abs( m13 ) < 0.99999 ) {
M
Mr.doob 已提交
541

542 543
				this.x = Math.atan2( - m23, m33 );
				this.z = Math.atan2( - m12, m11 );
M
Mr.doob 已提交
544

545
			} else {
M
Mr.doob 已提交
546

547
				this.x = Math.atan2( m32, m22 );
548
				this.z = 0;
M
Mr.doob 已提交
549

550 551 552
			}

		} else if ( order === 'YXZ' ) {
M
Mr.doob 已提交
553

554
			this.x = Math.asin( - clamp( m23 ) );
M
Mr.doob 已提交
555

556
			if ( Math.abs( m23 ) < 0.99999 ) {
M
Mr.doob 已提交
557

558 559
				this.y = Math.atan2( m13, m33 );
				this.z = Math.atan2( m21, m22 );
M
Mr.doob 已提交
560

561
			} else {
M
Mr.doob 已提交
562

563 564
				this.y = Math.atan2( - m31, m11 );
				this.z = 0;
M
Mr.doob 已提交
565

566
			}
M
Mr.doob 已提交
567

568
		} else if ( order === 'ZXY' ) {
M
Mr.doob 已提交
569

570
			this.x = Math.asin( clamp( m32 ) );
M
Mr.doob 已提交
571

572
			if ( Math.abs( m32 ) < 0.99999 ) {
M
Mr.doob 已提交
573

574 575
				this.y = Math.atan2( - m31, m33 );
				this.z = Math.atan2( - m12, m22 );
M
Mr.doob 已提交
576

577
			} else {
M
Mr.doob 已提交
578

579
				this.y = 0;
580
				this.z = Math.atan2( m21, m11 );
M
Mr.doob 已提交
581

582 583 584
			}

		} else if ( order === 'ZYX' ) {
M
Mr.doob 已提交
585

586
			this.y = Math.asin( - clamp( m31 ) );
M
Mr.doob 已提交
587

588
			if ( Math.abs( m31 ) < 0.99999 ) {
M
Mr.doob 已提交
589

590 591
				this.x = Math.atan2( m32, m33 );
				this.z = Math.atan2( m21, m11 );
M
Mr.doob 已提交
592

593
			} else {
M
Mr.doob 已提交
594

595 596
				this.x = 0;
				this.z = Math.atan2( - m12, m22 );
M
Mr.doob 已提交
597

598
			}
M
Mr.doob 已提交
599

600
		} else if ( order === 'YZX' ) {
M
Mr.doob 已提交
601

602
			this.z = Math.asin( clamp( m21 ) );
M
Mr.doob 已提交
603

604
			if ( Math.abs( m21 ) < 0.99999 ) {
M
Mr.doob 已提交
605

606 607
				this.x = Math.atan2( - m23, m22 );
				this.y = Math.atan2( - m31, m11 );
M
Mr.doob 已提交
608

609
			} else {
M
Mr.doob 已提交
610

611
				this.x = 0;
612
				this.y = Math.atan2( m13, m33 );
M
Mr.doob 已提交
613

614
			}
M
Mr.doob 已提交
615

616
		} else if ( order === 'XZY' ) {
M
Mr.doob 已提交
617

618
			this.z = Math.asin( - clamp( m12 ) );
M
Mr.doob 已提交
619

620
			if ( Math.abs( m12 ) < 0.99999 ) {
M
Mr.doob 已提交
621

622 623
				this.x = Math.atan2( m32, m22 );
				this.y = Math.atan2( m13, m11 );
M
Mr.doob 已提交
624

625
			} else {
M
Mr.doob 已提交
626

627
				this.x = Math.atan2( - m23, m33 );
628
				this.y = 0;
M
Mr.doob 已提交
629

630
			}
M
Mr.doob 已提交
631

632
		}
M
Mr.doob 已提交
633

634 635 636 637
		return this;

	},

W
WestLangley 已提交
638 639 640
	setEulerFromQuaternion: function ( q, order ) {

		// q is assumed to be normalized
M
Mr.doob 已提交
641

W
WestLangley 已提交
642
		// clamp, to handle numerical problems
M
Mr.doob 已提交
643 644 645 646 647 648 649 650

		function clamp( x ) {

			return Math.min( Math.max( x, -1 ), 1 );

		}

		// http://www.mathworks.com/matlabcentral/fileexchange/20696-function-to-convert-between-dcm-euler-angles-quaternions-and-euler-vectors/content/SpinCalc.m
M
Mr.doob 已提交
651

M
Mr.doob 已提交
652 653 654 655
		var sqx = q.x * q.x;
		var sqy = q.y * q.y;
		var sqz = q.z * q.z;
		var sqw = q.w * q.w;
656 657 658 659 660 661

		if ( order === undefined || order === 'XYZ' ) {

			this.x = Math.atan2( 2 * ( q.x * q.w - q.y * q.z ), ( sqw - sqx - sqy + sqz ) );
			this.y = Math.asin(  clamp( 2 * ( q.x * q.z + q.y * q.w ) ) );
			this.z = Math.atan2( 2 * ( q.z * q.w - q.x * q.y ), ( sqw + sqx - sqy - sqz ) );
M
Mr.doob 已提交
662

663
		} else if ( order ===  'YXZ' ) {
M
Mr.doob 已提交
664

665 666 667
			this.x = Math.asin(  clamp( 2 * ( q.x * q.w - q.y * q.z ) ) );
			this.y = Math.atan2( 2 * ( q.x * q.z + q.y * q.w ), ( sqw - sqx - sqy + sqz ) );
			this.z = Math.atan2( 2 * ( q.x * q.y + q.z * q.w ), ( sqw - sqx + sqy - sqz ) );
M
Mr.doob 已提交
668

669
		} else if ( order === 'ZXY' ) {
M
Mr.doob 已提交
670

671 672 673
			this.x = Math.asin(  clamp( 2 * ( q.x * q.w + q.y * q.z ) ) );
			this.y = Math.atan2( 2 * ( q.y * q.w - q.z * q.x ), ( sqw - sqx - sqy + sqz ) );
			this.z = Math.atan2( 2 * ( q.z * q.w - q.x * q.y ), ( sqw - sqx + sqy - sqz ) );
M
Mr.doob 已提交
674

675
		} else if ( order === 'ZYX' ) {
M
Mr.doob 已提交
676

677 678 679
			this.x = Math.atan2( 2 * ( q.x * q.w + q.z * q.y ), ( sqw - sqx - sqy + sqz ) );
			this.y = Math.asin(  clamp( 2 * ( q.y * q.w - q.x * q.z ) ) );
			this.z = Math.atan2( 2 * ( q.x * q.y + q.z * q.w ), ( sqw + sqx - sqy - sqz ) );
M
Mr.doob 已提交
680

681
		} else if ( order === 'YZX' ) {
M
Mr.doob 已提交
682

683 684 685
			this.x = Math.atan2( 2 * ( q.x * q.w - q.z * q.y ), ( sqw - sqx + sqy - sqz ) );
			this.y = Math.atan2( 2 * ( q.y * q.w - q.x * q.z ), ( sqw + sqx - sqy - sqz ) );
			this.z = Math.asin(  clamp( 2 * ( q.x * q.y + q.z * q.w ) ) );
M
Mr.doob 已提交
686

687
		} else if ( order === 'XZY' ) {
M
Mr.doob 已提交
688

689 690 691
			this.x = Math.atan2( 2 * ( q.x * q.w + q.y * q.z ), ( sqw - sqx + sqy - sqz ) );
			this.y = Math.atan2( 2 * ( q.x * q.z + q.y * q.w ), ( sqw + sqx - sqy - sqz ) );
			this.z = Math.asin(  clamp( 2 * ( q.z * q.w - q.x * q.y ) ) );
M
Mr.doob 已提交
692

A
alteredq 已提交
693
		}
M
Mr.doob 已提交
694

W
WestLangley 已提交
695
		return this;
A
alteredq 已提交
696 697

	},
698

W
WestLangley 已提交
699 700 701 702 703 704 705 706 707 708
	getPositionFromMatrix: function ( m ) {

		this.x = m.elements[12];
		this.y = m.elements[13];
		this.z = m.elements[14];

		return this;

	},

M
Mr.doob 已提交
709 710
	getScaleFromMatrix: function ( m ) {

711 712 713
		var sx = this.set( m.elements[0], m.elements[1], m.elements[2] ).length();
		var sy = this.set( m.elements[4], m.elements[5], m.elements[6] ).length();
		var sz = this.set( m.elements[8], m.elements[9], m.elements[10] ).length();
M
Mr.doob 已提交
714

715 716 717
		this.x = sx;
		this.y = sy;
		this.z = sz;
M
Mr.doob 已提交
718

719
		return this;
M
Mr.doob 已提交
720 721
	},

722
	getColumnFromMatrix: function ( index, matrix ) {
W
WestLangley 已提交
723

724
		var offset = index * 4;
W
WestLangley 已提交
725

726
		var me = matrix.elements;
W
WestLangley 已提交
727

728 729 730
		this.x = me[ offset ];
		this.y = me[ offset + 1 ];
		this.z = me[ offset + 2 ];
W
WestLangley 已提交
731 732 733 734 735

		return this;

	},

M
Mr.doob 已提交
736
	equals: function ( v ) {
737 738 739

		return ( ( v.x === this.x ) && ( v.y === this.y ) && ( v.z === this.z ) );

740 741
	},

742 743 744 745 746 747
	fromArray: function ( array ) {

		this.x = array[ 0 ];
		this.y = array[ 1 ];
		this.z = array[ 2 ];

M
Mr.doob 已提交
748 749
		return this;

750 751
	},

752 753 754
	toArray: function () {

		return [ this.x, this.y, this.z ];
755

756 757
	},

M
Mr.doob 已提交
758 759 760 761
	clone: function () {

		return new THREE.Vector3( this.x, this.y, this.z );

762
	}
763

764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841
};

THREE.extend( THREE.Vector3.prototype, {

	applyEuler: function () {

		var q1 = new THREE.Quaternion();

		return function ( v, eulerOrder ) {

			var quaternion = q1.setFromEuler( v, eulerOrder );

			this.applyQuaternion( quaternion );

			return this;

		};

	}(),

	applyAxisAngle: function () {

		var q1 = new THREE.Quaternion();

		return function ( axis, angle ) {

			var quaternion = q1.setFromAxisAngle( axis, angle );

			this.applyQuaternion( quaternion );

			return this;

		};

	}(),

	projectOnVector: function () {

		var v1 = new THREE.Vector3();

		return function ( vector ) {

			v1.copy( vector ).normalize();
			var d = this.dot( v1 );
			return this.copy( v1 ).multiplyScalar( d );

		};

	}(),

	projectOnPlane: function () {

		var v1 = new THREE.Vector3();

		return function ( planeNormal ) {

			v1.copy( this ).projectOnVector( planeNormal );

			return this.sub( v1 );

		}

	}(),

	reflect: function () {

		var v1 = new THREE.Vector3();

		return function ( vector ) {

		    v1.copy( this ).projectOnVector( vector ).multiplyScalar( 2 );

		    return this.subVectors( v1, this );

		}

	}()

M
Mr.doob 已提交
842
} );