depthRendererSceneComponent.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import { Scene } from "../scene.js";
  2. import { DepthRenderer } from "./depthRenderer.js";
  3. import { SceneComponentConstants } from "../sceneComponent.js";
  4. Scene.prototype.enableDepthRenderer = function (camera, storeNonLinearDepth = false, force32bitsFloat = false, samplingMode = 3, storeCameraSpaceZ = false) {
  5. camera = camera || this.activeCamera;
  6. if (!camera) {
  7. // eslint-disable-next-line no-throw-literal
  8. throw "No camera available to enable depth renderer";
  9. }
  10. if (!this._depthRenderer) {
  11. this._depthRenderer = {};
  12. }
  13. if (!this._depthRenderer[camera.id]) {
  14. const supportFullfloat = !!this.getEngine().getCaps().textureFloatRender;
  15. let textureType = 0;
  16. if (this.getEngine().getCaps().textureHalfFloatRender && (!force32bitsFloat || !supportFullfloat)) {
  17. textureType = 2;
  18. }
  19. else if (supportFullfloat) {
  20. textureType = 1;
  21. }
  22. else {
  23. textureType = 0;
  24. }
  25. this._depthRenderer[camera.id] = new DepthRenderer(this, textureType, camera, storeNonLinearDepth, samplingMode, storeCameraSpaceZ);
  26. }
  27. return this._depthRenderer[camera.id];
  28. };
  29. Scene.prototype.disableDepthRenderer = function (camera) {
  30. camera = camera || this.activeCamera;
  31. if (!camera || !this._depthRenderer || !this._depthRenderer[camera.id]) {
  32. return;
  33. }
  34. this._depthRenderer[camera.id].dispose();
  35. };
  36. /**
  37. * Defines the Depth Renderer scene component responsible to manage a depth buffer useful
  38. * in several rendering techniques.
  39. */
  40. export class DepthRendererSceneComponent {
  41. /**
  42. * Creates a new instance of the component for the given scene
  43. * @param scene Defines the scene to register the component in
  44. */
  45. constructor(scene) {
  46. /**
  47. * The component name helpful to identify the component in the list of scene components.
  48. */
  49. this.name = SceneComponentConstants.NAME_DEPTHRENDERER;
  50. this.scene = scene;
  51. }
  52. /**
  53. * Registers the component in a given scene
  54. */
  55. register() {
  56. this.scene._gatherRenderTargetsStage.registerStep(SceneComponentConstants.STEP_GATHERRENDERTARGETS_DEPTHRENDERER, this, this._gatherRenderTargets);
  57. this.scene._gatherActiveCameraRenderTargetsStage.registerStep(SceneComponentConstants.STEP_GATHERACTIVECAMERARENDERTARGETS_DEPTHRENDERER, this, this._gatherActiveCameraRenderTargets);
  58. }
  59. /**
  60. * Rebuilds the elements related to this component in case of
  61. * context lost for instance.
  62. */
  63. rebuild() {
  64. // Nothing to do for this component
  65. }
  66. /**
  67. * Disposes the component and the associated resources
  68. */
  69. dispose() {
  70. for (const key in this.scene._depthRenderer) {
  71. this.scene._depthRenderer[key].dispose();
  72. }
  73. }
  74. _gatherRenderTargets(renderTargets) {
  75. if (this.scene._depthRenderer) {
  76. for (const key in this.scene._depthRenderer) {
  77. const depthRenderer = this.scene._depthRenderer[key];
  78. if (depthRenderer.enabled && !depthRenderer.useOnlyInActiveCamera) {
  79. renderTargets.push(depthRenderer.getDepthMap());
  80. }
  81. }
  82. }
  83. }
  84. _gatherActiveCameraRenderTargets(renderTargets) {
  85. if (this.scene._depthRenderer) {
  86. for (const key in this.scene._depthRenderer) {
  87. const depthRenderer = this.scene._depthRenderer[key];
  88. if (depthRenderer.enabled && depthRenderer.useOnlyInActiveCamera && this.scene.activeCamera.id === key) {
  89. renderTargets.push(depthRenderer.getDepthMap());
  90. }
  91. }
  92. }
  93. }
  94. }
  95. DepthRenderer._SceneComponentInitialization = (scene) => {
  96. // Register the G Buffer component to the scene.
  97. let component = scene._getComponent(SceneComponentConstants.NAME_DEPTHRENDERER);
  98. if (!component) {
  99. component = new DepthRendererSceneComponent(scene);
  100. scene._addComponent(component);
  101. }
  102. };
  103. //# sourceMappingURL=depthRendererSceneComponent.js.map