babylon.depthRendererSceneComponent.ts 4.8 KB

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