diff --git a/src/postprocessing/AfterimagePass.js b/src/postprocessing/AfterimagePass.js deleted file mode 100644 index 33561f6a..00000000 --- a/src/postprocessing/AfterimagePass.js +++ /dev/null @@ -1,84 +0,0 @@ -import { - LinearFilter, - MeshBasicMaterial, - NearestFilter, - RGBAFormat, - ShaderMaterial, - UniformsUtils, - WebGLRenderTarget, -} from 'three' -import { Pass, FullScreenQuad } from '../postprocessing/Pass' -import { AfterimageShader } from '../shaders/AfterimageShader' - -var AfterimagePass = function (damp) { - Pass.call(this) - - if (AfterimageShader === undefined) console.error('THREE.AfterimagePass relies on AfterimageShader') - - this.shader = AfterimageShader - - this.uniforms = UniformsUtils.clone(this.shader.uniforms) - - this.uniforms['damp'].value = damp !== undefined ? damp : 0.96 - - this.textureComp = new WebGLRenderTarget(window.innerWidth, window.innerHeight, { - minFilter: LinearFilter, - magFilter: NearestFilter, - format: RGBAFormat, - }) - - this.textureOld = new WebGLRenderTarget(window.innerWidth, window.innerHeight, { - minFilter: LinearFilter, - magFilter: NearestFilter, - format: RGBAFormat, - }) - - this.shaderMaterial = new ShaderMaterial({ - uniforms: this.uniforms, - vertexShader: this.shader.vertexShader, - fragmentShader: this.shader.fragmentShader, - }) - - this.compFsQuad = new FullScreenQuad(this.shaderMaterial) - - var material = new MeshBasicMaterial() - this.copyFsQuad = new FullScreenQuad(material) -} - -AfterimagePass.prototype = Object.assign(Object.create(Pass.prototype), { - constructor: AfterimagePass, - - render: function (renderer, writeBuffer, readBuffer) { - this.uniforms['tOld'].value = this.textureOld.texture - this.uniforms['tNew'].value = readBuffer.texture - - renderer.setRenderTarget(this.textureComp) - this.compFsQuad.render(renderer) - - this.copyFsQuad.material.map = this.textureComp.texture - - if (this.renderToScreen) { - renderer.setRenderTarget(null) - this.copyFsQuad.render(renderer) - } else { - renderer.setRenderTarget(writeBuffer) - - if (this.clear) renderer.clear() - - this.copyFsQuad.render(renderer) - } - - // Swap buffers. - var temp = this.textureOld - this.textureOld = this.textureComp - this.textureComp = temp - // Now textureOld contains the latest image, ready for the next frame. - }, - - setSize: function (width, height) { - this.textureComp.setSize(width, height) - this.textureOld.setSize(width, height) - }, -}) - -export { AfterimagePass } diff --git a/src/postprocessing/AfterimagePass.ts b/src/postprocessing/AfterimagePass.ts new file mode 100644 index 00000000..89346c64 --- /dev/null +++ b/src/postprocessing/AfterimagePass.ts @@ -0,0 +1,87 @@ +import { + LinearFilter, + MeshBasicMaterial, + NearestFilter, + RGBAFormat, + WebGLRenderer, + ShaderMaterial, + UniformsUtils, + WebGLRenderTarget, +} from 'three' +import { Pass, FullScreenQuad } from './Pass' +import { AfterimageShader } from '../shaders/AfterimageShader' + +class AfterimagePass extends Pass { + public shader + public uniforms + public textureComp: WebGLRenderTarget + public textureOld: WebGLRenderTarget + public shaderMaterial: ShaderMaterial + public compFsQuad: FullScreenQuad + public copyFsQuad: FullScreenQuad + + constructor(damp = 0.96, shader = AfterimageShader) { + super() + + this.shader = shader + this.uniforms = UniformsUtils.clone(shader.uniforms) + this.uniforms['damp'].value = damp + + this.textureComp = new WebGLRenderTarget(window.innerWidth, window.innerHeight, { + minFilter: LinearFilter, + magFilter: NearestFilter, + format: RGBAFormat, + }) + + this.textureOld = new WebGLRenderTarget(window.innerWidth, window.innerHeight, { + minFilter: LinearFilter, + magFilter: NearestFilter, + format: RGBAFormat, + }) + + this.shaderMaterial = new ShaderMaterial({ + uniforms: this.uniforms, + vertexShader: this.shader.vertexShader, + fragmentShader: this.shader.fragmentShader, + }) + + this.compFsQuad = new FullScreenQuad(this.shaderMaterial) + + let material = new MeshBasicMaterial() + this.copyFsQuad = new FullScreenQuad(material) + } + + public render(renderer: WebGLRenderer, writeBuffer: WebGLRenderTarget, readBuffer: WebGLRenderTarget): void { + this.uniforms['tOld'].value = this.textureOld.texture + this.uniforms['tNew'].value = readBuffer.texture + + renderer.setRenderTarget(this.textureComp) + this.compFsQuad.render(renderer) + + this.copyFsQuad.material.map = this.textureComp.texture + + if (this.renderToScreen) { + renderer.setRenderTarget(null) + this.copyFsQuad.render(renderer) + } else { + renderer.setRenderTarget(writeBuffer) + + if (this.clear) renderer.clear() + + this.copyFsQuad.render(renderer) + } + + // Swap buffers. + let temp = this.textureOld + this.textureOld = this.textureComp + this.textureComp = temp + // Now textureOld contains the latest image, ready for the next frame. + } + + public setSize(width: number, height: number): void { + this.textureComp.setSize(width, height) + this.textureOld.setSize(width, height) + } +} + +export { AfterimagePass } diff --git a/src/shaders/AfterimageShader.js b/src/shaders/AfterimageShader.ts similarity index 97% rename from src/shaders/AfterimageShader.js rename to src/shaders/AfterimageShader.ts index 223ef041..63457e54 100644 --- a/src/shaders/AfterimageShader.js +++ b/src/shaders/AfterimageShader.ts @@ -4,7 +4,7 @@ * https://codepen.io/brunoimbrizi/pen/MoRJaN?page=1& */ -var AfterimageShader = { +const AfterimageShader = { uniforms: { damp: { value: 0.96 }, tOld: { value: null },