Scene.js 2.5 KB
Newer Older
M
Mr.doob 已提交
1
/**
M
Mr.doob 已提交
2
 * @author mrdoob / http://mrdoob.com/
M
Mr.doob 已提交
3 4
 */

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

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

9
	this.fog = null;
M
Mr.doob 已提交
10 11
	this.overrideMaterial = null;

12
	this.autoUpdate = true; // checked by the renderer
M
Mr.doob 已提交
13
	this.matrixAutoUpdate = false;
M
Mr.doob 已提交
14

15
	this.__lights = [];
A
alteredq 已提交
16 17 18 19

	this.__objectsAdded = [];
	this.__objectsRemoved = [];

20
};
M
Mr.doob 已提交
21

22
THREE.Scene.prototype = Object.create( THREE.Object3D.prototype );
A
alteredq 已提交
23

24
THREE.Scene.prototype.__addObject = function ( object ) {
A
alteredq 已提交
25 26 27

	if ( object instanceof THREE.Light ) {

28
		if ( this.__lights.indexOf( object ) === - 1 ) {
A
alteredq 已提交
29

30
			this.__lights.push( object );
A
alteredq 已提交
31 32 33

		}

34 35 36 37 38 39
		if ( object.target && object.target.parent === undefined ) {

			this.add( object.target );

		}

A
alteredq 已提交
40 41
	} else if ( !( object instanceof THREE.Camera || object instanceof THREE.Bone ) ) {

42
		this.__objectsAdded.push( object );
A
alteredq 已提交
43

44
		// check if previously removed
A
alteredq 已提交
45

46
		var i = this.__objectsRemoved.indexOf( object );
A
alteredq 已提交
47

48
		if ( i !== -1 ) {
A
alteredq 已提交
49

50
			this.__objectsRemoved.splice( i, 1 );
A
alteredq 已提交
51 52 53 54 55

		}

	}

56 57 58
	this.dispatchEvent( { type: 'objectAdded', object: object } );
	object.dispatchEvent( { type: 'addedToScene', scene: this } );

A
alteredq 已提交
59 60
	for ( var c = 0; c < object.children.length; c ++ ) {

61
		this.__addObject( object.children[ c ] );
A
alteredq 已提交
62 63 64 65 66

	}

};

67
THREE.Scene.prototype.__removeObject = function ( object ) {
A
alteredq 已提交
68 69 70

	if ( object instanceof THREE.Light ) {

71
		var i = this.__lights.indexOf( object );
A
alteredq 已提交
72 73 74

		if ( i !== -1 ) {

75
			this.__lights.splice( i, 1 );
A
alteredq 已提交
76 77 78

		}

79
		if ( object.shadowCascadeArray ) {
M
Mr.doob 已提交
80 81 82

			for ( var x = 0; x < object.shadowCascadeArray.length; x ++ ) {

83
				this.__removeObject( object.shadowCascadeArray[ x ] );
M
Mr.doob 已提交
84

85
			}
M
Mr.doob 已提交
86

87 88
		}

A
alteredq 已提交
89 90
	} else if ( !( object instanceof THREE.Camera ) ) {

91
		this.__objectsRemoved.push( object );
A
alteredq 已提交
92

93
		// check if previously added
A
alteredq 已提交
94

95
		var i = this.__objectsAdded.indexOf( object );
A
alteredq 已提交
96

97
		if ( i !== -1 ) {
A
alteredq 已提交
98

99
			this.__objectsAdded.splice( i, 1 );
A
alteredq 已提交
100 101 102 103 104

		}

	}

105 106 107
	this.dispatchEvent( { type: 'objectRemoved', object: object } );
	object.dispatchEvent( { type: 'removedFromScene', scene: this } );

A
alteredq 已提交
108 109
	for ( var c = 0; c < object.children.length; c ++ ) {

110
		this.__removeObject( object.children[ c ] );
A
alteredq 已提交
111 112 113 114

	}

};
C
CMP 已提交
115

M
Mr.doob 已提交
116 117 118 119 120 121 122 123 124 125 126 127 128 129
THREE.Scene.prototype.clone = function ( object ) {

	if ( object === undefined ) object = new THREE.Scene();

	THREE.Object3D.prototype.clone.call(this, object);

	if ( this.fog !== null ) object.fog = this.fog.clone();
	if ( this.overrideMaterial !== null ) object.overrideMaterial = this.overrideMaterial.clone();

	object.autoUpdate = this.autoUpdate;
	object.matrixAutoUpdate = this.matrixAutoUpdate;

	return object;

C
CMP 已提交
130
};