Object3D.js 4.4 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 62 63 64 65 66 67
	applyMatrix: function ( matrix ) {

		this.matrix.multiply( matrix, this.matrix );
		this.position.getPositionFromMatrix( this.matrix );
		this.rotation.getRotationFromMatrix( this.matrix );
		this.scale.getScaleFromMatrix( this.matrix );

	},

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

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

	},

75
	translateX: function ( distance ) {
76

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

	},

81
	translateY: function ( distance ) {
82

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

	},

87
	translateZ: function ( distance ) {
88

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

	},
92

93
	lookAt: function ( vector ) {
94 95 96

		// TODO: Add hierarchy support.

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

		if ( this.rotationAutoUpdate ) {

M
Mr.doob 已提交
101
			this.rotation.getRotationFromMatrix( this.matrix );
102 103

		}
104 105 106

	},

107
	add: function ( object ) {
108

109 110 111 112 113 114 115
		if ( object === this ) {

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

		}

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

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

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

A
alteredq 已提交
122 123
			}

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

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

			var scene = this;

			while ( scene.parent !== undefined ) {

				scene = scene.parent;

			}

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

				scene.addObject( object );

			}

143
		}
M
Mr.doob 已提交
144

145
	},
146

147
	remove: function ( object ) {
148

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

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

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

A
alteredq 已提交
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
			// 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 已提交
172
		}
173

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

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

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

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

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

			if ( child.name === name ) {

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

188
			}
M
Mr.doob 已提交
189 190 191

			if ( doRecurse ) {

192
				recurseResult = child.getChildByName( name, doRecurse );
M
Mr.doob 已提交
193 194 195

				if ( recurseResult !== undefined ) {

196
					return recurseResult;
M
Mr.doob 已提交
197

198
				}
M
Mr.doob 已提交
199

200
			}
M
Mr.doob 已提交
201

202
		}
M
Mr.doob 已提交
203

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

206
	},
207

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

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

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

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

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

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

M
Mr.doob 已提交
220
		}
221

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

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

M
Mr.doob 已提交
227
		}
228

229
		this.matrixWorldNeedsUpdate = true;
230

M
Mr.doob 已提交
231
	},
232

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

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

237
		// update matrixWorld
238

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

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

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

245
			} else {
246

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

249
			}
250

251
			this.matrixWorldNeedsUpdate = false;
252

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

255
		}
256

257
		// update children
258

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

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

M
Mr.doob 已提交
263
		}
264

265
	}
266

267
};
268 269

THREE.Object3DCount = 0;