babylon.ssaoRenderingPipeline.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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.002
  54. */
  55. this.radius = 0.002;
  56. this._firstUpdate = true;
  57. this._scene = scene;
  58. // Set up assets
  59. this._createRandomTexture();
  60. this._depthTexture = scene.enableDepthRenderer().getDepthMap(); // Force depth renderer "on"
  61. var ssaoRatio = ratio.ssaoRatio || ratio;
  62. var combineRatio = ratio.combineRatio || ratio;
  63. this._originalColorPostProcess = new BABYLON.PassPostProcess("SSAOOriginalSceneColor", combineRatio, null, BABYLON.Texture.BILINEAR_SAMPLINGMODE, scene.getEngine(), false);
  64. this._createSSAOPostProcess(ssaoRatio);
  65. this._blurHPostProcess = new BABYLON.BlurPostProcess("SSAOBlurH", new BABYLON.Vector2(1.0, 0.0), 4.0, ssaoRatio, null, BABYLON.Texture.BILINEAR_SAMPLINGMODE, scene.getEngine(), false);
  66. this._blurVPostProcess = new BABYLON.BlurPostProcess("SSAOBlurV", new BABYLON.Vector2(0.0, 1.0), 4.0, ssaoRatio, null, BABYLON.Texture.BILINEAR_SAMPLINGMODE, scene.getEngine(), false);
  67. this._createSSAOCombinePostProcess(combineRatio);
  68. // Set up pipeline
  69. this.addEffect(new BABYLON.PostProcessRenderEffect(scene.getEngine(), this.SSAOOriginalSceneColorEffect, function () {
  70. return _this._originalColorPostProcess;
  71. }, true));
  72. this.addEffect(new BABYLON.PostProcessRenderEffect(scene.getEngine(), this.SSAORenderEffect, function () {
  73. return _this._ssaoPostProcess;
  74. }, true));
  75. this.addEffect(new BABYLON.PostProcessRenderEffect(scene.getEngine(), this.SSAOBlurHRenderEffect, function () {
  76. return _this._blurHPostProcess;
  77. }, true));
  78. this.addEffect(new BABYLON.PostProcessRenderEffect(scene.getEngine(), this.SSAOBlurVRenderEffect, function () {
  79. return _this._blurVPostProcess;
  80. }, true));
  81. this.addEffect(new BABYLON.PostProcessRenderEffect(scene.getEngine(), this.SSAOCombineRenderEffect, function () {
  82. return _this._ssaoCombinePostProcess;
  83. }, true));
  84. // Finish
  85. scene.postProcessRenderPipelineManager.addPipeline(this);
  86. if (cameras)
  87. scene.postProcessRenderPipelineManager.attachCamerasToRenderPipeline(name, cameras);
  88. }
  89. // Public Methods
  90. /**
  91. * Returns the horizontal blur PostProcess
  92. * @return {BABYLON.BlurPostProcess} The horizontal blur post-process
  93. */
  94. SSAORenderingPipeline.prototype.getBlurHPostProcess = function () {
  95. return this._blurHPostProcess;
  96. };
  97. /**
  98. * Returns the vertical blur PostProcess
  99. * @return {BABYLON.BlurPostProcess} The vertical blur post-process
  100. */
  101. SSAORenderingPipeline.prototype.getBlurVPostProcess = function () {
  102. return this._blurVPostProcess;
  103. };
  104. /**
  105. * Removes the internal pipeline assets and detatches the pipeline from the scene cameras
  106. */
  107. SSAORenderingPipeline.prototype.dispose = function (disableDepthRender) {
  108. if (disableDepthRender === void 0) { disableDepthRender = false; }
  109. this._scene.postProcessRenderPipelineManager.detachCamerasFromRenderPipeline(this._name, this._scene.cameras);
  110. this._originalColorPostProcess = undefined;
  111. this._ssaoPostProcess = undefined;
  112. this._blurHPostProcess = undefined;
  113. this._blurVPostProcess = undefined;
  114. this._ssaoCombinePostProcess = undefined;
  115. this._randomTexture.dispose();
  116. if (disableDepthRender)
  117. this._scene.disableDepthRenderer();
  118. };
  119. // Private Methods
  120. SSAORenderingPipeline.prototype._createSSAOPostProcess = function (ratio) {
  121. var _this = this;
  122. var sampleSphere = [
  123. 0.5381,
  124. 0.1856,
  125. -0.4319,
  126. 0.1379,
  127. 0.2486,
  128. 0.4430,
  129. 0.3371,
  130. 0.5679,
  131. -0.0057,
  132. -0.6999,
  133. -0.0451,
  134. -0.0019,
  135. 0.0689,
  136. -0.1598,
  137. -0.8547,
  138. 0.0560,
  139. 0.0069,
  140. -0.1843,
  141. -0.0146,
  142. 0.1402,
  143. 0.0762,
  144. 0.0100,
  145. -0.1924,
  146. -0.0344,
  147. -0.3577,
  148. -0.5301,
  149. -0.4358,
  150. -0.3169,
  151. 0.1063,
  152. 0.0158,
  153. 0.0103,
  154. -0.5869,
  155. 0.0046,
  156. -0.0897,
  157. -0.4940,
  158. 0.3287,
  159. 0.7119,
  160. -0.0154,
  161. -0.0918,
  162. -0.0533,
  163. 0.0596,
  164. -0.5411,
  165. 0.0352,
  166. -0.0631,
  167. 0.5460,
  168. -0.4776,
  169. 0.2847,
  170. -0.0271
  171. ];
  172. var samplesFactor = 1.0 / 16.0;
  173. this._ssaoPostProcess = new BABYLON.PostProcess("ssao", "ssao", ["sampleSphere", "samplesFactor", "randTextureTiles", "totalStrength", "radius"], ["randomSampler"], ratio, null, BABYLON.Texture.BILINEAR_SAMPLINGMODE, this._scene.getEngine(), false);
  174. this._ssaoPostProcess.onApply = function (effect) {
  175. if (_this._firstUpdate) {
  176. effect.setArray3("sampleSphere", sampleSphere);
  177. effect.setFloat("samplesFactor", samplesFactor);
  178. effect.setFloat("randTextureTiles", 4.0 / ratio);
  179. _this._firstUpdate = false;
  180. }
  181. effect.setFloat("totalStrength", _this.totalStrength);
  182. effect.setFloat("radius", _this.radius);
  183. effect.setTexture("textureSampler", _this._depthTexture);
  184. effect.setTexture("randomSampler", _this._randomTexture);
  185. };
  186. };
  187. SSAORenderingPipeline.prototype._createSSAOCombinePostProcess = function (ratio) {
  188. var _this = this;
  189. this._ssaoCombinePostProcess = new BABYLON.PostProcess("ssaoCombine", "ssaoCombine", [], ["originalColor"], ratio, null, BABYLON.Texture.BILINEAR_SAMPLINGMODE, this._scene.getEngine(), false);
  190. this._ssaoCombinePostProcess.onApply = function (effect) {
  191. effect.setTextureFromPostProcess("originalColor", _this._originalColorPostProcess);
  192. };
  193. };
  194. SSAORenderingPipeline.prototype._createRandomTexture = function () {
  195. var size = 512;
  196. this._randomTexture = new BABYLON.DynamicTexture("SSAORandomTexture", size, this._scene, false, BABYLON.Texture.BILINEAR_SAMPLINGMODE);
  197. this._randomTexture.wrapU = BABYLON.Texture.WRAP_ADDRESSMODE;
  198. this._randomTexture.wrapV = BABYLON.Texture.WRAP_ADDRESSMODE;
  199. var context = this._randomTexture.getContext();
  200. var rand = function (min, max) {
  201. return Math.random() * (max - min) + min;
  202. };
  203. for (var x = 0; x < size; x++) {
  204. for (var y = 0; y < size; y++) {
  205. var randVector = BABYLON.Vector3.Zero();
  206. randVector.x = Math.floor(rand(0.0, 1.0) * 255);
  207. randVector.y = Math.floor(rand(0.0, 1.0) * 255);
  208. randVector.z = Math.floor(rand(0.0, 1.0) * 255);
  209. context.fillStyle = 'rgb(' + randVector.x + ', ' + randVector.y + ', ' + randVector.z + ')';
  210. context.fillRect(x, y, 1, 1);
  211. }
  212. }
  213. this._randomTexture.update(false);
  214. };
  215. return SSAORenderingPipeline;
  216. })(BABYLON.PostProcessRenderPipeline);
  217. BABYLON.SSAORenderingPipeline = SSAORenderingPipeline;
  218. })(BABYLON || (BABYLON = {}));
  219. //# sourceMappingURL=babylon.ssaoRenderingPipeline.js.map