babylon.ssaoRenderingPipeline.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. /**
  30. * The output strength of the SSAO post-process. Default value is 1.0.
  31. * @type {number}
  32. */
  33. public totalStrength: number = 1.0;
  34. /**
  35. * The radius around the analyzed pixel used by the SSAO post-process. Default value is 0.0006
  36. * @type {number}
  37. */
  38. public radius: number = 0.0001;
  39. /**
  40. * Related to fallOff, used to interpolate SSAO samples (first interpolate function input) based on the occlusion difference of each pixel
  41. * Must not be equal to fallOff and superior to fallOff.
  42. * Default value is 0.975
  43. * @type {number}
  44. */
  45. public area: number = 0.0075;
  46. /**
  47. * Related to area, used to interpolate SSAO samples (second interpolate function input) based on the occlusion difference of each pixel
  48. * Must not be equal to area and inferior to area.
  49. * Default value is 0.0
  50. * @type {number}
  51. */
  52. public fallOff: number = 0.000001;
  53. /**
  54. * The base color of the SSAO post-process
  55. * The final result is "base + ssao" between [0, 1]
  56. * @type {number}
  57. */
  58. public base: number = 0.5;
  59. private _scene: Scene;
  60. private _depthTexture: RenderTargetTexture;
  61. private _randomTexture: DynamicTexture;
  62. private _originalColorPostProcess: PassPostProcess;
  63. private _ssaoPostProcess: PostProcess;
  64. private _blurHPostProcess: BlurPostProcess;
  65. private _blurVPostProcess: BlurPostProcess;
  66. private _ssaoCombinePostProcess: PostProcess;
  67. private _firstUpdate: boolean = true;
  68. /**
  69. * @constructor
  70. * @param {string} name - The rendering pipeline name
  71. * @param {BABYLON.Scene} scene - The scene linked to this pipeline
  72. * @param {any} ratio - The size of the postprocesses. Can be a number shared between passes or an object for more precision: { ssaoRatio: 0.5, combineRatio: 1.0 }
  73. * @param {BABYLON.Camera[]} cameras - The array of cameras that the rendering pipeline will be attached to
  74. */
  75. constructor(name: string, scene: Scene, ratio: any, cameras?: Camera[]) {
  76. super(scene.getEngine(), name);
  77. this._scene = scene;
  78. // Set up assets
  79. this._createRandomTexture();
  80. this._depthTexture = scene.enableDepthRenderer().getDepthMap(); // Force depth renderer "on"
  81. var ssaoRatio = ratio.ssaoRatio || ratio;
  82. var combineRatio = ratio.combineRatio || ratio;
  83. this._originalColorPostProcess = new PassPostProcess("SSAOOriginalSceneColor", combineRatio, null, Texture.BILINEAR_SAMPLINGMODE, scene.getEngine(), false);
  84. this._createSSAOPostProcess(ssaoRatio);
  85. this._blurHPostProcess = new BlurPostProcess("SSAOBlurH", new Vector2(1.0, 0.0), 2.0, ssaoRatio, null, Texture.BILINEAR_SAMPLINGMODE, scene.getEngine(), false);
  86. this._blurVPostProcess = new BlurPostProcess("SSAOBlurV", new Vector2(0.0, 1.0), 2.0, ssaoRatio, null, Texture.BILINEAR_SAMPLINGMODE, scene.getEngine(), false);
  87. this._createSSAOCombinePostProcess(combineRatio);
  88. // Set up pipeline
  89. this.addEffect(new PostProcessRenderEffect(scene.getEngine(), this.SSAOOriginalSceneColorEffect, () => { return this._originalColorPostProcess; }, true));
  90. this.addEffect(new PostProcessRenderEffect(scene.getEngine(), this.SSAORenderEffect, () => { return this._ssaoPostProcess; }, true));
  91. this.addEffect(new PostProcessRenderEffect(scene.getEngine(), this.SSAOBlurHRenderEffect, () => { return this._blurHPostProcess; }, true));
  92. this.addEffect(new PostProcessRenderEffect(scene.getEngine(), this.SSAOBlurVRenderEffect, () => { return this._blurVPostProcess; }, true));
  93. this.addEffect(new PostProcessRenderEffect(scene.getEngine(), this.SSAOCombineRenderEffect, () => { return this._ssaoCombinePostProcess; }, true));
  94. // Finish
  95. scene.postProcessRenderPipelineManager.addPipeline(this);
  96. if (cameras)
  97. scene.postProcessRenderPipelineManager.attachCamerasToRenderPipeline(name, cameras);
  98. }
  99. // Public Methods
  100. /**
  101. * Returns the horizontal blur PostProcess
  102. * @return {BABYLON.BlurPostProcess} The horizontal blur post-process
  103. */
  104. public getBlurHPostProcess(): BlurPostProcess {
  105. return this._blurHPostProcess;
  106. }
  107. /**
  108. * Returns the vertical blur PostProcess
  109. * @return {BABYLON.BlurPostProcess} The vertical blur post-process
  110. */
  111. public getBlurVPostProcess(): BlurPostProcess {
  112. return this._blurVPostProcess;
  113. }
  114. /**
  115. * Removes the internal pipeline assets and detatches the pipeline from the scene cameras
  116. */
  117. public dispose(disableDepthRender: boolean = false): void {
  118. this._scene.postProcessRenderPipelineManager.detachCamerasFromRenderPipeline(this._name, this._scene.cameras);
  119. this._originalColorPostProcess = undefined;
  120. this._ssaoPostProcess = undefined;
  121. this._blurHPostProcess = undefined;
  122. this._blurVPostProcess = undefined;
  123. this._ssaoCombinePostProcess = undefined;
  124. this._randomTexture.dispose();
  125. if (disableDepthRender)
  126. this._scene.disableDepthRenderer();
  127. }
  128. // Private Methods
  129. private _createSSAOPostProcess(ratio: number): void {
  130. var numSamples = 16;
  131. var sampleSphere = [
  132. 0.5381, 0.1856, -0.4319,
  133. 0.1379, 0.2486, 0.4430,
  134. 0.3371, 0.5679, -0.0057,
  135. -0.6999, -0.0451, -0.0019,
  136. 0.0689, -0.1598, -0.8547,
  137. 0.0560, 0.0069, -0.1843,
  138. -0.0146, 0.1402, 0.0762,
  139. 0.0100, -0.1924, -0.0344,
  140. -0.3577, -0.5301, -0.4358,
  141. -0.3169, 0.1063, 0.0158,
  142. 0.0103, -0.5869, 0.0046,
  143. -0.0897, -0.4940, 0.3287,
  144. 0.7119, -0.0154, -0.0918,
  145. -0.0533, 0.0596, -0.5411,
  146. 0.0352, -0.0631, 0.5460,
  147. -0.4776, 0.2847, -0.0271
  148. ];
  149. var samplesFactor = 1.0 / numSamples;
  150. this._ssaoPostProcess = new PostProcess("ssao", "ssao",
  151. [
  152. "sampleSphere", "samplesFactor", "randTextureTiles", "totalStrength", "radius",
  153. "area", "fallOff", "base"
  154. ],
  155. ["randomSampler"],
  156. ratio, null, Texture.BILINEAR_SAMPLINGMODE,
  157. this._scene.getEngine(), false,
  158. "#define SAMPLES " + numSamples);
  159. this._ssaoPostProcess.onApply = (effect: Effect) => {
  160. if (this._firstUpdate) {
  161. effect.setArray3("sampleSphere", sampleSphere);
  162. effect.setFloat("samplesFactor", samplesFactor);
  163. effect.setFloat("randTextureTiles", 4.0);
  164. this._firstUpdate = false;
  165. }
  166. effect.setFloat("totalStrength", this.totalStrength);
  167. effect.setFloat("radius", this.radius);
  168. effect.setFloat("area", this.area);
  169. effect.setFloat("fallOff", this.fallOff);
  170. effect.setFloat("base", this.base);
  171. effect.setTexture("textureSampler", this._depthTexture);
  172. effect.setTexture("randomSampler", this._randomTexture);
  173. };
  174. }
  175. private _createSSAOCombinePostProcess(ratio: number): void {
  176. this._ssaoCombinePostProcess = new PostProcess("ssaoCombine", "ssaoCombine", [], ["originalColor"],
  177. ratio, null, Texture.BILINEAR_SAMPLINGMODE,
  178. this._scene.getEngine(), false);
  179. this._ssaoCombinePostProcess.onApply = (effect: Effect) => {
  180. effect.setTextureFromPostProcess("originalColor", this._originalColorPostProcess);
  181. };
  182. }
  183. private _createRandomTexture(): void {
  184. var size = 512;
  185. this._randomTexture = new DynamicTexture("SSAORandomTexture", size, this._scene, false, Texture.BILINEAR_SAMPLINGMODE);
  186. this._randomTexture.wrapU = Texture.WRAP_ADDRESSMODE;
  187. this._randomTexture.wrapV = Texture.WRAP_ADDRESSMODE;
  188. var context = this._randomTexture.getContext();
  189. var rand = (min, max) => {
  190. return Math.random() * (max - min) + min;
  191. }
  192. var randVector = Vector3.Zero();
  193. for (var x = 0; x < size; x++) {
  194. for (var y = 0; y < size; y++) {
  195. randVector.x = Math.floor(rand(-1.0, 1.0) * 255);
  196. randVector.y = Math.floor(rand(-1.0, 1.0) * 255);
  197. randVector.z = Math.floor(rand(-1.0, 1.0) * 255);
  198. context.fillStyle = 'rgb(' + randVector.x + ', ' + randVector.y + ', ' + randVector.z + ')';
  199. context.fillRect(x, y, 1, 1);
  200. }
  201. }
  202. this._randomTexture.update(false);
  203. }
  204. }
  205. }