-
Notifications
You must be signed in to change notification settings - Fork 209
/
BokehPass.js
128 lines (92 loc) · 3.23 KB
/
BokehPass.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import {
Color,
MeshBasicMaterial,
MeshDepthMaterial,
NearestFilter,
NoBlending,
RGBADepthPacking,
ShaderMaterial,
UniformsUtils,
WebGLRenderTarget,
Scene,
DepthTexture,
UnsignedShortType,
FloatType,
RGBAFormat,
} from 'three';
import { Pass, FullScreenQuad } from 'three/examples/jsm/postprocessing/Pass.js';
import { BokehShader } from './BokehShader.js';
const oldParentCache = new WeakMap();
const oldMaterialCache = new WeakMap();
/**
* Depth-of-field post-process with bokeh shader
*/
class BokehPass extends Pass {
constructor( scene, camera, params, depthPass ) {
super();
this.scene = scene;
// this.customScene = new Scene();
// this.customScene.autoUpdate = false;
this.camera = camera;
this.depthPass = depthPass;
const focus = ( params.focus !== undefined ) ? params.focus : 1.0;
const aspect = ( params.aspect !== undefined ) ? params.aspect : camera.aspect;
const aperture = ( params.aperture !== undefined ) ? params.aperture : 0.025;
const maxblur = ( params.maxblur !== undefined ) ? params.maxblur : 1.0;
// render targets
// const width = params.width || window.innerWidth || 1;
// const height = params.height || window.innerHeight || 1;
// depth material
/* this.materialDepth = new MeshBasicMaterial();
this.materialDepth.depthPacking = RGBADepthPacking;
this.materialDepth.blending = NoBlending; */
// bokeh material
if ( BokehShader === undefined ) {
console.error( 'THREE.BokehPass relies on BokehShader' );
}
const bokehShader = BokehShader;
const bokehUniforms = UniformsUtils.clone( bokehShader.uniforms );
bokehUniforms[ 'tDepth' ].value = this.depthPass.normalRenderTarget.depthTexture;
bokehUniforms[ 'focus' ].value = focus;
bokehUniforms[ 'aspect' ].value = 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.needsSwap = false;
this.fsQuad = new FullScreenQuad( this.materialBokeh );
this._oldClearColor = new Color();
}
render( renderer, writeBuffer, readBuffer/*, deltaTime, maskActive*/ ) {
renderer.getClearColor( this._oldClearColor );
const oldClearAlpha = renderer.getClearAlpha();
const oldAutoClear = renderer.autoClear;
renderer.autoClear = false;
renderer.setClearColor( 0xffffff );
renderer.setClearAlpha( 1.0 );
// Render bokeh composite
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;
}
}
export { BokehPass };