babylon.ssaoRenderingPipeline.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. var __extends = this.__extends || function (d, b) {
  2. for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
  3. function __() { this.constructor = d; }
  4. __.prototype = b.prototype;
  5. d.prototype = new __();
  6. };
  7. var BABYLON;
  8. (function (BABYLON) {
  9. var SSAORenderingPipeline = (function (_super) {
  10. __extends(SSAORenderingPipeline, _super);
  11. /**
  12. * @constructor
  13. * @param {string} name - The rendering pipeline name
  14. * @param {BABYLON.Scene} scene - The scene linked to this pipeline
  15. * @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 }
  16. * @param {BABYLON.Camera[]} cameras - The array of cameras that the rendering pipeline will be attached to
  17. */
  18. function SSAORenderingPipeline(name, scene, ratio, cameras) {
  19. var _this = this;
  20. _super.call(this, scene.getEngine(), name);
  21. // Members
  22. /**
  23. * The PassPostProcess id in the pipeline that contains the original scene color
  24. * @type {string}
  25. */
  26. this.SSAOOriginalSceneColorEffect = "SSAOOriginalSceneColorEffect";
  27. /**
  28. * The SSAO PostProcess id in the pipeline
  29. * @type {string}
  30. */
  31. this.SSAORenderEffect = "SSAORenderEffect";
  32. /**
  33. * The horizontal blur PostProcess id in the pipeline
  34. * @type {string}
  35. */
  36. this.SSAOBlurHRenderEffect = "SSAOBlurHRenderEffect";
  37. /**
  38. * The vertical blur PostProcess id in the pipeline
  39. * @type {string}
  40. */
  41. this.SSAOBlurVRenderEffect = "SSAOBlurVRenderEffect";
  42. /**
  43. * The PostProcess id in the pipeline that combines the SSAO-Blur output with the original scene color (SSAOOriginalSceneColorEffect)
  44. * @type {string}
  45. */
  46. this.SSAOCombineRenderEffect = "SSAOCombineRenderEffect";
  47. /**
  48. * The output strength of the SSAO post-process. Default value is 1.0.
  49. * @type {number}
  50. */
  51. this.totalStrength = 1.0;
  52. /**
  53. * The radius around the analyzed pixel used by the SSAO post-process. Default value is 0.0002
  54. * @type {number}
  55. */
  56. this.radius = 0.0002;
  57. /**
  58. * Related to fallOff, used to interpolate SSAO samples (first interpolate function input) based on the occlusion difference of each pixel
  59. * Must not be equal to fallOff and superior to fallOff.
  60. * Default value is 0.0075
  61. * @type {number}
  62. */
  63. this.area = 0.0075;
  64. /**
  65. * Related to area, used to interpolate SSAO samples (second interpolate function input) based on the occlusion difference of each pixel
  66. * Must not be equal to area and inferior to area.
  67. * Default value is 0.0002
  68. * @type {number}
  69. */
  70. this.fallOff = 0.0002;
  71. this._firstUpdate = true;
  72. this._scene = scene;
  73. // Set up assets
  74. this._createRandomTexture();
  75. this._depthTexture = scene.enableDepthRenderer().getDepthMap(); // Force depth renderer "on"
  76. var ssaoRatio = ratio.ssaoRatio || ratio;
  77. var combineRatio = ratio.combineRatio || ratio;
  78. this._originalColorPostProcess = new BABYLON.PassPostProcess("SSAOOriginalSceneColor", combineRatio, null, BABYLON.Texture.BILINEAR_SAMPLINGMODE, scene.getEngine(), false);
  79. this._createSSAOPostProcess(ssaoRatio);
  80. this._blurHPostProcess = new BABYLON.BlurPostProcess("SSAOBlurH", new BABYLON.Vector2(2.0, 0.0), 2.0, ssaoRatio, null, BABYLON.Texture.BILINEAR_SAMPLINGMODE, scene.getEngine(), false);
  81. this._blurVPostProcess = new BABYLON.BlurPostProcess("SSAOBlurV", new BABYLON.Vector2(0.0, 2.0), 2.0, ssaoRatio, null, BABYLON.Texture.BILINEAR_SAMPLINGMODE, scene.getEngine(), false);
  82. this._createSSAOCombinePostProcess(combineRatio);
  83. // Set up pipeline
  84. this.addEffect(new BABYLON.PostProcessRenderEffect(scene.getEngine(), this.SSAOOriginalSceneColorEffect, function () {
  85. return _this._originalColorPostProcess;
  86. }, true));
  87. this.addEffect(new BABYLON.PostProcessRenderEffect(scene.getEngine(), this.SSAORenderEffect, function () {
  88. return _this._ssaoPostProcess;
  89. }, true));
  90. this.addEffect(new BABYLON.PostProcessRenderEffect(scene.getEngine(), this.SSAOBlurHRenderEffect, function () {
  91. return _this._blurHPostProcess;
  92. }, true));
  93. this.addEffect(new BABYLON.PostProcessRenderEffect(scene.getEngine(), this.SSAOBlurVRenderEffect, function () {
  94. return _this._blurVPostProcess;
  95. }, true));
  96. this.addEffect(new BABYLON.PostProcessRenderEffect(scene.getEngine(), this.SSAOCombineRenderEffect, function () {
  97. return _this._ssaoCombinePostProcess;
  98. }, true));
  99. // Finish
  100. scene.postProcessRenderPipelineManager.addPipeline(this);
  101. if (cameras)
  102. scene.postProcessRenderPipelineManager.attachCamerasToRenderPipeline(name, cameras);
  103. }
  104. // Public Methods
  105. /**
  106. * Returns the horizontal blur PostProcess
  107. * @return {BABYLON.BlurPostProcess} The horizontal blur post-process
  108. */
  109. SSAORenderingPipeline.prototype.getBlurHPostProcess = function () {
  110. return this._blurHPostProcess;
  111. };
  112. /**
  113. * Returns the vertical blur PostProcess
  114. * @return {BABYLON.BlurPostProcess} The vertical blur post-process
  115. */
  116. SSAORenderingPipeline.prototype.getBlurVPostProcess = function () {
  117. return this._blurVPostProcess;
  118. };
  119. /**
  120. * Removes the internal pipeline assets and detatches the pipeline from the scene cameras
  121. */
  122. SSAORenderingPipeline.prototype.dispose = function (disableDepthRender) {
  123. if (disableDepthRender === void 0) { disableDepthRender = false; }
  124. this._scene.postProcessRenderPipelineManager.detachCamerasFromRenderPipeline(this._name, this._scene.cameras);
  125. this._originalColorPostProcess = undefined;
  126. this._ssaoPostProcess = undefined;
  127. this._blurHPostProcess = undefined;
  128. this._blurVPostProcess = undefined;
  129. this._ssaoCombinePostProcess = undefined;
  130. this._randomTexture.dispose();
  131. if (disableDepthRender)
  132. this._scene.disableDepthRenderer();
  133. };
  134. // Private Methods
  135. SSAORenderingPipeline.prototype._createSSAOPostProcess = function (ratio) {
  136. var _this = this;
  137. var sampleSphere = [
  138. 0.5381,
  139. 0.1856,
  140. -0.4319,
  141. 0.1379,
  142. 0.2486,
  143. 0.4430,
  144. 0.3371,
  145. 0.5679,
  146. -0.0057,
  147. -0.6999,
  148. -0.0451,
  149. -0.0019,
  150. 0.0689,
  151. -0.1598,
  152. -0.8547,
  153. 0.0560,
  154. 0.0069,
  155. -0.1843,
  156. -0.0146,
  157. 0.1402,
  158. 0.0762,
  159. 0.0100,
  160. -0.1924,
  161. -0.0344,
  162. -0.3577,
  163. -0.5301,
  164. -0.4358,
  165. -0.3169,
  166. 0.1063,
  167. 0.0158,
  168. 0.0103,
  169. -0.5869,
  170. 0.0046,
  171. -0.0897,
  172. -0.4940,
  173. 0.3287,
  174. 0.7119,
  175. -0.0154,
  176. -0.0918,
  177. -0.0533,
  178. 0.0596,
  179. -0.5411,
  180. 0.0352,
  181. -0.0631,
  182. 0.5460,
  183. -0.4776,
  184. 0.2847,
  185. -0.0271
  186. ];
  187. var samplesFactor = 1.0 / 16.0;
  188. this._ssaoPostProcess = new BABYLON.PostProcess("ssao", "ssao", ["sampleSphere", "samplesFactor", "randTextureTiles", "totalStrength", "radius", "area", "fallOff"], ["randomSampler"], ratio, null, BABYLON.Texture.BILINEAR_SAMPLINGMODE, this._scene.getEngine(), false);
  189. this._ssaoPostProcess.onApply = function (effect) {
  190. if (_this._firstUpdate) {
  191. effect.setArray3("sampleSphere", sampleSphere);
  192. effect.setFloat("samplesFactor", samplesFactor);
  193. effect.setFloat("randTextureTiles", 4.0 / ratio);
  194. _this._firstUpdate = false;
  195. }
  196. effect.setFloat("totalStrength", _this.totalStrength);
  197. effect.setFloat("radius", _this.radius);
  198. effect.setFloat("area", _this.area);
  199. effect.setFloat("fallOff", _this.fallOff);
  200. effect.setTexture("textureSampler", _this._depthTexture);
  201. effect.setTexture("randomSampler", _this._randomTexture);
  202. };
  203. };
  204. SSAORenderingPipeline.prototype._createSSAOCombinePostProcess = function (ratio) {
  205. var _this = this;
  206. this._ssaoCombinePostProcess = new BABYLON.PostProcess("ssaoCombine", "ssaoCombine", [], ["originalColor"], ratio, null, BABYLON.Texture.BILINEAR_SAMPLINGMODE, this._scene.getEngine(), false);
  207. this._ssaoCombinePostProcess.onApply = function (effect) {
  208. effect.setTextureFromPostProcess("originalColor", _this._originalColorPostProcess);
  209. };
  210. };
  211. SSAORenderingPipeline.prototype._createRandomTexture = function () {
  212. var size = 512;
  213. this._randomTexture = new BABYLON.DynamicTexture("SSAORandomTexture", size, this._scene, false, BABYLON.Texture.BILINEAR_SAMPLINGMODE);
  214. this._randomTexture.wrapU = BABYLON.Texture.WRAP_ADDRESSMODE;
  215. this._randomTexture.wrapV = BABYLON.Texture.WRAP_ADDRESSMODE;
  216. var context = this._randomTexture.getContext();
  217. var rand = function (min, max) {
  218. return Math.random() * (max - min) + min;
  219. };
  220. for (var x = 0; x < size; x++) {
  221. for (var y = 0; y < size; y++) {
  222. var randVector = BABYLON.Vector3.Zero();
  223. randVector.x = Math.floor(rand(0.0, 1.0) * 255);
  224. randVector.y = Math.floor(rand(0.0, 1.0) * 255);
  225. randVector.z = Math.floor(rand(0.0, 1.0) * 255);
  226. context.fillStyle = 'rgb(' + randVector.x + ', ' + randVector.y + ', ' + randVector.z + ')';
  227. context.fillRect(x, y, 1, 1);
  228. }
  229. }
  230. this._randomTexture.update(false);
  231. };
  232. return SSAORenderingPipeline;
  233. })(BABYLON.PostProcessRenderPipeline);
  234. BABYLON.SSAORenderingPipeline = SSAORenderingPipeline;
  235. })(BABYLON || (BABYLON = {}));
  236. //# sourceMappingURL=babylon.ssaoRenderingPipeline.js.map