babylon.layerSceneComponent.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. module BABYLON {
  2. export interface AbstractScene {
  3. /**
  4. * The list of layers (background and foreground) of the scene
  5. */
  6. layers: Array<Layer>;
  7. }
  8. /**
  9. * Defines the layer scene component responsible to manage any layers
  10. * in a given scene.
  11. */
  12. export class LayerSceneComponent implements ISceneComponent {
  13. /**
  14. * The component name helpfull to identify the component in the list of scene components.
  15. */
  16. public readonly name = SceneComponentConstants.NAME_LAYER;
  17. /**
  18. * The scene the component belongs to.
  19. */
  20. public scene: Scene;
  21. private _engine: Engine;
  22. private _layers: Array<Layer>;
  23. /**
  24. * Registers the component in a given scene
  25. * @param scene Defines the scene to register the component in
  26. */
  27. public register(scene: Scene): void {
  28. this.scene = scene;
  29. this._engine = scene.getEngine();
  30. this._layers = scene.layers = new Array<Layer>();
  31. scene._beforeCameraDrawStage.registerStep(SceneComponentConstants.STEP_BEFORECAMERADRAW_LAYER, this, this._drawBackground);
  32. scene._afterCameraDrawStage.registerStep(SceneComponentConstants.STEP_AFTERCAMERADRAW_LAYER, this, this._drawForeground);
  33. }
  34. /**
  35. * Rebuilds the elements related to this component in case of
  36. * context lost for instance.
  37. */
  38. public rebuild(): void {
  39. for (let layer of this._layers) {
  40. layer._rebuild();
  41. }
  42. }
  43. /**
  44. * Serializes the component data to the specified json object
  45. * @param serializationObject The object to serialize to
  46. */
  47. public serialize(serializationObject: any): void {
  48. // TODO. Implement layer serialization
  49. // serializationObject.layers = [];
  50. // for (let layer of this._layers) {
  51. // if (layer.serialize) {
  52. // serializationObject.layers.push(layer.serialize());
  53. // }
  54. // }
  55. }
  56. /**
  57. * Adds all the element from the container to the scene
  58. * @param container the container holding the elements
  59. */
  60. public addFromContainer(container: AbstractScene): void {
  61. if (!container.layers) {
  62. return;
  63. }
  64. // container.layers.forEach((o) => {
  65. // this.scene.addLayer(o);
  66. // });
  67. }
  68. /**
  69. * Removes all the elements in the container from the scene
  70. * @param container contains the elements to remove
  71. */
  72. public removeFromContainer(container: AbstractScene): void {
  73. if (!container.layers) {
  74. return;
  75. }
  76. // container.layers.forEach((o) => {
  77. // this.scene.removeLayer(o);
  78. // });
  79. }
  80. /**
  81. * Disposes the component and the associated ressources.
  82. */
  83. public dispose(): void {
  84. while (this._layers.length) {
  85. this._layers[0].dispose();
  86. }
  87. }
  88. private _draw(camera: Camera, isBackground: boolean): void {
  89. if (this._layers.length) {
  90. this._engine.setDepthBuffer(false);
  91. const cameraLayerMask = camera.layerMask;
  92. for (let layer of this._layers) {
  93. if (layer.isBackground === isBackground && ((layer.layerMask & cameraLayerMask) !== 0)) {
  94. layer.render();
  95. }
  96. }
  97. this._engine.setDepthBuffer(true);
  98. }
  99. }
  100. private _drawBackground(camera: Camera): void {
  101. this._draw(camera, true);
  102. }
  103. private _drawForeground(camera: Camera): void {
  104. this._draw(camera, false);
  105. }
  106. }
  107. }