ShaderPass.js 1.6 KB

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