Vector3.js 12.6 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
	setScalar: function ( scalar ) {

		this.x = scalar;
		this.y = scalar;
		this.z = scalar;

		return this;

	},

M
Mr.doob 已提交
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
	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 已提交
66
	setComponent: function ( index, value ) {
B
Ben Houston 已提交
67

M
Mr.doob 已提交
68
		switch ( index ) {
69

M
Mr.doob 已提交
70 71 72
			case 0: this.x = value; break;
			case 1: this.y = value; break;
			case 2: this.z = value; break;
73
			default: throw new Error( 'index is out of range: ' + index );
B
Ben Houston 已提交
74

M
Mr.doob 已提交
75
		}
B
Ben Houston 已提交
76

M
Mr.doob 已提交
77
	},
78

M
Mr.doob 已提交
79
	getComponent: function ( index ) {
80

M
Mr.doob 已提交
81
		switch ( index ) {
82

M
Mr.doob 已提交
83 84 85
			case 0: return this.x;
			case 1: return this.y;
			case 2: return this.z;
86
			default: throw new Error( 'index is out of range: ' + index );
87

M
Mr.doob 已提交
88
		}
89

M
Mr.doob 已提交
90
	},
91

92 93 94 95 96 97
	clone: function () {

		return new this.constructor( this.x, this.y, this.z );

	},

98
	copy: function ( v ) {
M
Mr.doob 已提交
99

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

104
		return this;
M
Mr.doob 已提交
105

106
	},
107

108 109 110 111
	add: function ( v, w ) {

		if ( w !== undefined ) {

112
			console.warn( 'THREE.Vector3: .add() now only accepts one argument. Use .addVectors( a, b ) instead.' );
113 114 115
			return this.addVectors( v, w );

		}
M
Mr.doob 已提交
116

M
Mr.doob 已提交
117 118 119
		this.x += v.x;
		this.y += v.y;
		this.z += v.z;
120

B
Ben Nolan 已提交
121 122
		return this;

123
	},
124

125
	addScalar: function ( s ) {
M
Mr.doob 已提交
126

M
Mr.doob 已提交
127 128 129
		this.x += s;
		this.y += s;
		this.z += s;
130

B
Ben Nolan 已提交
131 132
		return this;

133
	},
134

135
	addVectors: function ( a, b ) {
136

137 138 139
		this.x = a.x + b.x;
		this.y = a.y + b.y;
		this.z = a.z + b.z;
140

141
		return this;
M
Mr.doob 已提交
142

143
	},
144

G
gero3 已提交
145 146
	addScaledVector: function ( v, s ) {

G
gero3 已提交
147 148 149
		this.x += v.x * s;
		this.y += v.y * s;
		this.z += v.z * s;
G
gero3 已提交
150

G
gero3 已提交
151
		return this;
G
gero3 已提交
152

G
gero3 已提交
153
	},
154

155 156 157 158
	sub: function ( v, w ) {

		if ( w !== undefined ) {

159
			console.warn( 'THREE.Vector3: .sub() now only accepts one argument. Use .subVectors( a, b ) instead.' );
160 161 162
			return this.subVectors( v, w );

		}
163

M
Mr.doob 已提交
164 165 166
		this.x -= v.x;
		this.y -= v.y;
		this.z -= v.z;
167

B
Ben Nolan 已提交
168 169
		return this;

170
	},
171

C
Corey Birnbaum 已提交
172 173 174 175 176 177 178 179 180
	subScalar: function ( s ) {

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

		return this;

	},
181

182
	subVectors: function ( a, b ) {
183

184 185 186
		this.x = a.x - b.x;
		this.y = a.y - b.y;
		this.z = a.z - b.z;
187

188
		return this;
M
Mr.doob 已提交
189

190
	},
191

192 193 194 195
	multiply: function ( v, w ) {

		if ( w !== undefined ) {

196
			console.warn( 'THREE.Vector3: .multiply() now only accepts one argument. Use .multiplyVectors( a, b ) instead.' );
197 198 199
			return this.multiplyVectors( v, w );

		}
200

M
Mr.doob 已提交
201 202
		this.x *= v.x;
		this.y *= v.y;
N
Nico Kruithof 已提交
203
		this.z *= v.z;
M
Mr.doob 已提交
204 205 206 207 208

		return this;

	},

