Object3D.js 4.8 KB
Newer Older
M
Mr.doob 已提交
1 2
/**
 * @author mr.doob / 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

9 10
	this.id = THREE.Object3DCount ++;

11 12
	this.name = '';

13 14 15
	this.parent = undefined;
	this.children = [];

16
	this.up = new THREE.Vector3( 0, 1, 0 );
17

18 19
	this.position = new THREE.Vector3();
	this.rotation = new THREE.Vector3();
20
	this.eulerOrder = 'XYZ';
21
	this.scale = new THREE.Vector3( 1, 1, 1 );
22

23 24
	this.doubleSided = false;
	this.flipSided = false;
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
		this.rotation.getRotationFromMatrix( this.matrix, this.scale );
65
		this.position.getPositionFromMatrix( this.matrix );
M
Mr.doob 已提交
66 67 68

	},

69
	translate: function ( distance, axis ) {
70 71 72 73 74 75

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

	},

76
	translateX: function ( distance ) {
77

78
		this.translate( distance, this._vector.set( 1, 0, 0 ) );
79 80 81

	},

82
	translateY: function ( distance ) {
83

84
		this.translate( distance, this._vector.set( 0, 1, 0 ) );
85 86 87

	},

88
	translateZ: function ( distance ) {
89

90
		this.translate( distance, this._vector.set( 0, 0, 1 ) );
91 92

	},
93

94
	lookAt: function ( vector ) {
95 96 97

		// TODO: Add hierarchy support.

M
Mr.doob 已提交
98
		this.matrix.lookAt( vector, this.position, this.up );
99 100 101

		if ( this.rotationAutoUpdate ) {

102
			this.rotation.getRotationFromMatrix( this.matrix );
103 104

		}
105 106 107

	},

108
	add: function ( object ) {
109

110 111
		if ( object === this ) {

112
			console.warn( 'THREE.Object3D.add: An object can\'t be added as a child of itself.' );
113 114 115 116
			return;

		}

117
		if ( object instanceof THREE.Object3D ) { // && this.children.indexOf( object ) === - 1
A
alteredq 已提交
118

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

M
Mr.doob 已提交
121
				object.parent.remove( object );
122

A
alteredq 已提交
123 124
			}

125 126
			object.parent = this;
			this.children.push( object );
M
Mr.doob 已提交
127

A
alteredq 已提交
128 129 130 131 132 133 134 135 136 137 138 139
			// add to scene

			var scene = this;

			while ( scene.parent !== undefined ) {

				scene = scene.parent;

			}

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

140
				scene.__addObject( object );
A
alteredq 已提交
141 142 143

			}

144
		}
M
Mr.doob 已提交
145

146
	},
147

148
	remove: function ( object ) {
149

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

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

154
			object.parent = undefined;
M
Mr.doob 已提交
155
			this.children.splice( index, 1 );
156

A
alteredq 已提交
157 158 159 160 161 162 163 164 165 166 167 168
			// remove from scene

			var scene = this;

			while ( scene.parent !== undefined ) {

				scene = scene.parent;

			}

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

169
				scene.__removeObject( object );
A
alteredq 已提交
170 171 172

			}

M
Mr.doob 已提交
173
		}
174

M
Mr.doob 已提交
175
	},
M
Mr.doob 已提交
176

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

M
Mr.doob 已提交
179
		var c, cl, child;
M
Mr.doob 已提交
180

181
		for ( c = 0, cl = this.children.length; c < cl; c ++ ) {
M
Mr.doob 已提交
182

183
			child = this.children[ c ];
M
Mr.doob 已提交
184 185 186

			if ( child.name === name ) {

187
				return child;
M
Mr.doob 已提交
188

189
			}
M
Mr.doob 已提交
190

M
Mr.doob 已提交
191
			if ( recursive ) {
M
Mr.doob 已提交
192

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

M
Mr.doob 已提交
195
				if ( child !== undefined ) {
M
Mr.doob 已提交
196

M
Mr.doob 已提交
197
					return child;
M
Mr.doob 已提交
198

199
				}
M
Mr.doob 已提交
200

201
			}
M
Mr.doob 已提交
202

203
		}
M
Mr.doob 已提交
204

205
		return undefined;
M
Mr.doob 已提交
206

207
	},
208

M
Mr.doob 已提交
209
	updateMatrix: function () {
210

211
		this.matrix.setPosition( this.position );
212

M
Mr.doob 已提交
213
		if ( this.useQuaternion )  {
214

215
			this.matrix.setRotationFromQuaternion( this.quaternion );
216

M
Mr.doob 已提交
217
		} else {
218

219
			this.matrix.setRotationFromEuler( this.rotation, this.eulerOrder );
220

M
Mr.doob 已提交
221
		}
222

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

225
			this.matrix.scale( this.scale );
M
Mr.doob 已提交
226
			this.boundRadiusScale = Math.max( this.scale.x, Math.max( this.scale.y, this.scale.z ) );
227

M
Mr.doob 已提交
228
		}
229

230
		this.matrixWorldNeedsUpdate = true;
231

M
Mr.doob 已提交
232
	},
233

M
Mr.doob 已提交
234
	updateMatrixWorld: function ( force ) {
235

236
		this.matrixAutoUpdate && this.updateMatrix();
237

238
		// update matrixWorld
239

M
Mr.doob 已提交
240
		if ( this.matrixWorldNeedsUpdate || force ) {
241

M
Mr.doob 已提交
242
			if ( this.parent ) {
243

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

246
			} else {
247

248
				this.matrixWorld.copy( this.matrix );
249

250
			}
251

252
			this.matrixWorldNeedsUpdate = false;
253

M
Mr.doob 已提交
254
			force = true;
255

256
		}
257

258
		// update children
259

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

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

M
Mr.doob 已提交
264
		}
265

266 267 268 269 270 271 272 273 274 275 276 277 278 279
	},

	worldToLocal: function ( vector ) {

		if ( !this.__inverseMatrixWorld ) this.__inverseMatrixWorld = new THREE.Matrix4();

		return this.__inverseMatrixWorld.getInverse( this.matrixWorld ).multiplyVector3( vector );

	},

	localToWorld: function ( vector ) {

		return this.matrixWorld.multiplyVector3( vector );

280
	}
281

282
};
283 284

THREE.Object3DCount = 0;