Object3D.js 12.8 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
E
elephantatwork 已提交
6
 * @author elephantatwork / www.elephantatwork.ch
M
Mr.doob 已提交
7 8
 */

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

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

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

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

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

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

23 24 25 26 27
	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 已提交
28
	var onRotationChange = function () {
29
		quaternion.setFromEuler( rotation, false );
M
Mr.doob 已提交
30
	};
31

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

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

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

58 59
	this.rotationAutoUpdate = true;

60 61
	this.matrix = new THREE.Matrix4();
	this.matrixWorld = new THREE.Matrix4();
62

63
	this.matrixAutoUpdate = true;
64
	this.matrixWorldNeedsUpdate = false;
65 66

	this.visible = true;
67

68 69 70
	this.castShadow = false;
	this.receiveShadow = false;

A
alteredq 已提交
71
	this.frustumCulled = true;
72
	this.renderOrder = 0;
A
alteredq 已提交
73

74 75
	this.userData = {};

76 77
};

78
THREE.Object3D.DefaultUp = new THREE.Vector3( 0, 1, 0 );
79

M
Mr.doob 已提交
80
THREE.Object3D.prototype = {
81

82 83
	constructor: THREE.Object3D,

84 85
	get eulerOrder () {

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

		return this.rotation.order;

	},

	set eulerOrder ( value ) {

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

		this.rotation.order = value;

	},

	get useQuaternion () {

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

	},

	set useQuaternion ( value ) {

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

	},

112 113
	set renderDepth ( value ) {

114
		console.warn( 'THREE.Object3D: .renderDepth has been removed. Use .renderOrder, instead.' );
115 116 117

	},

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

W
WestLangley 已提交
120
		this.matrix.multiplyMatrices( matrix, this.matrix );
A
alteredq 已提交
121

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

W
WestLangley 已提交
124
	},
M
Mr.doob 已提交
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 152 153 154 155
	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 );

	},

156
	rotateOnAxis: function () {
157

158 159 160 161 162 163 164 165
		// 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 );
166

167
			this.quaternion.multiply( q1 );
168 169 170 171 172 173 174

			return this;

		}

	}(),

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 207 208 209 210
	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 );

		};

	}(),

211 212 213 214 215 216 217 218
	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 ) {
219

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

222
			this.position.add( v1.multiplyScalar( distance ) );
223

224 225
			return this;

226
		}
227 228 229

	}(),

230 231
	translate: function ( distance, axis ) {

232
		console.warn( 'THREE.Object3D: .translate() has been removed. Use .translateOnAxis( axis, distance ) instead.' );
233 234 235 236
		return this.translateOnAxis( axis, distance );

	},

237 238 239 240 241 242
	translateX: function () {

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

		return function ( distance ) {

243
			return this.translateOnAxis( v1, distance );
244 245 246 247 248 249 250 251 252 253 254

		};

	}(),

	translateY: function () {

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

		return function ( distance ) {

255
			return this.translateOnAxis( v1, distance );
256 257 258 259 260 261 262 263 264 265 266

		};

	}(),

	translateZ: function () {

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

		return function ( distance ) {

267
			return this.translateOnAxis( v1, distance );
268 269 270 271

		};

	}(),
272

M
Mr.doob 已提交
273 274
	localToWorld: function ( vector ) {

275
		return vector.applyMatrix4( this.matrixWorld );
M
Mr.doob 已提交
276 277 278

	},

279
	worldToLocal: function () {
M
Mr.doob 已提交
280

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

283 284 285 286 287
		return function ( vector ) {

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

		};
M
Mr.doob 已提交
288

289
	}(),
290

291
	lookAt: function () {
292

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

295
		var m1 = new THREE.Matrix4();
296

297
		return function ( vector ) {
298

299 300
			m1.lookAt( vector, this.position, this.up );

301
			this.quaternion.setFromRotationMatrix( m1 );
302

303
		};
304

305
	}(),
306

