123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218 |
- import {
- Color,
- HalfFloatType,
- MeshDepthMaterial,
- NearestFilter,
- NoBlending,
- RGBADepthPacking,
- ShaderMaterial,
- UniformsUtils,
- WebGLRenderTarget
- } from 'three';
- import { Pass, FullScreenQuad } from './Pass.js';
- import { BokehShader } from '../shaders/BokehShader.js';
- class BokehPass extends Pass {
-
- constructor( scene, camera, params ) {
- super();
-
- this.scene = scene;
-
- this.camera = camera;
- const focus = ( params.focus !== undefined ) ? params.focus : 1.0;
- const aperture = ( params.aperture !== undefined ) ? params.aperture : 0.025;
- const maxblur = ( params.maxblur !== undefined ) ? params.maxblur : 1.0;
-
- this._renderTargetDepth = new WebGLRenderTarget( 1, 1, {
- minFilter: NearestFilter,
- magFilter: NearestFilter,
- type: HalfFloatType
- } );
- this._renderTargetDepth.texture.name = 'BokehPass.depth';
-
- this._materialDepth = new MeshDepthMaterial();
- this._materialDepth.depthPacking = RGBADepthPacking;
- this._materialDepth.blending = NoBlending;
-
- const bokehUniforms = UniformsUtils.clone( BokehShader.uniforms );
- bokehUniforms[ 'tDepth' ].value = this._renderTargetDepth.texture;
- bokehUniforms[ 'focus' ].value = focus;
- bokehUniforms[ 'aspect' ].value = camera.aspect;
- bokehUniforms[ 'aperture' ].value = aperture;
- bokehUniforms[ 'maxblur' ].value = maxblur;
- bokehUniforms[ 'nearClip' ].value = camera.near;
- bokehUniforms[ 'farClip' ].value = camera.far;
-
- this.materialBokeh = new ShaderMaterial( {
- defines: Object.assign( {}, BokehShader.defines ),
- uniforms: bokehUniforms,
- vertexShader: BokehShader.vertexShader,
- fragmentShader: BokehShader.fragmentShader
- } );
-
- this.uniforms = bokehUniforms;
-
- this._fsQuad = new FullScreenQuad( this.materialBokeh );
- this._oldClearColor = new Color();
- }
-
- render( renderer, writeBuffer, readBuffer ) {
-
- this.scene.overrideMaterial = this._materialDepth;
- renderer.getClearColor( this._oldClearColor );
- const oldClearAlpha = renderer.getClearAlpha();
- const oldAutoClear = renderer.autoClear;
- renderer.autoClear = false;
- renderer.setClearColor( 0xffffff );
- renderer.setClearAlpha( 1.0 );
- renderer.setRenderTarget( this._renderTargetDepth );
- renderer.clear();
- renderer.render( this.scene, this.camera );
-
- this.uniforms[ 'tColor' ].value = readBuffer.texture;
- this.uniforms[ 'nearClip' ].value = this.camera.near;
- this.uniforms[ 'farClip' ].value = this.camera.far;
- if ( this.renderToScreen ) {
- renderer.setRenderTarget( null );
- this._fsQuad.render( renderer );
- } else {
- renderer.setRenderTarget( writeBuffer );
- renderer.clear();
- this._fsQuad.render( renderer );
- }
- this.scene.overrideMaterial = null;
- renderer.setClearColor( this._oldClearColor );
- renderer.setClearAlpha( oldClearAlpha );
- renderer.autoClear = oldAutoClear;
- }
-
- setSize( width, height ) {
- this.materialBokeh.uniforms[ 'aspect' ].value = width / height;
- this._renderTargetDepth.setSize( width, height );
- }
-
- dispose() {
- this._renderTargetDepth.dispose();
- this._materialDepth.dispose();
- this.materialBokeh.dispose();
- this._fsQuad.dispose();
- }
- }
- export { BokehPass };
|