209
	multiplyScalar: function ( scalar ) {
M
Mr.doob 已提交
210

M
Mr.doob 已提交
211
		if ( isFinite( scalar ) ) {
M
Mr.doob 已提交
212

M
Mr.doob 已提交
213 214 215
			this.x *= scalar;
			this.y *= scalar;
			this.z *= scalar;
M
Mr.doob 已提交
216

M
Mr.doob 已提交
217
		} else {
M
Mr.doob 已提交
218

M
Mr.doob 已提交
219 220 221
			this.x = 0;
			this.y = 0;
			this.z = 0;
M
Mr.doob 已提交
222

M
Mr.doob 已提交
223
		}
224

B
Ben Nolan 已提交
225 226
		return this;

227
	},
228

229 230 231 232 233 234 235 236 237 238
	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 已提交
239 240 241 242
	applyEuler: function () {

		var quaternion;

243
		return function applyEuler( euler ) {
M
Mr.doob 已提交
244 245 246

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

P
paulmasson 已提交
247
				console.error( 'THREE.Vector3: .applyEuler() now expects an Euler rotation rather than a Vector3 and order.' );
M
Mr.doob 已提交
248 249 250 251 252

			}

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

253
			return this.applyQuaternion( quaternion.setFromEuler( euler ) );
M
Mr.doob 已提交
254 255 256 257 258 259 260 261 262

		};

	}(),

	applyAxisAngle: function () {

		var quaternion;

263
		return function applyAxisAngle( axis, angle ) {
M
Mr.doob 已提交
264 265 266

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

267
			return this.applyQuaternion( quaternion.setFromAxisAngle( axis, angle ) );
M
Mr.doob 已提交
268 269 270 271 272

		};

	}(),

273
	applyMatrix3: function ( m ) {
274

275
		var x = this.x, y = this.y, z = this.z;
276 277
		var e = m.elements;

278 279 280
		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;
281 282 283 284 285

		return this;

	},

286
	applyMatrix4: function ( m ) {
287

288 289 290
		// input: THREE.Matrix4 affine matrix

		var x = this.x, y = this.y, z = this.z;
291 292
		var e = m.elements;

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

		return this;

	},

301 302 303 304 305 306
	applyProjection: function ( m ) {

		// input: THREE.Matrix4 projection matrix

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

309 310 311
		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;
312 313 314 315 316

		return this;

	},

317
	applyQuaternion: function ( q ) {
318

319 320
		var x = this.x, y = this.y, z = this.z;
		var qx = q.x, qy = q.y, qz = q.z, qw = q.w;
321 322 323 324 325 326

		// 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;
327
		var iw = - qx * x - qy * y - qz * z;
328 329 330

		// calculate result * inverse quat

331 332 333
		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;
334 335 336 337 338

		return this;

	},

339
	project: function () {
340

341
		var matrix;
342

343
		return function project( camera ) {
344

345
			if ( matrix === undefined ) matrix = new THREE.Matrix4();
346

347 348
			matrix.multiplyMatrices( camera.projectionMatrix, matrix.getInverse( camera.matrixWorld ) );
			return this.applyProjection( matrix );
349 350 351 352 353

		};

	}(),

354
	unproject: function () {
355

356
		var matrix;
357

358
		return function unproject( camera ) {
359

360
			if ( matrix === undefined ) matrix = new THREE.Matrix4();
361

362 363
			matrix.multiplyMatrices( camera.matrixWorld, matrix.getInverse( camera.projectionMatrix ) );
			return this.applyProjection( matrix );
364 365 366 367 368

		};

	}(),

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

377 378 379
		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;
380

381
		return this.normalize();
382 383 384

	},

385
	divide: function ( v ) {
386

387 388 389 390 391
		this.x /= v.x;
		this.y /= v.y;
		this.z /= v.z;

		return this;
M
Mr.doob 已提交
392

M
Mr.doob 已提交
393
	},
394

395
	divideScalar: function ( scalar ) {
396

M
Mr.doob 已提交
397
		return this.multiplyScalar( 1 / scalar );
B
Ben Nolan 已提交
398

399
	},
400

401
	min: function ( v ) {
402

M
Mr.doob 已提交
403 404 405
		this.x = Math.min( this.x, v.x );
		this.y = Math.min( this.y, v.y );
		this.z = Math.min( this.z, v.z );
406 407 408 409 410

		return this;

	},

411
	max: function ( v ) {
412

M
Mr.doob 已提交
413 414 415
		this.x = Math.max( this.x, v.x );
		this.y = Math.max( this.y, v.y );
		this.z = Math.max( this.z, v.z );
416 417 418 419

		return this;

	},
