Object3D.js 13.9 KB
Newer Older
M
Mr.doob 已提交
1
/**
M
Mr.doob 已提交
2
 * @author mrdoob / http://mrdoob.com/
3 4
 * @author mikael emtinger / http://gomo.se/
 * @author alteredq / http://alteredqualia.com/
5
 * @author WestLangley / http://github.com/WestLangley
M
Mr.doob 已提交
6 7
 */

M
Mr.doob 已提交
8
THREE.Object3D = function () {
9

M
Mr.doob 已提交
10 11
	Object.defineProperty( this, 'id', { value: THREE.Object3DIdCount ++ } );

12
	this.uuid = THREE.Math.generateUUID();
M
Mr.doob 已提交
13

14
	this.name = '';
15
	this.type = 'Object3D';
16

17 18 19
	this.parent = undefined;
	this.children = [];

20
	this.up = THREE.Object3D.DefaultUp.clone();
21

22
	var scope = this;
23

24 25 26 27 28
	var position = new THREE.Vector3();
	var rotation = new THREE.Euler();
	var quaternion = new THREE.Quaternion();
	var scale = new THREE.Vector3( 1, 1, 1 );

M
Mr.doob 已提交
29
	var onRotationChange = function () {
30
		quaternion.setFromEuler( rotation, false );
M
Mr.doob 已提交
31
	};
32

M
Mr.doob 已提交
33
	var onQuaternionChange = function () {
34
		rotation.setFromQuaternion( quaternion, undefined, false );
M
Mr.doob 已提交
35 36 37 38
	};

	rotation.onChange( onRotationChange );
	quaternion.onChange( onQuaternionChange );
39 40

	Object.defineProperties( this, {
41 42
		position: {
			enumerable: true,
43
			value: position
44
		},
45
		rotation: {
46
			enumerable: true,
47
			value: rotation
48 49
		},
		quaternion: {
50
			enumerable: true,
51
			value: quaternion
52 53
		},
		scale: {
54
			enumerable: true,
55 56
			value: scale
		},
57
	} );
58

M
Mr.doob 已提交
59 60
	this.renderDepth = null;

61 62
	this.rotationAutoUpdate = true;

63 64
	this.matrix = new THREE.Matrix4();
	this.matrixWorld = new THREE.Matrix4();
65

66
	this.matrixAutoUpdate = true;
67
	this.matrixWorldNeedsUpdate = false;
68 69

	this.visible = true;
70

71 72 73
	this.castShadow = false;
	this.receiveShadow = false;

A
alteredq 已提交
74 75
	this.frustumCulled = true;

76 77
	this.userData = {};

78 79
};

80
THREE.Object3D.DefaultUp = new THREE.Vector3( 0, 1, 0 );
81

