BokehPass.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import {
  2. Color,
  3. MeshDepthMaterial,
  4. NearestFilter,
  5. NoBlending,
  6. RGBADepthPacking,
  7. ShaderMaterial,
  8. UniformsUtils,
  9. WebGLRenderTarget
  10. } from 'three';
  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. this.renderTargetDepth = new WebGLRenderTarget( 1, 1, { // will be resized later
  27. minFilter: NearestFilter,
  28. magFilter: NearestFilter
  29. } );
  30. this.renderTargetDepth.texture.name = 'BokehPass.depth';
  31. // depth material
  32. this.materialDepth = new MeshDepthMaterial();
  33. this.materialDepth.depthPacking = RGBADepthPacking;
  34. this.materialDepth.blending = NoBlending;
  35. // bokeh material
  36. const bokehShader = BokehShader;
  37. const bokehUniforms = UniformsUtils.clone( bokehShader.uniforms );
  38. bokehUniforms[ 'tDepth' ].value = this.renderTargetDepth.texture;
  39. bokehUniforms[ 'focus' ].value = focus;
  40. bokehUniforms[ 'aspect' ].value = aspect;
  41. bokehUniforms[ 'aperture' ].value = aperture;
  42. bokehUniforms[ 'maxblur' ].value = maxblur;
  43. bokehUniforms[ 'nearClip' ].value = camera.near;
  44. bokehUniforms[ 'farClip' ].value = camera.far;
  45. this.materialBokeh = new ShaderMaterial( {
  46. defines: Object.assign( {}, bokehShader.defines ),
  47. uniforms: bokehUniforms,
  48. vertexShader: bokehShader.vertexShader,
  49. fragmentShader: bokehShader.fragmentShader
  50. } );
  51. this.uniforms = bokehUniforms;
  52. this.fsQuad = new FullScreenQuad( this.materialBokeh );
  53. this._oldClearColor = new Color();
  54. }
  55. render( renderer, writeBuffer, readBuffer/*, deltaTime, maskActive*/ ) {
  56. // Render depth into texture
  57. this.scene.overrideMaterial = this.materialDepth;
  58. renderer.getClearColor( this._oldClearColor );
  59. const oldClearAlpha = renderer.getClearAlpha();
  60. const oldAutoClear = renderer.autoClear;
  61. renderer.autoClear = false;
  62. renderer.setClearColor( 0xffffff );
  63. renderer.setClearAlpha( 1.0 );
  64. renderer.setRenderTarget( this.renderTargetDepth );
  65. renderer.clear();
  66. renderer.render( this.scene, this.camera );
  67. // Render bokeh composite
  68. this.uniforms[ 'tColor' ].value = readBuffer.texture;
  69. this.uniforms[ 'nearClip' ].value = this.camera.near;
  70. this.uniforms[ 'farClip' ].value = this.camera.far;
  71. if ( this.renderToScreen ) {
  72. renderer.setRenderTarget( null );
  73. this.fsQuad.render( renderer );
  74. } else {
  75. renderer.setRenderTarget( writeBuffer );
  76. renderer.clear();
  77. this.fsQuad.render( renderer );
  78. }
  79. this.scene.overrideMaterial = null;
  80. renderer.setClearColor( this._oldClearColor );
  81. renderer.setClearAlpha( oldClearAlpha );
  82. renderer.autoClear = oldAutoClear;
  83. }
  84. setSize( width, height ) {
  85. this.renderTargetDepth.setSize( width, height );
  86. }
  87. dispose() {
  88. this.renderTargetDepth.dispose();
  89. this.materialDepth.dispose();
  90. this.materialBokeh.dispose();
  91. this.fsQuad.dispose();
  92. }
  93. }
  94. export { BokehPass };