Object3D.js 3.0 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 13
	this.up = new THREE.Vector3( 0.0, 1.0, 0.0 );

14 15 16
	this.position = new THREE.Vector3();
	this.rotation = new THREE.Vector3();
	this.scale = new THREE.Vector3( 1.0, 1.0, 1.0 );
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 36 37
};


M
Mr.doob 已提交
38
THREE.Object3D.prototype = {
39

40
	/*
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
	translateX : function () {

		

	},

	translateY : function () {

		

	},

	translateZ : function () {

		

	},
58 59
	*/

M
Mr.doob 已提交
60
	lookAt : function ( vector ) {
61 62 63

		// TODO: Add hierarchy support.

M
Mr.doob 已提交
64
		this.matrix.lookAt( this.position, vector, this.up );
65 66 67 68 69 70

		if ( this.rotationAutoUpdate ) {

			this.rotation.setRotationFromMatrix( this.matrix );

		}
71 72 73

	},

74
	addChild: function ( child ) {
75

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

78
			if( child.parent !== undefined ) {
A
alteredq 已提交
79

80
				child.parent.removeChild( child );
81

A
alteredq 已提交
82 83
			}

84 85
			child.parent = this;
			this.children.push( child );
M
Mr.doob 已提交
86

M
Mr.doob 已提交
87
			// add to scene
88

M
Mr.doob 已提交
89
			var scene = this;
90

91 92
			while ( scene instanceof THREE.Scene === false && scene !== undefined ) {

M
Mr.doob 已提交
93
				scene = scene.parent;
94

95 96 97 98
			}

			if ( scene !== undefined )  {

M
Mr.doob 已提交
99 100
				scene.addChildRecurse( child );

101 102
			}

103
		}
M
Mr.doob 已提交
104

105
	},
106

107
	removeChild: function ( child ) {
108

109
		var childIndex = this.children.indexOf( child );
110

111
		if ( childIndex !== - 1 ) {
112

113
			child.parent = undefined;
M
Mr.doob 已提交
114
			this.children.splice( childIndex, 1 );
115

M
Mr.doob 已提交
116
		}
117

M
Mr.doob 已提交
118
	},
119

M
Mr.doob 已提交
120
	updateMatrix: function () {
121

122
		this.matrix.setPosition( this.position );
123

M
Mr.doob 已提交
124
		if ( this.useQuaternion )  {
125

126
			this.matrix.setRotationFromQuaternion( this.quaternion );
127

M
Mr.doob 已提交
128
		} else {
129

130
			this.matrix.setRotationFromEuler( this.rotation );
131

M
Mr.doob 已提交
132
		}
133

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

136
			this.matrix.scale( this.scale );
M
Mr.doob 已提交
137
			this.boundRadiusScale = Math.max( this.scale.x, Math.max( this.scale.y, this.scale.z ) );
138

M
Mr.doob 已提交
139
		}
140

M
Mr.doob 已提交
141
		return true;
142

M
Mr.doob 已提交
143
	},
144

145
	update: function ( parentMatrixWorld, forceUpdate, camera ) {
146

147
		if ( this.visible ) {
148

149
			if ( this.matrixAutoUpdate ) {
150

151
				forceUpdate |= this.updateMatrix();
152

153
			}
154

155
			// update matrixWorld
156

157
			if ( forceUpdate || this.matrixWorldNeedsUpdate ) {
158

159
				if ( parentMatrixWorld ) {
160

161
					this.matrixWorld.multiply( parentMatrixWorld, this.matrix );
162

163 164 165 166 167 168
				} else {

					this.matrixWorld.copy( this.matrix );

				}

169
				this.matrixRotationWorld.extractRotation( this.matrixWorld, this.scale );
170

171
				this.matrixWorldNeedsUpdate = false;
172 173 174 175 176 177
				forceUpdate = true;

			}

			// update children

M
Mr.doob 已提交
178
			for ( var i = 0, l = this.children.length; i < l; i ++ ) {
179 180 181 182

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

			}
183

M
Mr.doob 已提交
184
		}
185

M
Mr.doob 已提交
186
	}
187

M
Mr.doob 已提交
188
}