babylon.ssaoRenderingPipeline.js 9.7 KB

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