babylon.ssaoRenderingPipeline.ts 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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) {
  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._randomTexture.dispose();
  49. }
  50. // Private Methods
  51. private _createSSAOPostProcess(ratio: number): PostProcess {
  52. var sampleSphere = [
  53. 0.5381, 0.1856, -0.4319,
  54. 0.1379, 0.2486, 0.4430,
  55. 0.3371, 0.5679, -0.0057,
  56. -0.6999, -0.0451, -0.0019,
  57. 0.0689, -0.1598, -0.8547,
  58. 0.0560, 0.0069, -0.1843,
  59. -0.0146, 0.1402, 0.0762,
  60. 0.0100, -0.1924, -0.0344,
  61. -0.3577, -0.5301, -0.4358,
  62. -0.3169, 0.1063, 0.0158,
  63. 0.0103, -0.5869, 0.0046,
  64. -0.0897, -0.4940, 0.3287,
  65. 0.7119, -0.0154, -0.0918,
  66. -0.0533, 0.0596, -0.5411,
  67. 0.0352, -0.0631, 0.5460,
  68. -0.4776, 0.2847, -0.0271
  69. ];
  70. var samplesFactor = 1.0 / 8.0;
  71. this._ssaoPostProcess = new PostProcess("ssao", "ssao", ["sampleSphere", "samplesFactor"], ["randomSampler"],
  72. ratio, null, Texture.BILINEAR_SAMPLINGMODE,
  73. this._scene.getEngine(), false);
  74. this._ssaoPostProcess.onApply = (effect: Effect) => {
  75. if (this._firstUpdate === true) {
  76. effect.setArray3("sampleSphere", sampleSphere);
  77. effect.setFloat("samplesFactor", samplesFactor);
  78. this._firstUpdate = false;
  79. }
  80. effect.setTexture("textureSampler", this._depthTexture);
  81. effect.setTexture("randomSampler", this._randomTexture);
  82. };
  83. return this._ssaoPostProcess;
  84. }
  85. private _createSSAOCombinePostProcess(): PostProcess {
  86. this._ssaoCombinePostProcess = new PostProcess("ssaoCombine", "ssaoCombine", [], ["originalColor"],
  87. 1.0, null, Texture.BILINEAR_SAMPLINGMODE,
  88. this._scene.getEngine(), false);
  89. this._ssaoCombinePostProcess.onApply = (effect: Effect) => {
  90. effect.setTextureFromPostProcess("originalColor", this._originalColorPostProcess);
  91. };
  92. return this._ssaoCombinePostProcess;
  93. }
  94. private _createRandomTexture(): void {
  95. var size = 512;
  96. this._randomTexture = new BABYLON.DynamicTexture("SSAORandomTexture", size, this._scene, false, BABYLON.Texture.BILINEAR_SAMPLINGMODE);
  97. this._randomTexture.wrapU = Texture.WRAP_ADDRESSMODE;
  98. this._randomTexture.wrapV = Texture.WRAP_ADDRESSMODE;
  99. var context = this._randomTexture.getContext();
  100. var rand = (min, max) => {
  101. return Math.random() * (max - min) + min;
  102. }
  103. for (var x = 0; x < size; x++) {
  104. for (var y = 0; y < size; y++) {
  105. var randVector = BABYLON.Vector3.Zero();
  106. randVector.x = Math.floor(rand(0.0, 1.0) * 255);
  107. randVector.y = Math.floor(rand(0.0, 1.0) * 255);
  108. randVector.z = Math.floor(rand(0.0, 1.0) * 255);
  109. context.fillStyle = 'rgb(' + randVector.x + ', ' + randVector.y + ', ' + randVector.z + ')';
  110. context.fillRect(x, y, 1, 1);
  111. }
  112. }
  113. this._randomTexture.update(false);
  114. }
  115. }
  116. }