babylon.ssaoRenderingPipeline.ts 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. module BABYLON {
  2. export class SSAORenderingPipeline extends PostProcessRenderPipeline {
  3. // Members
  4. public SSAOOriginalSceneColorEffect: string = "SSAOOriginalSceneColorEffect";
  5. public SSAORenderEffect: string = "SSAORenderEffect";
  6. public SSAOBlurHRenderEffect: string = "SSAOBlurHRenderEffect";
  7. public SSAOBlurVRenderEffect: string = "SSAOBlurVRenderEffect";
  8. public SSAOCombineRenderEffect: string = "SSAOCombineRenderEffect";
  9. private _scene: Scene;
  10. private _depthTexture: RenderTargetTexture;
  11. private _randomTexture: DynamicTexture;
  12. private _originalColorPostProcess: PassPostProcess;
  13. private _ssaoPostProcess: PostProcess;
  14. private _blurHPostProcess: BlurPostProcess;
  15. private _blurVPostProcess: BlurPostProcess;
  16. private _ssaoCombinePostProcess: PostProcess;
  17. private _firstUpdate: boolean = true;
  18. constructor(name: string, scene: Scene, ratio: number = 1.0, cameras?: Camera[]) {
  19. super(scene.getEngine(), name);
  20. this._scene = scene;
  21. // Set up assets
  22. this._createRandomTexture();
  23. this._depthTexture = scene.enableDepthRenderer().getDepthMap(); // Force depth renderer "on"
  24. this._originalColorPostProcess = new PassPostProcess("SSAOOriginalSceneColor", 1.0, null, Texture.BILINEAR_SAMPLINGMODE, scene.getEngine(), false);
  25. this._createSSAOPostProcess(ratio);
  26. this._blurHPostProcess = new BlurPostProcess("SSAOBlurH", new Vector2(1.0, 0.0), 1.0, ratio, null, Texture.BILINEAR_SAMPLINGMODE, scene.getEngine(), false);
  27. this._blurVPostProcess = new BlurPostProcess("SSAOBlurV", new Vector2(0.0, 1.0), 1.0, ratio, null, Texture.BILINEAR_SAMPLINGMODE, scene.getEngine(), false);
  28. this._createSSAOCombinePostProcess();
  29. // Set up pipeline
  30. this.addEffect(new PostProcessRenderEffect(scene.getEngine(), this.SSAOOriginalSceneColorEffect, () => { return this._originalColorPostProcess; }, true));
  31. this.addEffect(new PostProcessRenderEffect(scene.getEngine(), this.SSAORenderEffect, () => { return this._ssaoPostProcess; }, true));
  32. this.addEffect(new PostProcessRenderEffect(scene.getEngine(), this.SSAOBlurHRenderEffect, () => { return this._blurHPostProcess; }, true));
  33. this.addEffect(new PostProcessRenderEffect(scene.getEngine(), this.SSAOBlurVRenderEffect, () => { return this._blurVPostProcess; }, true));
  34. this.addEffect(new PostProcessRenderEffect(scene.getEngine(), this.SSAOCombineRenderEffect, () => { return this._ssaoCombinePostProcess; }, true));
  35. // Finish
  36. scene.postProcessRenderPipelineManager.addPipeline(this);
  37. }
  38. // Public Methods
  39. public getBlurHPostProcess(): BlurPostProcess {
  40. return this._blurHPostProcess;
  41. }
  42. public getBlurVPostProcess(): BlurPostProcess {
  43. return this._blurVPostProcess;
  44. }
  45. public dispose(cameras: any): void {
  46. if (cameras !== undefined)
  47. this._scene.postProcessRenderPipelineManager.detachCamerasFromRenderPipeline(this._name, cameras);
  48. this._originalColorPostProcess = undefined;
  49. this._ssaoPostProcess = undefined;
  50. this._blurHPostProcess = undefined;
  51. this._blurVPostProcess = undefined;
  52. this._ssaoCombinePostProcess = undefined;
  53. this._randomTexture.dispose();
  54. }
  55. // Private Methods
  56. private _createSSAOPostProcess(ratio: number): PostProcess {
  57. var sampleSphere = [
  58. 0.5381, 0.1856, -0.4319,
  59. 0.1379, 0.2486, 0.4430,
  60. 0.3371, 0.5679, -0.0057,
  61. -0.6999, -0.0451, -0.0019,
  62. 0.0689, -0.1598, -0.8547,
  63. 0.0560, 0.0069, -0.1843,
  64. -0.0146, 0.1402, 0.0762,
  65. 0.0100, -0.1924, -0.0344,
  66. -0.3577, -0.5301, -0.4358,
  67. -0.3169, 0.1063, 0.0158,
  68. 0.0103, -0.5869, 0.0046,
  69. -0.0897, -0.4940, 0.3287,
  70. 0.7119, -0.0154, -0.0918,
  71. -0.0533, 0.0596, -0.5411,
  72. 0.0352, -0.0631, 0.5460,
  73. -0.4776, 0.2847, -0.0271
  74. ];
  75. var samplesFactor = 1.0 / 16.0;
  76. this._ssaoPostProcess = new PostProcess("ssao", "ssao", ["sampleSphere", "samplesFactor"], ["randomSampler"],
  77. ratio, null, Texture.BILINEAR_SAMPLINGMODE,
  78. this._scene.getEngine(), false);
  79. this._ssaoPostProcess.onApply = (effect: Effect) => {
  80. if (this._firstUpdate) {
  81. effect.setArray3("sampleSphere", sampleSphere);
  82. effect.setFloat("samplesFactor", samplesFactor);
  83. this._firstUpdate = false;
  84. }
  85. effect.setTexture("textureSampler", this._depthTexture);
  86. effect.setTexture("randomSampler", this._randomTexture);
  87. };
  88. return this._ssaoPostProcess;
  89. }
  90. private _createSSAOCombinePostProcess(): PostProcess {
  91. this._ssaoCombinePostProcess = new PostProcess("ssaoCombine", "ssaoCombine", [], ["originalColor"],
  92. 1.0, null, Texture.BILINEAR_SAMPLINGMODE,
  93. this._scene.getEngine(), false);
  94. this._ssaoCombinePostProcess.onApply = (effect: Effect) => {
  95. effect.setTextureFromPostProcess("originalColor", this._originalColorPostProcess);
  96. };
  97. return this._ssaoCombinePostProcess;
  98. }
  99. private _createRandomTexture(): void {
  100. var size = 512;
  101. this._randomTexture = new DynamicTexture("SSAORandomTexture", size, this._scene, false, Texture.BILINEAR_SAMPLINGMODE);
  102. this._randomTexture.wrapU = Texture.WRAP_ADDRESSMODE;
  103. this._randomTexture.wrapV = Texture.WRAP_ADDRESSMODE;
  104. var context = this._randomTexture.getContext();
  105. var rand = (min, max) => {
  106. return Math.random() * (max - min) + min;
  107. }
  108. for (var x = 0; x < size; x++) {
  109. for (var y = 0; y < size; y++) {
  110. var randVector = Vector3.Zero();
  111. randVector.x = Math.floor(rand(0.0, 1.0) * 255);
  112. randVector.y = Math.floor(rand(0.0, 1.0) * 255);
  113. randVector.z = Math.floor(rand(0.0, 1.0) * 255);
  114. context.fillStyle = 'rgb(' + randVector.x + ', ' + randVector.y + ', ' + randVector.z + ')';
  115. context.fillRect(x, y, 1, 1);
  116. }
  117. }
  118. this._randomTexture.update(false);
  119. }
  120. }
  121. }