geometryBufferRendererSceneComponent.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import { Nullable } from "../types";
  2. import { Scene } from "../scene";
  3. import { ISceneComponent, SceneComponentConstants } from "../sceneComponent";
  4. import { SmartArrayNoDuplicate } from "../Misc/smartArray";
  5. import { RenderTargetTexture } from "../Materials/Textures/renderTargetTexture";
  6. import { GeometryBufferRenderer } from "./geometryBufferRenderer";
  7. declare module "../scene" {
  8. export interface Scene {
  9. /** @hidden (Backing field) */
  10. _geometryBufferRenderer: Nullable<GeometryBufferRenderer>;
  11. /**
  12. * Gets or Sets the current geometry buffer associated to the scene.
  13. */
  14. geometryBufferRenderer: Nullable<GeometryBufferRenderer>;
  15. /**
  16. * Enables a GeometryBufferRender and associates it with the scene
  17. * @param ratio defines the scaling ratio to apply to the renderer (1 by default which means same resolution)
  18. * @returns the GeometryBufferRenderer
  19. */
  20. enableGeometryBufferRenderer(ratio?: number): Nullable<GeometryBufferRenderer>;
  21. /**
  22. * Disables the GeometryBufferRender associated with the scene
  23. */
  24. disableGeometryBufferRenderer(): void;
  25. }
  26. }
  27. Object.defineProperty(Scene.prototype, "geometryBufferRenderer", {
  28. get: function(this: Scene) {
  29. this._geometryBufferRenderer;
  30. },
  31. set: function(this: Scene, value: Nullable<GeometryBufferRenderer>) {
  32. if (value && value.isSupported) {
  33. this._geometryBufferRenderer = value;
  34. }
  35. },
  36. enumerable: true,
  37. configurable: true
  38. });
  39. Scene.prototype.enableGeometryBufferRenderer = function(ratio: number = 1): Nullable<GeometryBufferRenderer> {
  40. if (this._geometryBufferRenderer) {
  41. return this._geometryBufferRenderer;
  42. }
  43. this._geometryBufferRenderer = new GeometryBufferRenderer(this, ratio);
  44. if (!this._geometryBufferRenderer.isSupported) {
  45. this._geometryBufferRenderer = null;
  46. }
  47. return this._geometryBufferRenderer;
  48. };
  49. Scene.prototype.disableGeometryBufferRenderer = function(): void {
  50. if (!this._geometryBufferRenderer) {
  51. return;
  52. }
  53. this._geometryBufferRenderer.dispose();
  54. this._geometryBufferRenderer = null;
  55. };
  56. /**
  57. * Defines the Geometry Buffer scene component responsible to manage a G-Buffer useful
  58. * in several rendering techniques.
  59. */
  60. export class GeometryBufferRendererSceneComponent implements ISceneComponent {
  61. /**
  62. * The component name helpful to identify the component in the list of scene components.
  63. */
  64. public readonly name = SceneComponentConstants.NAME_GEOMETRYBUFFERRENDERER;
  65. /**
  66. * The scene the component belongs to.
  67. */
  68. public scene: Scene;
  69. /**
  70. * Creates a new instance of the component for the given scene
  71. * @param scene Defines the scene to register the component in
  72. */
  73. constructor(scene: Scene) {
  74. this.scene = scene;
  75. }
  76. /**
  77. * Registers the component in a given scene
  78. */
  79. public register(): void {
  80. this.scene._gatherRenderTargetsStage.registerStep(SceneComponentConstants.STEP_GATHERRENDERTARGETS_GEOMETRYBUFFERRENDERER, this, this._gatherRenderTargets);
  81. }
  82. /**
  83. * Rebuilds the elements related to this component in case of
  84. * context lost for instance.
  85. */
  86. public rebuild(): void {
  87. // Nothing to do for this component
  88. }
  89. /**
  90. * Disposes the component and the associated ressources
  91. */
  92. public dispose(): void {
  93. // Nothing to do for this component
  94. }
  95. private _gatherRenderTargets(renderTargets: SmartArrayNoDuplicate<RenderTargetTexture>): void {
  96. if (this.scene._geometryBufferRenderer) {
  97. renderTargets.push(this.scene._geometryBufferRenderer.getGBuffer());
  98. }
  99. }
  100. }
  101. GeometryBufferRenderer._SceneComponentInitialization = (scene: Scene) => {
  102. // Register the G Buffer component to the scene.
  103. let component = scene._getComponent(SceneComponentConstants.NAME_GEOMETRYBUFFERRENDERER) as GeometryBufferRendererSceneComponent;
  104. if (!component) {
  105. component = new GeometryBufferRendererSceneComponent(scene);
  106. scene._addComponent(component);
  107. }
  108. };