bloomEffect.js 4.3 KB

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