M
Mr.doob 已提交
82
THREE.Object3D.prototype = {
83

84 85
	constructor: THREE.Object3D,

86 87
	get eulerOrder () {

88
		console.warn( 'THREE.Object3D: .eulerOrder has been moved to .rotation.order.' );
89 90 91 92 93 94 95

		return this.rotation.order;

	},

	set eulerOrder ( value ) {

96
		console.warn( 'THREE.Object3D: .eulerOrder has been moved to .rotation.order.' );
97 98 99 100 101 102 103

		this.rotation.order = value;

	},

	get useQuaternion () {

104
		console.warn( 'THREE.Object3D: .useQuaternion has been removed. The library now uses quaternions by default.' );
105 106 107 108 109

	},

	set useQuaternion ( value ) {

110
		console.warn( 'THREE.Object3D: .useQuaternion has been removed. The library now uses quaternions by default.' );
111 112 113

	},

W
WestLangley 已提交
114
	applyMatrix: function ( matrix ) {
M
Mr.doob 已提交
115

W
WestLangley 已提交
116
		this.matrix.multiplyMatrices( matrix, this.matrix );
A
alteredq 已提交
117

W
WestLangley 已提交
118
		this.matrix.decompose( this.position, this.quaternion, this.scale );
119

W
WestLangley 已提交
120
	},
M
Mr.doob 已提交
121

122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
	setRotationFromAxisAngle: function ( axis, angle ) {

		// assumes axis is normalized

		this.quaternion.setFromAxisAngle( axis, angle );

	},

	setRotationFromEuler: function ( euler ) {

		this.quaternion.setFromEuler( euler, true );

	},

	setRotationFromMatrix: function ( m ) {

		// assumes the upper 3x3 of m is a pure rotation matrix (i.e, unscaled)

		this.quaternion.setFromRotationMatrix( m );

	},

	setRotationFromQuaternion: function ( q ) {

		// assumes q is normalized

		this.quaternion.copy( q );

	},

152
	rotateOnAxis: function () {
153

154 155 156 157 158 159 160 161
		// rotate object on axis in object space
		// axis is assumed to be normalized

		var q1 = new THREE.Quaternion();

		return function ( axis, angle ) {

			q1.setFromAxisAngle( axis, angle );
162

163
			this.quaternion.multiply( q1 );
164 165 166 167 168 169 170

			return this;

		}

	}(),

171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
	rotateX: function () {

		var v1 = new THREE.Vector3( 1, 0, 0 );

		return function ( angle ) {

			return this.rotateOnAxis( v1, angle );

		};

	}(),

	rotateY: function () {

		var v1 = new THREE.Vector3( 0, 1, 0 );

		return function ( angle ) {

			return this.rotateOnAxis( v1, angle );

		};

	}(),

	rotateZ: function () {

		var v1 = new THREE.Vector3( 0, 0, 1 );

		return function ( angle ) {

			return this.rotateOnAxis( v1, angle );

		};

	}(),

207 208 209 210 211 212 213 214
	translateOnAxis: function () {

		// translate object by distance along axis in object space
		// axis is assumed to be normalized

		var v1 = new THREE.Vector3();

		return function ( axis, distance ) {
215

M
Mr.doob 已提交
216
			v1.copy( axis ).applyQuaternion( this.quaternion );
217

218
			this.position.add( v1.multiplyScalar( distance ) );
219

220 221
			return this;

222
		}
223 224 225

	}(),

226 227
	translate: function ( distance, axis ) {

228
		console.warn( 'THREE.Object3D: .translate() has been removed. Use .translateOnAxis( axis, distance ) instead.' );
229 230 231 232
		return this.translateOnAxis( axis, distance );

	},

233 234 235 236 237 238
	translateX: function () {

		var v1 = new THREE.Vector3( 1, 0, 0 );

		return function ( distance ) {

239
			return this.translateOnAxis( v1, distance );
240 241 242 243 244 245 246 247 248 249 250

		};

	}(),

	translateY: function () {

		var v1 = new THREE.Vector3( 0, 1, 0 );

		return function ( distance ) {

251
			return this.translateOnAxis( v1, distance );
252 253 254 255 256 257 258 259 260 261 262

		};

	}(),

	translateZ: function () {

		var v1 = new THREE.Vector3( 0, 0, 1 );

		return function ( distance ) {

263
			return this.translateOnAxis( v1, distance );
264 265 266 267

		};

	}(),
268

M
Mr.doob 已提交
269 270
	localToWorld: function ( vector ) {

271
		return vector.applyMatrix4( this.matrixWorld );
M
Mr.doob 已提交
272 273 274

	},

275
	worldToLocal: function () {
M
Mr.doob 已提交
276

277
		var m1 = new THREE.Matrix4();
M
Mr.doob 已提交
278

279 280 281 282 283
		return function ( vector ) {

			return vector.applyMatrix4( m1.getInverse( this.matrixWorld ) );

		};
M
Mr.doob 已提交
284

285
	}(),
286

287
	lookAt: function () {
288

289
		// This routine does not support objects with rotated and/or translated parent(s)
290

291
		var m1 = new THREE.Matrix4();
292

293
		return function ( vector ) {
294

295 296
			m1.lookAt( vector, this.position, this.up );

297
			this.quaternion.setFromRotationMatrix( m1 );
298

299
		};
300

301
	}(),
302

303
	add: function ( object ) {
304

305 306 307 308 309 310 311 312 313 314 315 316
		if ( arguments.length > 1 ) {

			for ( var i = 0; i < arguments.length; i++ ) {

				this.add( arguments[ i ] );

			}

			return this;

		};

317 318
		if ( object === this ) {

M
Mr.doob 已提交
319
			console.error( "THREE.Object3D.add:", object, "can't be added as a child of itself." );
320
			return this;
321 322 323

		}

M
Mr.doob 已提交
324
		if ( object instanceof THREE.Object3D ) {
A
alteredq 已提交
325

M
Mr.doob 已提交
326
			if ( object.parent !== undefined ) {
A
alteredq 已提交
327

M
Mr.doob 已提交
328
				object.parent.remove( object );
329

A
alteredq 已提交
330 331
			}

332
			object.parent = this;
333 334
			object.dispatchEvent( { type: 'added' } );

335
			this.children.push( object );
M
Mr.doob 已提交
336

M
Mr.doob 已提交
337
		} else {
M
Mr.doob 已提交
338

M
Mr.doob 已提交
339
			console.error( "THREE.Object3D.add:", object, "is not an instance of THREE.Object3D." );
M
Mr.doob 已提交
340

341
		}
M
Mr.doob 已提交
342

343 344
		return this;

345
	},
346

347
	remove: function ( object ) {
348

349 350 351 352 353 354 355 356 357 358
		if ( arguments.length > 1 ) {

			for ( var i = 0; i < arguments.length; i++ ) {

				this.remove( arguments[ i ] );

			}

		};

M
Mr.doob 已提交
359
		var index = this.children.indexOf( object );
360

M
Mr.doob 已提交
361
		if ( index !== - 1 ) {
362

363
			object.parent = undefined;
364

365 366
			object.dispatchEvent( { type: 'removed' } );

367
			this.children.splice( index, 1 );
368

M
Mr.doob 已提交
369
		}
370

M
Mr.doob 已提交
371
	},
372

373
	getChildByName: function ( name, recursive ) {
M
Mr.doob 已提交
374

375 376
		console.warn( 'THREE.Object3D: .getChildByName() has been renamed to .getObjectByName().' );
		return this.getObjectByName( name, recursive );
M
Mr.doob 已提交
377 378 379

	},

380 381
	getObjectById: function ( id, recursive ) {

382 383
		if ( this.id === id ) return this;

384 385 386
		for ( var i = 0, l = this.children.length; i < l; i ++ ) {

			var child = this.children[ i ];
387
			var object = child.getObjectById( id, recursive );
388

389
			if ( object !== undefined ) {
390

391
				return object;
392 393 394 395 396 397 398 399 400 401

			}

		}

		return undefined;

	},

	getObjectByName: function ( name, recursive ) {
402

403 404
		if ( this.name === name ) return this;

405
		for ( var i = 0, l = this.children.length; i < l; i ++ ) {
M
Mr.doob 已提交
406

407
			var child = this.children[ i ];
408
			var object = child.getObjectByName( name, recursive );
M
Mr.doob 已提交
409

410
			if ( object !== undefined ) {
M
Mr.doob 已提交
411

412
				return object;
M
Mr.doob 已提交
413

414
			}
M
Mr.doob 已提交
415

416
		}
M
Mr.doob 已提交
417

418
		return undefined;
M
Mr.doob 已提交
419

420
	},
M
Mr.doob 已提交
421

422
	getWorldPosition: function ( optionalTarget ) {
W
WestLangley 已提交
423 424 425 426 427 428 429 430 431

		var result = optionalTarget || new THREE.Vector3();

		this.updateMatrixWorld( true );

		return result.setFromMatrixPosition( this.matrixWorld );

	},

432
	getWorldQuaternion: function () {
W
WestLangley 已提交
433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450

		var position = new THREE.Vector3();
		var scale = new THREE.Vector3();

		return function ( optionalTarget ) {

			var result = optionalTarget || new THREE.Quaternion();

			this.updateMatrixWorld( true );

			this.matrixWorld.decompose( position, result, scale );

			return result;

		}

	}(),

451
	getWorldRotation: function () {
W
WestLangley 已提交
452 453 454 455 456 457 458

		var quaternion = new THREE.Quaternion();

		return function ( optionalTarget ) {

			var result = optionalTarget || new THREE.Euler();

M
Mr.doob 已提交
459
			this.getWorldQuaternion( quaternion );
W
WestLangley 已提交
460 461 462 463 464 465 466

			return result.setFromQuaternion( quaternion, this.rotation.order, false );

		}

	}(),

467
	getWorldScale: function () {
W
WestLangley 已提交
468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485

		var position = new THREE.Vector3();
		var quaternion = new THREE.Quaternion();

		return function ( optionalTarget ) {

			var result = optionalTarget || new THREE.Vector3();

			this.updateMatrixWorld( true );

			this.matrixWorld.decompose( position, quaternion, result );

			return result;

		}

	}(),

486
	getWorldDirection: function () {
W
WestLangley 已提交
487 488 489 490 491 492 493

		var quaternion = new THREE.Quaternion();

		return function ( optionalTarget ) {

			var result = optionalTarget || new THREE.Vector3();

M
Mr.doob 已提交
494
			this.getWorldQuaternion( quaternion );
W
WestLangley 已提交
495 496 497 498 499 500 501

			return result.set( 0, 0, 1 ).applyQuaternion( quaternion );

		}

	}(),

502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569
	raycast: function () {},

	traverse: function ( callback ) {

		callback( this );

		for ( var i = 0, l = this.children.length; i < l; i ++ ) {

			this.children[ i ].traverse( callback );

		}

	},

	traverseVisible: function ( callback ) {

		if ( this.visible === false ) return;

		callback( this );

		for ( var i = 0, l = this.children.length; i < l; i ++ ) {

			this.children[ i ].traverseVisible( callback );

		}

	},

	updateMatrix: function () {

		this.matrix.compose( this.position, this.quaternion, this.scale );

		this.matrixWorldNeedsUpdate = true;

	},

	updateMatrixWorld: function ( force ) {

		if ( this.matrixAutoUpdate === true ) this.updateMatrix();

		if ( this.matrixWorldNeedsUpdate === true || force === true ) {

			if ( this.parent === undefined ) {

				this.matrixWorld.copy( this.matrix );

			} else {

				this.matrixWorld.multiplyMatrices( this.parent.matrixWorld, this.matrix );

			}

			this.matrixWorldNeedsUpdate = false;

			force = true;

		}

		// update children

		for ( var i = 0, l = this.children.length; i < l; i ++ ) {

			this.children[ i ].updateMatrixWorld( force );

		}

	},

570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697
	toJSON: function () {

		var output = {
			metadata: {
				version: 4.3,
				type: 'Object',
				generator: 'ObjectExporter'
			}
		};

		//

		var geometries = {};

		var parseGeometry = function ( geometry ) {

			if ( output.geometries === undefined ) {

				output.geometries = [];

			}

			if ( geometries[ geometry.uuid ] === undefined ) {

				var json = geometry.toJSON();

				delete json.metadata;

				geometries[ geometry.uuid ] = json;

				output.geometries.push( json );

			}

			return geometry.uuid;

		};

		//

		var materials = {};

		var parseMaterial = function ( material ) {

			if ( output.materials === undefined ) {

				output.materials = [];

			}

			if ( materials[ material.uuid ] === undefined ) {

				var json = material.toJSON();

				delete json.metadata;

				materials[ material.uuid ] = json;

				output.materials.push( json );

			}

			return material.uuid;

		};

		//

		var parseObject = function ( object ) {

			var data = {};

			data.uuid = object.uuid;
			data.type = object.type;

			if ( object.name !== '' ) data.name = object.name;
			if ( JSON.stringify( object.userData ) !== '{}' ) data.userData = object.userData;
			if ( object.visible !== true ) data.visible = object.visible;

			if ( object instanceof THREE.PerspectiveCamera ) {

				data.fov = object.fov;
				data.aspect = object.aspect;
				data.near = object.near;
				data.far = object.far;

			} else if ( object instanceof THREE.OrthographicCamera ) {

				data.left = object.left;
				data.right = object.right;
				data.top = object.top;
				data.bottom = object.bottom;
				data.near = object.near;
				data.far = object.far;

			} else if ( object instanceof THREE.AmbientLight ) {

				data.color = object.color.getHex();

			} else if ( object instanceof THREE.DirectionalLight ) {

				data.color = object.color.getHex();
				data.intensity = object.intensity;

			} else if ( object instanceof THREE.PointLight ) {

				data.color = object.color.getHex();
				data.intensity = object.intensity;
				data.distance = object.distance;

			} else if ( object instanceof THREE.SpotLight ) {

				data.color = object.color.getHex();
				data.intensity = object.intensity;
				data.distance = object.distance;
				data.angle = object.angle;
				data.exponent = object.exponent;

			} else if ( object instanceof THREE.HemisphereLight ) {

				data.color = object.color.getHex();
				data.groundColor = object.groundColor.getHex();

			} else if ( object instanceof THREE.Mesh ) {

				data.geometry = parseGeometry( object.geometry );
				data.material = parseMaterial( object.material );

M
Mr.doob 已提交
698 699 700 701 702
			} else if ( object instanceof THREE.Line ) {

				data.geometry = parseGeometry( object.geometry );
				data.material = parseMaterial( object.material );

703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732
			} else if ( object instanceof THREE.Sprite ) {

				data.material = parseMaterial( object.material );

			}

			data.matrix = object.matrix.toArray();

			if ( object.children.length > 0 ) {

				data.children = [];

				for ( var i = 0; i < object.children.length; i ++ ) {

					data.children.push( parseObject( object.children[ i ] ) );

				}

			}

			return data;

		}

		output.object = parseObject( this );

		return output;

	},

M
Mr.doob 已提交
733
	clone: function ( object, recursive ) {
734

735
		if ( object === undefined ) object = new THREE.Object3D();
M
Mr.doob 已提交
736
		if ( recursive === undefined ) recursive = true;
737 738 739 740 741 742

		object.name = this.name;

		object.up.copy( this.up );

		object.position.copy( this.position );
743
		object.quaternion.copy( this.quaternion );
744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762
		object.scale.copy( this.scale );

		object.renderDepth = this.renderDepth;

		object.rotationAutoUpdate = this.rotationAutoUpdate;

		object.matrix.copy( this.matrix );
		object.matrixWorld.copy( this.matrixWorld );

		object.matrixAutoUpdate = this.matrixAutoUpdate;
		object.matrixWorldNeedsUpdate = this.matrixWorldNeedsUpdate;

		object.visible = this.visible;

		object.castShadow = this.castShadow;
		object.receiveShadow = this.receiveShadow;

		object.frustumCulled = this.frustumCulled;

M
Mr.doob 已提交
763 764
		object.userData = JSON.parse( JSON.stringify( this.userData ) );

M
Mr.doob 已提交
765
		if ( recursive === true ) {
M
Mr.doob 已提交
766

M
Mr.doob 已提交
767 768 769 770 771 772
			for ( var i = 0; i < this.children.length; i ++ ) {

				var child = this.children[ i ];
				object.add( child.clone() );

			}
773 774 775

		}

776
		return object;
777

778
	}
779

780
};
781

M
Mr.doob 已提交
782 783
THREE.EventDispatcher.prototype.apply( THREE.Object3D.prototype );

784
THREE.Object3DIdCount = 0;