depthReducer.js 3.4 KB

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