Object3D.js 4.6 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 64
		this.position.getPositionFromMatrix( this.matrix );
		this.scale.getScaleFromMatrix( this.matrix );
65
		this.rotation.getRotationFromMatrix( this.matrix, this.scale );
A
alteredq 已提交
66

67
		/*
A
alteredq 已提交
68 69
		this.matrix.decompose( this.position, this.quaternion, this.scale );
		this.rotation.getRotationFromQuaternion( this.quaternion );
70
		*/
M
Mr.doob 已提交
71 72 73

	},

74
	translate: function ( distance, axis ) {
75 76 77 78 79 80

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

	},

81
	translateX: function ( distance ) {
82

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

	},

87
	translateY: function ( distance ) {
88

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

	},

93
	translateZ: function ( distance ) {
94

95
		this.translate( distance, this._vector.set( 0, 0, 1 ) );
96 97

	},
98

99
	lookAt: function ( vector ) {
100 101 102

		// TODO: Add hierarchy support.

M
Mr.doob 已提交
103
		this.matrix.lookAt( vector, this.position, this.up );
104 105 106

		if ( this.rotationAutoUpdate ) {

107
			this.rotation.getRotationFromMatrix( this.matrix );
108 109

		}
110 111 112

	},

113
	add: function ( object ) {
114

115 116 117 118 119 120 121
		if ( object === this ) {

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

		}

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

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

M
Mr.doob 已提交
126
				object.parent.remove( object );
127

A
alteredq 已提交
128 129
			}

130 131
			object.parent = this;
			this.children.push( object );
M
Mr.doob 已提交
132

A
alteredq 已提交
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
			// add to scene

			var scene = this;

			while ( scene.parent !== undefined ) {

				scene = scene.parent;

			}

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

				scene.addObject( object );

			}

149
		}
M
Mr.doob 已提交
150

151
	},
152

153
	remove: function ( object ) {
154

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

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

159
			object.parent = undefined;
M
Mr.doob 已提交
160
			this.children.splice( index, 1 );
161

A
alteredq 已提交
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
			// remove from scene

			var scene = this;

			while ( scene.parent !== undefined ) {

				scene = scene.parent;

			}

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

				scene.removeObject( object );

			}

M
Mr.doob 已提交
178
		}
179

M
Mr.doob 已提交
180
	},
M
Mr.doob 已提交
181

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

184
		var c, cl, child, recurseResult;
M
Mr.doob 已提交
185

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

188
			child = this.children[ c ];
M
Mr.doob 已提交
189 190 191

			if ( child.name === name ) {

192
				return child;
M
Mr.doob 已提交
193

194
			}
M
Mr.doob 已提交
195 196 197

			if ( doRecurse ) {

198
				recurseResult = child.getChildByName( name, doRecurse );
M
Mr.doob 已提交
199 200 201

				if ( recurseResult !== undefined ) {

202
					return recurseResult;
M
Mr.doob 已提交
203

204
				}
M
Mr.doob 已提交
205

206
			}
M
Mr.doob 已提交
207

208
		}
M
Mr.doob 已提交
209

210
		return undefined;
M
Mr.doob 已提交
211

212
	},
213

M
Mr.doob 已提交
214
	updateMatrix: function () {
215

216
		this.matrix.setPosition( this.position );
217

M
Mr.doob 已提交
218
		if ( this.useQuaternion )  {
219

220
			this.matrix.setRotationFromQuaternion( this.quaternion );
221

M
Mr.doob 已提交
222
		} else {
223

224
			this.matrix.setRotationFromEuler( this.rotation, this.eulerOrder );
225

M
Mr.doob 已提交
226
		}
227

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

230
			this.matrix.scale( this.scale );
M
Mr.doob 已提交
231
			this.boundRadiusScale = Math.max( this.scale.x, Math.max( this.scale.y, this.scale.z ) );
232

M
Mr.doob 已提交
233
		}
234

235
		this.matrixWorldNeedsUpdate = true;
236

M
Mr.doob 已提交
237
	},
238

M
Mr.doob 已提交
239
	updateMatrixWorld: function ( force ) {
240

241
		this.matrixAutoUpdate && this.updateMatrix();
242

243
		// update matrixWorld
244

M
Mr.doob 已提交
245
		if ( this.matrixWorldNeedsUpdate || force ) {
246

M
Mr.doob 已提交
247
			if ( this.parent ) {
248

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

251
			} else {
252

253
				this.matrixWorld.copy( this.matrix );
254

255
			}
256

257
			this.matrixWorldNeedsUpdate = false;
258

M
Mr.doob 已提交
259
			force = true;
260

261
		}
262

263
		// update children
264

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

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

M
Mr.doob 已提交
269
		}
270

271
	}
272

273
};
274 275

THREE.Object3DCount = 0;