layerSceneComponent.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. import { SceneComponentConstants } from "../sceneComponent.js";
  2. import { EngineStore } from "../Engines/engineStore.js";
  3. /**
  4. * Defines the layer scene component responsible to manage any layers
  5. * in a given scene.
  6. */
  7. export class LayerSceneComponent {
  8. /**
  9. * Creates a new instance of the component for the given scene
  10. * @param scene Defines the scene to register the component in
  11. */
  12. constructor(scene) {
  13. /**
  14. * The component name helpful to identify the component in the list of scene components.
  15. */
  16. this.name = SceneComponentConstants.NAME_LAYER;
  17. this.scene = scene || EngineStore.LastCreatedScene;
  18. if (!this.scene) {
  19. return;
  20. }
  21. this._engine = this.scene.getEngine();
  22. this.scene.layers = [];
  23. }
  24. /**
  25. * Registers the component in a given scene
  26. */
  27. register() {
  28. this.scene._beforeCameraDrawStage.registerStep(SceneComponentConstants.STEP_BEFORECAMERADRAW_LAYER, this, this._drawCameraBackground);
  29. this.scene._afterCameraDrawStage.registerStep(SceneComponentConstants.STEP_AFTERCAMERADRAW_LAYER, this, this._drawCameraForegroundWithPostProcessing);
  30. this.scene._afterCameraPostProcessStage.registerStep(SceneComponentConstants.STEP_AFTERCAMERAPOSTPROCESS_LAYER, this, this._drawCameraForegroundWithoutPostProcessing);
  31. this.scene._beforeRenderTargetDrawStage.registerStep(SceneComponentConstants.STEP_BEFORERENDERTARGETDRAW_LAYER, this, this._drawRenderTargetBackground);
  32. this.scene._afterRenderTargetDrawStage.registerStep(SceneComponentConstants.STEP_AFTERRENDERTARGETDRAW_LAYER, this, this._drawRenderTargetForegroundWithPostProcessing);
  33. this.scene._afterRenderTargetPostProcessStage.registerStep(SceneComponentConstants.STEP_AFTERRENDERTARGETPOSTPROCESS_LAYER, this, this._drawRenderTargetForegroundWithoutPostProcessing);
  34. }
  35. /**
  36. * Rebuilds the elements related to this component in case of
  37. * context lost for instance.
  38. */
  39. rebuild() {
  40. const layers = this.scene.layers;
  41. for (const layer of layers) {
  42. layer._rebuild();
  43. }
  44. }
  45. /**
  46. * Disposes the component and the associated resources.
  47. */
  48. dispose() {
  49. const layers = this.scene.layers;
  50. while (layers.length) {
  51. layers[0].dispose();
  52. }
  53. }
  54. _draw(predicate) {
  55. const layers = this.scene.layers;
  56. if (layers.length) {
  57. this._engine.setDepthBuffer(false);
  58. for (const layer of layers) {
  59. if (predicate(layer)) {
  60. layer.render();
  61. }
  62. }
  63. this._engine.setDepthBuffer(true);
  64. }
  65. }
  66. _drawCameraPredicate(layer, isBackground, applyPostProcess, cameraLayerMask) {
  67. return (!layer.renderOnlyInRenderTargetTextures &&
  68. layer.isBackground === isBackground &&
  69. layer.applyPostProcess === applyPostProcess &&
  70. (layer.layerMask & cameraLayerMask) !== 0);
  71. }
  72. _drawCameraBackground(camera) {
  73. this._draw((layer) => {
  74. return this._drawCameraPredicate(layer, true, true, camera.layerMask);
  75. });
  76. }
  77. _drawCameraForegroundWithPostProcessing(camera) {
  78. this._draw((layer) => {
  79. return this._drawCameraPredicate(layer, false, true, camera.layerMask);
  80. });
  81. }
  82. _drawCameraForegroundWithoutPostProcessing(camera) {
  83. this._draw((layer) => {
  84. return this._drawCameraPredicate(layer, false, false, camera.layerMask);
  85. });
  86. }
  87. _drawRenderTargetPredicate(layer, isBackground, applyPostProcess, cameraLayerMask, renderTargetTexture) {
  88. return (layer.renderTargetTextures.length > 0 &&
  89. layer.isBackground === isBackground &&
  90. layer.applyPostProcess === applyPostProcess &&
  91. layer.renderTargetTextures.indexOf(renderTargetTexture) > -1 &&
  92. (layer.layerMask & cameraLayerMask) !== 0);
  93. }
  94. _drawRenderTargetBackground(renderTarget) {
  95. this._draw((layer) => {
  96. return this._drawRenderTargetPredicate(layer, true, true, this.scene.activeCamera.layerMask, renderTarget);
  97. });
  98. }
  99. _drawRenderTargetForegroundWithPostProcessing(renderTarget) {
  100. this._draw((layer) => {
  101. return this._drawRenderTargetPredicate(layer, false, true, this.scene.activeCamera.layerMask, renderTarget);
  102. });
  103. }
  104. _drawRenderTargetForegroundWithoutPostProcessing(renderTarget) {
  105. this._draw((layer) => {
  106. return this._drawRenderTargetPredicate(layer, false, false, this.scene.activeCamera.layerMask, renderTarget);
  107. });
  108. }
  109. /**
  110. * Adds all the elements from the container to the scene
  111. * @param container the container holding the elements
  112. */
  113. addFromContainer(container) {
  114. if (!container.layers) {
  115. return;
  116. }
  117. container.layers.forEach((layer) => {
  118. this.scene.layers.push(layer);
  119. });
  120. }
  121. /**
  122. * Removes all the elements in the container from the scene
  123. * @param container contains the elements to remove
  124. * @param dispose if the removed element should be disposed (default: false)
  125. */
  126. removeFromContainer(container, dispose = false) {
  127. if (!container.layers) {
  128. return;
  129. }
  130. container.layers.forEach((layer) => {
  131. const index = this.scene.layers.indexOf(layer);
  132. if (index !== -1) {
  133. this.scene.layers.splice(index, 1);
  134. }
  135. if (dispose) {
  136. layer.dispose();
  137. }
  138. });
  139. }
  140. }
  141. //# sourceMappingURL=layerSceneComponent.js.map