babylon.ssaoRenderingPipeline.js 9.6 KB

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