AfterimagePass.js 2.0 KB

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