-
Notifications
You must be signed in to change notification settings - Fork 120
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Move AfterimagePass to a class with customizable shader (#49)
* Move AfterimagePass to a class * Allow passing in custom shaders This lets us define any shader that uses tOld and tNew instead of just afterimageShader
- Loading branch information
1 parent
017051d
commit 4450baf
Showing
3 changed files
with
88 additions
and
85 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<ShaderMaterial> | ||
public copyFsQuad: FullScreenQuad<MeshBasicMaterial> | ||
|
||
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 } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4450baf
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs: