HalftonePass.js 1.7 KB

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