-
Notifications
You must be signed in to change notification settings - Fork 209
/
DepthPass.js
193 lines (143 loc) · 4.63 KB
/
DepthPass.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import {
Color,
MeshBasicMaterial,
MeshDepthMaterial,
MeshNormalMaterial,
NearestFilter,
LinearFilter,
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 DepthPass extends Pass {
constructor( scenes, camera, {width, height, onBeforeRenderScene} ) {
super();
this.scenes = scenes;
this.camera = camera;
this.width = width;
this.height = height;
this.onBeforeRenderScene = onBeforeRenderScene;
const depthTexture = new DepthTexture();
// depthTexture.type = UnsignedShortType;
/* this.beautyRenderTarget = new WebGLRenderTarget( this.width, this.height, {
minFilter: LinearFilter,
magFilter: LinearFilter,
format: RGBAFormat
} ); */
// normal render target with depth buffer
this.normalRenderTarget = new WebGLRenderTarget( this.width, this.height, {
minFilter: NearestFilter,
magFilter: NearestFilter,
format: RGBAFormat,
depthTexture: depthTexture
} );
this.normalMaterial = new MeshNormalMaterial();
this.normalMaterial.blending = NoBlending;
this.customScene = new Scene();
this.customScene.autoUpdate = false;
this._visibilityCache = new Map();
this.originalClearColor = new Color();
}
renderOverride( renderer, overrideMaterial, renderTarget, clearColor, clearAlpha ) {
renderer.getClearColor( this.originalClearColor );
const originalClearAlpha = renderer.getClearAlpha();
const originalAutoClear = renderer.autoClear;
renderer.setRenderTarget( renderTarget );
renderer.autoClear = false;
clearColor = overrideMaterial.clearColor || clearColor;
clearAlpha = overrideMaterial.clearAlpha || clearAlpha;
if ( ( clearColor !== undefined ) && ( clearColor !== null ) ) {
renderer.setClearColor( clearColor );
renderer.setClearAlpha( clearAlpha || 0.0 );
renderer.clear();
}
for (const scene of this.scenes) {
const cachedNodes = [];
const _recurse = o => {
if (o.isMesh && o.customPostMaterial) {
cachedNodes.push(o);
} else {
for (const child of o.children) {
_recurse(child);
}
}
};
_recurse(scene);
for (const o of cachedNodes) {
oldParentCache.set(o, o.parent);
oldMaterialCache.set(o, o.material);
o.material = o.customPostMaterial;
this.customScene.add(o);
}
{
const pop = this.onBeforeRenderScene(scene);
renderer.render( this.customScene, this.camera );
pop();
}
{
const pop = this.onBeforeRenderScene(scene);
scene.overrideMaterial = overrideMaterial;
renderer.render( scene, this.camera );
scene.overrideMaterial = null;
pop();
}
for (const child of cachedNodes) {
oldParentCache.get(child).add(child);
child.material = oldMaterialCache.get(child);
oldParentCache.delete(child);
oldMaterialCache.delete(child);
}
}
// restore original state
renderer.autoClear = originalAutoClear;
renderer.setClearColor( this.originalClearColor );
renderer.setClearAlpha( originalClearAlpha );
}
render( renderer, writeBuffer /*, readBuffer, deltaTime, maskActive */ ) {
// render beauty
// renderer.setRenderTarget( this.normalRenderTarget );
// renderer.clear();
// renderer.render( this.scene, this.camera );
// render normals and depth (honor only meshes, points and lines do not contribute to SSAO)
// this.overrideVisibility();
this.renderOverride( renderer, this.normalMaterial, this.normalRenderTarget, 0xffffff, 1.0 );
// this.restoreVisibility();
}
/* overrideVisibility() {
const scene = this.scene;
const cache = this._visibilityCache;
const self = this;
for (const scene of this.scenes) {
scene.traverse( function ( object ) {
cache.set( object, object.visible );
if ( object.isPoints || object.isLine ) object.visible = false;
} );
}
}
restoreVisibility() {
const scene = this.scene;
const cache = this._visibilityCache;
for (const scene of this.scenes) {
scene.traverse( function ( object ) {
const visible = cache.get( object );
object.visible = visible;
} );
}
cache.clear();
} */
}
export { DepthPass };