ShaderPass.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import {
  2. ShaderMaterial,
  3. UniformsUtils
  4. } from 'three';
  5. import { Pass, FullScreenQuad } from './Pass.js';
  6. /**
  7. * This pass can be used to create a post processing effect
  8. * with a raw GLSL shader object. Useful for implementing custom
  9. * effects.
  10. *
  11. * ```js
  12. * const fxaaPass = new ShaderPass( FXAAShader );
  13. * composer.addPass( fxaaPass );
  14. * ```
  15. *
  16. * @augments Pass
  17. */
  18. class ShaderPass extends Pass {
  19. /**
  20. * Constructs a new shader pass.
  21. *
  22. * @param {Object|ShaderMaterial} [shader] - A shader object holding vertex and fragment shader as well as
  23. * defines and uniforms. It's also valid to pass a custom shader material.
  24. * @param {string} [textureID='tDiffuse'] - The name of the texture uniform that should sample
  25. * the read buffer.
  26. */
  27. constructor( shader, textureID = 'tDiffuse' ) {
  28. super();
  29. /**
  30. * The name of the texture uniform that should sample the read buffer.
  31. *
  32. * @type {string}
  33. * @default 'tDiffuse'
  34. */
  35. this.textureID = textureID;
  36. /**
  37. * The pass uniforms.
  38. *
  39. * @type {?Object}
  40. */
  41. this.uniforms = null;
  42. /**
  43. * The pass material.
  44. *
  45. * @type {?ShaderMaterial}
  46. */
  47. this.material = null;
  48. if ( shader instanceof ShaderMaterial ) {
  49. this.uniforms = shader.uniforms;
  50. this.material = shader;
  51. } else if ( shader ) {
  52. this.uniforms = UniformsUtils.clone( shader.uniforms );
  53. this.material = new ShaderMaterial( {
  54. name: ( shader.name !== undefined ) ? shader.name : 'unspecified',
  55. defines: Object.assign( {}, shader.defines ),
  56. uniforms: this.uniforms,
  57. vertexShader: shader.vertexShader,
  58. fragmentShader: shader.fragmentShader
  59. } );
  60. }
  61. // internals
  62. this._fsQuad = new FullScreenQuad( this.material );
  63. }
  64. /**
  65. * Performs the shader pass.
  66. *
  67. * @param {WebGLRenderer} renderer - The renderer.
  68. * @param {WebGLRenderTarget} writeBuffer - The write buffer. This buffer is intended as the rendering
  69. * destination for the pass.
  70. * @param {WebGLRenderTarget} readBuffer - The read buffer. The pass can access the result from the
  71. * previous pass from this buffer.
  72. * @param {number} deltaTime - The delta time in seconds.
  73. * @param {boolean} maskActive - Whether masking is active or not.
  74. */
  75. render( renderer, writeBuffer, readBuffer /*, deltaTime, maskActive */ ) {
  76. if ( this.uniforms[ this.textureID ] ) {
  77. this.uniforms[ this.textureID ].value = readBuffer.texture;
  78. }
  79. this._fsQuad.material = this.material;
  80. if ( this.renderToScreen ) {
  81. renderer.setRenderTarget( null );
  82. this._fsQuad.render( renderer );
  83. } else {
  84. renderer.setRenderTarget( writeBuffer );
  85. // TODO: Avoid using autoClear properties, see https://github.com/mrdoob/three.js/pull/15571#issuecomment-465669600
  86. if ( this.clear ) renderer.clear( renderer.autoClearColor, renderer.autoClearDepth, renderer.autoClearStencil );
  87. this._fsQuad.render( renderer );
  88. }
  89. }
  90. /**
  91. * Frees the GPU-related resources allocated by this instance. Call this
  92. * method whenever the pass is no longer used in your app.
  93. */
  94. dispose() {
  95. this.material.dispose();
  96. this._fsQuad.dispose();
  97. }
  98. }
  99. export { ShaderPass };
粤ICP备19079148号