M
Mr.doob 已提交
420

421
	clamp: function ( min, max ) {
422 423 424

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

M
Mr.doob 已提交
425 426 427
		this.x = Math.max( min.x, Math.min( max.x, this.x ) );
		this.y = Math.max( min.y, Math.min( max.y, this.y ) );
		this.z = Math.max( min.z, Math.min( max.z, this.z ) );
428 429

		return this;
A
alteredq 已提交
430

431 432
	},

433
	clampScalar: function () {
M
Mr.doob 已提交
434

435 436
		var min, max;

437
		return function clampScalar( minVal, maxVal ) {
M
Mr.doob 已提交
438 439 440

			if ( min === undefined ) {

441 442
				min = new THREE.Vector3();
				max = new THREE.Vector3();
M
Mr.doob 已提交
443

444 445
			}

M
Mr.doob 已提交
446 447 448 449
			min.set( minVal, minVal, minVal );
			max.set( maxVal, maxVal, maxVal );

			return this.clamp( min, max );
450 451

		};
M
Mr.doob 已提交
452

453
	}(),
454

M
Mugen87 已提交
455
	clampLength: function ( min, max ) {
456

M
Mugen87 已提交
457
		var length = this.length();
458

459
		return this.multiplyScalar( Math.max( min, Math.min( max, length ) ) / length );
460

M
Mugen87 已提交
461
	},
462

M
Mr.doob 已提交
463 464 465 466 467 468
	floor: function () {

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

469
		return this;
M
Mr.doob 已提交
470

471 472
	},

M
Mr.doob 已提交
473 474 475 476 477 478
	ceil: function () {

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

479
		return this;
M
Mr.doob 已提交
480

481 482
	},

M
Mr.doob 已提交
483 484 485 486 487 488
	round: function () {

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

489
		return this;
M
Mr.doob 已提交
490

491 492
	},

M
Mr.doob 已提交
493 494 495 496 497 498
	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 );

499
		return this;
M
Mr.doob 已提交
500

501
	},
502

503
	negate: function () {
M
Mr.doob 已提交
504

M
Mr.doob 已提交
505 506 507 508 509
		this.x = - this.x;
		this.y = - this.y;
		this.z = - this.z;

		return this;
B
Ben Nolan 已提交
510

511
	},
512

513
	dot: function ( v ) {
M
Mr.doob 已提交
514

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

M
Mr.doob 已提交
517
	},
M
Mr.doob 已提交
518

519
	lengthSq: function () {
M
Mr.doob 已提交
520

M
Mr.doob 已提交
521
		return this.x * this.x + this.y * this.y + this.z * this.z;
M
Mr.doob 已提交
522 523 524

	},

525
	length: function () {
M
Mr.doob 已提交
526

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

M
Mr.doob 已提交
529
	},
530

531
	lengthManhattan: function () {
532

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

535 536
	},

537
	normalize: function () {
538

M
Mr.doob 已提交
539
		return this.divideScalar( this.length() );
540

541
	},
542

543
	setLength: function ( length ) {
544

M
Mr.doob 已提交
545
		return this.multiplyScalar( length / this.length() );
546

547
	},
548

549
	lerp: function ( v, alpha ) {
550 551 552 553 554 555 556 557

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

		return this;

	},
558

W
WestLangley 已提交
559 560
	lerpVectors: function ( v1, v2, alpha ) {

561
		return this.subVectors( v2, v1 ).multiplyScalar( alpha ).add( v1 );
W
WestLangley 已提交
562 563 564

	},

565 566 567 568
	cross: function ( v, w ) {

		if ( w !== undefined ) {

569
			console.warn( 'THREE.Vector3: .cross() now only accepts one argument. Use .crossVectors( a, b ) instead.' );
570 571 572
			return this.crossVectors( v, w );

		}
573

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

576 577 578
		this.x = y * v.z - z * v.y;
		this.y = z * v.x - x * v.z;
		this.z = x * v.y - y * v.x;
579

580
		return this;
581 582 583

	},

584 585
	crossVectors: function ( a, b ) {

586 587 588 589 590 591
		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;
592 593 594 595 596

		return this;

	},

597
	projectOnVector: function ( vector ) {
M
Mr.doob 已提交
598

599 600 601 602 603
		var scalar = vector.dot( this ) / vector.lengthSq();
	
		return this.copy( vector ).multiplyScalar( scalar );
	
	},
