|
| 1 | +/** |
| 2 | + * Depth-of-field post-process with bokeh shader |
| 3 | + */ |
| 4 | + |
| 5 | + |
| 6 | +THREE.BokehPass = function ( scene, camera, params ) { |
| 7 | + |
| 8 | + this.scene = scene; |
| 9 | + this.camera = camera; |
| 10 | + |
| 11 | + var focus = ( params.focus !== undefined ) ? params.focus : 1.0; |
| 12 | + var aspect = ( params.aspect !== undefined ) ? params.aspect : camera.aspect; |
| 13 | + var aperture = ( params.aperture !== undefined ) ? params.aperture : 0.025; |
| 14 | + var maxblur = ( params.maxblur !== undefined ) ? params.maxblur : 1.0; |
| 15 | + |
| 16 | + // render targets |
| 17 | + |
| 18 | + var width = params.width || window.innerWidth || 1; |
| 19 | + var height = params.height || window.innerHeight || 1; |
| 20 | + |
| 21 | + this.renderTargetColor = new THREE.WebGLRenderTarget( width, height, { |
| 22 | + minFilter: THREE.LinearFilter, |
| 23 | + magFilter: THREE.LinearFilter, |
| 24 | + format: THREE.RGBFormat |
| 25 | + } ); |
| 26 | + |
| 27 | + this.renderTargetDepth = this.renderTargetColor.clone(); |
| 28 | + |
| 29 | + // depth material |
| 30 | + |
| 31 | + this.materialDepth = new THREE.MeshDepthMaterial(); |
| 32 | + |
| 33 | + // bokeh material |
| 34 | + |
| 35 | + if ( THREE.BokehShader === undefined ) { |
| 36 | + console.error( "THREE.BokehPass relies on THREE.BokehShader" ); |
| 37 | + } |
| 38 | + |
| 39 | + var bokehShader = THREE.BokehShader; |
| 40 | + var bokehUniforms = THREE.UniformsUtils.clone( bokehShader.uniforms ); |
| 41 | + |
| 42 | + bokehUniforms[ "tDepth" ].value = this.renderTargetDepth; |
| 43 | + |
| 44 | + bokehUniforms[ "focus" ].value = focus; |
| 45 | + bokehUniforms[ "aspect" ].value = aspect; |
| 46 | + bokehUniforms[ "aperture" ].value = aperture; |
| 47 | + bokehUniforms[ "maxblur" ].value = maxblur; |
| 48 | + |
| 49 | + this.materialBokeh = new THREE.ShaderMaterial({ |
| 50 | + uniforms: bokehUniforms, |
| 51 | + vertexShader: bokehShader.vertexShader, |
| 52 | + fragmentShader: bokehShader.fragmentShader |
| 53 | + }); |
| 54 | + |
| 55 | + this.uniforms = bokehUniforms; |
| 56 | + this.enabled = true; |
| 57 | + this.needsSwap = false; |
| 58 | + this.renderToScreen = false; |
| 59 | + this.clear = false; |
| 60 | + |
| 61 | +}; |
| 62 | + |
| 63 | +THREE.BokehPass.prototype = { |
| 64 | + |
| 65 | + render: function ( renderer, writeBuffer, readBuffer, delta, maskActive ) { |
| 66 | + |
| 67 | + var composer = THREE.EffectComposer; |
| 68 | + |
| 69 | + composer.quad.material = this.materialBokeh; |
| 70 | + |
| 71 | + // Render depth into texture |
| 72 | + |
| 73 | + this.scene.overrideMaterial = this.materialDepth; |
| 74 | + |
| 75 | + renderer.render( this.scene, this.camera, this.renderTargetDepth, true ); |
| 76 | + |
| 77 | + // Render bokeh composite |
| 78 | + |
| 79 | + this.uniforms[ "tColor" ].value = readBuffer; |
| 80 | + |
| 81 | + if ( this.renderToScreen ) { |
| 82 | + |
| 83 | + renderer.render( composer.scene, composer.camera ); |
| 84 | + |
| 85 | + } else { |
| 86 | + |
| 87 | + renderer.render( composer.scene, composer.camera, writeBuffer, this.clear ); |
| 88 | + |
| 89 | + } |
| 90 | + |
| 91 | + this.scene.overrideMaterial = null; |
| 92 | + |
| 93 | + } |
| 94 | + |
| 95 | +}; |
| 96 | + |
0 commit comments