AfterimagePass.js 2.2 KB

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