BokehPass.js 3.3 KB

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