Scene.js 2.3 KB
Newer Older
M
Mr.doob 已提交
1 2
/**
 * @author mr.doob / http://mrdoob.com/
3
 * @author mikael emtinger / http://gomo.se/
M
Mr.doob 已提交
4 5
 */

M
Mr.doob 已提交
6
THREE.Scene = function () {
7

M
Mr.doob 已提交
8
	THREE.Object3D.call( this );
9

10 11 12 13
	this.matrixAutoUpdate = false;

	this.fog = null;

A
alteredq 已提交
14
	this.collisions = null;
15

16
	this.objects = [];
M
Mr.doob 已提交
17 18 19
	this.lights = [];
	this.sounds = [];

20 21
	this.__objectsAdded = [];
	this.__objectsRemoved = [];
M
Mr.doob 已提交
22

23
};
M
Mr.doob 已提交
24

M
Mr.doob 已提交
25
THREE.Scene.prototype = new THREE.Object3D();
26
THREE.Scene.prototype.constructor = THREE.Scene;
M
Mr.doob 已提交
27
THREE.Scene.prototype.supr = THREE.Object3D.prototype;
M
Mr.doob 已提交
28

29
THREE.Scene.prototype.addChild = function( child ) {
30

31 32
	this.supr.addChild.call( this, child );
	this.addChildRecurse( child );
33

34
}
35

A
alteredq 已提交
36
THREE.Scene.prototype.addChildRecurse = function( child ) {
M
Mr.doob 已提交
37 38 39 40 41 42 43 44 45

	if ( child instanceof THREE.Light ) {

		if ( this.lights.indexOf( child ) === -1 ) {

			this.lights.push( child );

		}

M
Mr.doob 已提交
46
	} else if ( child instanceof THREE.Sound ) {
M
Mr.doob 已提交
47 48 49

		if ( this.sounds.indexOf( child ) === -1 ) {

A
alteredq 已提交
50 51
			this.sounds.push( child );

M
Mr.doob 已提交
52 53 54 55 56 57
		}

	} else if ( !( child instanceof THREE.Camera || child instanceof THREE.Bone ) ) {

		if ( this.objects.indexOf( child ) === -1 ) {

A
alteredq 已提交
58
			this.objects.push( child );
59
			this.__objectsAdded.push( child );
60

M
Mr.doob 已提交
61 62
		}

63
	}
M
Mr.doob 已提交
64 65 66

	for ( var c = 0; c < child.children.length; c++ ) {

A
alteredq 已提交
67
		this.addChildRecurse( child.children[ c ] );
68

M
Mr.doob 已提交
69 70
	}

71
}
72

73

74
THREE.Scene.prototype.removeChild = function( child ) {
75

76 77
	this.supr.removeChild.call( this, child );
	this.removeChildRecurse( child );
78

79
}
M
Mr.doob 已提交
80

81
THREE.Scene.prototype.removeChildRecurse = function( child ) {
M
Mr.doob 已提交
82 83 84

	if ( child instanceof THREE.Light ) {

A
alteredq 已提交
85
		var i = this.lights.indexOf( child );
M
Mr.doob 已提交
86 87 88

		if ( i !== -1 ) {

89
			this.lights.splice( i, 1 );
M
Mr.doob 已提交
90

M
Mr.doob 已提交
91 92
		}

M
Mr.doob 已提交
93
	} else if ( child instanceof THREE.Sound ) {
M
Mr.doob 已提交
94

A
alteredq 已提交
95
		var i = this.sounds.indexOf( child );
M
Mr.doob 已提交
96 97 98

		if( i !== -1 ) {

A
alteredq 已提交
99 100
			this.sounds.splice( i, 1 );

M
Mr.doob 已提交
101 102 103 104
		}

	} else if ( !( child instanceof THREE.Camera ) ) {

A
alteredq 已提交
105
		var i = this.objects.indexOf( child );
M
Mr.doob 已提交
106 107 108

		if( i !== -1 ) {

109
			this.objects.splice( i, 1 );
110
			this.__objectsRemoved.push( child );
M
Mr.doob 已提交
111

M
Mr.doob 已提交
112 113
		}

114
	}
M
Mr.doob 已提交
115

M
Mr.doob 已提交
116
	for ( var c = 0; c < child.children.length; c++ ) {
M
Mr.doob 已提交
117

M
Mr.doob 已提交
118
		this.removeChildRecurse( child.children[ c ] );
119

M
Mr.doob 已提交
120
	}
M
Mr.doob 已提交
121

M
Mr.doob 已提交
122
}
123

M
Mr.doob 已提交
124
THREE.Scene.prototype.addObject = THREE.Scene.prototype.addChild;
125
THREE.Scene.prototype.removeObject = THREE.Scene.prototype.removeChild;
M
Mr.doob 已提交
126 127
THREE.Scene.prototype.addLight = THREE.Scene.prototype.addChild;
THREE.Scene.prototype.removeLight = THREE.Scene.prototype.removeChild;