Scene.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. */
  4. THREE.Scene = function () {
  5. THREE.Object3D.call( this );
  6. this.fog = null;
  7. this.overrideMaterial = null;
  8. this.autoUpdate = true; // checked by the renderer
  9. this.matrixAutoUpdate = false;
  10. this.__lights = [];
  11. this.__objectsAdded = [];
  12. this.__objectsRemoved = [];
  13. };
  14. THREE.Scene.prototype = Object.create( THREE.Object3D.prototype );
  15. THREE.Scene.prototype.__addObject = function ( object ) {
  16. if ( object instanceof THREE.Light ) {
  17. if ( this.__lights.indexOf( object ) === - 1 ) {
  18. this.__lights.push( object );
  19. }
  20. if ( object.target && object.target.parent === undefined ) {
  21. this.add( object.target );
  22. }
  23. } else if ( !( object instanceof THREE.Camera || object instanceof THREE.Bone ) ) {
  24. this.__objectsAdded.push( object );
  25. // check if previously removed
  26. var i = this.__objectsRemoved.indexOf( object );
  27. if ( i !== -1 ) {
  28. this.__objectsRemoved.splice( i, 1 );
  29. }
  30. }
  31. this.dispatchEvent( { type: 'objectAdded', object: object } );
  32. object.dispatchEvent( { type: 'addedToScene', scene: this } );
  33. for ( var c = 0; c < object.children.length; c ++ ) {
  34. this.__addObject( object.children[ c ] );
  35. }
  36. };
  37. THREE.Scene.prototype.__removeObject = function ( object ) {
  38. if ( object instanceof THREE.Light ) {
  39. var i = this.__lights.indexOf( object );
  40. if ( i !== -1 ) {
  41. this.__lights.splice( i, 1 );
  42. }
  43. if ( object.shadowCascadeArray ) {
  44. for ( var x = 0; x < object.shadowCascadeArray.length; x ++ ) {
  45. this.__removeObject( object.shadowCascadeArray[ x ] );
  46. }
  47. }
  48. } else if ( !( object instanceof THREE.Camera ) ) {
  49. this.__objectsRemoved.push( object );
  50. // check if previously added
  51. var i = this.__objectsAdded.indexOf( object );
  52. if ( i !== -1 ) {
  53. this.__objectsAdded.splice( i, 1 );
  54. }
  55. }
  56. this.dispatchEvent( { type: 'objectRemoved', object: object } );
  57. object.dispatchEvent( { type: 'removedFromScene', scene: this } );
  58. for ( var c = 0; c < object.children.length; c ++ ) {
  59. this.__removeObject( object.children[ c ] );
  60. }
  61. };
  62. THREE.Scene.prototype.clone = function ( object ) {
  63. if ( object === undefined ) object = new THREE.Scene();
  64. THREE.Object3D.prototype.clone.call(this, object);
  65. if ( this.fog !== null ) object.fog = this.fog.clone();
  66. if ( this.overrideMaterial !== null ) object.overrideMaterial = this.overrideMaterial.clone();
  67. object.autoUpdate = this.autoUpdate;
  68. object.matrixAutoUpdate = this.matrixAutoUpdate;
  69. return object;
  70. };