depthRendererSceneComponent.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import { Nullable } from "../types";
  2. import { Scene } from "../scene";
  3. import { SmartArrayNoDuplicate } from "../Misc/smartArray";
  4. import { DepthRenderer } from "./depthRenderer";
  5. import { Camera } from "../Cameras/camera";
  6. import { Constants } from "../Engines/constants";
  7. import { ISceneComponent, SceneComponentConstants } from "../sceneComponent";
  8. import { RenderTargetTexture } from "../Materials/Textures/renderTargetTexture";
  9. declare module "../scene" {
  10. export interface Scene {
  11. /** @hidden (Backing field) */
  12. _depthRenderer: { [id: string]: DepthRenderer };
  13. /**
  14. * Creates a depth renderer a given camera which contains a depth map which can be used for post processing.
  15. * @param camera The camera to create the depth renderer on (default: scene's active camera)
  16. * @returns the created depth renderer
  17. */
  18. enableDepthRenderer(camera?: Nullable<Camera>): DepthRenderer;
  19. /**
  20. * Disables a depth renderer for a given camera
  21. * @param camera The camera to disable the depth renderer on (default: scene's active camera)
  22. */
  23. disableDepthRenderer(camera?: Nullable<Camera>): void;
  24. }
  25. }
  26. Scene.prototype.enableDepthRenderer = function(camera?: Nullable<Camera>): DepthRenderer {
  27. camera = camera || this.activeCamera;
  28. if (!camera) {
  29. throw "No camera available to enable depth renderer";
  30. }
  31. if (!this._depthRenderer) {
  32. this._depthRenderer = {};
  33. }
  34. if (!this._depthRenderer[camera.id]) {
  35. var textureType = 0;
  36. if (this.getEngine().getCaps().textureHalfFloatRender) {
  37. textureType = Constants.TEXTURETYPE_HALF_FLOAT;
  38. }
  39. else if (this.getEngine().getCaps().textureFloatRender) {
  40. textureType = Constants.TEXTURETYPE_FLOAT;
  41. } else {
  42. throw "Depth renderer does not support int texture type";
  43. }
  44. this._depthRenderer[camera.id] = new DepthRenderer(this, textureType, camera);
  45. }
  46. return this._depthRenderer[camera.id];
  47. };
  48. Scene.prototype.disableDepthRenderer = function(camera?: Nullable<Camera>): void {
  49. camera = camera || this.activeCamera;
  50. if (!camera || !this._depthRenderer || !this._depthRenderer[camera.id]) {
  51. return;
  52. }
  53. this._depthRenderer[camera.id].dispose();
  54. delete this._depthRenderer[camera.id];
  55. };
  56. /**
  57. * Defines the Depth Renderer scene component responsible to manage a depth buffer useful
  58. * in several rendering techniques.
  59. */
  60. export class DepthRendererSceneComponent implements ISceneComponent {
  61. /**
  62. * The component name helpfull to identify the component in the list of scene components.
  63. */
  64. public readonly name = SceneComponentConstants.NAME_DEPTHRENDERER;
  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_DEPTHRENDERER, this, this._gatherRenderTargets);
  81. this.scene._gatherActiveCameraRenderTargetsStage.registerStep(SceneComponentConstants.STEP_GATHERACTIVECAMERARENDERTARGETS_DEPTHRENDERER, this, this._gatherActiveCameraRenderTargets);
  82. }
  83. /**
  84. * Rebuilds the elements related to this component in case of
  85. * context lost for instance.
  86. */
  87. public rebuild(): void {
  88. // Nothing to do for this component
  89. }
  90. /**
  91. * Disposes the component and the associated ressources
  92. */
  93. public dispose(): void {
  94. for (var key in this.scene._depthRenderer) {
  95. this.scene._depthRenderer[key].dispose();
  96. }
  97. }
  98. private _gatherRenderTargets(renderTargets: SmartArrayNoDuplicate<RenderTargetTexture>): void {
  99. if (this.scene._depthRenderer) {
  100. for (var key in this.scene._depthRenderer) {
  101. let depthRenderer = this.scene._depthRenderer[key];
  102. if (!depthRenderer.useOnlyInActiveCamera) {
  103. renderTargets.push(depthRenderer.getDepthMap());
  104. }
  105. }
  106. }
  107. }
  108. private _gatherActiveCameraRenderTargets(renderTargets: SmartArrayNoDuplicate<RenderTargetTexture>): void {
  109. if (this.scene._depthRenderer) {
  110. for (var key in this.scene._depthRenderer) {
  111. let depthRenderer = this.scene._depthRenderer[key];
  112. if (depthRenderer.useOnlyInActiveCamera && this.scene.activeCamera!.id === key) {
  113. renderTargets.push(depthRenderer.getDepthMap());
  114. }
  115. }
  116. }
  117. }
  118. }