307
	add: function ( object ) {
308

309 310
		if ( arguments.length > 1 ) {

G
gero3 已提交
311
			for ( var i = 0; i < arguments.length; i ++ ) {
312 313 314 315 316 317 318

				this.add( arguments[ i ] );

			}

			return this;

B
brason 已提交
319
		}
320

321 322
		if ( object === this ) {

323
			console.error( "THREE.Object3D.add: object can't be added as a child of itself.", object );
324
			return this;
325 326 327

		}

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

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

M
Mr.doob 已提交
332
				object.parent.remove( object );
333

A
alteredq 已提交
334 335
			}

336
			object.parent = this;
337 338
			object.dispatchEvent( { type: 'added' } );

339
			this.children.push( object );
M
Mr.doob 已提交
340

M
Mr.doob 已提交
341
		} else {
M
Mr.doob 已提交
342

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

345
		}
M
Mr.doob 已提交
346

347 348
		return this;

349
	},
350

351
	remove: function ( object ) {
352

353 354
		if ( arguments.length > 1 ) {

G
gero3 已提交
355
			for ( var i = 0; i < arguments.length; i ++ ) {
356 357 358 359 360

				this.remove( arguments[ i ] );

			}

B
brason 已提交
361
		}
362

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

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

367
			object.parent = undefined;
368

369 370
			object.dispatchEvent( { type: 'removed' } );

371
			this.children.splice( index, 1 );
372

M
Mr.doob 已提交
373
		}
374

M
Mr.doob 已提交
375
	},
376

377
	getChildByName: function ( name ) {
M
Mr.doob 已提交
378

379
		console.warn( 'THREE.Object3D: .getChildByName() has been renamed to .getObjectByName().' );
380
		return this.getObjectByName( name );
M
Mr.doob 已提交
381 382 383

	},

384
	getObjectById: function ( id ) {
385

386
		return this.getObjectByProperty( 'id', id );
387

W
Wilt 已提交
388
	},
389

390
	getObjectByName: function ( name ) {
391

392
		return this.getObjectByProperty( 'name', name );
393 394

	},
M
Mr.doob 已提交
395

396
	getObjectByProperty: function ( name, value ) {
397

M
Mr.doob 已提交
398
		if ( this[ name ] === value ) return this;
399

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

402
			var child = this.children[ i ];
403
			var object = child.getObjectByProperty( name, value );
M
Mr.doob 已提交
404

405
			if ( object !== undefined ) {
M
Mr.doob 已提交
406

407
				return object;
M
Mr.doob 已提交
408

409
			}
M
Mr.doob 已提交
410

411
		}
M
Mr.doob 已提交
412

413
		return undefined;
M
Mr.doob 已提交
414

415
	},
M
Mr.doob 已提交
416

417
	getWorldPosition: function ( optionalTarget ) {
W
WestLangley 已提交
418 419 420 421 422 423 424 425 426

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

		this.updateMatrixWorld( true );

		return result.setFromMatrixPosition( this.matrixWorld );

	},

427
	getWorldQuaternion: function () {
W
WestLangley 已提交
428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445

		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;

		}

	}(),

446
	getWorldRotation: function () {
W
WestLangley 已提交
447 448 449 450 451 452 453

		var quaternion = new THREE.Quaternion();

		return function ( optionalTarget ) {

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

M
Mr.doob 已提交
454
			this.getWorldQuaternion( quaternion );
W
WestLangley 已提交
455 456 457 458 459 460 461

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

		}

	}(),

462
	getWorldScale: function () {
W
WestLangley 已提交
463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480

		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;

		}

	}(),

481
	getWorldDirection: function () {
W
WestLangley 已提交
482 483 484 485 486 487 488

		var quaternion = new THREE.Quaternion();

		return function ( optionalTarget ) {

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

M
Mr.doob 已提交
489
			this.getWorldQuaternion( quaternion );
W
WestLangley 已提交
490 491 492 493 494 495 496

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

		}

	}(),

497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524
	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 );

		}

	},

