|
| 1 | +/* global THREE */ |
| 2 | +/** |
| 3 | + * Unreal Bloom Effect |
| 4 | + * Implementation for A-Frame |
| 5 | + * Code modified from Akbartus's post-processing A-Frame integration |
| 6 | + * https://github.com/akbartus/A-Frame-Component-Postprocessing |
| 7 | + */ |
| 8 | + |
| 9 | +import AFRAME from 'aframe'; |
| 10 | +import { EffectComposer } from 'three/addons/postprocessing/EffectComposer.js'; |
| 11 | +import { RenderPass } from 'three/addons/postprocessing/RenderPass.js'; |
| 12 | +import { UnrealBloomPass } from 'three/addons/postprocessing/UnrealBloomPass.js'; |
| 13 | +import { OutputPass } from 'three/addons/postprocessing/OutputPass.js'; |
| 14 | + |
| 15 | +AFRAME.registerComponent('bloom', { |
| 16 | + schema: { |
| 17 | + enabled: { type: 'boolean', default: true }, |
| 18 | + threshold: { type: 'number', default: 1 }, |
| 19 | + strength: { type: 'number', default: 0.5 }, |
| 20 | + radius: { type: 'number', default: 1 } |
| 21 | + }, |
| 22 | + events: { |
| 23 | + rendererresize: function () { |
| 24 | + this.renderer.getSize(this.size); |
| 25 | + this.composer.setSize(this.size.width, this.size.height); |
| 26 | + } |
| 27 | + }, |
| 28 | + init: function () { |
| 29 | + this.size = new THREE.Vector2(); |
| 30 | + this.scene = this.el.object3D; |
| 31 | + this.renderer = this.el.renderer; |
| 32 | + this.camera = this.el.camera; |
| 33 | + this.originalRender = this.el.renderer.render; |
| 34 | + this.bind(); |
| 35 | + }, |
| 36 | + update: function (oldData) { |
| 37 | + if (oldData.enabled === false && this.data.enabled === true) { |
| 38 | + this.bind(); |
| 39 | + } |
| 40 | + |
| 41 | + if (oldData.enabled === true && this.data.enabled === false) { |
| 42 | + this.el.renderer.render = this.originalRender; |
| 43 | + } |
| 44 | + |
| 45 | + if (this.composer) { |
| 46 | + this.composer.dispose(); |
| 47 | + } |
| 48 | + // create composer with multisampling to avoid aliasing |
| 49 | + var resolution = this.renderer.getDrawingBufferSize(new THREE.Vector2()); |
| 50 | + var renderTarget = new THREE.WebGLRenderTarget( |
| 51 | + resolution.width, |
| 52 | + resolution.height, |
| 53 | + { type: THREE.HalfFloatType, samples: 8 } |
| 54 | + ); |
| 55 | + |
| 56 | + this.composer = new EffectComposer(this.renderer, renderTarget); |
| 57 | + |
| 58 | + // create render pass |
| 59 | + var renderScene = new RenderPass(this.scene, this.camera); |
| 60 | + this.composer.addPass(renderScene); |
| 61 | + |
| 62 | + // create bloom pass |
| 63 | + var strength = this.data.strength; |
| 64 | + var radius = this.data.radius; |
| 65 | + var threshold = this.data.threshold; |
| 66 | + if (this.bloomPass) { |
| 67 | + this.bloomPass.dispose(); |
| 68 | + } |
| 69 | + this.bloomPass = new UnrealBloomPass( |
| 70 | + resolution, |
| 71 | + strength, |
| 72 | + radius, |
| 73 | + threshold |
| 74 | + ); |
| 75 | + this.composer.addPass(this.bloomPass); |
| 76 | + |
| 77 | + // create output pass |
| 78 | + if (this.outputPass) { |
| 79 | + this.outputPass.dispose(); |
| 80 | + } |
| 81 | + this.outputPass = new OutputPass(); |
| 82 | + this.composer.addPass(this.outputPass); |
| 83 | + }, |
| 84 | + |
| 85 | + bind: function () { |
| 86 | + var self = this; |
| 87 | + var isInsideComposerRender = false; |
| 88 | + |
| 89 | + this.el.renderer.render = function () { |
| 90 | + if (isInsideComposerRender) { |
| 91 | + self.originalRender.apply(this, arguments); |
| 92 | + } else { |
| 93 | + isInsideComposerRender = true; |
| 94 | + self.composer.render(self.el.sceneEl.delta / 1000); |
| 95 | + isInsideComposerRender = false; |
| 96 | + } |
| 97 | + }; |
| 98 | + }, |
| 99 | + |
| 100 | + remove: function () { |
| 101 | + this.el.renderer.render = this.originalRender; |
| 102 | + this.bloomPass.dispose(); |
| 103 | + this.outputPass.dispose(); |
| 104 | + this.composer.dispose(); |
| 105 | + } |
| 106 | +}); |
0 commit comments