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
			case 0: this.x = value; break;
			case 1: this.y = value; break;
			case 2: this.z = value; break;
63
			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
			case 0: return this.x;
			case 1: return this.y;
			case 2: return this.z;
76
			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
	add: function ( v, w ) {

		if ( w !== undefined ) {

96
			console.warn( 'THREE.Vector3: .add() now only accepts one argument. Use .addVectors( a, b ) instead.' );
97 98 99
			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

G
gero3 已提交
129 130
	addScaledVector: function ( v, s ) {

G
gero3 已提交
131 132 133
		this.x += v.x * s;
		this.y += v.y * s;
		this.z += v.z * s;
G
gero3 已提交
134

G
gero3 已提交
135
		return this;
G
gero3 已提交
136

G
gero3 已提交
137
	},
138

139 140 141 142
	sub: function ( v, w ) {

		if ( w !== undefined ) {

143
			console.warn( 'THREE.Vector3: .sub() now only accepts one argument. Use .subVectors( a, b ) instead.' );
144 145 146
			return this.subVectors( v, w );

		}
147

M
Mr.doob 已提交
148 149 150
		this.x -= v.x;
		this.y -= v.y;
		this.z -= v.z;
151

B
Ben Nolan 已提交
152 153
		return this;

154
	},
155

C
Corey Birnbaum 已提交
156 157 158 159 160 161 162 163 164
	subScalar: function ( s ) {

		this.x -= s;
		this.y -= s;
		this.z -= s;

		return this;

	},
165

166
	subVectors: function ( a, b ) {
167

168 169 170
		this.x = a.x - b.x;
		this.y = a.y - b.y;
		this.z = a.z - b.z;
171

172
		return this;
M
Mr.doob 已提交
173

174
	},
175

176 177 178 179
	multiply: function ( v, w ) {

		if ( w !== undefined ) {

180
			console.warn( 'THREE.Vector3: .multiply() now only accepts one argument. Use .multiplyVectors( a, b ) instead.' );
181 182 183
			return this.multiplyVectors( v, w );

		}
184

M
Mr.doob 已提交
185 186
		this.x *= v.x;
		this.y *= v.y;
N
Nico Kruithof 已提交
187
		this.z *= v.z;
M
Mr.doob 已提交
188 189 190 191 192

		return this;

	},

193
	multiplyScalar: function ( scalar ) {
M
Mr.doob 已提交
194

195 196 197
		this.x *= scalar;
		this.y *= scalar;
		this.z *= scalar;
198

B
Ben Nolan 已提交
199 200
		return this;

201
	},
202

203 204 205 206 207 208 209 210 211 212
	multiplyVectors: function ( a, b ) {

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

		return this;

	},

M
Mr.doob 已提交
213 214 215 216
	applyEuler: function () {

		var quaternion;

217
		return function applyEuler( euler ) {
M
Mr.doob 已提交
218 219 220

			if ( euler instanceof THREE.Euler === false ) {

221
				console.error( 'THREE.Vector3: .applyEuler() now expects a Euler rotation rather than a Vector3 and order.' );
M
Mr.doob 已提交
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238

			}

			if ( quaternion === undefined ) quaternion = new THREE.Quaternion();

			this.applyQuaternion( quaternion.setFromEuler( euler ) );

			return this;

		};

	}(),

	applyAxisAngle: function () {

		var quaternion;

239
		return function applyAxisAngle( axis, angle ) {
M
Mr.doob 已提交
240 241 242 243 244 245 246 247 248 249 250

			if ( quaternion === undefined ) quaternion = new THREE.Quaternion();

			this.applyQuaternion( quaternion.setFromAxisAngle( axis, angle ) );

			return this;

		};

	}(),

251
	applyMatrix3: function ( m ) {
252 253 254 255 256 257 258

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

		var e = m.elements;

259 260 261
		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;
262 263 264 265 266

		return this;

	},

267
	applyMatrix4: function ( m ) {
268

269 270 271
		// input: THREE.Matrix4 affine matrix

		var x = this.x, y = this.y, z = this.z;
272 273 274

		var e = m.elements;

275 276 277
		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 ];
278 279 280 281 282

		return this;

	},

283 284 285 286 287 288 289
	applyProjection: function ( m ) {

		// input: THREE.Matrix4 projection matrix

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

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

292 293 294
		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;
295 296 297 298 299

		return this;

	},

300
	applyQuaternion: function ( q ) {
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315

		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;
316
		var iw = - qx * x - qy * y - qz * z;
317 318 319

		// calculate result * inverse quat

320 321 322
		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;
323 324 325 326 327

		return this;

	},

328
	project: function () {
329

330
		var matrix;
331

332
		return function project( camera ) {
333

334
			if ( matrix === undefined ) matrix = new THREE.Matrix4();
335

336 337
			matrix.multiplyMatrices( camera.projectionMatrix, matrix.getInverse( camera.matrixWorld ) );
			return this.applyProjection( matrix );
338 339 340 341 342

		};

	}(),

343
	unproject: function () {
344

345
		var matrix;
346

347
		return function unproject( camera ) {
348

349
			if ( matrix === undefined ) matrix = new THREE.Matrix4();
350

351 352
			matrix.multiplyMatrices( camera.matrixWorld, matrix.getInverse( camera.projectionMatrix ) );
			return this.applyProjection( matrix );
353 354 355 356 357

		};

	}(),

358 359 360 361 362 363 364 365 366
	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;

367 368 369
		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;
370 371 372 373 374 375 376

		this.normalize();

		return this;

	},

