depthReducer.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import { Nullable } from "../types";
  2. import { RenderTargetTexture } from "../Materials/Textures/renderTargetTexture";
  3. import { Camera } from "../Cameras/camera";
  4. import { Constants } from "../Engines/constants";
  5. import { DepthRenderer } from "../Rendering/depthRenderer";
  6. import { MinMaxReducer } from "./minMaxReducer";
  7. /**
  8. * This class is a small wrapper around the MinMaxReducer class to compute the min/max values of a depth texture
  9. */
  10. export class DepthReducer extends MinMaxReducer {
  11. private _depthRenderer: Nullable<DepthRenderer>;
  12. private _depthRendererId: string;
  13. /**
  14. * Gets the depth renderer used for the computation.
  15. * Note that the result is null if you provide your own renderer when calling setDepthRenderer.
  16. */
  17. public get depthRenderer(): Nullable<DepthRenderer> {
  18. return this._depthRenderer;
  19. }
  20. /**
  21. * Creates a depth reducer
  22. * @param camera The camera used to render the depth texture
  23. */
  24. constructor(camera: Camera) {
  25. super(camera);
  26. }
  27. /**
  28. * Sets the depth renderer to use to generate the depth map
  29. * @param depthRenderer The depth renderer to use. If not provided, a new one will be created automatically
  30. * @param type The texture type of the depth map (default: TEXTURETYPE_HALF_FLOAT)
  31. * @param forceFullscreenViewport Forces the post processes used for the reduction to be applied without taking into account viewport (defaults to true)
  32. */
  33. public setDepthRenderer(depthRenderer: Nullable<DepthRenderer> = null, type: number = Constants.TEXTURETYPE_HALF_FLOAT, forceFullscreenViewport = true): void {
  34. const scene = this._camera.getScene();
  35. if (this._depthRenderer) {
  36. delete scene._depthRenderer[this._depthRendererId];
  37. this._depthRenderer.dispose();
  38. this._depthRenderer = null;
  39. }
  40. if (depthRenderer === null) {
  41. if (!scene._depthRenderer) {
  42. scene._depthRenderer = {};
  43. }
  44. depthRenderer = this._depthRenderer = new DepthRenderer(scene, type, this._camera, false);
  45. depthRenderer.enabled = false;
  46. this._depthRendererId = "minmax" + this._camera.id;
  47. scene._depthRenderer[this._depthRendererId] = depthRenderer;
  48. }
  49. super.setSourceTexture(depthRenderer.getDepthMap(), true, type, forceFullscreenViewport);
  50. }
  51. /** @hidden */
  52. public setSourceTexture(sourceTexture: RenderTargetTexture, depthRedux: boolean, type: number = Constants.TEXTURETYPE_HALF_FLOAT, forceFullscreenViewport = true): void {
  53. super.setSourceTexture(sourceTexture, depthRedux, type, forceFullscreenViewport);
  54. }
  55. /**
  56. * Activates the reduction computation.
  57. * When activated, the observers registered in onAfterReductionPerformed are
  58. * called after the compuation is performed
  59. */
  60. public activate(): void {
  61. if (this._depthRenderer) {
  62. this._depthRenderer.enabled = true;
  63. }
  64. super.activate();
  65. }
  66. /**
  67. * Deactivates the reduction computation.
  68. */
  69. public deactivate(): void {
  70. super.deactivate();
  71. if (this._depthRenderer) {
  72. this._depthRenderer.enabled = false;
  73. }
  74. }
  75. /**
  76. * Disposes the depth reducer
  77. * @param disposeAll true to dispose all the resources. You should always call this function with true as the parameter (or without any parameter as it is the default one). This flag is meant to be used internally.
  78. */
  79. public dispose(disposeAll = true): void {
  80. super.dispose(disposeAll);
  81. if (this._depthRenderer && disposeAll) {
  82. delete this._depthRenderer.getDepthMap().getScene()?._depthRenderer[this._depthRendererId];
  83. this._depthRenderer.dispose();
  84. this._depthRenderer = null;
  85. }
  86. }
  87. }