HalftonePass.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import {
  2. ShaderMaterial,
  3. UniformsUtils
  4. } from '../../../build/three.module.js';
  5. import { Pass, FullScreenQuad } from './Pass.js';
  6. import { HalftoneShader } from '../shaders/HalftoneShader.js';
  7. /**
  8. * RGB Halftone pass for three.js effects composer. Requires HalftoneShader.
  9. */
  10. class HalftonePass extends Pass {
  11. constructor( width, height, params ) {
  12. super();
  13. if ( HalftoneShader === undefined ) {
  14. console.error( 'THREE.HalftonePass requires HalftoneShader' );
  15. }
  16. this.uniforms = UniformsUtils.clone( HalftoneShader.uniforms );
  17. this.material = new ShaderMaterial( {
  18. uniforms: this.uniforms,
  19. fragmentShader: HalftoneShader.fragmentShader,
  20. vertexShader: HalftoneShader.vertexShader
  21. } );
  22. // set params
  23. this.uniforms.width.value = width;
  24. this.uniforms.height.value = height;
  25. for ( const key in params ) {
  26. if ( params.hasOwnProperty( key ) && this.uniforms.hasOwnProperty( key ) ) {
  27. this.uniforms[ key ].value = params[ key ];
  28. }
  29. }
  30. this.fsQuad = new FullScreenQuad( this.material );
  31. }
  32. render( renderer, writeBuffer, readBuffer/*, deltaTime, maskActive*/ ) {
  33. this.material.uniforms[ 'tDiffuse' ].value = readBuffer.texture;
  34. if ( this.renderToScreen ) {
  35. renderer.setRenderTarget( null );
  36. this.fsQuad.render( renderer );
  37. } else {
  38. renderer.setRenderTarget( writeBuffer );
  39. if ( this.clear ) renderer.clear();
  40. this.fsQuad.render( renderer );
  41. }
  42. }
  43. setSize( width, height ) {
  44. this.uniforms.width.value = width;
  45. this.uniforms.height.value = height;
  46. }
  47. }
  48. export { HalftonePass };