babylon.ssaoRenderingPipeline.ts 6.3 KB

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