babylon.ssaoRenderingPipeline.ts 13 KB

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