Vector3.js 12.8 KB
Newer Older
R
Rich Harris 已提交
1 2 3 4
import { _Math } from './Math';
import { Matrix4 } from './Matrix4';
import { Quaternion } from './Quaternion';

M
Mr.doob 已提交
5
/**
M
Mr.doob 已提交
6
 * @author mrdoob / http://mrdoob.com/
7
 * @author *kile / http://kile.stravaganza.org/
M
Mr.doob 已提交
8
 * @author philogb / http://blog.thejit.org/
9
 * @author mikael emtinger / http://gomo.se/
M
Mr.doob 已提交
10
 * @author egraether / http://egraether.com/
W
WestLangley 已提交
11
 * @author WestLangley / http://github.com/WestLangley
M
Mr.doob 已提交
12 13
 */

M
Mr.doob 已提交
14
function Vector3( x, y, z ) {
15

16 17 18
	this.x = x || 0;
	this.y = y || 0;
	this.z = z || 0;
M
Mr.doob 已提交
19

20
};
21

R
Rich Harris 已提交
22
Vector3.prototype = {
23

R
Rich Harris 已提交
24
	constructor: Vector3,
25

26 27
	isVector3: true,

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

A
alteredq 已提交
30 31 32
		this.x = x;
		this.y = y;
		this.z = z;
M
Mr.doob 已提交
33

34
		return this;
35

36
	},
37

M
Mr.doob 已提交
38 39 40 41 42 43 44 45 46 47
	setScalar: function ( scalar ) {

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

		return this;

	},

M
Mr.doob 已提交
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
	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 已提交
72
	setComponent: function ( index, value ) {
B
Ben Houston 已提交
73

M
Mr.doob 已提交
74
		switch ( index ) {
75

M
Mr.doob 已提交
76 77 78
			case 0: this.x = value; break;
			case 1: this.y = value; break;
			case 2: this.z = value; break;
79
			default: throw new Error( 'index is out of range: ' + index );
B
Ben Houston 已提交
80

M
Mr.doob 已提交
81
		}
B
Ben Houston 已提交
82

M
Mr.doob 已提交
83
	},
84

M
Mr.doob 已提交
85
	getComponent: function ( index ) {
86

M
Mr.doob 已提交
87
		switch ( index ) {
88

M
Mr.doob 已提交
89 90 91
			case 0: return this.x;
			case 1: return this.y;
			case 2: return this.z;
92
			default: throw new Error( 'index is out of range: ' + index );
93

M
Mr.doob 已提交
94
		}
95

M
Mr.doob 已提交
96
	},
97

98 99 100 101 102 103
	clone: function () {

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

	},

104
	copy: function ( v ) {
M
Mr.doob 已提交
105

M
Mr.doob 已提交
106 107 108
		this.x = v.x;
		this.y = v.y;
		this.z = v.z;
M
Mr.doob 已提交
109

110
		return this;
M
Mr.doob 已提交
111

112
	},
113

114 115 116 117
	add: function ( v, w ) {

		if ( w !== undefined ) {

118
			console.warn( 'THREE.Vector3: .add() now only accepts one argument. Use .addVectors( a, b ) instead.' );
119 120 121
			return this.addVectors( v, w );

		}
M
Mr.doob 已提交
122

M
Mr.doob 已提交
123 124 125
		this.x += v.x;
		this.y += v.y;
		this.z += v.z;
126

B
Ben Nolan 已提交
127 128
		return this;

129
	},
130

131
	addScalar: function ( s ) {
M
Mr.doob 已提交
132

M
Mr.doob 已提交
133 134 135
		this.x += s;
		this.y += s;
		this.z += s;
136

B
Ben Nolan 已提交
137 138
		return this;

139
	},
140

141
	addVectors: function ( a, b ) {
142

143 144 145
		this.x = a.x + b.x;
		this.y = a.y + b.y;
		this.z = a.z + b.z;
146

147
		return this;
M
Mr.doob 已提交
148

149
	},
150

G
gero3 已提交
151 152
	addScaledVector: function ( v, s ) {

G
gero3 已提交
153 154 155
		this.x += v.x * s;
		this.y += v.y * s;
		this.z += v.z * s;
G
gero3 已提交
156

G
gero3 已提交
157
		return this;
G
gero3 已提交
158

G
gero3 已提交
159
	},
160

161 162 163 164
	sub: function ( v, w ) {

		if ( w !== undefined ) {

165
			console.warn( 'THREE.Vector3: .sub() now only accepts one argument. Use .subVectors( a, b ) instead.' );
166 167 168
			return this.subVectors( v, w );

		}
169

M
Mr.doob 已提交
170 171 172
		this.x -= v.x;
		this.y -= v.y;
		this.z -= v.z;
173

B
Ben Nolan 已提交
174 175
		return this;

176
	},
