babylon.ssaoRenderingPipeline.js 7.7 KB

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