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

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.scale = new THREE.Vector3( 1, 1, 1 );
17

18 19
	this.rotationAutoUpdate = true;

20 21
	this.matrix = new THREE.Matrix4();
	this.matrixWorld = new THREE.Matrix4();
22
	this.matrixRotationWorld = new THREE.Matrix4();
23

24
	this.matrixAutoUpdate = true;
25
	this.matrixWorldNeedsUpdate = true;
26

27 28 29 30 31 32
	this.quaternion = new THREE.Quaternion();
	this.useQuaternion = false;

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

33
	this.visible = true;
34

35
	this._vector = new THREE.Vector3();
36 37
	
	this.name = "";
38

39 40 41
};


M
Mr.doob 已提交
42
THREE.Object3D.prototype = {
43

44 45 46 47 48 49 50 51
	translate : function ( distance, axis ) {

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

	},

	translateX : function ( distance ) {
52

53
		this.translate( distance, this._vector.set( 1, 0, 0 ) );
54 55 56

	},

57
	translateY : function ( distance ) {
58

59
		this.translate( distance, this._vector.set( 0, 1, 0 ) );
60 61 62

	},

63
	translateZ : function ( distance ) {
64

65
		this.translate( distance, this._vector.set( 0, 0, 1 ) );
66 67

	},
68

M
Mr.doob 已提交
69
	lookAt : function ( vector ) {
70 71 72

		// TODO: Add hierarchy support.

M
Mr.doob 已提交
73
		this.matrix.lookAt( vector, this.position, this.up );
74 75 76 77 78 79

		if ( this.rotationAutoUpdate ) {

			this.rotation.setRotationFromMatrix( this.matrix );

		}
80 81 82

	},

83
	addChild: function ( child ) {
84

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

87
			if( child.parent !== undefined ) {
A
alteredq 已提交
88

89
				child.parent.removeChild( child );
90

A
alteredq 已提交
91 92
			}

93 94
			child.parent = this;
			this.children.push( child );
M
Mr.doob 已提交
95

M
Mr.doob 已提交
96
			// add to scene
97

M
Mr.doob 已提交
98
			var scene = this;
99

100
			while ( scene.parent !== undefined ) {
101

M
Mr.doob 已提交
102
				scene = scene.parent;
103

104 105
			}

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

M
Mr.doob 已提交
108 109
				scene.addChildRecurse( child );

110 111
			}

112
		}
M
Mr.doob 已提交
113

114
	},
115

116
	removeChild: function ( child ) {
117

118
		var childIndex = this.children.indexOf( child );
119

120
		if ( childIndex !== - 1 ) {
121

122
			child.parent = undefined;
M
Mr.doob 已提交
123
			this.children.splice( childIndex, 1 );
124

M
Mr.doob 已提交
125
		}
126

M
Mr.doob 已提交
127
	},
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
	
	getChildByName: function ( name, doRecurse ) {
		
		var c, cl, child, recurseResult;
		
		for( c = 0, cl = this.children.length; c < cl; c++ ) {
			
			child = this.children[ c ];
			
			if( child.name === name ) {
				
				return child;
				
			}
			
			if( doRecurse ) {
				
				recurseResult = child.getChildByName( name, doRecurse );
				
				if( recurseResult !== undefined ) {
					
					return recurseResult;
					
				}
				
			}
			
		}
		
		return undefined;
		
	},
160

M
Mr.doob 已提交
161
	updateMatrix: function () {
162

163
		this.matrix.setPosition( this.position );
164

M
Mr.doob 已提交
165
		if ( this.useQuaternion )  {
166

167
			this.matrix.setRotationFromQuaternion( this.quaternion );
168

M
Mr.doob 已提交
169
		} else {
170

171
			this.matrix.setRotationFromEuler( this.rotation );
172

M
Mr.doob 已提交
173
		}
174

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

177
			this.matrix.scale( this.scale );
M
Mr.doob 已提交
178
			this.boundRadiusScale = Math.max( this.scale.x, Math.max( this.scale.y, this.scale.z ) );
179

M
Mr.doob 已提交
180
		}
181

182
		this.matrixWorldNeedsUpdate = true;
183

M
Mr.doob 已提交
184
	},
185

186
	update: function ( parentMatrixWorld, forceUpdate, camera ) {
187

188
		this.matrixAutoUpdate && this.updateMatrix();
189

190
		// update matrixWorld
191

192
		if ( this.matrixWorldNeedsUpdate || forceUpdate ) {
193

194
			if ( parentMatrixWorld ) {
195

196
				this.matrixWorld.multiply( parentMatrixWorld, this.matrix );
197

198
			} else {
199

200
				this.matrixWorld.copy( this.matrix );
201

202
			}
203

204
			this.matrixRotationWorld.extractRotation( this.matrixWorld, this.scale );
205

206
			this.matrixWorldNeedsUpdate = false;
207

208
			forceUpdate = true;
209

210
		}
211

212
		// update children
213

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

216
			this.children[ i ].update( this.matrixWorld, forceUpdate, camera );
217

M
Mr.doob 已提交
218
		}
219

M
Mr.doob 已提交
220
	}
221

222
};