ShaderPass.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import {
  2. ShaderMaterial,
  3. UniformsUtils
  4. } from '../../../build/three.module.js';
  5. import { Pass, FullScreenQuad } from './Pass.js';
  6. class ShaderPass extends Pass {
  7. constructor( shader, textureID ) {
  8. super();
  9. this.textureID = ( textureID !== undefined ) ? textureID : 'tDiffuse';
  10. if ( shader instanceof ShaderMaterial ) {
  11. this.uniforms = shader.uniforms;
  12. this.material = shader;
  13. } else if ( shader ) {
  14. this.uniforms = UniformsUtils.clone( shader.uniforms );
  15. this.material = new ShaderMaterial( {
  16. defines: Object.assign( {}, shader.defines ),
  17. uniforms: this.uniforms,
  18. vertexShader: shader.vertexShader,
  19. fragmentShader: shader.fragmentShader
  20. } );
  21. }
  22. this.fsQuad = new FullScreenQuad( this.material );
  23. }
  24. render( renderer, writeBuffer, readBuffer /*, deltaTime, maskActive */ ) {
  25. if ( this.uniforms[ this.textureID ] ) {
  26. this.uniforms[ this.textureID ].value = readBuffer.texture;
  27. }
  28. this.fsQuad.material = this.material;
  29. if ( this.renderToScreen ) {
  30. renderer.setRenderTarget( null );
  31. this.fsQuad.render( renderer );
  32. } else {
  33. renderer.setRenderTarget( writeBuffer );
  34. // TODO: Avoid using autoClear properties, see https://github.com/mrdoob/three.js/pull/15571#issuecomment-465669600
  35. if ( this.clear ) renderer.clear( renderer.autoClearColor, renderer.autoClearDepth, renderer.autoClearStencil );
  36. this.fsQuad.render( renderer );
  37. }
  38. }
  39. }
  40. export { ShaderPass };