BokehPass.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. ( function () {
  2. /**
  3. * Depth-of-field post-process with bokeh shader
  4. */
  5. class BokehPass extends THREE.Pass {
  6. constructor( scene, camera, params ) {
  7. super();
  8. this.scene = scene;
  9. this.camera = camera;
  10. const focus = params.focus !== undefined ? params.focus : 1.0;
  11. const aspect = params.aspect !== undefined ? params.aspect : camera.aspect;
  12. const aperture = params.aperture !== undefined ? params.aperture : 0.025;
  13. const maxblur = params.maxblur !== undefined ? params.maxblur : 1.0; // render targets
  14. const width = params.width || window.innerWidth || 1;
  15. const height = params.height || window.innerHeight || 1;
  16. this.renderTargetDepth = new THREE.WebGLRenderTarget( width, height, {
  17. minFilter: THREE.NearestFilter,
  18. magFilter: THREE.NearestFilter
  19. } );
  20. this.renderTargetDepth.texture.name = 'BokehPass.depth'; // depth material
  21. this.materialDepth = new THREE.MeshDepthMaterial();
  22. this.materialDepth.depthPacking = THREE.RGBADepthPacking;
  23. this.materialDepth.blending = THREE.NoBlending; // bokeh material
  24. if ( THREE.BokehShader === undefined ) {
  25. console.error( 'THREE.BokehPass relies on THREE.BokehShader' );
  26. }
  27. const bokehShader = THREE.BokehShader;
  28. const bokehUniforms = THREE.UniformsUtils.clone( bokehShader.uniforms );
  29. bokehUniforms[ 'tDepth' ].value = this.renderTargetDepth.texture;
  30. bokehUniforms[ 'focus' ].value = focus;
  31. bokehUniforms[ 'aspect' ].value = aspect;
  32. bokehUniforms[ 'aperture' ].value = aperture;
  33. bokehUniforms[ 'maxblur' ].value = maxblur;
  34. bokehUniforms[ 'nearClip' ].value = camera.near;
  35. bokehUniforms[ 'farClip' ].value = camera.far;
  36. this.materialBokeh = new THREE.ShaderMaterial( {
  37. defines: Object.assign( {}, bokehShader.defines ),
  38. uniforms: bokehUniforms,
  39. vertexShader: bokehShader.vertexShader,
  40. fragmentShader: bokehShader.fragmentShader
  41. } );
  42. this.uniforms = bokehUniforms;
  43. this.needsSwap = false;
  44. this.fsQuad = new THREE.FullScreenQuad( this.materialBokeh );
  45. this._oldClearColor = new THREE.Color();
  46. }
  47. render( renderer, writeBuffer, readBuffer
  48. /*, deltaTime, maskActive*/
  49. ) {
  50. // Render depth into texture
  51. this.scene.overrideMaterial = this.materialDepth;
  52. renderer.getClearColor( this._oldClearColor );
  53. const oldClearAlpha = renderer.getClearAlpha();
  54. const oldAutoClear = renderer.autoClear;
  55. renderer.autoClear = false;
  56. renderer.setClearColor( 0xffffff );
  57. renderer.setClearAlpha( 1.0 );
  58. renderer.setRenderTarget( this.renderTargetDepth );
  59. renderer.clear();
  60. renderer.render( this.scene, this.camera ); // Render bokeh composite
  61. this.uniforms[ 'tColor' ].value = readBuffer.texture;
  62. this.uniforms[ 'nearClip' ].value = this.camera.near;
  63. this.uniforms[ 'farClip' ].value = this.camera.far;
  64. if ( this.renderToScreen ) {
  65. renderer.setRenderTarget( null );
  66. this.fsQuad.render( renderer );
  67. } else {
  68. renderer.setRenderTarget( writeBuffer );
  69. renderer.clear();
  70. this.fsQuad.render( renderer );
  71. }
  72. this.scene.overrideMaterial = null;
  73. renderer.setClearColor( this._oldClearColor );
  74. renderer.setClearAlpha( oldClearAlpha );
  75. renderer.autoClear = oldAutoClear;
  76. }
  77. }
  78. THREE.BokehPass = BokehPass;
  79. } )();