177

C
Corey Birnbaum 已提交
178 179 180 181 182 183 184 185 186
	subScalar: function ( s ) {

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

		return this;

	},
187

188
	subVectors: function ( a, b ) {
189

190 191 192
		this.x = a.x - b.x;
		this.y = a.y - b.y;
		this.z = a.z - b.z;
193

194
		return this;
M
Mr.doob 已提交
195

196
	},
197

198 199 200 201
	multiply: function ( v, w ) {

		if ( w !== undefined ) {

202
			console.warn( 'THREE.Vector3: .multiply() now only accepts one argument. Use .multiplyVectors( a, b ) instead.' );
203 204 205
			return this.multiplyVectors( v, w );

		}
206

M
Mr.doob 已提交
207 208
		this.x *= v.x;
		this.y *= v.y;
N
Nico Kruithof 已提交
209
		this.z *= v.z;
M
Mr.doob 已提交
210 211 212 213 214

		return this;

	},

215
	multiplyScalar: function ( scalar ) {
M
Mr.doob 已提交
216

M
Mr.doob 已提交
217
		if ( isFinite( scalar ) ) {
M
Mr.doob 已提交
218

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

M
Mr.doob 已提交
223
		} else {
M
Mr.doob 已提交
224

M
Mr.doob 已提交
225 226 227
			this.x = 0;
			this.y = 0;
			this.z = 0;
M
Mr.doob 已提交
228

M
Mr.doob 已提交
229
		}
230

B
Ben Nolan 已提交
231 232
		return this;

233
	},
234

235 236 237 238 239 240 241 242 243 244
	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 已提交
245 246 247 248
	applyEuler: function () {

		var quaternion;

249
		return function applyEuler( euler ) {
M
Mr.doob 已提交
250

R
Rich Harris 已提交
251
			if ( (euler && euler.isEuler) === false ) {
M
Mr.doob 已提交
252

P
paulmasson 已提交
253
				console.error( 'THREE.Vector3: .applyEuler() now expects an Euler rotation rather than a Vector3 and order.' );
M
Mr.doob 已提交
254 255 256

			}

R
Rich Harris 已提交
257
			if ( quaternion === undefined ) quaternion = new Quaternion();
M
Mr.doob 已提交
258

259
			return this.applyQuaternion( quaternion.setFromEuler( euler ) );
M
Mr.doob 已提交
260 261 262 263 264 265 266 267 268

		};

	}(),

	applyAxisAngle: function () {

		var quaternion;

269
		return function applyAxisAngle( axis, angle ) {
M
Mr.doob 已提交
270

R
Rich Harris 已提交
271
			if ( quaternion === undefined ) quaternion = new Quaternion();
M
Mr.doob 已提交
272

273
			return this.applyQuaternion( quaternion.setFromAxisAngle( axis, angle ) );
M
Mr.doob 已提交
274 275 276 277 278

		};

	}(),

279
	applyMatrix3: function ( m ) {
280

281
		var x = this.x, y = this.y, z = this.z;
282 283
		var e = m.elements;

284 285 286
		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;
287 288 289 290 291

		return this;

	},

292
	applyMatrix4: function ( m ) {
293

294 295 296
		// input: THREE.Matrix4 affine matrix

		var x = this.x, y = this.y, z = this.z;
297 298
		var e = m.elements;

299 300 301
		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 ];
302 303 304 305 306

		return this;

	},

307 308 309 310 311 312
	applyProjection: function ( m ) {

		// input: THREE.Matrix4 projection matrix

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

315 316 317
		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;
318 319 320 321 322

		return this;

	},

323
	applyQuaternion: function ( q ) {
324

325 326
		var x = this.x, y = this.y, z = this.z;
		var qx = q.x, qy = q.y, qz = q.z, qw = q.w;
327 328 329 330 331 332

		// 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;
333
		var iw = - qx * x - qy * y - qz * z;
334 335 336

		// calculate result * inverse quat

337 338 339
		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;
340 341 342 343 344

		return this;

	},

345
	project: function () {
346

347
		var matrix;
348

349
		return function project( camera ) {
350

R
Rich Harris 已提交
351
			if ( matrix === undefined ) matrix = new Matrix4();
352

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

		};

	}(),

360
	unproject: function () {
361

362
		var matrix;
363

364
		return function unproject( camera ) {
365

R
Rich Harris 已提交
366
			if ( matrix === undefined ) matrix = new Matrix4();
367

368 369
			matrix.multiplyMatrices( camera.matrixWorld, matrix.getInverse( camera.projectionMatrix ) );
			return this.applyProjection( matrix );
370 371 372 373 374

		};

	}(),

