BokehPass.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /**
  2. * Depth-of-field post-process with bokeh shader
  3. */
  4. THREE.BokehPass = function ( scene, camera, params ) {
  5. THREE.Pass.call( this );
  6. this.scene = scene;
  7. this.camera = camera;
  8. var focus = ( params.focus !== undefined ) ? params.focus : 1.0;
  9. var aspect = ( params.aspect !== undefined ) ? params.aspect : camera.aspect;
  10. var aperture = ( params.aperture !== undefined ) ? params.aperture : 0.025;
  11. var maxblur = ( params.maxblur !== undefined ) ? params.maxblur : 1.0;
  12. // render targets
  13. var width = params.width || window.innerWidth || 1;
  14. var height = params.height || window.innerHeight || 1;
  15. this.renderTargetColor = new THREE.WebGLRenderTarget( width, height, {
  16. minFilter: THREE.LinearFilter,
  17. magFilter: THREE.LinearFilter,
  18. format: THREE.RGBFormat
  19. } );
  20. this.renderTargetDepth = this.renderTargetColor.clone();
  21. // depth material
  22. this.materialDepth = new THREE.MeshDepthMaterial();
  23. // bokeh material
  24. if ( THREE.BokehShader === undefined ) {
  25. console.error( "THREE.BokehPass relies on THREE.BokehShader" );
  26. }
  27. var bokehShader = THREE.BokehShader;
  28. var 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. this.materialBokeh = new THREE.ShaderMaterial( {
  35. uniforms: bokehUniforms,
  36. vertexShader: bokehShader.vertexShader,
  37. fragmentShader: bokehShader.fragmentShader
  38. } );
  39. this.uniforms = bokehUniforms;
  40. this.needsSwap = false;
  41. this.camera2 = new THREE.OrthographicCamera( - 1, 1, 1, - 1, 0, 1 );
  42. this.scene2 = new THREE.Scene();
  43. this.quad2 = new THREE.Mesh( new THREE.PlaneBufferGeometry( 2, 2 ), null );
  44. this.quad2.frustumCulled = false; // Avoid getting clipped
  45. this.scene2.add( this.quad2 );
  46. };
  47. THREE.BokehPass.prototype = Object.assign( Object.create( THREE.Pass.prototype ), {
  48. constructor: THREE.BokehPass,
  49. render: function ( renderer, writeBuffer, readBuffer, delta, maskActive ) {
  50. this.quad2.material = this.materialBokeh;
  51. // Render depth into texture
  52. this.scene.overrideMaterial = this.materialDepth;
  53. renderer.render( this.scene, this.camera, this.renderTargetDepth, true );
  54. // Render bokeh composite
  55. this.uniforms[ "tColor" ].value = readBuffer.texture;
  56. if ( this.renderToScreen ) {
  57. renderer.render( this.scene2, this.camera2 );
  58. } else {
  59. renderer.render( this.scene2, this.camera2, writeBuffer, this.clear );
  60. }
  61. this.scene.overrideMaterial = null;
  62. }
  63. } );