bloomEffect.ts 5.0 KB

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