Object3D.js 3.9 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() {

9 10 11
	this.parent = undefined;
	this.children = [];

12
	this.up = new THREE.Vector3( 0, 1, 0 );
13

14 15
	this.position = new THREE.Vector3();
	this.rotation = new THREE.Vector3();
16
	this.eulerOrder = 'XYZ';
17
	this.scale = new THREE.Vector3( 1, 1, 1 );
18

19
	this.dynamic = false; // when true it retains arrays so they can be updated with __dirty*
20 21 22
	
	this.doubleSided = false;
	this.flipSided = false;
23

M
Mr.doob 已提交
24 25
	this.renderDepth = null;

26 27
	this.rotationAutoUpdate = true;

28 29
	this.matrix = new THREE.Matrix4();
	this.matrixWorld = new THREE.Matrix4();
30
	this.matrixRotationWorld = new THREE.Matrix4();
31

32
	this.matrixAutoUpdate = true;
33
	this.matrixWorldNeedsUpdate = true;
34

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

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

41
	this.visible = true;
42

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

45
	this.name = "";
46

47 48 49
};


M
Mr.doob 已提交
50
THREE.Object3D.prototype = {
51

52 53 54 55 56 57 58 59
	translate : function ( distance, axis ) {

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

	},

	translateX : function ( distance ) {
60

61
		this.translate( distance, this._vector.set( 1, 0, 0 ) );
62 63 64

	},

65
	translateY : function ( distance ) {
66

67
		this.translate( distance, this._vector.set( 0, 1, 0 ) );
68 69 70

	},

71
	translateZ : function ( distance ) {
72

73
		this.translate( distance, this._vector.set( 0, 0, 1 ) );
74 75

	},
76

M
Mr.doob 已提交
77
	lookAt : function ( vector ) {
78 79 80

		// TODO: Add hierarchy support.

M
Mr.doob 已提交
81
		this.matrix.lookAt( vector, this.position, this.up );
82 83 84 85 86 87

		if ( this.rotationAutoUpdate ) {

			this.rotation.setRotationFromMatrix( this.matrix );

		}
88 89 90

	},

91
	addChild: function ( child ) {
92

93
		if ( this.children.indexOf( child ) === - 1 ) {
A
alteredq 已提交
94

95
			if( child.parent !== undefined ) {
A
alteredq 已提交
96

97
				child.parent.removeChild( child );
98

A
alteredq 已提交
99 100
			}

101 102
			child.parent = this;
			this.children.push( child );
M
Mr.doob 已提交
103

M
Mr.doob 已提交
104
			// add to scene
105

M
Mr.doob 已提交
106
			var scene = this;
107

108
			while ( scene.parent !== undefined ) {
109

M
Mr.doob 已提交
110
				scene = scene.parent;
111

112 113
			}

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

M
Mr.doob 已提交
116 117
				scene.addChildRecurse( child );

118 119
			}

120
		}
M
Mr.doob 已提交
121

122
	},
123

124
	removeChild: function ( child ) {
125

126
		var childIndex = this.children.indexOf( child );
127

128
		if ( childIndex !== - 1 ) {
129

130
			child.parent = undefined;
M
Mr.doob 已提交
131
			this.children.splice( childIndex, 1 );
132

M
Mr.doob 已提交
133
		}
134

M
Mr.doob 已提交
135
	},
M
Mr.doob 已提交
136

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

139
		var c, cl, child, recurseResult;
M
Mr.doob 已提交
140 141 142

		for ( c = 0, cl = this.children.length; c < cl; c++ ) {

143
			child = this.children[ c ];
M
Mr.doob 已提交
144 145 146

			if ( child.name === name ) {

147
				return child;
M
Mr.doob 已提交
148

149
			}
M
Mr.doob 已提交
150 151 152

			if ( doRecurse ) {

153
				recurseResult = child.getChildByName( name, doRecurse );
M
Mr.doob 已提交
154 155 156

				if ( recurseResult !== undefined ) {

157
					return recurseResult;
M
Mr.doob 已提交
158

159
				}
M
Mr.doob 已提交
160

161
			}
M
Mr.doob 已提交
162

163
		}
M
Mr.doob 已提交
164

165
		return undefined;
M
Mr.doob 已提交
166

167
	},
168

M
Mr.doob 已提交
169
	updateMatrix: function () {
170

171
		this.matrix.setPosition( this.position );
172

M
Mr.doob 已提交
173
		if ( this.useQuaternion )  {
174

175
			this.matrix.setRotationFromQuaternion( this.quaternion );
176

M
Mr.doob 已提交
177
		} else {
178

179
			this.matrix.setRotationFromEuler( this.rotation, this.eulerOrder );
180

M
Mr.doob 已提交
181
		}
182

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

185
			this.matrix.scale( this.scale );
M
Mr.doob 已提交
186
			this.boundRadiusScale = Math.max( this.scale.x, Math.max( this.scale.y, this.scale.z ) );
187

M
Mr.doob 已提交
188
		}
189

190
		this.matrixWorldNeedsUpdate = true;
191

M
Mr.doob 已提交
192
	},
193

194
	update: function ( parentMatrixWorld, forceUpdate, camera ) {
195

196
		this.matrixAutoUpdate && this.updateMatrix();
197

198
		// update matrixWorld
199

200
		if ( this.matrixWorldNeedsUpdate || forceUpdate ) {
201

202
			if ( parentMatrixWorld ) {
203

204
				this.matrixWorld.multiply( parentMatrixWorld, this.matrix );
205

206
			} else {
207

208
				this.matrixWorld.copy( this.matrix );
209

210
			}
211

212
			this.matrixRotationWorld.extractRotation( this.matrixWorld, this.scale );
213

214
			this.matrixWorldNeedsUpdate = false;
215

216
			forceUpdate = true;
217

218
		}
219

220
		// update children
221

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

224
			this.children[ i ].update( this.matrixWorld, forceUpdate, camera );
225

M
Mr.doob 已提交
226
		}
227

M
Mr.doob 已提交
228
	}
229

230
};