Object3D.js 4.7 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
 */

7 8
THREE.Object3D = function() {

M
Mr.doob 已提交
9 10
	this.name = '';

11 12
	this.id = THREE.Object3DCount ++;

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
	this.dynamic = false; // when true it retains arrays so they can be updated with __dirty*
24

25 26
	this.doubleSided = false;
	this.flipSided = false;
27

M
Mr.doob 已提交
28 29
	this.renderDepth = null;

30 31
	this.rotationAutoUpdate = true;

32 33
	this.matrix = new THREE.Matrix4();
	this.matrixWorld = new THREE.Matrix4();
34
	this.matrixRotationWorld = new THREE.Matrix4();
35

36
	this.matrixAutoUpdate = true;
37
	this.matrixWorldNeedsUpdate = true;
38

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

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

45
	this.visible = true;
46

47 48 49
	this.castShadow = false;
	this.receiveShadow = false;

A
alteredq 已提交
50 51
	this.frustumCulled = true;

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

54 55 56
};


M
Mr.doob 已提交
57
THREE.Object3D.prototype = {
58

59 60 61
	constructor: THREE.Object3D,

	translate: function ( distance, axis ) {
62 63 64 65 66 67

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

	},

68
	translateX: function ( distance ) {
69

70
		this.translate( distance, this._vector.set( 1, 0, 0 ) );
71 72 73

	},

74
	translateY: function ( distance ) {
75

76
		this.translate( distance, this._vector.set( 0, 1, 0 ) );
77 78 79

	},

80
	translateZ: function ( distance ) {
81

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

	},
85

86
	lookAt: function ( vector ) {
87 88 89

		// TODO: Add hierarchy support.

M
Mr.doob 已提交
90 91 92 93 94 95 96 97 98
		if ( this instanceof THREE.Camera ) {

			this.matrix.lookAt( this.position, vector, this.up );

		} else {

			this.matrix.lookAt( vector, this.position, this.up );

		}
99 100 101 102 103 104

		if ( this.rotationAutoUpdate ) {

			this.rotation.setRotationFromMatrix( this.matrix );

		}
105 106 107

	},

108
	add: function ( object ) {
109

110
		if ( this.children.indexOf( object ) === - 1 ) {
A
alteredq 已提交
111

112
			if( object.parent !== undefined ) {
A
alteredq 已提交
113

114
				object.parent.removeChild( object );
115

A
alteredq 已提交
116 117
			}

118 119
			object.parent = this;
			this.children.push( object );
M
Mr.doob 已提交
120

M
Mr.doob 已提交
121
			// add to scene
122

M
Mr.doob 已提交
123
			var scene = this;
124

125
			while ( scene.parent !== undefined ) {
126

M
Mr.doob 已提交
127
				scene = scene.parent;
128

129 130
			}

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

133
				scene.addChildRecurse( object );
M
Mr.doob 已提交
134

135 136
			}

137
		}
M
Mr.doob 已提交
138

139
	},
140

141
	remove: function ( object ) {
142

143
		var scene = this;
144

145
		var childIndex = this.children.indexOf( object );
146

147
		if ( childIndex !== - 1 ) {
148

149
			object.parent = undefined;
M
Mr.doob 已提交
150
			this.children.splice( childIndex, 1 );
151

152 153 154 155 156 157 158 159 160 161
			// remove from scene

			while ( scene.parent !== undefined ) {

				scene = scene.parent;

			}

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

162
				scene.removeChildRecurse( object );
163 164 165

			}

M
Mr.doob 已提交
166
		}
167

M
Mr.doob 已提交
168
	},
M
Mr.doob 已提交
169

170
	getChildByName: function ( name, doRecurse ) {
M
Mr.doob 已提交
171

172
		var c, cl, child, recurseResult;
M
Mr.doob 已提交
173

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

176
			child = this.children[ c ];
M
Mr.doob 已提交
177 178 179

			if ( child.name === name ) {

180
				return child;
M
Mr.doob 已提交
181

182
			}
M
Mr.doob 已提交
183 184 185

			if ( doRecurse ) {

186
				recurseResult = child.getChildByName( name, doRecurse );
M
Mr.doob 已提交
187 188 189

				if ( recurseResult !== undefined ) {

190
					return recurseResult;
M
Mr.doob 已提交
191

192
				}
M
Mr.doob 已提交
193

194
			}
M
Mr.doob 已提交
195

196
		}
M
Mr.doob 已提交
197

198
		return undefined;
M
Mr.doob 已提交
199

200
	},
201

M
Mr.doob 已提交
202
	updateMatrix: function () {
203

204
		this.matrix.setPosition( this.position );
205

M
Mr.doob 已提交
206
		if ( this.useQuaternion )  {
207

208
			this.matrix.setRotationFromQuaternion( this.quaternion );
209

M
Mr.doob 已提交
210
		} else {
211

212
			this.matrix.setRotationFromEuler( this.rotation, this.eulerOrder );
213

M
Mr.doob 已提交
214
		}
215

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

218
			this.matrix.scale( this.scale );
M
Mr.doob 已提交
219
			this.boundRadiusScale = Math.max( this.scale.x, Math.max( this.scale.y, this.scale.z ) );
220

M
Mr.doob 已提交
221
		}
222

223
		this.matrixWorldNeedsUpdate = true;
224

M
Mr.doob 已提交
225
	},
226

227
	update: function ( parentMatrixWorld, forceUpdate, camera ) {
228

229
		this.matrixAutoUpdate && this.updateMatrix();
230

231
		// update matrixWorld
232

233
		if ( this.matrixWorldNeedsUpdate || forceUpdate ) {
234

235
			if ( parentMatrixWorld ) {
236

237
				this.matrixWorld.multiply( parentMatrixWorld, this.matrix );
238

239
			} else {
240

241
				this.matrixWorld.copy( this.matrix );
242

243
			}
244

245
			this.matrixRotationWorld.extractRotation( this.matrixWorld, this.scale );
246

247
			this.matrixWorldNeedsUpdate = false;
248

249
			forceUpdate = true;
250

251
		}
252

253
		// update children
254

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

257
			this.children[ i ].update( this.matrixWorld, forceUpdate, camera );
258

M
Mr.doob 已提交
259
		}
260

261 262 263 264 265 266
	},

	// DEPRECATED

	addChild: function ( child ) {

M
Mr.doob 已提交
267
		console.warn( 'DEPRECATED: Object3D.addChild() is now Object3D.add().' );
268 269 270 271 272 273
		this.add( child );

	},

	removeChild: function ( child ) {

M
Mr.doob 已提交
274
		console.warn( 'DEPRECATED: Object3D.removeChild() is now Object3D.remove().' );
275 276
		this.remove( child );

277
	}
278

279
};
280 281

THREE.Object3DCount = 0;