377
	divide: function ( v ) {
378

379 380 381 382 383
		this.x /= v.x;
		this.y /= v.y;
		this.z /= v.z;

		return this;
M
Mr.doob 已提交
384

M
Mr.doob 已提交
385
	},
386

387
	divideScalar: function ( scalar ) {
388

389
		if ( scalar !== 0 ) {
390

391 392 393 394 395
			var invScalar = 1 / scalar;

			this.x *= invScalar;
			this.y *= invScalar;
			this.z *= invScalar;
M
Mr.doob 已提交
396

M
Mr.doob 已提交
397
		} else {
M
Mr.doob 已提交
398

399 400 401
			this.x = 0;
			this.y = 0;
			this.z = 0;
402

M
Mr.doob 已提交
403
		}
404

B
Ben Nolan 已提交
405 406
		return this;

407
	},
408

409
	min: function ( v ) {
410

A
alteredq 已提交
411 412
		if ( this.x > v.x ) {

413
			this.x = v.x;
A
alteredq 已提交
414

415
		}
A
alteredq 已提交
416 417 418

		if ( this.y > v.y ) {

419
			this.y = v.y;
A
alteredq 已提交
420

421
		}
A
alteredq 已提交
422 423 424

		if ( this.z > v.z ) {

425
			this.z = v.z;
A
alteredq 已提交
426

427
		}
428 429 430 431 432

		return this;

	},

433
	max: function ( v ) {
434

A
alteredq 已提交
435 436
		if ( this.x < v.x ) {

437
			this.x = v.x;
A
alteredq 已提交
438

439
		}
A
alteredq 已提交
440 441 442

		if ( this.y < v.y ) {

443
			this.y = v.y;
A
alteredq 已提交
444

445
		}
A
alteredq 已提交
446 447 448

		if ( this.z < v.z ) {

449
			this.z = v.z;
A
alteredq 已提交
450

451
		}
452 453 454 455

		return this;

	},
M
Mr.doob 已提交
456

457
	clamp: function ( min, max ) {
458 459 460

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

A
alteredq 已提交
461 462
		if ( this.x < min.x ) {

463
			this.x = min.x;
A
alteredq 已提交
464 465 466

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

467
			this.x = max.x;
A
alteredq 已提交
468

469 470
		}

A
alteredq 已提交
471 472
		if ( this.y < min.y ) {

473
			this.y = min.y;
A
alteredq 已提交
474 475 476

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

477
			this.y = max.y;
A
alteredq 已提交
478

479 480
		}

A
alteredq 已提交
481 482
		if ( this.z < min.z ) {

483
			this.z = min.z;
A
alteredq 已提交
484 485 486

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

487
			this.z = max.z;
A
alteredq 已提交
488

489 490 491
		}

		return this;
A
alteredq 已提交
492

493 494
	},

495
	clampScalar: function () {
M
Mr.doob 已提交
496

497 498
		var min, max;

499
		return function clampScalar( minVal, maxVal ) {
M
Mr.doob 已提交
500 501 502

			if ( min === undefined ) {

503 504
				min = new THREE.Vector3();
				max = new THREE.Vector3();
M
Mr.doob 已提交
505

506 507
			}

M
Mr.doob 已提交
508 509 510 511
			min.set( minVal, minVal, minVal );
			max.set( maxVal, maxVal, maxVal );

			return this.clamp( min, max );
512 513

		};
M
Mr.doob 已提交
514

515
	}(),
516

M
Mr.doob 已提交
517 518 519 520 521 522
	floor: function () {

		this.x = Math.floor( this.x );
		this.y = Math.floor( this.y );
		this.z = Math.floor( this.z );

523
		return this;
M
Mr.doob 已提交
524

525 526
	},

M
Mr.doob 已提交
527 528 529 530 531 532
	ceil: function () {

		this.x = Math.ceil( this.x );
		this.y = Math.ceil( this.y );
		this.z = Math.ceil( this.z );

533
		return this;
M
Mr.doob 已提交
534

535 536
	},

