BokehPass.js 3.5 KB

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