prePassRendererSceneComponent.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. import { Nullable } from "../types";
  2. import { Scene } from "../scene";
  3. import { ISceneComponent, SceneComponentConstants } from "../sceneComponent";
  4. import { PrePassRenderer } from "./prePassRenderer";
  5. import { Logger } from "../Misc/logger";
  6. declare module "../abstractScene" {
  7. export interface AbstractScene {
  8. /** @hidden (Backing field) */
  9. _prePassRenderer: Nullable<PrePassRenderer>;
  10. /**
  11. * Gets or Sets the current prepass renderer associated to the scene.
  12. */
  13. prePassRenderer: Nullable<PrePassRenderer>;
  14. /**
  15. * Enables the prepass and associates it with the scene
  16. * @returns the PrePassRenderer
  17. */
  18. enablePrePassRenderer(): Nullable<PrePassRenderer>;
  19. /**
  20. * Disables the prepass associated with the scene
  21. */
  22. disablePrePassRenderer(): void;
  23. }
  24. }
  25. Object.defineProperty(Scene.prototype, "prePassRenderer", {
  26. get: function(this: Scene) {
  27. return this._prePassRenderer;
  28. },
  29. set: function(this: Scene, value: Nullable<PrePassRenderer>) {
  30. if (value && value.isSupported) {
  31. this._prePassRenderer = value;
  32. }
  33. },
  34. enumerable: true,
  35. configurable: true
  36. });
  37. Scene.prototype.enablePrePassRenderer = function(): Nullable<PrePassRenderer> {
  38. if (this._prePassRenderer) {
  39. return this._prePassRenderer;
  40. }
  41. this._prePassRenderer = new PrePassRenderer(this);
  42. if (!this._prePassRenderer.isSupported) {
  43. this._prePassRenderer = null;
  44. Logger.Error("PrePassRenderer needs WebGL 2 support.\n" +
  45. "Maybe you tried to use the following features that need the PrePassRenderer :\n" +
  46. " + Subsurface Scattering");
  47. }
  48. return this._prePassRenderer;
  49. };
  50. Scene.prototype.disablePrePassRenderer = function(): void {
  51. if (!this._prePassRenderer) {
  52. return;
  53. }
  54. this._prePassRenderer.dispose();
  55. this._prePassRenderer = null;
  56. };
  57. /**
  58. * Defines the Geometry Buffer scene component responsible to manage a G-Buffer useful
  59. * in several rendering techniques.
  60. */
  61. export class PrePassRendererSceneComponent implements ISceneComponent {
  62. /**
  63. * The component name helpful to identify the component in the list of scene components.
  64. */
  65. public readonly name = SceneComponentConstants.NAME_PREPASSRENDERER;
  66. /**
  67. * The scene the component belongs to.
  68. */
  69. public scene: Scene;
  70. /**
  71. * Creates a new instance of the component for the given scene
  72. * @param scene Defines the scene to register the component in
  73. */
  74. constructor(scene: Scene) {
  75. this.scene = scene;
  76. }
  77. /**
  78. * Registers the component in a given scene
  79. */
  80. public register(): void {
  81. this.scene._beforeCameraDrawStage.registerStep(SceneComponentConstants.STEP_BEFORECAMERADRAW_PREPASS, this, this._beforeCameraDraw);
  82. this.scene._afterCameraDrawStage.registerStep(SceneComponentConstants.STEP_AFTERCAMERADRAW_PREPASS, this, this._afterCameraDraw);
  83. this.scene._beforeClearStage.registerStep(SceneComponentConstants.STEP_BEFORECLEARSTAGE_PREPASS, this, this._beforeClearStage);
  84. }
  85. private _beforeCameraDraw() {
  86. if (this.scene.prePassRenderer) {
  87. this.scene.prePassRenderer._beforeCameraDraw();
  88. }
  89. }
  90. private _afterCameraDraw() {
  91. if (this.scene.prePassRenderer) {
  92. this.scene.prePassRenderer._afterCameraDraw();
  93. }
  94. }
  95. private _beforeClearStage() {
  96. if (this.scene.prePassRenderer) {
  97. this.scene.prePassRenderer.clear();
  98. }
  99. }
  100. /**
  101. * Rebuilds the elements related to this component in case of
  102. * context lost for instance.
  103. */
  104. public rebuild(): void {
  105. // Nothing to do for this component
  106. }
  107. /**
  108. * Disposes the component and the associated ressources
  109. */
  110. public dispose(): void {
  111. // Nothing to do for this component
  112. }
  113. }
  114. PrePassRenderer._SceneComponentInitialization = (scene: Scene) => {
  115. // Register the G Buffer component to the scene.
  116. let component = scene._getComponent(SceneComponentConstants.NAME_PREPASSRENDERER) as PrePassRendererSceneComponent;
  117. if (!component) {
  118. component = new PrePassRendererSceneComponent(scene);
  119. scene._addComponent(component);
  120. }
  121. };