M
Mr.doob 已提交
537 538 539 540 541 542
	round: function () {

		this.x = Math.round( this.x );
		this.y = Math.round( this.y );
		this.z = Math.round( this.z );

543
		return this;
M
Mr.doob 已提交
544

545 546
	},

M
Mr.doob 已提交
547 548 549 550 551 552
	roundToZero: function () {

		this.x = ( this.x < 0 ) ? Math.ceil( this.x ) : Math.floor( this.x );
		this.y = ( this.y < 0 ) ? Math.ceil( this.y ) : Math.floor( this.y );
		this.z = ( this.z < 0 ) ? Math.ceil( this.z ) : Math.floor( this.z );

553
		return this;
M
Mr.doob 已提交
554

555
	},
556

557
	negate: function () {
M
Mr.doob 已提交
558

M
Mr.doob 已提交
559 560 561 562 563
		this.x = - this.x;
		this.y = - this.y;
		this.z = - this.z;

		return this;
B
Ben Nolan 已提交
564

565
	},
566

567
	dot: function ( v ) {
M
Mr.doob 已提交
568

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

M
Mr.doob 已提交
571
	},
M
Mr.doob 已提交
572

573
	lengthSq: function () {
M
Mr.doob 已提交
574

M
Mr.doob 已提交
575
		return this.x * this.x + this.y * this.y + this.z * this.z;
M
Mr.doob 已提交
576 577 578

	},

579
	length: function () {
M
Mr.doob 已提交
580

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

M
Mr.doob 已提交
583
	},
584

585
	lengthManhattan: function () {
586

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

589 590
	},

591
	normalize: function () {
592

M
Mr.doob 已提交
593
		return this.divideScalar( this.length() );
594

595
	},
596

597
	setLength: function ( l ) {
598

599
		var oldLength = this.length();
M
Mr.doob 已提交
600

B
Ben Houston 已提交
601
		if ( oldLength !== 0 && l !== oldLength  ) {
602 603

			this.multiplyScalar( l / oldLength );
G
gero3 已提交
604

605 606 607
		}

		return this;
608

609
	},
610

611
	lerp: function ( v, alpha ) {
612 613 614 615 616 617 618 619

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

		return this;

	},
620

W
WestLangley 已提交
621 622
	lerpVectors: function ( v1, v2, alpha ) {

G
gero3 已提交
623
		this.subVectors( v2, v1 ).multiplyScalar( alpha ).add( v1 );
W
WestLangley 已提交
624

G
gero3 已提交
625
		return this;
W
WestLangley 已提交
626 627 628

	},

629 630 631 632
	cross: function ( v, w ) {

		if ( w !== undefined ) {

633
			console.warn( 'THREE.Vector3: .cross() now only accepts one argument. Use .crossVectors( a, b ) instead.' );
634 635 636
			return this.crossVectors( v, w );

		}
637

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

640 641 642
		this.x = y * v.z - z * v.y;
		this.y = z * v.x - x * v.z;
		this.z = x * v.y - y * v.x;
643

644
		return this;
645 646 647

	},

648 649
	crossVectors: function ( a, b ) {

650 651 652 653 654 655
		var ax = a.x, ay = a.y, az = a.z;
		var bx = b.x, by = b.y, bz = b.z;

		this.x = ay * bz - az * by;
		this.y = az * bx - ax * bz;
		this.z = ax * by - ay * bx;
656 657 658 659 660

		return this;

	},

M
Mr.doob 已提交
661 662
	projectOnVector: function () {

M
Mr.doob 已提交
663
		var v1, dot;
M
Mr.doob 已提交
664

665
		return function projectOnVector( vector ) {
M
Mr.doob 已提交
666 667 668 669 670

			if ( v1 === undefined ) v1 = new THREE.Vector3();

			v1.copy( vector ).normalize();

M
Mr.doob 已提交
671 672 673
			dot = this.dot( v1 );

			return this.copy( v1 ).multiplyScalar( dot );
M
Mr.doob 已提交
674 675 676 677 678 679 680 681 682

		};

	}(),

	projectOnPlane: function () {

		var v1;

683
		return function projectOnPlane( planeNormal ) {
M
Mr.doob 已提交
684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701

			if ( v1 === undefined ) v1 = new THREE.Vector3();

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

			return this.sub( v1 );

		}

	}(),

	reflect: function () {

		// reflect incident vector off plane orthogonal to normal
		// normal is assumed to have unit length

		var v1;

702
		return function reflect( normal ) {
M
Mr.doob 已提交
703 704 705 706 707 708 709 710 711

			if ( v1 === undefined ) v1 = new THREE.Vector3();

			return this.sub( v1.copy( normal ).multiplyScalar( 2 * this.dot( normal ) ) );

		}

	}(),

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

