Object3D.js 6.7 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/
M
Mr.doob 已提交
5 6
 */

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

M
Mr.doob 已提交
9
	THREE.Object3DLibrary.push( this );
10

M
Mr.doob 已提交
11 12
	this.id = THREE.Object3DIdCount ++;

13
	this.name = '';
M
Mr.doob 已提交
14
	this.properties = {};
15

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

19
	this.up = new THREE.Vector3( 0, 1, 0 );
20

21 22
	this.position = new THREE.Vector3();
	this.rotation = new THREE.Vector3();
23
	this.eulerOrder = THREE.Object3D.defaultEulerOrder;
24
	this.scale = new THREE.Vector3( 1, 1, 1 );
25

M
Mr.doob 已提交
26 27
	this.renderDepth = null;

28 29
	this.rotationAutoUpdate = true;

30 31
	this.matrix = new THREE.Matrix4();
	this.matrixWorld = new THREE.Matrix4();
32
	this.matrixRotationWorld = new THREE.Matrix4();
33

34
	this.matrixAutoUpdate = true;
35
	this.matrixWorldNeedsUpdate = true;
36

37 38 39 40 41 42
	this.quaternion = new THREE.Quaternion();
	this.useQuaternion = false;

	this.boundRadius = 0.0;
	this.boundRadiusScale = 1.0;

43
	this.visible = true;
44

45 46 47
	this.castShadow = false;
	this.receiveShadow = false;

A
alteredq 已提交
48 49
	this.frustumCulled = true;

50
	this._vector = new THREE.Vector3();
M
Mr.doob 已提交
51

52 53 54
};


M
Mr.doob 已提交
55
THREE.Object3D.prototype = {
56

57 58
	constructor: THREE.Object3D,

M
Mr.doob 已提交
59 60 61
	applyMatrix: function ( matrix ) {

		this.matrix.multiply( matrix, this.matrix );
A
alteredq 已提交
62

M
Mr.doob 已提交
63
		this.scale.getScaleFromMatrix( this.matrix );
64

W
WestLangley 已提交
65 66
		var mat = new THREE.Matrix4().extractRotation( this.matrix );
		this.rotation.setEulerFromRotationMatrix( mat, this.eulerOrder );
67

68
		this.position.getPositionFromMatrix( this.matrix );
M
Mr.doob 已提交
69 70 71

	},

72
	translate: function ( distance, axis ) {
73 74 75 76 77 78

		this.matrix.rotateAxis( axis );
		this.position.addSelf( axis.multiplyScalar( distance ) );

	},

79
	translateX: function ( distance ) {
80

81
		this.translate( distance, this._vector.set( 1, 0, 0 ) );
82 83 84

	},

85
	translateY: function ( distance ) {
86

87
		this.translate( distance, this._vector.set( 0, 1, 0 ) );
88 89 90

	},

91
	translateZ: function ( distance ) {
92

93
		this.translate( distance, this._vector.set( 0, 0, 1 ) );
94 95

	},
96

M
Mr.doob 已提交
97 98 99 100 101 102 103 104 105 106 107 108
	localToWorld: function ( vector ) {

		return this.matrixWorld.multiplyVector3( vector );

	},

	worldToLocal: function ( vector ) {

		return THREE.Object3D.__m1.getInverse( this.matrixWorld ).multiplyVector3( vector );

	},

109
	lookAt: function ( vector ) {
110 111 112

		// TODO: Add hierarchy support.

M
Mr.doob 已提交
113
		this.matrix.lookAt( vector, this.position, this.up );
114 115 116

		if ( this.rotationAutoUpdate ) {

W
WestLangley 已提交
117
			this.rotation.setEulerFromRotationMatrix( this.matrix, this.eulerOrder );
118 119

		}
120 121 122

	},

123
	add: function ( object ) {
124

125 126
		if ( object === this ) {

127
			console.warn( 'THREE.Object3D.add: An object can\'t be added as a child of itself.' );
128 129 130 131
			return;

		}

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

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

M
Mr.doob 已提交
136
				object.parent.remove( object );
137

A
alteredq 已提交
138 139
			}

140 141
			object.parent = this;
			this.children.push( object );
M
Mr.doob 已提交
142

A
alteredq 已提交
143 144 145 146 147 148 149 150 151 152 153 154
			// add to scene

			var scene = this;

			while ( scene.parent !== undefined ) {

				scene = scene.parent;

			}

			if ( scene !== undefined && scene instanceof THREE.Scene )  {

155
				scene.__addObject( object );
A
alteredq 已提交
156 157 158

			}

159
		}
M
Mr.doob 已提交
160

161
	},
162

163
	remove: function ( object ) {
164

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

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

169
			object.parent = undefined;
M
Mr.doob 已提交
170
			this.children.splice( index, 1 );
171

A
alteredq 已提交
172 173 174 175 176 177 178 179 180 181 182 183
			// remove from scene

			var scene = this;

			while ( scene.parent !== undefined ) {

				scene = scene.parent;

			}

			if ( scene !== undefined && scene instanceof THREE.Scene ) {

184
				scene.__removeObject( object );
A
alteredq 已提交
185 186 187

			}

M
Mr.doob 已提交
188
		}
