Object3D.js 2.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 9
THREE.Object3D = function() {

	this.id = THREE.Object3DCounter.value ++;
10 11 12 13 14

	this.parent = undefined;
	this.children = [];

	this.position = new THREE.Vector3();
15
	this.positionScreen = new THREE.Vector4();
16 17
	this.rotation = new THREE.Vector3();
	this.scale = new THREE.Vector3( 1.0, 1.0, 1.0 );
18

19 20
	this.matrix = new THREE.Matrix4();
	this.matrixWorld = new THREE.Matrix4();
21
	this.matrixRotationWorld = new THREE.Matrix4();
22
	this.matrixNeedsUpdate = true;
23
	this.matrixAutoUpdate = true;
24

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

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

31
	this.visible = true;
32

33 34 35
};


M
Mr.doob 已提交
36
THREE.Object3D.prototype = {
37

38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
	translateX : function () {

		

	},

	translateY : function () {

		

	},

	translateZ : function () {

		

	},

	lookAt : function ( object ) {
	
		
	
	},

62
	addChild: function ( child ) {
63

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

66
			if( child.parent !== undefined ) {
A
alteredq 已提交
67

68
				child.parent.removeChild( child );
69

A
alteredq 已提交
70 71
			}

72 73
			child.parent = this;
			this.children.push( child );
M
Mr.doob 已提交
74

M
Mr.doob 已提交
75
			// add to scene
76

M
Mr.doob 已提交
77
			var scene = this;
78

M
Mr.doob 已提交
79 80
			while( scene instanceof THREE.Scene === false && scene !== undefined )
				scene = scene.parent;
81

M
Mr.doob 已提交
82 83 84
			if( scene !== undefined ) 
				scene.addChildRecurse( child );

85
		}
M
Mr.doob 已提交
86

87
	},
88

89
	removeChild: function ( child ) {
90

91
		var childIndex = this.children.indexOf( child );
92

93
		if ( childIndex !== - 1 ) {
94

95
			child.parent = undefined;
M
Mr.doob 已提交
96
			this.children.splice( childIndex, 1 );
97

M
Mr.doob 已提交
98
		}
99

M
Mr.doob 已提交
100
	},
101

M
Mr.doob 已提交
102
	updateMatrix: function () {
103

104
		this.matrix.setPosition( this.position );
105

M
Mr.doob 已提交
106
		if ( this.useQuaternion )  {
107

108
			this.matrix.setRotationFromQuaternion( this.quaternion );
109

M
Mr.doob 已提交
110
		} else {
111

112
			this.matrix.setRotationFromEuler( this.rotation );
113

M
Mr.doob 已提交
114
		}
115

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

118
			this.matrix.scale( this.scale );
M
Mr.doob 已提交
119
			this.boundRadiusScale = Math.max( this.scale.x, Math.max( this.scale.y, this.scale.z ) );
120

M
Mr.doob 已提交
121
		}
122

M
Mr.doob 已提交
123
		return true;
124

M
Mr.doob 已提交
125
	},
126

127
	update: function ( parentMatrixWorld, forceUpdate, camera ) {
128

129
		if ( this.visible ) {
130

131
			if ( this.matrixAutoUpdate ) {
132

133
				forceUpdate |= this.updateMatrix();
134

135
			}
136

137
			// update matrixWorld
138

139
			if ( forceUpdate || this.matrixNeedsUpdate ) {
140

141
				if ( parentMatrixWorld ) {
142

143
					this.matrixWorld.multiply( parentMatrixWorld, this.matrix );
144

145 146 147 148 149 150
				} else {

					this.matrixWorld.copy( this.matrix );

				}

151
				this.matrixRotationWorld.extractRotation( this.matrixWorld, this.scale );
152

153 154 155 156 157 158 159
				this.matrixNeedsUpdate = false;
				forceUpdate = true;

			}

			// update children

M
Mr.doob 已提交
160
			for ( var i = 0, l = this.children.length; i < l; i ++ ) {
161 162 163 164

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

			}
165

M
Mr.doob 已提交
166
		}
167

M
Mr.doob 已提交
168
	}
169

M
Mr.doob 已提交
170
}
M
Mr.doob 已提交
171 172

THREE.Object3DCounter = { value: 0 };