Skip to content

Instantly share code, notes, and snippets.

@robksawyer
Created September 25, 2020 10:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robksawyer/d94da398e298e91a3cdcb7c1057dba9c to your computer and use it in GitHub Desktop.
Save robksawyer/d94da398e298e91a3cdcb7c1057dba9c to your computer and use it in GitHub Desktop.
function initScene() {
_triangleGeometry = World.QUAD, _luminosityShader = _this.initClass(Shader, "UnrealBloomLuminosity", {
tDiffuse: {
value: null,
ignoreUIL: !0
},
luminosityThreshold: {
value: 1
},
smoothWidth: {
value: .01,
ignoreUIL: !0
},
defaultColor: {
value: new Color(0),
ignoreUIL: !0
},
defaultOpacity: {
value: 0,
ignoreUIL: !0
},
unique: _unique
}), (_mesh = new Mesh(_triangleGeometry, _luminosityShader)).frustumCulled = !1
}(),
function initBlurShaders() {
let resx = Math.round(_this.resolution.x / 2),
resy = Math.round(_this.resolution.y / 2);
for (let i = 0; i < _nMips; i++) {
let shader = _this.initClass(Shader, "UnrealBloomGaussian", {
unique: _unique,
colorTexture: {
value: null
},
texSize: {
value: new Vector2(resx, resy)
},
direction: {
value: new Vector2(.5, .5)
}
}, null, glsl => `\n#define KERNEL_RADIUS ${_kernelSizeArray[i]}\n#define SIGMA ${_kernelSizeArray[i]}\n${glsl}`, "gaussian" + i);
_separableBlurShaders.push(shader), resx = Math.round(resx / 2), resy = Math.round(resy / 2)
}
}(),
function initCompositeShader() {
let uniforms = {
bloomStrength: {
value: 1
},
bloomTintColor: {
value: new Color("#ffffff")
},
bloomRadius: {
value: 0
},
unique: _unique
};
for (let i = 0; i < _nMips; i++) uniforms["blurTexture" + (i + 1)] = {
value: _useRTPool ? null : _renderTargetsVertical[i].texture,
ignoreUIL: !0
};
(_compositeShader = _this.initClass(Shader, "UnrealBloomComposite", uniforms, null, (glsl, type) => {
if ("vs" === type) return glsl;
let compositeUniforms = "",
compositeMain = "";
for (let i = 0; i < _nMips; i++) compositeUniforms += `uniform sampler2D blurTexture${i+1};\n`, compositeMain += `lerpBloomFactor(${_bloomFactors[i].toFixed(4)}) * vec4(bloomTintColor, 1.0) * texture2D(blurTexture${i+1}, vUv) ${i<_nMips-1?"+ ":""}`;
return (glsl = glsl.replace("uniform sampler2D blurTexture1;", compositeUniforms)).replace("lerpBloomFactor(1.0) * vec4(bloomTintColor, 1.0) * texture2D(blurTexture1, vUv)", compositeMain)
})).needsUpdate = !0
}(),
function initPass() {
_this.pass = _this.initClass(NukePass, "UnrealBloomPass", _this.uniforms)
}(),
function addListeners() {
_this.events.sub(Events.RESIZE, resizeHandler), _this.events.sub(RenderManager.POST_RENDER, render)
}(), ShaderUIL.add(_luminosityShader).setLabel("UnrealBloom Luminosity"), ShaderUIL.add(_compositeShader).setLabel("UnrealBloom Composite"), this.set("texture", texture => {
_inputTexture = texture
}), this.set("dpr", dpr => {
_DPR = dpr, resizeHandler()
}), this.renderBloom = render, this.renderMesh = _mesh, this.luminosity = _luminosityShader, this.composite = _compositeShader, this.onDestroy = function () {
_renderTargetsHorizontal.forEach(r => r.destroy()), _renderTargetsVertical.forEach(r => r.destroy()), _this.renderTargetBright && _this.renderTargetBright.destroy()
}, this.getRTs = function () {
_this.renderTargetBright = RTPool.instance().getRT();
for (let i = 0; i < _nMips; i++) _renderTargetsHorizontal.push(RTPool.instance().getRT()), _renderTargetsVertical.push(RTPool.instance().getRT()), _compositeShader.uniforms["blurTexture" + (i + 1)].value = _renderTargetsVertical[i].texture;
_this.outputTexture = _renderTargetsHorizontal[0].texture, _this.uniforms.tUnrealBloom.value = _renderTargetsHorizontal[0].texture, resizeHandler()
}, this.putRTs = function () {
_this.renderTargetBright && RTPool.instance().putRT(_this.renderTargetBright), _this.renderTargetBright = null;
for (let i = 0; i < _renderTargetsHorizontal.length; i++) RTPool.instance().putRT(_renderTargetsHorizontal[i]), RTPool.instance().putRT(_renderTargetsVertical[i]);
_renderTargetsHorizontal = [], _renderTargetsVertical = []
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment