BokehPass.js 2.5 KB

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