depthRendererSceneComponent.ts 5.1 KB

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