U
unknown 已提交
525 526 527
	traverseAncestors: function ( callback ) {

		if ( this.parent ) {
528 529

			callback( this.parent );
U
unknown 已提交
530 531 532

			this.parent.traverseAncestors( callback );

533
		}
U
unknown 已提交
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 570 571 572 573 574 575 576
	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 );

		}

	},

577
	toJSON: function ( meta ) {
E
elephantatwork 已提交
578

579
		var isRootObject = ( meta === undefined );
580

581 582 583 584 585 586 587 588 589 590
		var data = {};

		// meta is a hash used to collect geometries, materials.
		// not providing it implies that this is the root object
		// being serialized.
		if ( isRootObject ) {

			// initialize meta obj
			meta = {
				geometries: {},
E
elephantatwork 已提交
591
				materials: {},
592 593
				textures: {},
				images: {}
B
brason 已提交
594
			};
595

596 597
			data.metadata = {
				version: 4.4,
598 599
				type: 'Object',
				generator: 'Object3D.toJSON'
600
			};
601

602
		}
603

604
		// standard Object3D serialization
605

606
		data.uuid = this.uuid;
M
Mr.doob 已提交
607
		data.type = this.type;
608

609 610 611
		if ( this.name !== '' ) data.name = this.name;
		if ( JSON.stringify( this.userData ) !== '{}' ) data.userData = this.userData;
		if ( this.visible !== true ) data.visible = this.visible;
612

613
		data.matrix = this.matrix.toArray();
614

615
		if ( this.children.length > 0 ) {
616

617
			data.children = [];
618

619
			for ( var i = 0; i < this.children.length; i ++ ) {
620

621
				data.children.push( this.children[ i ].toJSON( meta ).object );
622

623
			}
624

625
		}
626

M
Mr.doob 已提交
627
		var output = {};
M
Mr.doob 已提交
628

629
		if ( isRootObject ) {
M
Mr.doob 已提交
630

M
Mr.doob 已提交
631 632
			var geometries = extractFromCache( meta.geometries );
			var materials = extractFromCache( meta.materials );
633 634
			var textures = extractFromCache( meta.textures );
			var images = extractFromCache( meta.images );
635

M
Mr.doob 已提交
636 637
			if ( geometries.length > 0 ) output.geometries = geometries;
			if ( materials.length > 0 ) output.materials = materials;
638 639
			if ( textures.length > 0 ) output.textures = textures;
			if ( images.length > 0 ) output.images = images;
640

641
		}
642

M
Mr.doob 已提交
643 644
		output.object = data;

645 646 647 648 649 650 651 652 653 654 655 656 657 658
		return output;

		// extract data from the cache hash
		// remove metadata on each item
		// and return as array
		function extractFromCache ( cache ) {
			var values = [];
			for ( var key in cache ) {
				var data = cache[ key ];
				delete data.metadata;
				values.push( data );
			}
			return values;
		}
659 660 661

	},

662 663 664 665 666 667 668 669
	clone: function ( recursive ) {

		var object = new THREE.Object3D();
		return this.cloneProperties( object, recursive );

	},

	cloneProperties: function ( object, recursive ) {
670

M
Mr.doob 已提交
671
		if ( recursive === undefined ) recursive = true;
672 673 674 675 676 677

		object.name = this.name;

		object.up.copy( this.up );

		object.position.copy( this.position );
678
		object.quaternion.copy( this.quaternion );
679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694
		object.scale.copy( this.scale );

		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;
695
		object.renderOrder = this.renderOrder;
696

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

M
Mr.doob 已提交
699
		if ( recursive === true ) {
M
Mr.doob 已提交
700

M
Mr.doob 已提交
701 702 703 704 705 706
			for ( var i = 0; i < this.children.length; i ++ ) {

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

			}
707 708 709

		}

710
		return object;
711

712
	}
713

714
};
715

M
Mr.doob 已提交
716 717
THREE.EventDispatcher.prototype.apply( THREE.Object3D.prototype );

718
THREE.Object3DIdCount = 0;