geometryBufferRendererSceneComponent.ts 4.0 KB

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