|
| 1 | +import { |
| 2 | + LinearFilter, |
| 3 | + RGBFormat, |
| 4 | + Uniform, |
| 5 | + Vector2, |
| 6 | + WebGLRenderTarget |
| 7 | +} from "three"; |
| 8 | + |
| 9 | +import { KernelSize, LuminanceMaterial } from "../materials"; |
| 10 | +import { BlurPass, ShaderPass } from "../passes"; |
| 11 | +import { BlendFunction } from "./blending/BlendFunction.js"; |
| 12 | +import { Effect, EffectType } from "./Effect.js"; |
| 13 | + |
| 14 | +import fragment from "./glsl/texture/shader.frag"; |
| 15 | + |
| 16 | +/** |
| 17 | + * A bloom pass. |
| 18 | + * |
| 19 | + * This pass uses the fast Kawase convolution technique and a luminance filter |
| 20 | + * to blur bright highlights. |
| 21 | + */ |
| 22 | + |
| 23 | +export class BloomEffect extends Effect { |
| 24 | + |
| 25 | + /** |
| 26 | + * Constructs a new bloom pass. |
| 27 | + * |
| 28 | + * @param {Object} [options] - The options. |
| 29 | + * @param {BlendFunction} [options.blendFunction=BlendFunction.SCREEN] - The blend function of this effect. |
| 30 | + * @param {Number} [options.resolutionScale=0.5] - The render texture resolution scale, relative to the screen render size. |
| 31 | + * @param {Number} [options.kernelSize=KernelSize.LARGE] - The blur kernel size. |
| 32 | + * @param {Number} [options.distinction=1.0] - The luminance distinction factor. Raise this value to bring out the brighter elements in the scene. |
| 33 | + */ |
| 34 | + |
| 35 | + constructor(options = {}) { |
| 36 | + |
| 37 | + const settings = Object.assign({ |
| 38 | + blendFunction: BlendFunction.SCREEN, |
| 39 | + resolutionScale: 0.5, |
| 40 | + kernelSize: KernelSize.LARGE, |
| 41 | + distinction: 1.0 |
| 42 | + }, options); |
| 43 | + |
| 44 | + super("BloomEffect", fragment, { |
| 45 | + |
| 46 | + type: EffectType.CONVOLUTION, |
| 47 | + blendFunction: settings.blendFunction, |
| 48 | + |
| 49 | + uniforms: new Map([ |
| 50 | + ["texture", new Uniform(null)] |
| 51 | + ]) |
| 52 | + |
| 53 | + }); |
| 54 | + |
| 55 | + /** |
| 56 | + * A render target. |
| 57 | + * |
| 58 | + * @type {WebGLRenderTarget} |
| 59 | + * @private |
| 60 | + */ |
| 61 | + |
| 62 | + this.renderTarget = new WebGLRenderTarget(1, 1, { |
| 63 | + minFilter: LinearFilter, |
| 64 | + magFilter: LinearFilter, |
| 65 | + stencilBuffer: false, |
| 66 | + depthBuffer: false |
| 67 | + }); |
| 68 | + |
| 69 | + this.renderTarget.texture.name = "Bloom.Target"; |
| 70 | + this.renderTarget.texture.generateMipmaps = false; |
| 71 | + |
| 72 | + this.uniforms.get("texture").value = this.renderTarget.texture; |
| 73 | + |
| 74 | + /** |
| 75 | + * The original resolution. |
| 76 | + * |
| 77 | + * @type {Vector2} |
| 78 | + * @private |
| 79 | + */ |
| 80 | + |
| 81 | + this.resolution = new Vector2(); |
| 82 | + |
| 83 | + /** |
| 84 | + * A blur pass. |
| 85 | + * |
| 86 | + * @type {BlurPass} |
| 87 | + * @private |
| 88 | + */ |
| 89 | + |
| 90 | + this.blurPass = new BlurPass(settings); |
| 91 | + |
| 92 | + /** |
| 93 | + * A luminance shader pass. |
| 94 | + * |
| 95 | + * @type {ShaderPass} |
| 96 | + * @private |
| 97 | + */ |
| 98 | + |
| 99 | + this.luminancePass = new ShaderPass(new LuminanceMaterial(true)); |
| 100 | + |
| 101 | + this.distinction = settings.distinction; |
| 102 | + this.kernelSize = settings.kernelSize; |
| 103 | + |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Indicates whether dithering is enabled. |
| 108 | + * |
| 109 | + * @type {Boolean} |
| 110 | + */ |
| 111 | + |
| 112 | + get dithering() { |
| 113 | + |
| 114 | + return this.blurPass.dithering; |
| 115 | + |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * Enables or disables dithering. |
| 120 | + * |
| 121 | + * @type {Boolean} |
| 122 | + */ |
| 123 | + |
| 124 | + set dithering(value) { |
| 125 | + |
| 126 | + this.blurPass.dithering = value; |
| 127 | + |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * The blur kernel size. |
| 132 | + * |
| 133 | + * @type {KernelSize} |
| 134 | + */ |
| 135 | + |
| 136 | + get kernelSize() { |
| 137 | + |
| 138 | + return this.blurPass.kernelSize; |
| 139 | + |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * @type {KernelSize} |
| 144 | + */ |
| 145 | + |
| 146 | + set kernelSize(value = KernelSize.LARGE) { |
| 147 | + |
| 148 | + this.blurPass.kernelSize = value; |
| 149 | + |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * The luminance distinction factor. |
| 154 | + * |
| 155 | + * @type {Number} |
| 156 | + */ |
| 157 | + |
| 158 | + get distinction() { |
| 159 | + |
| 160 | + return this.luminancePass.getFullscreenMaterial().uniforms.distinction.value; |
| 161 | + |
| 162 | + } |
| 163 | + |
| 164 | + /** |
| 165 | + * @type {Number} |
| 166 | + */ |
| 167 | + |
| 168 | + set distinction(value = 1.0) { |
| 169 | + |
| 170 | + this.luminancePass.getFullscreenMaterial().uniforms.distinction.value = value; |
| 171 | + |
| 172 | + } |
| 173 | + |
| 174 | + /** |
| 175 | + * A texture that contains the intermediate result of this effect. |
| 176 | + * |
| 177 | + * This texture will be applied to the scene colors unless the blend function |
| 178 | + * is set to `SKIP`. |
| 179 | + * |
| 180 | + * @type {Texture} |
| 181 | + */ |
| 182 | + |
| 183 | + get texture() { |
| 184 | + |
| 185 | + return this.renderTarget.texture; |
| 186 | + |
| 187 | + } |
| 188 | + |
| 189 | + /** |
| 190 | + * Returns the current resolution scale. |
| 191 | + * |
| 192 | + * @return {Number} The resolution scale. |
| 193 | + */ |
| 194 | + |
| 195 | + getResolutionScale() { |
| 196 | + |
| 197 | + return this.blurPass.getResolutionScale(); |
| 198 | + |
| 199 | + } |
| 200 | + |
| 201 | + /** |
| 202 | + * Sets the resolution scale. |
| 203 | + * |
| 204 | + * @param {Number} scale - The new resolution scale. |
| 205 | + */ |
| 206 | + |
| 207 | + setResolutionScale(scale) { |
| 208 | + |
| 209 | + this.blurPass.setResolutionScale(scale); |
| 210 | + this.setSize(this.resolution.x, this.resolution.y); |
| 211 | + |
| 212 | + } |
| 213 | + |
| 214 | + /** |
| 215 | + * Updates this effect. |
| 216 | + * |
| 217 | + * @param {WebGLRenderer} renderer - The renderer. |
| 218 | + * @param {WebGLRenderTarget} inputBuffer - A frame buffer that contains the result of the previous pass. |
| 219 | + * @param {Number} [delta] - The time between the last frame and the current one in seconds. |
| 220 | + */ |
| 221 | + |
| 222 | + update(renderer, inputBuffer, delta) { |
| 223 | + |
| 224 | + const renderTarget = this.renderTarget; |
| 225 | + |
| 226 | + this.luminancePass.render(renderer, inputBuffer, renderTarget); |
| 227 | + this.blurPass.render(renderer, renderTarget, renderTarget); |
| 228 | + |
| 229 | + } |
| 230 | + |
| 231 | + /** |
| 232 | + * Updates the size of internal render targets. |
| 233 | + * |
| 234 | + * @param {Number} width - The width. |
| 235 | + * @param {Number} height - The height. |
| 236 | + */ |
| 237 | + |
| 238 | + setSize(width, height) { |
| 239 | + |
| 240 | + // Remember the original resolution. |
| 241 | + this.resolution.set(width, height); |
| 242 | + |
| 243 | + this.blurPass.setSize(width, height); |
| 244 | + |
| 245 | + // The blur pass applies the resolution scale. |
| 246 | + width = this.blurPass.width; |
| 247 | + height = this.blurPass.height; |
| 248 | + |
| 249 | + this.renderTarget.setSize(width, height); |
| 250 | + |
| 251 | + } |
| 252 | + |
| 253 | + /** |
| 254 | + * Performs initialization tasks. |
| 255 | + * |
| 256 | + * @param {WebGLRenderer} renderer - The renderer. |
| 257 | + * @param {Boolean} alpha - Whether the renderer uses the alpha channel or not. |
| 258 | + */ |
| 259 | + |
| 260 | + initialize(renderer, alpha) { |
| 261 | + |
| 262 | + this.blurPass.initialize(renderer, alpha); |
| 263 | + |
| 264 | + if(!alpha) { |
| 265 | + |
| 266 | + this.renderTarget.texture.format = RGBFormat; |
| 267 | + |
| 268 | + } |
| 269 | + |
| 270 | + } |
| 271 | + |
| 272 | +} |
0 commit comments