189

M
Mr.doob 已提交
190
	},
M
Mr.doob 已提交
191

192 193 194 195 196 197 198
	traverse: function ( callback ) {

		callback( this );

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

			this.children[ i ].traverse( callback );
M
Mr.doob 已提交
199

200
		}
M
Mr.doob 已提交
201

202 203 204 205 206
	},

	getChildByName: function ( name, recursive ) {

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

208
			var child = this.children[ i ];
M
Mr.doob 已提交
209 210 211

			if ( child.name === name ) {

212
				return child;
M
Mr.doob 已提交
213

214
			}
M
Mr.doob 已提交
215

216
			if ( recursive === true ) {
M
Mr.doob 已提交
217

M
Mr.doob 已提交
218
				child = child.getChildByName( name, recursive );
M
Mr.doob 已提交
219

M
Mr.doob 已提交
220
				if ( child !== undefined ) {
M
Mr.doob 已提交
221

M
Mr.doob 已提交
222
					return child;
M
Mr.doob 已提交
223

224
				}
M
Mr.doob 已提交
225

226
			}
M
Mr.doob 已提交
227

228
		}
M
Mr.doob 已提交
229

230
		return undefined;
M
Mr.doob 已提交
231

232
	},
M
Mr.doob 已提交
233

234 235 236 237 238 239 240 241 242 243
	getDescendants: function ( array ) {

		if ( array === undefined ) array = [];

		Array.prototype.push.apply( array, this.children );

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

			this.children[ i ].getDescendants( array );

244
		}
M
Mr.doob 已提交
245

246
		return array;
M
Mr.doob 已提交
247

L
libra guest 已提交
248
	},
249

M
Mr.doob 已提交
250
	updateMatrix: function () {
251

252
		this.matrix.setPosition( this.position );
253

M
Mr.doob 已提交
254
		if ( this.useQuaternion === false )  {
255

M
Mr.doob 已提交
256
			this.matrix.setRotationFromEuler( this.rotation, this.eulerOrder );
257

M
Mr.doob 已提交
258
		} else {
259

M
Mr.doob 已提交
260
			this.matrix.setRotationFromQuaternion( this.quaternion );
261

M
Mr.doob 已提交
262
		}
263

M
Mr.doob 已提交
264
		if ( this.scale.x !== 1 || this.scale.y !== 1 || this.scale.z !== 1 ) {
265

266
			this.matrix.scale( this.scale );
M
Mr.doob 已提交
267
			this.boundRadiusScale = Math.max( this.scale.x, Math.max( this.scale.y, this.scale.z ) );
268

M
Mr.doob 已提交
269
		}
270

271
		this.matrixWorldNeedsUpdate = true;
272

M
Mr.doob 已提交
273
	},
274

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

M
Mr.doob 已提交
277
		if ( this.matrixAutoUpdate === true ) this.updateMatrix();
278

M
Mr.doob 已提交
279
		if ( this.matrixWorldNeedsUpdate === true || force === true ) {
280

M
Mr.doob 已提交
281
			if ( this.parent === undefined ) {
282

M
Mr.doob 已提交
283
				this.matrixWorld.copy( this.matrix );
284

285
			} else {
286

M
Mr.doob 已提交
287
				this.matrixWorld.multiply( this.parent.matrixWorld, this.matrix );
288

289
			}
290

291
			this.matrixWorldNeedsUpdate = false;
292

M
Mr.doob 已提交
293
			force = true;
294

295
		}
296

297
		// update children
298

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

M
Mr.doob 已提交
301
			this.children[ i ].updateMatrixWorld( force );
302

M
Mr.doob 已提交
303
		}
304

305 306
	},

307
	clone: function ( object ) {
308

309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
		if ( object === undefined ) object = new THREE.Object3D();

		object.name = this.name;

		object.up.copy( this.up );

		object.position.copy( this.position );
		if ( object.rotation instanceof THREE.Vector3 ) object.rotation.copy( this.rotation ); // because of Sprite madness
		object.eulerOrder = this.eulerOrder;
		object.scale.copy( this.scale );

		object.renderDepth = this.renderDepth;

		object.rotationAutoUpdate = this.rotationAutoUpdate;

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

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

		object.quaternion.copy( this.quaternion );
		object.useQuaternion = this.useQuaternion;

		object.boundRadius = this.boundRadius;
		object.boundRadiusScale = this.boundRadiusScale;

		object.visible = this.visible;

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

		object.frustumCulled = this.frustumCulled;

344 345 346 347 348 349 350
		for ( var i = 0; i < this.children.length; i ++ ) {

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

		}

351
		return object;
352

353 354 355 356
	},

	deallocate: function () {

M
Mr.doob 已提交
357 358
		var index = THREE.Object3DLibrary.indexOf( this );
		if ( index !== -1 ) THREE.Object3DLibrary.splice( index, 1 );
359

360
	}
361

362
};
363

364
THREE.Object3D.__m1 = new THREE.Matrix4();
365
THREE.Object3D.defaultEulerOrder = 'XYZ',
366

M
Mr.doob 已提交
367
THREE.Object3DIdCount = 0;
M
Mr.doob 已提交
368
THREE.Object3DLibrary = [];