375 376 377 378 379 380 381 382
	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;

383 384 385
		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;
386

387
		return this.normalize();
388 389 390

	},

391
	divide: function ( v ) {
392

393 394 395 396 397
		this.x /= v.x;
		this.y /= v.y;
		this.z /= v.z;

		return this;
M
Mr.doob 已提交
398

M
Mr.doob 已提交
399
	},
400

401
	divideScalar: function ( scalar ) {
402

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

405
	},
406

407
	min: function ( v ) {
408

M
Mr.doob 已提交
409 410 411
		this.x = Math.min( this.x, v.x );
		this.y = Math.min( this.y, v.y );
		this.z = Math.min( this.z, v.z );
412 413 414 415 416

		return this;

	},

417
	max: function ( v ) {
418

M
Mr.doob 已提交
419 420 421
		this.x = Math.max( this.x, v.x );
		this.y = Math.max( this.y, v.y );
		this.z = Math.max( this.z, v.z );
422 423 424 425

		return this;

	},
M
Mr.doob 已提交
426

427
	clamp: function ( min, max ) {
428 429 430

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

M
Mr.doob 已提交
431 432 433
		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 ) );
434 435

		return this;
A
alteredq 已提交
436

437 438
	},

439
	clampScalar: function () {
M
Mr.doob 已提交
440

441 442
		var min, max;

443
		return function clampScalar( minVal, maxVal ) {
M
Mr.doob 已提交
444 445 446

			if ( min === undefined ) {

R
Rich Harris 已提交
447 448
				min = new Vector3();
				max = new Vector3();
M
Mr.doob 已提交
449

450 451
			}

M
Mr.doob 已提交
452 453 454 455
			min.set( minVal, minVal, minVal );
			max.set( maxVal, maxVal, maxVal );

			return this.clamp( min, max );
456 457

		};
M
Mr.doob 已提交
458

459
	}(),
460

M
Mugen87 已提交
461
	clampLength: function ( min, max ) {
462

M
Mugen87 已提交
463
		var length = this.length();
464

465
		return this.multiplyScalar( Math.max( min, Math.min( max, length ) ) / length );
466

M
Mugen87 已提交
467
	},
468

M
Mr.doob 已提交
469 470 471 472 473 474
	floor: function () {

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

475
		return this;
M
Mr.doob 已提交
476

477 478
	},

M
Mr.doob 已提交
479 480 481 482 483 484
	ceil: function () {

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

485
		return this;
M
Mr.doob 已提交
486

487 488
	},

M
Mr.doob 已提交
489 490 491 492 493 494
	round: function () {

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

495
		return this;
M
Mr.doob 已提交
496

497 498
	},

M
Mr.doob 已提交
499 500 501 502 503 504
	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 );

505
		return this;
M
Mr.doob 已提交
506

507
	},
508

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

M
Mr.doob 已提交
511 512 513 514 515
		this.x = - this.x;
		this.y = - this.y;
		this.z = - this.z;

		return this;
B
Ben Nolan 已提交
516

517
	},
518

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

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

M
Mr.doob 已提交
523
	},
M
Mr.doob 已提交
524

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

M
Mr.doob 已提交
527
		return this.x * this.x + this.y * this.y + this.z * this.z;
M
Mr.doob 已提交
528 529 530

	},

531
	length: function () {
M
Mr.doob 已提交
532

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

M
Mr.doob 已提交
535
	},
536

537
	lengthManhattan: function () {
538

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

541 542
	},

543
	normalize: function () {
544

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

547
	},
548

549
	setLength: function ( length ) {
550

M
Mr.doob 已提交
551
		return this.multiplyScalar( length / this.length() );
552

553
	},
554

555
	lerp: function ( v, alpha ) {
556 557 558 559 560 561 562 563

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

		return this;

	},
564

W
WestLangley 已提交
565 566
	lerpVectors: function ( v1, v2, alpha ) {

567
		return this.subVectors( v2, v1 ).multiplyScalar( alpha ).add( v1 );
W
WestLangley 已提交
568 569 570

	},

571 572 573 574
	cross: function ( v, w ) {

		if ( w !== undefined ) {

575
			console.warn( 'THREE.Vector3: .cross() now only accepts one argument. Use .crossVectors( a, b ) instead.' );
576 577 578
			return this.crossVectors( v, w );

		}
579

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

582 583 584
		this.x = y * v.z - z * v.y;
		this.y = z * v.x - x * v.z;
		this.z = x * v.y - y * v.x;
585

586
		return this;
587 588 589

	},

590 591
	crossVectors: function ( a, b ) {

592 593 594 595 596 597
		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;
598 599 600 601 602

		return this;

	},

