babylon.ssaoRenderingPipeline.ts 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. module BABYLON {
  2. export class SSAORenderingPipeline extends PostProcessRenderPipeline {
  3. // Members
  4. /**
  5. * The PassPostProcess id in the pipeline that contains the original scene color
  6. * @type {string}
  7. */
  8. public SSAOOriginalSceneColorEffect: string = "SSAOOriginalSceneColorEffect";
  9. /**
  10. * The SSAO PostProcess id in the pipeline
  11. * @type {string}
  12. */
  13. public SSAORenderEffect: string = "SSAORenderEffect";
  14. /**
  15. * The horizontal blur PostProcess id in the pipeline
  16. * @type {string}
  17. */
  18. public SSAOBlurHRenderEffect: string = "SSAOBlurHRenderEffect";
  19. /**
  20. * The vertical blur PostProcess id in the pipeline
  21. * @type {string}
  22. */
  23. public SSAOBlurVRenderEffect: string = "SSAOBlurVRenderEffect";
  24. /**
  25. * The PostProcess id in the pipeline that combines the SSAO-Blur output with the original scene color (SSAOOriginalSceneColorEffect)
  26. * @type {string}
  27. */
  28. public SSAOCombineRenderEffect: string = "SSAOCombineRenderEffect";
  29. private _scene: Scene;
  30. private _depthTexture: RenderTargetTexture;
  31. private _randomTexture: DynamicTexture;
  32. private _originalColorPostProcess: PassPostProcess;
  33. private _ssaoPostProcess: PostProcess;
  34. private _blurHPostProcess: BlurPostProcess;
  35. private _blurVPostProcess: BlurPostProcess;
  36. private _ssaoCombinePostProcess: PostProcess;
  37. private _firstUpdate: boolean = true;
  38. /**
  39. * @constructor
  40. * @param {string} name - The rendering pipeline name
  41. * @param {BABYLON.Scene} scene - The scene linked to this pipeline
  42. * @param {any} ratio - The size of the postprocesses (0.5 means that your postprocess will have a width = canvas.width 0.5 and a height = canvas.height 0.5)
  43. * @param {BABYLON.Camera[]} cameras - The array of cameras that the rendering pipeline will be attached to
  44. */
  45. constructor(name: string, scene: Scene, ratio: any, cameras?: Camera[]) {
  46. super(scene.getEngine(), name);
  47. this._scene = scene;
  48. // Set up assets
  49. this._createRandomTexture();
  50. this._depthTexture = scene.enableDepthRenderer().getDepthMap(); // Force depth renderer "on"
  51. var ssaoRatio = ratio.ssaoRatio || ratio;
  52. var combineRatio = ratio.combineRatio || ratio;
  53. this._originalColorPostProcess = new PassPostProcess("SSAOOriginalSceneColor", combineRatio, null, Texture.BILINEAR_SAMPLINGMODE, scene.getEngine(), false);
  54. this._createSSAOPostProcess(ssaoRatio);
  55. this._blurHPostProcess = new BlurPostProcess("SSAOBlurH", new Vector2(2.0, 0.0), 1.3, ssaoRatio, null, Texture.BILINEAR_SAMPLINGMODE, scene.getEngine(), false);
  56. this._blurVPostProcess = new BlurPostProcess("SSAOBlurV", new Vector2(0.0, 2.0), 1.3, ssaoRatio, null, Texture.BILINEAR_SAMPLINGMODE, scene.getEngine(), false);
  57. this._createSSAOCombinePostProcess(combineRatio);
  58. // Set up pipeline
  59. this.addEffect(new PostProcessRenderEffect(scene.getEngine(), this.SSAOOriginalSceneColorEffect, () => { return this._originalColorPostProcess; }, true));
  60. this.addEffect(new PostProcessRenderEffect(scene.getEngine(), this.SSAORenderEffect, () => { return this._ssaoPostProcess; }, true));
  61. this.addEffect(new PostProcessRenderEffect(scene.getEngine(), this.SSAOBlurHRenderEffect, () => { return this._blurHPostProcess; }, true));
  62. this.addEffect(new PostProcessRenderEffect(scene.getEngine(), this.SSAOBlurVRenderEffect, () => { return this._blurVPostProcess; }, true));
  63. this.addEffect(new PostProcessRenderEffect(scene.getEngine(), this.SSAOCombineRenderEffect, () => { return this._ssaoCombinePostProcess; }, true));
  64. // Finish
  65. scene.postProcessRenderPipelineManager.addPipeline(this);
  66. if (cameras)
  67. scene.postProcessRenderPipelineManager.attachCamerasToRenderPipeline(name, cameras);
  68. }
  69. // Public Methods
  70. /**
  71. * Returns the horizontal blur PostProcess
  72. * @return {BABYLON.BlurPostProcess} The horizontal blur post-process
  73. */
  74. public getBlurHPostProcess(): BlurPostProcess {
  75. return this._blurHPostProcess;
  76. }
  77. /**
  78. * Returns the vertical blur PostProcess
  79. * @return {BABYLON.BlurPostProcess} The vertical blur post-process
  80. */
  81. public getBlurVPostProcess(): BlurPostProcess {
  82. return this._blurVPostProcess;
  83. }
  84. /**
  85. * Removes the internal pipeline assets and detatches the pipeline from the scene cameras
  86. */
  87. public dispose(disableDepthRender: boolean = false): void {
  88. this._scene.postProcessRenderPipelineManager.detachCamerasFromRenderPipeline(this._name, this._scene.cameras);
  89. this._originalColorPostProcess = undefined;
  90. this._ssaoPostProcess = undefined;
  91. this._blurHPostProcess = undefined;
  92. this._blurVPostProcess = undefined;
  93. this._ssaoCombinePostProcess = undefined;
  94. this._randomTexture.dispose();
  95. if (disableDepthRender)
  96. this._scene.disableDepthRenderer();
  97. }
  98. // Private Methods
  99. private _createSSAOPostProcess(ratio: number): void {
  100. var sampleSphere = [
  101. 0.5381, 0.1856, -0.4319,
  102. 0.1379, 0.2486, 0.4430,
  103. 0.3371, 0.5679, -0.0057,
  104. -0.6999, -0.0451, -0.0019,
  105. 0.0689, -0.1598, -0.8547,
  106. 0.0560, 0.0069, -0.1843,
  107. -0.0146, 0.1402, 0.0762,
  108. 0.0100, -0.1924, -0.0344,
  109. -0.3577, -0.5301, -0.4358,
  110. -0.3169, 0.1063, 0.0158,
  111. 0.0103, -0.5869, 0.0046,
  112. -0.0897, -0.4940, 0.3287,
  113. 0.7119, -0.0154, -0.0918,
  114. -0.0533, 0.0596, -0.5411,
  115. 0.0352, -0.0631, 0.5460,
  116. -0.4776, 0.2847, -0.0271
  117. ];
  118. var samplesFactor = 1.0 / 16.0;
  119. this._ssaoPostProcess = new PostProcess("ssao", "ssao", ["sampleSphere", "samplesFactor", "randTextureTiles"],
  120. ["randomSampler"],
  121. ratio, null, Texture.BILINEAR_SAMPLINGMODE,
  122. this._scene.getEngine(), false);
  123. this._ssaoPostProcess.onApply = (effect: Effect) => {
  124. if (this._firstUpdate) {
  125. effect.setArray3("sampleSphere", sampleSphere);
  126. effect.setFloat("samplesFactor", samplesFactor);
  127. effect.setFloat("randTextureTiles", 4.0 / ratio);
  128. this._firstUpdate = false;
  129. }
  130. effect.setTexture("textureSampler", this._depthTexture);
  131. effect.setTexture("randomSampler", this._randomTexture);
  132. };
  133. }
  134. private _createSSAOCombinePostProcess(ratio: number): void {
  135. this._ssaoCombinePostProcess = new PostProcess("ssaoCombine", "ssaoCombine", [], ["originalColor"],
  136. ratio, null, Texture.BILINEAR_SAMPLINGMODE,
  137. this._scene.getEngine(), false);
  138. this._ssaoCombinePostProcess.onApply = (effect: Effect) => {
  139. effect.setTextureFromPostProcess("originalColor", this._originalColorPostProcess);
  140. };
  141. }
  142. private _createRandomTexture(): void {
  143. var size = 512;
  144. this._randomTexture = new DynamicTexture("SSAORandomTexture", size, this._scene, false, Texture.BILINEAR_SAMPLINGMODE);
  145. this._randomTexture.wrapU = Texture.WRAP_ADDRESSMODE;
  146. this._randomTexture.wrapV = Texture.WRAP_ADDRESSMODE;
  147. var context = this._randomTexture.getContext();
  148. var rand = (min, max) => {
  149. return Math.random() * (max - min) + min;
  150. }
  151. for (var x = 0; x < size; x++) {
  152. for (var y = 0; y < size; y++) {
  153. var randVector = Vector3.Zero();
  154. randVector.x = Math.floor(rand(0.0, 1.0) * 255);
  155. randVector.y = Math.floor(rand(0.0, 1.0) * 255);
  156. randVector.z = Math.floor(rand(0.0, 1.0) * 255);
  157. context.fillStyle = 'rgb(' + randVector.x + ', ' + randVector.y + ', ' + randVector.z + ')';
  158. context.fillRect(x, y, 1, 1);
  159. }
  160. }
  161. this._randomTexture.update(false);
  162. }
  163. }
  164. }