babylon.ssaoRenderingPipeline.js 7.8 KB

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