AfterimagePass.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. ( function () {
  2. class AfterimagePass extends THREE.Pass {
  3. constructor( damp = 0.96 ) {
  4. super();
  5. if ( THREE.AfterimageShader === undefined ) console.error( 'THREE.AfterimagePass relies on THREE.AfterimageShader' );
  6. this.shader = THREE.AfterimageShader;
  7. this.uniforms = THREE.UniformsUtils.clone( this.shader.uniforms );
  8. this.uniforms[ 'damp' ].value = damp;
  9. this.textureComp = new THREE.WebGLRenderTarget( window.innerWidth, window.innerHeight, {
  10. minFilter: THREE.LinearFilter,
  11. magFilter: THREE.NearestFilter,
  12. format: THREE.RGBAFormat
  13. } );
  14. this.textureOld = new THREE.WebGLRenderTarget( window.innerWidth, window.innerHeight, {
  15. minFilter: THREE.LinearFilter,
  16. magFilter: THREE.NearestFilter,
  17. format: THREE.RGBAFormat
  18. } );
  19. this.shaderMaterial = new THREE.ShaderMaterial( {
  20. uniforms: this.uniforms,
  21. vertexShader: this.shader.vertexShader,
  22. fragmentShader: this.shader.fragmentShader
  23. } );
  24. this.compFsQuad = new THREE.FullScreenQuad( this.shaderMaterial );
  25. const material = new THREE.MeshBasicMaterial();
  26. this.copyFsQuad = new THREE.FullScreenQuad( material );
  27. }
  28. render( renderer, writeBuffer, readBuffer
  29. /*, deltaTime, maskActive*/
  30. ) {
  31. this.uniforms[ 'tOld' ].value = this.textureOld.texture;
  32. this.uniforms[ 'tNew' ].value = readBuffer.texture;
  33. renderer.setRenderTarget( this.textureComp );
  34. this.compFsQuad.render( renderer );
  35. this.copyFsQuad.material.map = this.textureComp.texture;
  36. if ( this.renderToScreen ) {
  37. renderer.setRenderTarget( null );
  38. this.copyFsQuad.render( renderer );
  39. } else {
  40. renderer.setRenderTarget( writeBuffer );
  41. if ( this.clear ) renderer.clear();
  42. this.copyFsQuad.render( renderer );
  43. } // Swap buffers.
  44. const temp = this.textureOld;
  45. this.textureOld = this.textureComp;
  46. this.textureComp = temp; // Now textureOld contains the latest image, ready for the next frame.
  47. }
  48. setSize( width, height ) {
  49. this.textureComp.setSize( width, height );
  50. this.textureOld.setSize( width, height );
  51. }
  52. }
  53. THREE.AfterimagePass = AfterimagePass;
  54. } )();