babylon.bloomEffect.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. module BABYLON {
  2. /**
  3. * The bloom effect spreads bright areas of an image to simulate artifacts seen in cameras
  4. */
  5. export class BloomEffect extends PostProcessRenderEffect{
  6. /**
  7. * Internal
  8. */
  9. public _effects: Array<PostProcess> = [];
  10. /**
  11. * Internal
  12. */
  13. public _downscale:ExtractHighlightsPostProcess;
  14. private _blurX:BlurPostProcess;
  15. private _blurY:BlurPostProcess;
  16. private _merge:BloomMergePostProcess;
  17. /**
  18. * The luminance threshold to find bright areas of the image to bloom.
  19. */
  20. public get threshold():number{
  21. return this._downscale.threshold;
  22. }
  23. public set threshold(value: number){
  24. this._downscale.threshold = value;
  25. }
  26. /**
  27. * The strength of the bloom.
  28. */
  29. public get weight():number{
  30. return this._merge.weight;
  31. }
  32. public set weight(value: number){
  33. this._merge.weight = value;
  34. }
  35. /**
  36. * Specifies the size of the bloom blur kernel, relative to the final output size
  37. */
  38. public get kernel():number{
  39. return this._blurX.kernel / this.bloomScale;
  40. }
  41. public set kernel(value: number){
  42. this._blurX.kernel = value * this.bloomScale;
  43. this._blurY.kernel = value * this.bloomScale;
  44. }
  45. /**
  46. * Creates a new instance of @see BloomEffect
  47. * @param scene The scene the effect belongs to.
  48. * @param bloomScale The ratio of the blur texture to the input texture that should be used to compute the bloom.
  49. * @param bloomKernel The size of the kernel to be used when applying the blur.
  50. * @param bloomWeight The the strength of bloom.
  51. * @param pipelineTextureType The type of texture to be used when performing the post processing.
  52. * @param blockCompilation If compilation of the shader should not be done in the constructor. The updateEffect method can be used to compile the shader at a later time. (default: false)
  53. */
  54. constructor(scene: Scene, private bloomScale:number, bloomWeight:number, bloomKernel:number, pipelineTextureType = 0, blockCompilation = false) {
  55. super(scene.getEngine(), "bloom", ()=>{
  56. return this._effects;
  57. }, true);
  58. this._downscale = new ExtractHighlightsPostProcess("highlights", 1.0, null, Texture.BILINEAR_SAMPLINGMODE, scene.getEngine(), false, pipelineTextureType, blockCompilation);
  59. this._blurX = new BlurPostProcess("horizontal blur", new Vector2(1.0, 0), 10.0, bloomScale, null, Texture.BILINEAR_SAMPLINGMODE, scene.getEngine(), false, pipelineTextureType, undefined, blockCompilation);
  60. this._blurX.alwaysForcePOT = true;
  61. this._blurX.autoClear = false;
  62. this._blurY = new BlurPostProcess("vertical blur", new Vector2(0, 1.0), 10.0, bloomScale, null, Texture.BILINEAR_SAMPLINGMODE, scene.getEngine(), false, pipelineTextureType, undefined, blockCompilation);
  63. this._blurY.alwaysForcePOT = true;
  64. this._blurY.autoClear = false;
  65. this.kernel = bloomKernel;
  66. this._effects = [this._downscale, this._blurX, this._blurY];
  67. this._merge = new BloomMergePostProcess("bloomMerge", this._downscale, this._blurY, bloomWeight, bloomScale, null, BABYLON.Texture.BILINEAR_SAMPLINGMODE, scene.getEngine(), false, pipelineTextureType, blockCompilation);
  68. this._merge.autoClear = false;
  69. this._effects.push(this._merge);
  70. }
  71. /**
  72. * Disposes each of the internal effects for a given camera.
  73. * @param camera The camera to dispose the effect on.
  74. */
  75. public disposeEffects(camera:Camera){
  76. for(var effectIndex = 0; effectIndex < this._effects.length; effectIndex++){
  77. this._effects[effectIndex].dispose(camera);
  78. }
  79. }
  80. /**
  81. * Internal
  82. */
  83. public _updateEffects(){
  84. for(var effectIndex = 0; effectIndex < this._effects.length; effectIndex++){
  85. this._effects[effectIndex].updateEffect();
  86. }
  87. }
  88. /**
  89. * Internal
  90. * @returns if all the contained post processes are ready.
  91. */
  92. public _isReady(){
  93. for(var effectIndex = 0; effectIndex < this._effects.length; effectIndex++){
  94. if(!this._effects[effectIndex].isReady()){
  95. return false;
  96. }
  97. }
  98. return true;
  99. }
  100. }
  101. }