M
Mr.doob 已提交
604 605 606 607 608

	projectOnPlane: function () {

		var v1;

609
		return function projectOnPlane( planeNormal ) {
M
Mr.doob 已提交
610 611 612 613 614 615 616

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

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

			return this.sub( v1 );

M
Mr.doob 已提交
617
		};
M
Mr.doob 已提交
618 619 620 621 622 623 624 625 626 627

	}(),

	reflect: function () {

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

		var v1;

628
		return function reflect( normal ) {
M
Mr.doob 已提交
629 630 631 632 633

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

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

M
Mr.doob 已提交
634
		};
M
Mr.doob 已提交
635 636 637

	}(),

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

640
		var theta = this.dot( v ) / ( Math.sqrt( this.lengthSq() * v.lengthSq() ) );
641

642 643
		// clamp, to handle numerical problems

644
		return Math.acos( THREE.Math.clamp( theta, - 1, 1 ) );
M
Mr.doob 已提交
645 646

	},
W
Wilt 已提交
647

648
	distanceTo: function ( v ) {
649

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

M
Mr.doob 已提交
652 653
	},

654
	distanceToSquared: function ( v ) {
M
Mr.doob 已提交
655

656
		var dx = this.x - v.x, dy = this.y - v.y, dz = this.z - v.z;
M
Mr.doob 已提交
657

658
		return dx * dx + dy * dy + dz * dz;
M
Mr.doob 已提交
659

660
	},
M
Mr.doob 已提交
661

B
Ben Houston 已提交
662 663 664
	setFromSpherical: function( s ) {

		var sinPhiRadius = Math.sin( s.phi ) * s.radius;
M
Mr.doob 已提交
665

B
Ben Houston 已提交
666 667 668 669 670 671 672 673
		this.x = sinPhiRadius * Math.sin( s.theta );
		this.y = Math.cos( s.phi ) * s.radius;
		this.z = sinPhiRadius * Math.cos( s.theta );

		return this;

	},

674 675
	setFromMatrixPosition: function ( m ) {

M
Mr.doob 已提交
676
		return this.setFromMatrixColumn( m, 3 );
677 678 679 680 681

	},

	setFromMatrixScale: function ( m ) {

682 683 684 685 686 687 688
		var sx = this.setFromMatrixColumn( m, 0 ).length();
		var sy = this.setFromMatrixColumn( m, 1 ).length();
		var sz = this.setFromMatrixColumn( m, 2 ).length();

		this.x = sx;
		this.y = sy;
		this.z = sz;
M
Mr.doob 已提交
689

690
		return this;
G
gero3 已提交
691

M
Mr.doob 已提交
692 693
	},

694
	setFromMatrixColumn: function ( m, index ) {
695

696 697 698
		if ( typeof m === 'number' ) {

			console.warn( 'THREE.Vector3: setFromMatrixColumn now expects ( matrix, index ).' );
699 700 701
			var temp = m
			m = index;
			index = temp;
702 703 704

		}

M
Mr.doob 已提交
705
		return this.fromArray( m.elements, index * 4 );
W
WestLangley 已提交
706 707 708

	},

M
Mr.doob 已提交
709
	equals: function ( v ) {
710 711 712

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

713 714
	},

715
	fromArray: function ( array, offset ) {
716

717 718 719 720 721
		if ( offset === undefined ) offset = 0;

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

M
Mr.doob 已提交
723 724
		return this;

725 726
	},

727
	toArray: function ( array, offset ) {
728

A
arose 已提交
729 730
		if ( array === undefined ) array = [];
		if ( offset === undefined ) offset = 0;
731

A
arose 已提交
732 733 734
		array[ offset ] = this.x;
		array[ offset + 1 ] = this.y;
		array[ offset + 2 ] = this.z;
735

A
arose 已提交
736
		return array;
737

738 739
	},

W
WestLangley 已提交
740 741
	fromAttribute: function ( attribute, index, offset ) {

G
gero3 已提交
742
		if ( offset === undefined ) offset = 0;
W
WestLangley 已提交
743

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

G
gero3 已提交
746 747 748
		this.x = attribute.array[ index ];
		this.y = attribute.array[ index + 1 ];
		this.z = attribute.array[ index + 2 ];
W
WestLangley 已提交
749

G
gero3 已提交
750
		return this;
W
WestLangley 已提交
751

752
	}
753

754
};