babylon.ssaoRenderingPipeline.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. function SSAORenderingPipeline(name, scene, ratio) {
  12. var _this = this;
  13. if (ratio === void 0) { ratio = 1.0; }
  14. _super.call(this, scene.getEngine(), name);
  15. // Members
  16. this.SSAOOriginalSceneColorEffect = "SSAOOriginalSceneColorEffect";
  17. this.SSAORenderEffect = "SSAORenderEffect";
  18. this.SSAOBlurHRenderEffect = "SSAOBlurHRenderEffect";
  19. this.SSAOBlurVRenderEffect = "SSAOBlurVRenderEffect";
  20. this.SSAOCombineRenderEffect = "SSAOCombineRenderEffect";
  21. this._scene = null;
  22. this._depthTexture = null;
  23. this._randomTexture = null;
  24. this._originalColorPostProcess = null;
  25. this._ssaoPostProcess = null;
  26. this._blurHPostProcess = null;
  27. this._blurVPostProcess = null;
  28. this._ssaoCombinePostProcess = null;
  29. this._firstUpdate = true;
  30. this._scene = scene;
  31. // Set up assets
  32. this._createRandomTexture();
  33. this._depthTexture = scene.enableDepthRenderer().getDepthMap(); // Force depth renderer "on"
  34. this._originalColorPostProcess = new BABYLON.PassPostProcess("SSAOOriginalSceneColor", 1.0, null, BABYLON.Texture.BILINEAR_SAMPLINGMODE, scene.getEngine(), false);
  35. this._createSSAOPostProcess(ratio);
  36. this._blurHPostProcess = new BABYLON.BlurPostProcess("SSAOBlur", new BABYLON.Vector2(1.0, 0.0), 1.0, ratio, null, BABYLON.Texture.BILINEAR_SAMPLINGMODE, scene.getEngine(), false);
  37. this._blurVPostProcess = new BABYLON.BlurPostProcess("SSAOBlur", new BABYLON.Vector2(0.0, 1.0), 1.0, ratio, null, BABYLON.Texture.BILINEAR_SAMPLINGMODE, scene.getEngine(), false);
  38. this._createSSAOCombinePostProcess();
  39. // Set up pipeline
  40. this.addEffect(new BABYLON.PostProcessRenderEffect(scene.getEngine(), this.SSAOOriginalSceneColorEffect, function () {
  41. return _this._originalColorPostProcess;
  42. }, true));
  43. this.addEffect(new BABYLON.PostProcessRenderEffect(scene.getEngine(), this.SSAORenderEffect, function () {
  44. return _this._ssaoPostProcess;
  45. }, true));
  46. this.addEffect(new BABYLON.PostProcessRenderEffect(scene.getEngine(), this.SSAOBlurHRenderEffect, function () {
  47. return _this._blurHPostProcess;
  48. }, true));
  49. this.addEffect(new BABYLON.PostProcessRenderEffect(scene.getEngine(), this.SSAOBlurVRenderEffect, function () {
  50. return _this._blurVPostProcess;
  51. }, true));
  52. this.addEffect(new BABYLON.PostProcessRenderEffect(scene.getEngine(), this.SSAOCombineRenderEffect, function () {
  53. return _this._ssaoCombinePostProcess;
  54. }, true));
  55. // Finish
  56. scene.postProcessRenderPipelineManager.addPipeline(this);
  57. }
  58. // Public Methods
  59. SSAORenderingPipeline.prototype.getBlurHPostProcess = function () {
  60. return this._blurHPostProcess;
  61. };
  62. SSAORenderingPipeline.prototype.getBlurVPostProcess = function () {
  63. return this._blurVPostProcess;
  64. };
  65. // Private Methods
  66. SSAORenderingPipeline.prototype._createSSAOPostProcess = function (ratio) {
  67. var _this = this;
  68. var sampleSphere = [
  69. 0.5381,
  70. 0.1856,
  71. -0.4319,
  72. 0.1379,
  73. 0.2486,
  74. 0.4430,
  75. 0.3371,
  76. 0.5679,
  77. -0.0057,
  78. -0.6999,
  79. -0.0451,
  80. -0.0019,
  81. 0.0689,
  82. -0.1598,
  83. -0.8547,
  84. 0.0560,
  85. 0.0069,
  86. -0.1843,
  87. -0.0146,
  88. 0.1402,
  89. 0.0762,
  90. 0.0100,
  91. -0.1924,
  92. -0.0344,
  93. -0.3577,
  94. -0.5301,
  95. -0.4358,
  96. -0.3169,
  97. 0.1063,
  98. 0.0158,
  99. 0.0103,
  100. -0.5869,
  101. 0.0046,
  102. -0.0897,
  103. -0.4940,
  104. 0.3287,
  105. 0.7119,
  106. -0.0154,
  107. -0.0918,
  108. -0.0533,
  109. 0.0596,
  110. -0.5411,
  111. 0.0352,
  112. -0.0631,
  113. 0.5460,
  114. -0.4776,
  115. 0.2847,
  116. -0.0271
  117. ];
  118. this._ssaoPostProcess = new BABYLON.PostProcess("ssao", "ssao", ["sampleSphere"], ["randomSampler"], ratio, null, BABYLON.Texture.BILINEAR_SAMPLINGMODE, this._scene.getEngine(), false);
  119. this._ssaoPostProcess.onApply = function (effect) {
  120. if (_this._firstUpdate === true) {
  121. effect.setArray3("sampleSphere", sampleSphere);
  122. _this._firstUpdate = false;
  123. }
  124. effect.setTexture("textureSampler", _this._depthTexture);
  125. effect.setTexture("randomSampler", _this._randomTexture);
  126. };
  127. return this._ssaoPostProcess;
  128. };
  129. SSAORenderingPipeline.prototype._createSSAOCombinePostProcess = function () {
  130. var _this = this;
  131. this._ssaoCombinePostProcess = new BABYLON.PostProcess("ssaoCombine", "ssaoCombine", [], ["originalColor"], 1.0, null, BABYLON.Texture.BILINEAR_SAMPLINGMODE, this._scene.getEngine(), false);
  132. this._ssaoCombinePostProcess.onApply = function (effect) {
  133. effect.setTextureFromPostProcess("originalColor", _this._originalColorPostProcess);
  134. };
  135. return this._ssaoCombinePostProcess;
  136. };
  137. SSAORenderingPipeline.prototype._createRandomTexture = function () {
  138. var size = 512;
  139. this._randomTexture = new BABYLON.DynamicTexture("SSAORandomTexture", size, this._scene, false, BABYLON.Texture.BILINEAR_SAMPLINGMODE);
  140. this._randomTexture.wrapU = BABYLON.Texture.WRAP_ADDRESSMODE;
  141. this._randomTexture.wrapV = BABYLON.Texture.WRAP_ADDRESSMODE;
  142. var context = this._randomTexture.getContext();
  143. var rand = function (min, max) {
  144. return Math.random() * (max - min) + min;
  145. };
  146. for (var x = 0; x < size; x++) {
  147. for (var y = 0; y < size; y++) {
  148. var randVector = BABYLON.Vector3.Zero();
  149. randVector.x = Math.floor(rand(0.0, 1.0) * 255);
  150. randVector.y = Math.floor(rand(0.0, 1.0) * 255);
  151. randVector.z = Math.floor(rand(0.0, 1.0) * 255);
  152. context.fillStyle = 'rgb(' + randVector.x + ', ' + randVector.y + ', ' + randVector.z + ')';
  153. context.fillRect(x, y, 1, 1);
  154. }
  155. }
  156. this._randomTexture.update(false);
  157. };
  158. return SSAORenderingPipeline;
  159. })(BABYLON.PostProcessRenderPipeline);
  160. BABYLON.SSAORenderingPipeline = SSAORenderingPipeline;
  161. })(BABYLON || (BABYLON = {}));
  162. //# sourceMappingURL=babylon.ssaoRenderingPipeline.js.map