HalftonePass.js 1.7 KB

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