603
	projectOnVector: function ( vector ) {
M
Mr.doob 已提交
604

605
		var scalar = vector.dot( this ) / vector.lengthSq();
606

607
		return this.copy( vector ).multiplyScalar( scalar );
608

609
	},
M
Mr.doob 已提交
610 611 612 613 614

	projectOnPlane: function () {

		var v1;

615
		return function projectOnPlane( planeNormal ) {
M
Mr.doob 已提交
616

R
Rich Harris 已提交
617
			if ( v1 === undefined ) v1 = new Vector3();
M
Mr.doob 已提交
618 619 620 621 622

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

			return this.sub( v1 );

M
Mr.doob 已提交
623
		};
M
Mr.doob 已提交
624 625 626 627 628 629 630 631 632 633

	}(),

	reflect: function () {

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

		var v1;

634
		return function reflect( normal ) {
M
Mr.doob 已提交
635

R
Rich Harris 已提交
636
			if ( v1 === undefined ) v1 = new Vector3();
M
Mr.doob 已提交
637 638 639

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

M
Mr.doob 已提交
640
		};
M
Mr.doob 已提交
641 642 643

	}(),

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

646
		var theta = this.dot( v ) / ( Math.sqrt( this.lengthSq() * v.lengthSq() ) );
647

648 649
		// clamp, to handle numerical problems

R
Rich Harris 已提交
650
		return Math.acos( _Math.clamp( theta, - 1, 1 ) );
M
Mr.doob 已提交
651 652

	},
W
Wilt 已提交
653

654
	distanceTo: function ( v ) {
655

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

M
Mr.doob 已提交
658 659
	},

660
	distanceToSquared: function ( v ) {
M
Mr.doob 已提交
661

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

664
		return dx * dx + dy * dy + dz * dz;
M
Mr.doob 已提交
665

666
	},
M
Mr.doob 已提交
667

W
WestLangley 已提交
668 669 670 671 672 673
	distanceToManhattan: function ( v ) {

		return Math.abs( this.x - v.x ) + Math.abs( this.y - v.y ) + Math.abs( this.z - v.z );

	},

B
Ben Houston 已提交
674 675 676
	setFromSpherical: function( s ) {

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

B
Ben Houston 已提交
678 679 680 681 682 683 684 685
		this.x = sinPhiRadius * Math.sin( s.theta );
		this.y = Math.cos( s.phi ) * s.radius;
		this.z = sinPhiRadius * Math.cos( s.theta );

		return this;

	},

686 687
	setFromMatrixPosition: function ( m ) {

M
Mr.doob 已提交
688
		return this.setFromMatrixColumn( m, 3 );
689 690 691 692 693

	},

	setFromMatrixScale: function ( m ) {

694 695 696 697 698 699 700
		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 已提交
701

702
		return this;
G
gero3 已提交
703

M
Mr.doob 已提交
704 705
	},

706
	setFromMatrixColumn: function ( m, index ) {
707

708 709 710
		if ( typeof m === 'number' ) {

			console.warn( 'THREE.Vector3: setFromMatrixColumn now expects ( matrix, index ).' );
711 712 713
			var temp = m
			m = index;
			index = temp;
714 715 716

		}

M
Mr.doob 已提交
717
		return this.fromArray( m.elements, index * 4 );
W
WestLangley 已提交
718 719 720

	},

M
Mr.doob 已提交
721
	equals: function ( v ) {
722 723 724

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

725 726
	},

727
	fromArray: function ( array, offset ) {
728

729 730 731 732 733
		if ( offset === undefined ) offset = 0;

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

M
Mr.doob 已提交
735 736
		return this;

737 738
	},

739
	toArray: function ( array, offset ) {
740

A
arose 已提交
741 742
		if ( array === undefined ) array = [];
		if ( offset === undefined ) offset = 0;
743

A
arose 已提交
744 745 746
		array[ offset ] = this.x;
		array[ offset + 1 ] = this.y;
		array[ offset + 2 ] = this.z;
747

A
arose 已提交
748
		return array;
749

750 751
	},

W
WestLangley 已提交
752 753
	fromAttribute: function ( attribute, index, offset ) {

G
gero3 已提交
754
		if ( offset === undefined ) offset = 0;
W
WestLangley 已提交
755

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

G
gero3 已提交
758 759 760
		this.x = attribute.array[ index ];
		this.y = attribute.array[ index + 1 ];
		this.z = attribute.array[ index + 2 ];
W
WestLangley 已提交
761

G
gero3 已提交
762
		return this;
W
WestLangley 已提交
763

764
	}
765

766
};
R
Rich Harris 已提交
767 768


769
export { Vector3 };