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

M
Mr.doob 已提交
5
/**
M
Mr.doob 已提交
6
 * @author mrdoob / http://mrdoob.com/
M
Mr.doob 已提交
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

M
Mr.doob 已提交
20
}
21

22
Object.assign( Vector3.prototype, {
23

24 25
	isVector3: true,

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

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

32
		return this;
33

34
	},
35

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

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

		return this;

	},

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

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

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

M
Mr.doob 已提交
79
		}
M
Mugen87 已提交
80

M
matthias-w 已提交
81
		return this;
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

217 218 219
		this.x *= scalar;
		this.y *= scalar;
		this.z *= scalar;
220

B
Ben Nolan 已提交
221 222
		return this;

223
	},
224

225 226 227 228 229 230 231 232 233 234
	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 已提交
235 236
	applyEuler: function () {

M
Mr.doob 已提交
237
		var quaternion = new Quaternion();
M
Mr.doob 已提交
238

239
		return function applyEuler( euler ) {
M
Mr.doob 已提交
240

0
06wj 已提交
241
			if ( ! ( euler && euler.isEuler ) ) {
M
Mr.doob 已提交
242

P
paulmasson 已提交
243
				console.error( 'THREE.Vector3: .applyEuler() now expects an Euler rotation rather than a Vector3 and order.' );
M
Mr.doob 已提交
244 245 246

			}

247
			return this.applyQuaternion( quaternion.setFromEuler( euler ) );
M
Mr.doob 已提交
248 249 250 251 252 253 254

		};

	}(),

	applyAxisAngle: function () {

M
Mr.doob 已提交
255
		var quaternion = new Quaternion();
M
Mr.doob 已提交
256

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

259
			return this.applyQuaternion( quaternion.setFromAxisAngle( axis, angle ) );
M
Mr.doob 已提交
260 261 262 263 264

		};

	}(),

265
	applyMatrix3: function ( m ) {
266

267
		var x = this.x, y = this.y, z = this.z;
268 269
		var e = m.elements;

270 271 272
		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;
273 274 275 276 277

		return this;

	},

278
	applyMatrix4: function ( m ) {
279

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

M
Mr.doob 已提交
283
		var w = 1 / ( e[ 3 ] * x + e[ 7 ] * y + e[ 11 ] * z + e[ 15 ] );
284

M
Mugen87 已提交
285 286
		this.x = ( e[ 0 ] * x + e[ 4 ] * y + e[ 8 ] * z + e[ 12 ] ) * w;
		this.y = ( e[ 1 ] * x + e[ 5 ] * y + e[ 9 ] * z + e[ 13 ] ) * w;
M
Mr.doob 已提交
287 288 289
		this.z = ( e[ 2 ] * x + e[ 6 ] * y + e[ 10 ] * z + e[ 14 ] ) * w;

		return this;
290 291 292

	},

293
	applyQuaternion: function ( q ) {
294

295 296
		var x = this.x, y = this.y, z = this.z;
		var qx = q.x, qy = q.y, qz = q.z, qw = q.w;
297 298 299

		// calculate quat * vector

M
Mugen87 已提交
300 301 302
		var ix = qw * x + qy * z - qz * y;
		var iy = qw * y + qz * x - qx * z;
		var iz = qw * z + qx * y - qy * x;
303
		var iw = - qx * x - qy * y - qz * z;
304 305 306

		// calculate result * inverse quat

307 308 309
		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;
310 311 312 313 314

		return this;

	},

315
	project: function ( camera ) {
316

317
		return this.applyMatrix4( camera.matrixWorldInverse ).applyMatrix4( camera.projectionMatrix );
318

Z
zhaoy 已提交
319
	},
320

321
	unproject: function () {
322

M
Mr.doob 已提交
323
		var matrix = new Matrix4();
324

325
		return function unproject( camera ) {
326

327
			return this.applyMatrix4( matrix.getInverse( camera.projectionMatrix ) ).applyMatrix4( camera.matrixWorld );
328 329 330 331 332

		};

	}(),

333 334 335 336 337 338 339 340
	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;

M
Mugen87 已提交
341 342
		this.x = e[ 0 ] * x + e[ 4 ] * y + e[ 8 ] * z;
		this.y = e[ 1 ] * x + e[ 5 ] * y + e[ 9 ] * z;
343
		this.z = e[ 2 ] * x + e[ 6 ] * y + e[ 10 ] * z;
344

345
		return this.normalize();
346 347 348

	},

349
	divide: function ( v ) {
350

351 352 353 354 355
		this.x /= v.x;
		this.y /= v.y;
		this.z /= v.z;

		return this;
M
Mr.doob 已提交
356

M
Mr.doob 已提交
357
	},
358

359
	divideScalar: function ( scalar ) {
360

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

363
	},
364

365
	min: function ( v ) {
366

M
Mr.doob 已提交
367 368 369
		this.x = Math.min( this.x, v.x );
		this.y = Math.min( this.y, v.y );
		this.z = Math.min( this.z, v.z );
370 371 372 373 374

		return this;

	},

375
	max: function ( v ) {
376

M
Mr.doob 已提交
377 378 379
		this.x = Math.max( this.x, v.x );
		this.y = Math.max( this.y, v.y );
		this.z = Math.max( this.z, v.z );
380 381 382 383

		return this;

	},
M
Mr.doob 已提交
384

385
	clamp: function ( min, max ) {
386

W
WestLangley 已提交
387
		// assumes min < max, componentwise
388

M
Mr.doob 已提交
389 390 391
		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 ) );
392 393

		return this;
A
alteredq 已提交
394

395 396
	},

397
	clampScalar: function () {
M
Mr.doob 已提交
398

M
Mr.doob 已提交
399 400
		var min = new Vector3();
		var max = new Vector3();
401

402
		return function clampScalar( minVal, maxVal ) {
M
Mr.doob 已提交
403 404 405 406 407

			min.set( minVal, minVal, minVal );
			max.set( maxVal, maxVal, maxVal );

			return this.clamp( min, max );
408 409

		};
M
Mr.doob 已提交
410

411
	}(),
412

M
Mugen87 已提交
413
	clampLength: function ( min, max ) {
414

M
Mugen87 已提交
415
		var length = this.length();
416

W
WestLangley 已提交
417
		return this.divideScalar( length || 1 ).multiplyScalar( Math.max( min, Math.min( max, length ) ) );
418

M
Mugen87 已提交
419
	},
420

M
Mr.doob 已提交
421 422 423 424 425 426
	floor: function () {

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

427
		return this;
M
Mr.doob 已提交
428

429 430
	},

M
Mr.doob 已提交
431 432 433 434 435 436
	ceil: function () {

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

437
		return this;
M
Mr.doob 已提交
438

439 440
	},

M
Mr.doob 已提交
441 442 443 444 445 446
	round: function () {

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

447
		return this;
M
Mr.doob 已提交
448

449 450
	},

M
Mr.doob 已提交
451 452 453 454 455 456
	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 );

457
		return this;
M
Mr.doob 已提交
458

459
	},
460

461
	negate: function () {
M
Mr.doob 已提交
462

M
Mr.doob 已提交
463 464 465 466 467
		this.x = - this.x;
		this.y = - this.y;
		this.z = - this.z;

		return this;
B
Ben Nolan 已提交
468

469
	},
470

471
	dot: function ( v ) {
M
Mr.doob 已提交
472

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

M
Mr.doob 已提交
475
	},
M
Mr.doob 已提交
476

M
Mr.doob 已提交
477 478
	// TODO lengthSquared?

479
	lengthSq: function () {
M
Mr.doob 已提交
480

M
Mr.doob 已提交
481
		return this.x * this.x + this.y * this.y + this.z * this.z;
M
Mr.doob 已提交
482 483 484

	},

485
	length: function () {
M
Mr.doob 已提交
486

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

M
Mr.doob 已提交
489
	},
490

491
	manhattanLength: function () {
492

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

495 496
	},

497
	normalize: function () {
498

W
WestLangley 已提交
499
		return this.divideScalar( this.length() || 1 );
500

501
	},
502

503
	setLength: function ( length ) {
504

W
WestLangley 已提交
505
		return this.normalize().multiplyScalar( length );
506

507
	},
508

509
	lerp: function ( v, alpha ) {
510 511 512 513 514 515 516 517

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

		return this;

	},
518

W
WestLangley 已提交
519 520
	lerpVectors: function ( v1, v2, alpha ) {

521
		return this.subVectors( v2, v1 ).multiplyScalar( alpha ).add( v1 );
W
WestLangley 已提交
522 523 524

	},

525 526 527 528
	cross: function ( v, w ) {

		if ( w !== undefined ) {

529
			console.warn( 'THREE.Vector3: .cross() now only accepts one argument. Use .crossVectors( a, b ) instead.' );
530 531 532
			return this.crossVectors( v, w );

		}
533

W
WestLangley 已提交
534
		return this.crossVectors( this, v );
535 536 537

	},

538 539
	crossVectors: function ( a, b ) {

540 541 542 543 544 545
		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;
546 547 548 549 550

		return this;

	},

551
	projectOnVector: function ( vector ) {
M
Mr.doob 已提交
552

553
		var scalar = vector.dot( this ) / vector.lengthSq();
554

555
		return this.copy( vector ).multiplyScalar( scalar );
556

557
	},
M
Mr.doob 已提交
558 559 560

	projectOnPlane: function () {

M
Mr.doob 已提交
561
		var v1 = new Vector3();
M
Mr.doob 已提交
562

563
		return function projectOnPlane( planeNormal ) {
M
Mr.doob 已提交
564 565 566 567 568

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

			return this.sub( v1 );

M
Mr.doob 已提交
569
		};
M
Mr.doob 已提交
570 571 572 573 574 575 576 577

	}(),

	reflect: function () {

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

M
Mr.doob 已提交
578
		var v1 = new Vector3();
M
Mr.doob 已提交
579

580
		return function reflect( normal ) {
M
Mr.doob 已提交
581 582 583

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

M
Mr.doob 已提交
584
		};
M
Mr.doob 已提交
585 586 587

	}(),

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

590
		var theta = this.dot( v ) / ( Math.sqrt( this.lengthSq() * v.lengthSq() ) );
591

592 593
		// clamp, to handle numerical problems

R
Rich Harris 已提交
594
		return Math.acos( _Math.clamp( theta, - 1, 1 ) );
M
Mr.doob 已提交
595 596

	},
W
Wilt 已提交
597

598
	distanceTo: function ( v ) {
599

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

M
Mr.doob 已提交
602 603
	},

604
	distanceToSquared: function ( v ) {
M
Mr.doob 已提交
605

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

608
		return dx * dx + dy * dy + dz * dz;
M
Mr.doob 已提交
609

610
	},
M
Mr.doob 已提交
611

612
	manhattanDistanceTo: function ( v ) {
W
WestLangley 已提交
613 614 615 616 617

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

	},

M
Mr.doob 已提交
618
	setFromSpherical: function ( s ) {
B
Ben Houston 已提交
619

W
WestLangley 已提交
620
		return this.setFromSphericalCoords( s.radius, s.phi, s.theta );
M
Mr.doob 已提交
621

W
WestLangley 已提交
622 623 624 625 626 627 628 629 630
	},

	setFromSphericalCoords: function ( radius, phi, theta ) {

		var sinPhiRadius = Math.sin( phi ) * radius;

		this.x = sinPhiRadius * Math.sin( theta );
		this.y = Math.cos( phi ) * radius;
		this.z = sinPhiRadius * Math.cos( theta );
B
Ben Houston 已提交
631 632 633 634 635

		return this;

	},

M
Mr.doob 已提交
636
	setFromCylindrical: function ( c ) {
M
Mugen87 已提交
637

W
WestLangley 已提交
638 639 640 641 642 643 644 645 646
		return this.setFromCylindricalCoords( c.radius, c.theta, c.y );

	},

	setFromCylindricalCoords: function ( radius, theta, y ) {

		this.x = radius * Math.sin( theta );
		this.y = y;
		this.z = radius * Math.cos( theta );
M
Mugen87 已提交
647 648 649 650 651

		return this;

	},

652 653
	setFromMatrixPosition: function ( m ) {

654 655 656 657 658 659 660
		var e = m.elements;

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

		return this;
661 662 663 664 665

	},

	setFromMatrixScale: function ( m ) {

666 667 668 669 670 671 672
		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 已提交
673

674
		return this;
G
gero3 已提交
675

M
Mr.doob 已提交
676 677
	},

678
	setFromMatrixColumn: function ( m, index ) {
679

M
Mr.doob 已提交
680
		return this.fromArray( m.elements, index * 4 );
W
WestLangley 已提交
681 682 683

	},

M
Mr.doob 已提交
684
	equals: function ( v ) {
685 686 687

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

688 689
	},

690
	fromArray: function ( array, offset ) {
691

692 693 694 695 696
		if ( offset === undefined ) offset = 0;

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

M
Mr.doob 已提交
698 699
		return this;

700 701
	},

702
	toArray: function ( array, offset ) {
703

A
arose 已提交
704 705
		if ( array === undefined ) array = [];
		if ( offset === undefined ) offset = 0;
706

A
arose 已提交
707 708 709
		array[ offset ] = this.x;
		array[ offset + 1 ] = this.y;
		array[ offset + 2 ] = this.z;
710

A
arose 已提交
711
		return array;
712

713 714
	},

715
	fromBufferAttribute: function ( attribute, index, offset ) {
W
WestLangley 已提交
716

717 718
		if ( offset !== undefined ) {

719
			console.warn( 'THREE.Vector3: offset has been removed from .fromBufferAttribute().' );
W
WestLangley 已提交
720

721
		}
W
WestLangley 已提交
722

723 724 725
		this.x = attribute.getX( index );
		this.y = attribute.getY( index );
		this.z = attribute.getZ( index );
W
WestLangley 已提交
726

G
gero3 已提交
727
		return this;
W
WestLangley 已提交
728

729
	}
730

731
} );
R
Rich Harris 已提交
732 733


734
export { Vector3 };