714 715
		var theta = this.dot( v ) / ( this.length() * v.length() );

716 717
		// clamp, to handle numerical problems

718
		return Math.acos( THREE.Math.clamp( theta, - 1, 1 ) );
M
Mr.doob 已提交
719 720

	},
W
Wilt 已提交
721

722
	distanceTo: function ( v ) {
723

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

M
Mr.doob 已提交
726 727
	},

728
	distanceToSquared: function ( v ) {
M
Mr.doob 已提交
729

M
Mr.doob 已提交
730 731 732 733
		var dx = this.x - v.x;
		var dy = this.y - v.y;
		var dz = this.z - v.z;

734
		return dx * dx + dy * dy + dz * dz;
M
Mr.doob 已提交
735

736
	},
M
Mr.doob 已提交
737

738 739
	setEulerFromRotationMatrix: function ( m, order ) {

740
		console.error( 'THREE.Vector3: .setEulerFromRotationMatrix() has been removed. Use Euler.setFromRotationMatrix() instead.' );
741 742 743 744 745

	},

	setEulerFromQuaternion: function ( q, order ) {

746
		console.error( 'THREE.Vector3: .setEulerFromQuaternion() has been removed. Use Euler.setFromQuaternion() instead.' );
M
Mr.doob 已提交
747

748 749
	},

W
WestLangley 已提交
750 751
	getPositionFromMatrix: function ( m ) {

752
		console.warn( 'THREE.Vector3: .getPositionFromMatrix() has been renamed to .setFromMatrixPosition().' );
W
WestLangley 已提交
753

754
		return this.setFromMatrixPosition( m );
W
WestLangley 已提交
755 756 757

	},

M
Mr.doob 已提交
758 759
	getScaleFromMatrix: function ( m ) {

760
		console.warn( 'THREE.Vector3: .getScaleFromMatrix() has been renamed to .setFromMatrixScale().' );
761 762

		return this.setFromMatrixScale( m );
G
gero3 已提交
763

764 765 766 767
	},

	getColumnFromMatrix: function ( index, matrix ) {

768
		console.warn( 'THREE.Vector3: .getColumnFromMatrix() has been renamed to .setFromMatrixColumn().' );
769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785

		return this.setFromMatrixColumn( index, matrix );

	},

	setFromMatrixPosition: function ( m ) {

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

		return this;

	},

	setFromMatrixScale: function ( m ) {

G
gero3 已提交
786 787
		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();
788
		var sz = this.set( m.elements[ 8 ], m.elements[ 9 ], m.elements[ 10 ] ).length();
M
Mr.doob 已提交
789

790 791 792
		this.x = sx;
		this.y = sy;
		this.z = sz;
M
Mr.doob 已提交
793

794
		return this;
G
gero3 已提交
795

M
Mr.doob 已提交
796 797
	},

798
	setFromMatrixColumn: function ( index, matrix ) {
799

800
		var offset = index * 4;
W
WestLangley 已提交
801

802
		var me = matrix.elements;
W
WestLangley 已提交
803

804 805 806
		this.x = me[ offset ];
		this.y = me[ offset + 1 ];
		this.z = me[ offset + 2 ];
W
WestLangley 已提交
807 808 809 810 811

		return this;

	},

M
Mr.doob 已提交
812
	equals: function ( v ) {
813 814 815

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

816 817
	},

818
	fromArray: function ( array, offset ) {
819

820 821 822 823 824
		if ( offset === undefined ) offset = 0;

		this.x = array[ offset ];
		this.y = array[ offset + 1 ];
		this.z = array[ offset + 2 ];
825

M
Mr.doob 已提交
826 827
		return this;

828 829
	},

830
	toArray: function ( array, offset ) {
831

A
arose 已提交
832 833
		if ( array === undefined ) array = [];
		if ( offset === undefined ) offset = 0;
834

A
arose 已提交
835 836 837
		array[ offset ] = this.x;
		array[ offset + 1 ] = this.y;
		array[ offset + 2 ] = this.z;
838

A
arose 已提交
839
		return array;
840

841 842
	},

W
WestLangley 已提交
843 844
	fromAttribute: function ( attribute, index, offset ) {

G
gero3 已提交
845
		if ( offset === undefined ) offset = 0;
W
WestLangley 已提交
846

G
gero3 已提交
847
		index = index * attribute.itemSize + offset;
W
WestLangley 已提交
848

G
gero3 已提交
849 850 851
		this.x = attribute.array[ index ];
		this.y = attribute.array[ index + 1 ];
		this.z = attribute.array[ index + 2 ];
W
WestLangley 已提交
852

G
gero3 已提交
853
		return this;
W
WestLangley 已提交
854 855 856

	},

M
Mr.doob 已提交
857 858 859 860
	clone: function () {

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

861
	}
862

863
};