babylon.layerSceneComponent.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. /**
  23. * Creates a new instance of the component for the given scene
  24. * @param scene Defines the scene to register the component in
  25. */
  26. constructor(scene: Scene) {
  27. this.scene = scene;
  28. this._engine = scene.getEngine();
  29. scene.layers = new Array<Layer>();
  30. }
  31. /**
  32. * Registers the component in a given scene
  33. */
  34. public register(): void {
  35. this.scene._beforeCameraDrawStage.registerStep(SceneComponentConstants.STEP_BEFORECAMERADRAW_LAYER, this, this._drawBackground);
  36. this.scene._afterCameraDrawStage.registerStep(SceneComponentConstants.STEP_AFTERCAMERADRAW_LAYER, this, this._drawForeground);
  37. }
  38. /**
  39. * Rebuilds the elements related to this component in case of
  40. * context lost for instance.
  41. */
  42. public rebuild(): void {
  43. let layers = this.scene.layers;
  44. for (let layer of layers) {
  45. layer._rebuild();
  46. }
  47. }
  48. /**
  49. * Disposes the component and the associated ressources.
  50. */
  51. public dispose(): void {
  52. let layers = this.scene.layers;
  53. while (layers.length) {
  54. layers[0].dispose();
  55. }
  56. }
  57. private _draw(camera: Camera, isBackground: boolean): void {
  58. let layers = this.scene.layers;
  59. if (layers.length) {
  60. this._engine.setDepthBuffer(false);
  61. const cameraLayerMask = camera.layerMask;
  62. for (let layer of layers) {
  63. if (layer.isBackground === isBackground && ((layer.layerMask & cameraLayerMask) !== 0)) {
  64. layer.render();
  65. }
  66. }
  67. this._engine.setDepthBuffer(true);
  68. }
  69. }
  70. private _drawBackground(camera: Camera): void {
  71. this._draw(camera, true);
  72. }
  73. private _drawForeground(camera: Camera): void {
  74. this._draw(camera, false);
  75. }
  76. }
  77. }